Skip to main content

fc_ui/capabilities/navigation/
app_menu.rs

1//! Native application menu bar builder for desktop applications.
2
3use gpui::{Menu, MenuItem, SharedString, StyleRefinement, Styled, SystemMenuType};
4
5pub struct AppMenuBar {
6    menus: Vec<Menu>,
7}
8
9impl AppMenuBar {
10    pub fn new() -> Self {
11        Self { menus: Vec::new() }
12    }
13
14    pub fn menu(mut self, menu: AppMenu) -> Self {
15        self.menus.push(menu.build());
16        self
17    }
18
19    pub fn build(self) -> Vec<Menu> {
20        self.menus
21    }
22}
23
24impl Default for AppMenuBar {
25    fn default() -> Self {
26        Self::new()
27    }
28}
29
30pub struct AppMenu {
31    name: SharedString,
32    items: Vec<MenuItem>,
33    style: StyleRefinement,
34}
35
36impl AppMenu {
37    pub fn new(name: impl Into<SharedString>) -> Self {
38        Self {
39            name: name.into(),
40            items: Vec::new(),
41            style: StyleRefinement::default(),
42        }
43    }
44
45    pub fn action<A: gpui::Action>(mut self, label: impl Into<SharedString>, action: A) -> Self {
46        self.items.push(MenuItem::action(label.into(), action));
47        self
48    }
49
50    pub fn separator(mut self) -> Self {
51        self.items.push(MenuItem::separator());
52        self
53    }
54
55    pub fn submenu(mut self, submenu: AppMenu) -> Self {
56        let submenu_built = submenu.build();
57        self.items.push(MenuItem::submenu(submenu_built));
58        self
59    }
60
61    pub fn os_submenu(mut self, label: impl Into<SharedString>, menu_type: SystemMenuType) -> Self {
62        self.items
63            .push(MenuItem::os_submenu(label.into(), menu_type));
64        self
65    }
66
67    pub fn build(self) -> Menu {
68        Menu {
69            name: self.name,
70            icon: None,
71            items: self.items,
72        }
73    }
74}
75
76impl Styled for AppMenu {
77    fn style(&mut self) -> &mut StyleRefinement {
78        &mut self.style
79    }
80}
81
82pub fn file_menu() -> AppMenu {
83    AppMenu::new("File")
84}
85
86pub fn edit_menu() -> AppMenu {
87    AppMenu::new("Edit")
88}
89
90pub fn view_menu() -> AppMenu {
91    AppMenu::new("View")
92}
93
94pub fn window_menu() -> AppMenu {
95    AppMenu::new("Window")
96}
97
98pub fn help_menu() -> AppMenu {
99    AppMenu::new("Help")
100}
101
102pub struct StandardMacMenuBar {
103    #[cfg(target_os = "macos")]
104    app_name: SharedString,
105    file_menu: Option<AppMenu>,
106    edit_menu: Option<AppMenu>,
107    view_menu: Option<AppMenu>,
108    window_menu: Option<AppMenu>,
109    help_menu: Option<AppMenu>,
110}
111
112impl StandardMacMenuBar {
113    pub fn new(app_name: impl Into<SharedString>) -> Self {
114        #[cfg(target_os = "macos")]
115        let app_name = app_name.into();
116
117        #[cfg(not(target_os = "macos"))]
118        let _ = app_name;
119
120        Self {
121            #[cfg(target_os = "macos")]
122            app_name,
123            file_menu: None,
124            edit_menu: None,
125            view_menu: None,
126            window_menu: None,
127            help_menu: None,
128        }
129    }
130
131    pub fn file_menu(mut self, menu: AppMenu) -> Self {
132        self.file_menu = Some(menu);
133        self
134    }
135
136    pub fn edit_menu(mut self, menu: AppMenu) -> Self {
137        self.edit_menu = Some(menu);
138        self
139    }
140
141    pub fn view_menu(mut self, menu: AppMenu) -> Self {
142        self.view_menu = Some(menu);
143        self
144    }
145
146    pub fn window_menu(mut self, menu: AppMenu) -> Self {
147        self.window_menu = Some(menu);
148        self
149    }
150
151    pub fn help_menu(mut self, menu: AppMenu) -> Self {
152        self.help_menu = Some(menu);
153        self
154    }
155
156    pub fn build(self) -> Vec<Menu> {
157        let mut menus = Vec::new();
158
159        #[cfg(target_os = "macos")]
160        {
161            let app_menu = AppMenu::new(&self.app_name)
162                .os_submenu("Services", SystemMenuType::Services)
163                .separator();
164            menus.push(app_menu.build());
165        }
166
167        if let Some(file_menu) = self.file_menu {
168            menus.push(file_menu.build());
169        }
170
171        if let Some(edit_menu) = self.edit_menu {
172            menus.push(edit_menu.build());
173        }
174
175        if let Some(view_menu) = self.view_menu {
176            menus.push(view_menu.build());
177        }
178
179        if let Some(window_menu) = self.window_menu {
180            menus.push(window_menu.build());
181        }
182
183        if let Some(help_menu) = self.help_menu {
184            menus.push(help_menu.build());
185        }
186
187        menus
188    }
189}