From 39bd12a5577dec8996aaca8fc27a36c02bb39ab0 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 11 Mar 2024 00:56:45 -0700 Subject: [PATCH] gpui: add set menus example (#9131) Add an example showing how to add a menu item, register an action with the `AppContext`, and successfully call the action. Release Notes: - N/A --- crates/gpui/Cargo.toml | 6 ++++- crates/gpui/examples/set_menus.rs | 43 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 crates/gpui/examples/set_menus.rs diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index b4b117fabb0e1c2819fb67c317e069b160148df2..440a6788bcb38bcb1cacd2ff34acb1c292a4ece3 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -132,4 +132,8 @@ path = "examples/hello_world.rs" [[example]] name = "image" -path = "examples/image/image.rs" \ No newline at end of file +path = "examples/image/image.rs" + +[[example]] +name = "set_menus" +path = "examples/set_menus.rs" diff --git a/crates/gpui/examples/set_menus.rs b/crates/gpui/examples/set_menus.rs new file mode 100644 index 0000000000000000000000000000000000000000..1a46d2e8a83c3d6abb338d960b6ae1fcc5f8d7e8 --- /dev/null +++ b/crates/gpui/examples/set_menus.rs @@ -0,0 +1,43 @@ +use gpui::*; + +struct SetMenus; + +impl Render for SetMenus { + fn render(&mut self, _cx: &mut ViewContext) -> impl IntoElement { + div() + .flex() + .bg(rgb(0x2e7d32)) + .size_full() + .justify_center() + .items_center() + .text_xl() + .text_color(rgb(0xffffff)) + .child("Set Menus Example") + } +} + +fn main() { + App::new().run(|cx: &mut AppContext| { + // Bring the menu bar to the foreground (so you can see the menu bar) + cx.activate(true); + // Register the `quit` function so it can be referenced by the `MenuItem::action` in the menu bar + cx.on_action(quit); + // Add menu items + cx.set_menus(vec![Menu { + name: "set_menus", + items: vec![MenuItem::action("Quit", Quit)], + }]); + cx.open_window(WindowOptions::default(), |cx| { + cx.new_view(|_cx| SetMenus {}) + }); + }); +} + +// Associate actions using the `actions!` macro (or `impl_actions!` macro) +actions!(set_menus, [Quit]); + +// Define the quit function that is registered with the AppContext +fn quit(_: &Quit, cx: &mut AppContext) { + println!("Gracefully quitting the application . . ."); + cx.quit(); +}