1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! Authentic macOS AppKit `NSMenu` data structure and standard presets.
//!
//! Normative Reference: SPEC-007 §2.
use egui::Key;
use super::item::{MacKeyboardShortcut, NSMenuItem};
/// Data-driven application menu (`NSMenu` equivalent).
#[derive(Debug, Clone, PartialEq)]
pub struct NSMenu {
/// Title of the top-level menu (e.g. "File", "Edit").
pub title: String,
/// List of child menu items in this menu.
pub items: Vec<NSMenuItem>,
}
impl NSMenu {
/// Creates an empty menu with a title.
pub fn new(title: impl Into<String>) -> Self {
Self {
title: title.into(),
items: Vec::new(),
}
}
/// Creates a menu with a title and a list of items.
pub fn with_items(title: impl Into<String>, items: Vec<NSMenuItem>) -> Self {
Self {
title: title.into(),
items,
}
}
/// Appends an item to the menu.
pub fn item(mut self, item: NSMenuItem) -> Self {
self.items.push(item);
self
}
/// Recursively searches for a matching keyboard shortcut in this menu.
pub fn find_shortcut(&self, input: &egui::InputState) -> Option<String> {
Self::find_shortcut_in_items(&self.items, input)
}
/// Recursively searches for and consumes a matching keyboard shortcut in this menu.
pub fn consume_shortcut(&self, input: &mut egui::InputState) -> Option<String> {
Self::consume_shortcut_in_items(&self.items, input)
}
/// Recursively searches items for a matching keyboard shortcut and consumes it.
pub fn consume_shortcut_in_items(
items: &[NSMenuItem],
input: &mut egui::InputState,
) -> Option<String> {
for item in items {
if !item.enabled {
continue;
}
match &item.kind {
super::item::MacMenuItemKind::Action {
id,
shortcut: Some(sc),
} => {
if sc.consume(input) {
return Some(id.clone());
}
}
super::item::MacMenuItemKind::Submenu { menu } => {
if let Some(act) = Self::consume_shortcut_in_items(&menu.items, input) {
return Some(act);
}
}
_ => {}
}
}
None
}
/// Recursively searches items for a matching keyboard shortcut.
pub fn find_shortcut_in_items(
items: &[NSMenuItem],
input: &egui::InputState,
) -> Option<String> {
for item in items {
if !item.enabled {
continue;
}
match &item.kind {
super::item::MacMenuItemKind::Action {
id,
shortcut: Some(sc),
} => {
if sc.matches(input) {
return Some(id.clone());
}
}
super::item::MacMenuItemKind::Submenu { menu } => {
if let Some(act) = Self::find_shortcut_in_items(&menu.items, input) {
return Some(act);
}
}
_ => {}
}
}
None
}
/// Default macOS "File" menu (SPEC-007 §2.2).
pub fn default_file_menu() -> Self {
Self::new("File")
.item(NSMenuItem::action_with_shortcut(
"New",
"file.new",
MacKeyboardShortcut::cmd(Key::N),
))
.item(NSMenuItem::action_with_shortcut(
"Open...",
"file.open",
MacKeyboardShortcut::cmd(Key::O),
))
.item(NSMenuItem::separator())
.item(NSMenuItem::action_with_shortcut(
"Save",
"file.save",
MacKeyboardShortcut::cmd(Key::S),
))
.item(NSMenuItem::action("Export...", "file.export"))
.item(NSMenuItem::separator())
.item(NSMenuItem::action_with_shortcut(
"Close Window",
"file.close",
MacKeyboardShortcut::cmd(Key::W),
))
}
/// Default macOS "Edit" menu (SPEC-007 §2.2).
pub fn default_edit_menu() -> Self {
Self::new("Edit")
.item(NSMenuItem::action_with_shortcut(
"Undo",
"edit.undo",
MacKeyboardShortcut::cmd(Key::Z),
))
.item(NSMenuItem::action_with_shortcut(
"Redo",
"edit.redo",
MacKeyboardShortcut::cmd_shift(Key::Z),
))
.item(NSMenuItem::separator())
.item(NSMenuItem::action_with_shortcut(
"Cut",
"edit.cut",
MacKeyboardShortcut::cmd(Key::X),
))
.item(NSMenuItem::action_with_shortcut(
"Copy",
"edit.copy",
MacKeyboardShortcut::cmd(Key::C),
))
.item(NSMenuItem::action_with_shortcut(
"Paste",
"edit.paste",
MacKeyboardShortcut::cmd(Key::V),
))
.item(NSMenuItem::action_with_shortcut(
"Select All",
"edit.select_all",
MacKeyboardShortcut::cmd(Key::A),
))
}
/// Default macOS "View" menu (SPEC-007 §2.2).
pub fn default_view_menu() -> Self {
Self::new("View")
.item(NSMenuItem::action_with_shortcut(
"Zoom In",
"view.zoom_in",
MacKeyboardShortcut::cmd(Key::Plus),
))
.item(NSMenuItem::action_with_shortcut(
"Zoom Out",
"view.zoom_out",
MacKeyboardShortcut::cmd(Key::Minus),
))
.item(NSMenuItem::action_with_shortcut(
"Actual Size",
"view.zoom_reset",
MacKeyboardShortcut::cmd(Key::Num0),
))
.item(NSMenuItem::separator())
.item(NSMenuItem::action(
"Toggle Inspector",
"view.toggle_inspector",
))
.item(NSMenuItem::action("Fullscreen", "view.fullscreen"))
}
/// Default macOS "Window" menu (SPEC-007 §2.2).
pub fn default_window_menu() -> Self {
Self::new("Window")
.item(NSMenuItem::action_with_shortcut(
"Minimize",
"window.minimize",
MacKeyboardShortcut::cmd(Key::M),
))
.item(NSMenuItem::action("Zoom", "window.zoom"))
.item(NSMenuItem::separator())
.item(NSMenuItem::action(
"Bring All to Front",
"window.bring_all_to_front",
))
}
/// Default macOS "Help" menu (SPEC-007 §2.2).
pub fn default_help_menu() -> Self {
Self::new("Help")
.item(NSMenuItem::action("Documentation", "help.documentation"))
.item(NSMenuItem::action_with_shortcut(
"Keyboard Shortcuts...",
"help.shortcuts",
MacKeyboardShortcut::cmd(Key::K),
))
.item(NSMenuItem::separator())
.item(NSMenuItem::action("About", "help.about"))
}
/// Standard root menu bar containing all 5 standard top-level menus (SPEC-007 §2.2).
pub fn default_menu_bar() -> Vec<Self> {
vec![
Self::default_file_menu(),
Self::default_edit_menu(),
Self::default_view_menu(),
Self::default_window_menu(),
Self::default_help_menu(),
]
}
}