Skip to content

Commit 42022ef

Browse files
committed
Fix menu generation on non-macOS systems
1 parent 733dc55 commit 42022ef

File tree

1 file changed

+58
-61
lines changed

1 file changed

+58
-61
lines changed

backend/src/menu.rs

Lines changed: 58 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -41,67 +41,64 @@ impl TryFrom<&str> for IdWithNoColons {
4141
}
4242

4343
pub(crate) fn initialize_menu_handlers<R: Runtime>(handle: &AppHandle<R>) {
44-
handle.on_menu_event(move |app_handle, event| {
45-
match event.id().0.as_str() {
46-
"update-check" => {
47-
app_handle
48-
.emit("update-check", 0)
49-
.expect("Failed to emit menu event");
50-
}
51-
"start-sync" => {
52-
app_handle
53-
.emit("start-sync", 0)
54-
.expect("Failed to emit menu event");
55-
}
56-
"import-runbook" => {
57-
app_handle
58-
.emit("import-runbook", 0)
59-
.expect("Failed to emit menu event");
60-
}
61-
"new-runbook" => {
62-
app_handle
63-
.emit("new-runbook", 0)
64-
.expect("Failed to emit menu event");
65-
}
66-
"new-workspace" => {
67-
app_handle
68-
.emit("new-workspace", 0)
69-
.expect("Failed to emit menu event");
70-
}
71-
"export-markdown" => {
72-
app_handle
73-
.emit("export-markdown", 0)
74-
.expect("Failed to emit menu event");
75-
}
76-
"toggle-devtools" => {
77-
let window = app_handle.get_webview_window("main").unwrap();
78-
if window.is_devtools_open() {
79-
window.close_devtools();
80-
} else {
81-
window.open_devtools();
82-
}
83-
}
84-
// The following branch expects `id` and `href` to be in scope if needed
85-
other_id if other_id.starts_with("link-menu-item:") => {
86-
let href = other_id.splitn(3, ":").nth(2);
87-
if let Some(href) = href {
88-
let _ = open::that(href);
89-
} else {
90-
log::warn!("Unknown menu event: {other_id}");
91-
}
44+
handle.on_menu_event(move |app_handle, event| match event.id().0.as_str() {
45+
"update-check" => {
46+
app_handle
47+
.emit("update-check", 0)
48+
.expect("Failed to emit menu event");
49+
}
50+
"start-sync" => {
51+
app_handle
52+
.emit("start-sync", 0)
53+
.expect("Failed to emit menu event");
54+
}
55+
"import-runbook" => {
56+
app_handle
57+
.emit("import-runbook", 0)
58+
.expect("Failed to emit menu event");
59+
}
60+
"new-runbook" => {
61+
app_handle
62+
.emit("new-runbook", 0)
63+
.expect("Failed to emit menu event");
64+
}
65+
"new-workspace" => {
66+
app_handle
67+
.emit("new-workspace", 0)
68+
.expect("Failed to emit menu event");
69+
}
70+
"export-markdown" => {
71+
app_handle
72+
.emit("export-markdown", 0)
73+
.expect("Failed to emit menu event");
74+
}
75+
"toggle-devtools" => {
76+
let window = app_handle.get_webview_window("main").unwrap();
77+
if window.is_devtools_open() {
78+
window.close_devtools();
79+
} else {
80+
window.open_devtools();
9281
}
93-
other_id if other_id.starts_with("window-tab-item:") => {
94-
let url = other_id.split_once(":").map(|x| x.1);
95-
if let Some(url) = url {
96-
app_handle.emit("activate-tab", url).unwrap();
97-
} else {
98-
log::warn!("Unknown menu event: {other_id}");
99-
}
82+
}
83+
other_id if other_id.starts_with("link-menu-item:") => {
84+
let href = other_id.splitn(3, ":").nth(2);
85+
if let Some(href) = href {
86+
let _ = open::that(href);
87+
} else {
88+
log::warn!("Unknown menu event: {other_id}");
10089
}
101-
other_id => {
90+
}
91+
other_id if other_id.starts_with("window-tab-item:") => {
92+
let url = other_id.split_once(":").map(|x| x.1);
93+
if let Some(url) = url {
94+
app_handle.emit("activate-tab", url).unwrap();
95+
} else {
10296
log::warn!("Unknown menu event: {other_id}");
10397
}
10498
}
99+
other_id => {
100+
log::warn!("Unknown menu event: {other_id}");
101+
}
105102
});
106103
}
107104

@@ -203,7 +200,7 @@ pub fn menu<R: Runtime>(app_handle: &AppHandle<R>, tab_items: &[TabItem]) -> Res
203200
// Build tab menu items first
204201
let tab_menu_items: Vec<_> = tab_items
205202
.iter()
206-
.map(|tab| {
203+
.flat_map(|tab| {
207204
let app_handle = app_handle.clone();
208205
MenuItemBuilder::new(&tab.title)
209206
.id(format!("window-tab-item:{}", tab.url.clone()))
@@ -215,16 +212,16 @@ pub fn menu<R: Runtime>(app_handle: &AppHandle<R>, tab_items: &[TabItem]) -> Res
215212
// Create a vector of all window menu items
216213
let minimize = PredefinedMenuItem::minimize(app_handle, None)?;
217214
let maximize = PredefinedMenuItem::maximize(app_handle, None)?;
218-
#[cfg(target_os = "macos")]
219215
let separator1 = PredefinedMenuItem::separator(app_handle)?;
220216

221217
let mut window_items: Vec<&dyn IsMenuItem<R>> = vec![&minimize, &maximize];
222218

223-
#[cfg(target_os = "macos")]
224-
window_items.push(&separator1);
219+
if !tab_menu_items.is_empty() {
220+
window_items.push(&separator1);
221+
}
225222

226223
// Add tab menu items
227-
for item in tab_menu_items.iter().flatten() {
224+
for item in tab_menu_items.iter() {
228225
window_items.push(item);
229226
}
230227

0 commit comments

Comments
 (0)