From 9105588373cc8401bd9ad79bdef26f509b2d76b7 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Mon, 4 Jul 2022 01:26:32 +0200 Subject: [PATCH] feat: add implicit default menu for macOS only, closes #4551 (#4570) Co-authored-by: Lucas Nogueira --- .changes/builder-default-menu-macos.md | 5 +++++ .changes/cli-remove-menu-from-template.md | 6 ++++++ core/tauri/src/app.rs | 10 ++++++++++ examples/commands/main.rs | 10 +++------- examples/helloworld/main.rs | 10 +++------- examples/isolation/main.rs | 10 +++------- examples/multiwindow/main.rs | 10 +++------- examples/navigation/main.rs | 10 +++------- examples/parent-window/main.rs | 10 +++------- examples/resources/src-tauri/src/main.rs | 9 +-------- examples/sidecar/src-tauri/src/main.rs | 8 +------- examples/splashscreen/main.rs | 10 +++------- examples/state/main.rs | 10 +++------- examples/streaming/main.rs | 11 +++-------- examples/tauri-dynamic-lib/src-tauri/src/lib.rs | 8 +------- examples/updater/src-tauri/src/main.rs | 8 +------- tooling/cli/templates/app/src-tauri/src/main.rs | 8 +------- .../backend/examples/vanilla/src-tauri/src/main.rs | 8 +------- .../examples/svelte-app/src-tauri/src/main.rs | 8 +------- 19 files changed, 55 insertions(+), 114 deletions(-) create mode 100644 .changes/builder-default-menu-macos.md create mode 100644 .changes/cli-remove-menu-from-template.md diff --git a/.changes/builder-default-menu-macos.md b/.changes/builder-default-menu-macos.md new file mode 100644 index 000000000000..09245e9b7bb0 --- /dev/null +++ b/.changes/builder-default-menu-macos.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +`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)`. diff --git a/.changes/cli-remove-menu-from-template.md b/.changes/cli-remove-menu-from-template.md new file mode 100644 index 000000000000..0dde4bcd76e8 --- /dev/null +++ b/.changes/cli-remove-menu-from-template.md @@ -0,0 +1,6 @@ +--- +"cli.rs": patch +"cli.js": patch +--- + +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. diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 9829eefc5fc6..6d20213fc428 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -866,6 +866,10 @@ pub struct Builder { /// The menu set to all windows. menu: Option, + /// Enable macOS default menu creation. + #[allow(unused)] + enable_macos_default_menu: bool, + /// Menu event handlers that listens to all windows. menu_event_listeners: Vec>, @@ -902,6 +906,7 @@ impl Builder { uri_scheme_protocols: Default::default(), state: StateManager::new(), menu: None, + enable_macos_default_menu: true, menu_event_listeners: Vec::new(), window_event_listeners: Vec::new(), #[cfg(feature = "system-tray")] @@ -1332,6 +1337,11 @@ impl Builder { /// Builds the application. #[allow(clippy::type_complexity)] pub fn build(mut self, context: Context) -> crate::Result> { + #[cfg(target_os = "macos")] + if self.menu.is_none() && self.enable_macos_default_menu { + self.menu = Some(Menu::os_default(&context.package_info().name)); + } + #[cfg(feature = "system-tray")] let system_tray_icon = context.system_tray_icon.clone(); diff --git a/examples/commands/main.rs b/examples/commands/main.rs index ea4f60ce92f6..9c609757824b 100644 --- a/examples/commands/main.rs +++ b/examples/commands/main.rs @@ -157,13 +157,7 @@ fn borrow_cmd_async(argument: &str) -> &str { } fn main() { - let context = tauri::generate_context!("../../examples/commands/tauri.conf.json"); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .manage(MyState { value: 0, label: "Tauri!".into(), @@ -193,6 +187,8 @@ fn main() { future_simple_command_with_result, async_stateful_command_with_result, ]) - .run(context) + .run(tauri::generate_context!( + "../../examples/commands/tauri.conf.json" + )) .expect("error while running tauri application"); } diff --git a/examples/helloworld/main.rs b/examples/helloworld/main.rs index 182a620c2578..d538ecda7450 100644 --- a/examples/helloworld/main.rs +++ b/examples/helloworld/main.rs @@ -8,13 +8,9 @@ )] fn main() { - let context = tauri::generate_context!("../../examples/helloworld/tauri.conf.json"); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) - .run(context) + .run(tauri::generate_context!( + "../../examples/helloworld/tauri.conf.json" + )) .expect("error while running tauri application"); } diff --git a/examples/isolation/main.rs b/examples/isolation/main.rs index 3c2f7f577575..26b99642a74a 100644 --- a/examples/isolation/main.rs +++ b/examples/isolation/main.rs @@ -21,14 +21,10 @@ fn main() { #[cfg(feature = "isolation")] fn main() { - let context = tauri::generate_context!("../../examples/isolation/tauri.conf.json"); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .invoke_handler(tauri::generate_handler![ping]) - .run(context) + .run(tauri::generate_context!( + "../../examples/isolation/tauri.conf.json" + )) .expect("error while running tauri application"); } diff --git a/examples/multiwindow/main.rs b/examples/multiwindow/main.rs index 5f21450b5757..17340cb09a7b 100644 --- a/examples/multiwindow/main.rs +++ b/examples/multiwindow/main.rs @@ -10,13 +10,7 @@ use tauri::WindowBuilder; fn main() { - let context = tauri::generate_context!("../../examples/multiwindow/tauri.conf.json"); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .on_page_load(|window, _payload| { let label = window.label().to_string(); window.listen("clicked".to_string(), move |_payload| { @@ -33,6 +27,8 @@ fn main() { .build()?; Ok(()) }) - .run(context) + .run(tauri::generate_context!( + "../../examples/multiwindow/tauri.conf.json" + )) .expect("failed to run tauri application"); } diff --git a/examples/navigation/main.rs b/examples/navigation/main.rs index 1d56a2b1b3aa..334b996ffab3 100644 --- a/examples/navigation/main.rs +++ b/examples/navigation/main.rs @@ -8,13 +8,9 @@ )] fn main() { - let context = tauri::generate_context!("../../examples/navigation/tauri.conf.json"); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) - .run(context) + .run(tauri::generate_context!( + "../../examples/navigation/tauri.conf.json" + )) .expect("error while running tauri application"); } diff --git a/examples/parent-window/main.rs b/examples/parent-window/main.rs index c5b3b11398ea..ed64fdcf556f 100644 --- a/examples/parent-window/main.rs +++ b/examples/parent-window/main.rs @@ -24,13 +24,7 @@ async fn create_child_window(id: String, window: Window) { } fn main() { - let context = tauri::generate_context!("../../examples/parent-window/tauri.conf.json"); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .on_page_load(|window, _payload| { let label = window.label().to_string(); window.listen("clicked".to_string(), move |_payload| { @@ -45,6 +39,8 @@ fn main() { .build()?; Ok(()) }) - .run(context) + .run(tauri::generate_context!( + "../../examples/parent-window/tauri.conf.json" + )) .expect("failed to run tauri application"); } diff --git a/examples/resources/src-tauri/src/main.rs b/examples/resources/src-tauri/src/main.rs index e0371f2af243..71b5d3c0393b 100644 --- a/examples/resources/src-tauri/src/main.rs +++ b/examples/resources/src-tauri/src/main.rs @@ -13,14 +13,7 @@ fn main() { Manager, }; - let context = tauri::generate_context!(); - tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .setup(move |app| { let window = app.get_window("main").unwrap(); let script_path = app @@ -47,6 +40,6 @@ fn main() { Ok(()) }) - .run(context) + .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/examples/sidecar/src-tauri/src/main.rs b/examples/sidecar/src-tauri/src/main.rs index b78d214e2faa..5bd3afadd700 100644 --- a/examples/sidecar/src-tauri/src/main.rs +++ b/examples/sidecar/src-tauri/src/main.rs @@ -13,13 +13,7 @@ use tauri::{ }; fn main() { - let context = tauri::generate_context!(); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .setup(|app| { let window = app.get_window("main").unwrap(); tauri::async_runtime::spawn(async move { @@ -45,6 +39,6 @@ fn main() { Ok(()) }) - .run(context) + .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/examples/splashscreen/main.rs b/examples/splashscreen/main.rs index f4ea4b280178..375c9a2457b2 100644 --- a/examples/splashscreen/main.rs +++ b/examples/splashscreen/main.rs @@ -17,13 +17,7 @@ mod rust { fn close_splashscreen() {} pub fn main() { - let context = tauri::generate_context!("../../examples/splashscreen/tauri.conf.json"); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .setup(|app| { let splashscreen_window = app.get_window("splashscreen").unwrap(); let main_window = app.get_window("main").unwrap(); @@ -40,7 +34,9 @@ mod rust { Ok(()) }) .invoke_handler(tauri::generate_handler![close_splashscreen]) - .run(context) + .run(tauri::generate_context!( + "../../examples/splashscreen/tauri.conf.json" + )) .expect("failed to run app"); } } diff --git a/examples/state/main.rs b/examples/state/main.rs index 2b6aeedf8a1a..3a8dde6506a3 100644 --- a/examples/state/main.rs +++ b/examples/state/main.rs @@ -69,13 +69,7 @@ fn db_read(key: String, db: State<'_, Database>) -> Option { } fn main() { - let context = tauri::generate_context!("../../examples/state/tauri.conf.json"); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .manage(Counter(AtomicUsize::new(0))) .manage(Database(Default::default())) .manage(Connection(Default::default())) @@ -87,6 +81,8 @@ fn main() { disconnect, connection_send ]) - .run(context) + .run(tauri::generate_context!( + "../../examples/state/tauri.conf.json" + )) .expect("error while running tauri application"); } diff --git a/examples/streaming/main.rs b/examples/streaming/main.rs index 6900c53e3cde..b62d9db34bd4 100644 --- a/examples/streaming/main.rs +++ b/examples/streaming/main.rs @@ -38,14 +38,7 @@ fn main() { assert!(video_file.exists()); } - let context = tauri::generate_context!("../../examples/streaming/tauri.conf.json"); - tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .register_uri_scheme_protocol("stream", move |_app, request| { // prepare our response let mut response = ResponseBuilder::new(); @@ -119,6 +112,8 @@ fn main() { response.mimetype("video/mp4").status(status_code).body(buf) }) - .run(context) + .run(tauri::generate_context!( + "../../examples/streaming/tauri.conf.json" + )) .expect("error while running tauri application"); } diff --git a/examples/tauri-dynamic-lib/src-tauri/src/lib.rs b/examples/tauri-dynamic-lib/src-tauri/src/lib.rs index 9e065f0e0515..64e27bd6d18b 100644 --- a/examples/tauri-dynamic-lib/src-tauri/src/lib.rs +++ b/examples/tauri-dynamic-lib/src-tauri/src/lib.rs @@ -12,13 +12,7 @@ #[no_mangle] pub extern "C" fn run_tauri() { - let context = tauri::generate_context!("./tauri.conf.json"); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) - .run(context) + .run(tauri::generate_context!("./tauri.conf.json")) .expect("error while running tauri application"); } diff --git a/examples/updater/src-tauri/src/main.rs b/examples/updater/src-tauri/src/main.rs index b5f875c128d1..e5becf91ddbc 100644 --- a/examples/updater/src-tauri/src/main.rs +++ b/examples/updater/src-tauri/src/main.rs @@ -13,14 +13,8 @@ fn my_custom_command(argument: String) { } fn main() { - let context = tauri::generate_context!(); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .invoke_handler(tauri::generate_handler![my_custom_command]) - .run(context) + .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/tooling/cli/templates/app/src-tauri/src/main.rs b/tooling/cli/templates/app/src-tauri/src/main.rs index f07796ba614d..e994ea4d199c 100644 --- a/tooling/cli/templates/app/src-tauri/src/main.rs +++ b/tooling/cli/templates/app/src-tauri/src/main.rs @@ -4,13 +4,7 @@ )] fn main() { - let context = tauri::generate_context!(); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) - .run(context) + .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/tooling/cli/templates/plugin/backend/examples/vanilla/src-tauri/src/main.rs b/tooling/cli/templates/plugin/backend/examples/vanilla/src-tauri/src/main.rs index fa3b57f7f2e0..d052653bd0a8 100644 --- a/tooling/cli/templates/plugin/backend/examples/vanilla/src-tauri/src/main.rs +++ b/tooling/cli/templates/plugin/backend/examples/vanilla/src-tauri/src/main.rs @@ -4,14 +4,8 @@ )] fn main() { - let context = tauri::generate_context!(); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init()) - .run(context) + .run(tauri::generate_context!()) .expect("error while running tauri application"); } diff --git a/tooling/cli/templates/plugin/with-api/examples/svelte-app/src-tauri/src/main.rs b/tooling/cli/templates/plugin/with-api/examples/svelte-app/src-tauri/src/main.rs index 98c71c58e12c..cafd857d94fa 100644 --- a/tooling/cli/templates/plugin/with-api/examples/svelte-app/src-tauri/src/main.rs +++ b/tooling/cli/templates/plugin/with-api/examples/svelte-app/src-tauri/src/main.rs @@ -4,14 +4,8 @@ )] fn main() { - let context = tauri::generate_context!(); tauri::Builder::default() - .menu(if cfg!(target_os = "macos") { - tauri::Menu::os_default(&context.package_info().name) - } else { - tauri::Menu::default() - }) .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init()) - .run(context) + .run(tauri::generate_context!()) .expect("failed to run app"); }