Skip to content

Commit 9105588

Browse files
feat: add implicit default menu for macOS only, closes #4551 (#4570)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 0fa7453 commit 9105588

File tree

19 files changed

+55
-114
lines changed

19 files changed

+55
-114
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
`tauri::Builder` will now include a default menu for macOS without explicitly using `Menu::os_default`, you can still override it through `tauri::Builder::menu` or remove it using `tauri::Builder::enable_macos_default_menu(false)`.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Changed the app template to not set the default app menu as it is now set automatically on macOS which is the platform that needs a menu to function properly.

core/tauri/src/app.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,10 @@ pub struct Builder<R: Runtime> {
866866
/// The menu set to all windows.
867867
menu: Option<Menu>,
868868

869+
/// Enable macOS default menu creation.
870+
#[allow(unused)]
871+
enable_macos_default_menu: bool,
872+
869873
/// Menu event handlers that listens to all windows.
870874
menu_event_listeners: Vec<GlobalMenuEventListener<R>>,
871875

@@ -902,6 +906,7 @@ impl<R: Runtime> Builder<R> {
902906
uri_scheme_protocols: Default::default(),
903907
state: StateManager::new(),
904908
menu: None,
909+
enable_macos_default_menu: true,
905910
menu_event_listeners: Vec::new(),
906911
window_event_listeners: Vec::new(),
907912
#[cfg(feature = "system-tray")]
@@ -1332,6 +1337,11 @@ impl<R: Runtime> Builder<R> {
13321337
/// Builds the application.
13331338
#[allow(clippy::type_complexity)]
13341339
pub fn build<A: Assets>(mut self, context: Context<A>) -> crate::Result<App<R>> {
1340+
#[cfg(target_os = "macos")]
1341+
if self.menu.is_none() && self.enable_macos_default_menu {
1342+
self.menu = Some(Menu::os_default(&context.package_info().name));
1343+
}
1344+
13351345
#[cfg(feature = "system-tray")]
13361346
let system_tray_icon = context.system_tray_icon.clone();
13371347

examples/commands/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,7 @@ fn borrow_cmd_async(argument: &str) -> &str {
157157
}
158158

159159
fn main() {
160-
let context = tauri::generate_context!("../../examples/commands/tauri.conf.json");
161160
tauri::Builder::default()
162-
.menu(if cfg!(target_os = "macos") {
163-
tauri::Menu::os_default(&context.package_info().name)
164-
} else {
165-
tauri::Menu::default()
166-
})
167161
.manage(MyState {
168162
value: 0,
169163
label: "Tauri!".into(),
@@ -193,6 +187,8 @@ fn main() {
193187
future_simple_command_with_result,
194188
async_stateful_command_with_result,
195189
])
196-
.run(context)
190+
.run(tauri::generate_context!(
191+
"../../examples/commands/tauri.conf.json"
192+
))
197193
.expect("error while running tauri application");
198194
}

examples/helloworld/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
)]
99

1010
fn main() {
11-
let context = tauri::generate_context!("../../examples/helloworld/tauri.conf.json");
1211
tauri::Builder::default()
13-
.menu(if cfg!(target_os = "macos") {
14-
tauri::Menu::os_default(&context.package_info().name)
15-
} else {
16-
tauri::Menu::default()
17-
})
18-
.run(context)
12+
.run(tauri::generate_context!(
13+
"../../examples/helloworld/tauri.conf.json"
14+
))
1915
.expect("error while running tauri application");
2016
}

examples/isolation/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@ fn main() {
2121

2222
#[cfg(feature = "isolation")]
2323
fn main() {
24-
let context = tauri::generate_context!("../../examples/isolation/tauri.conf.json");
2524
tauri::Builder::default()
26-
.menu(if cfg!(target_os = "macos") {
27-
tauri::Menu::os_default(&context.package_info().name)
28-
} else {
29-
tauri::Menu::default()
30-
})
3125
.invoke_handler(tauri::generate_handler![ping])
32-
.run(context)
26+
.run(tauri::generate_context!(
27+
"../../examples/isolation/tauri.conf.json"
28+
))
3329
.expect("error while running tauri application");
3430
}

examples/multiwindow/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@
1010
use tauri::WindowBuilder;
1111

1212
fn main() {
13-
let context = tauri::generate_context!("../../examples/multiwindow/tauri.conf.json");
1413
tauri::Builder::default()
15-
.menu(if cfg!(target_os = "macos") {
16-
tauri::Menu::os_default(&context.package_info().name)
17-
} else {
18-
tauri::Menu::default()
19-
})
2014
.on_page_load(|window, _payload| {
2115
let label = window.label().to_string();
2216
window.listen("clicked".to_string(), move |_payload| {
@@ -33,6 +27,8 @@ fn main() {
3327
.build()?;
3428
Ok(())
3529
})
36-
.run(context)
30+
.run(tauri::generate_context!(
31+
"../../examples/multiwindow/tauri.conf.json"
32+
))
3733
.expect("failed to run tauri application");
3834
}

examples/navigation/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,9 @@
88
)]
99

1010
fn main() {
11-
let context = tauri::generate_context!("../../examples/navigation/tauri.conf.json");
1211
tauri::Builder::default()
13-
.menu(if cfg!(target_os = "macos") {
14-
tauri::Menu::os_default(&context.package_info().name)
15-
} else {
16-
tauri::Menu::default()
17-
})
18-
.run(context)
12+
.run(tauri::generate_context!(
13+
"../../examples/navigation/tauri.conf.json"
14+
))
1915
.expect("error while running tauri application");
2016
}

examples/parent-window/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ async fn create_child_window(id: String, window: Window) {
2424
}
2525

2626
fn main() {
27-
let context = tauri::generate_context!("../../examples/parent-window/tauri.conf.json");
2827
tauri::Builder::default()
29-
.menu(if cfg!(target_os = "macos") {
30-
tauri::Menu::os_default(&context.package_info().name)
31-
} else {
32-
tauri::Menu::default()
33-
})
3428
.on_page_load(|window, _payload| {
3529
let label = window.label().to_string();
3630
window.listen("clicked".to_string(), move |_payload| {
@@ -45,6 +39,8 @@ fn main() {
4539
.build()?;
4640
Ok(())
4741
})
48-
.run(context)
42+
.run(tauri::generate_context!(
43+
"../../examples/parent-window/tauri.conf.json"
44+
))
4945
.expect("failed to run tauri application");
5046
}

examples/resources/src-tauri/src/main.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,7 @@ fn main() {
1313
Manager,
1414
};
1515

16-
let context = tauri::generate_context!();
17-
1816
tauri::Builder::default()
19-
.menu(if cfg!(target_os = "macos") {
20-
tauri::Menu::os_default(&context.package_info().name)
21-
} else {
22-
tauri::Menu::default()
23-
})
2417
.setup(move |app| {
2518
let window = app.get_window("main").unwrap();
2619
let script_path = app
@@ -47,6 +40,6 @@ fn main() {
4740

4841
Ok(())
4942
})
50-
.run(context)
43+
.run(tauri::generate_context!())
5144
.expect("error while running tauri application");
5245
}

0 commit comments

Comments
 (0)