set_menus.rs

 1use gpui::*;
 2
 3struct SetMenus;
 4
 5impl Render for SetMenus {
 6    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
 7        div()
 8            .flex()
 9            .bg(rgb(0x2e7d32))
10            .size_full()
11            .justify_center()
12            .items_center()
13            .text_xl()
14            .text_color(rgb(0xffffff))
15            .child("Set Menus Example")
16    }
17}
18
19fn main() {
20    App::new().run(|cx: &mut AppContext| {
21        // Bring the menu bar to the foreground (so you can see the menu bar)
22        cx.activate(true);
23        // Register the `quit` function so it can be referenced by the `MenuItem::action` in the menu bar
24        cx.on_action(quit);
25        // Add menu items
26        cx.set_menus(vec![Menu {
27            name: "set_menus".into(),
28            items: vec![MenuItem::action("Quit", Quit)],
29        }]);
30        cx.open_window(WindowOptions::default(), |cx| {
31            cx.new_view(|_cx| SetMenus {})
32        })
33        .unwrap();
34    });
35}
36
37// Associate actions using the `actions!` macro (or `impl_actions!` macro)
38actions!(set_menus, [Quit]);
39
40// Define the quit function that is registered with the AppContext
41fn quit(_: &Quit, cx: &mut AppContext) {
42    println!("Gracefully quitting the application . . .");
43    cx.quit();
44}