set_menus.rs

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