set_menus.rs

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