feat: add implicit default menu for macOS only, closes #4551 (#4570)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Amr Bashir 2022-07-04 01:26:32 +02:00 committed by GitHub
parent 0fa745344e
commit 9105588373
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 55 additions and 114 deletions

View File

@ -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)`.

View File

@ -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.

View File

@ -866,6 +866,10 @@ pub struct Builder<R: Runtime> {
/// The menu set to all windows. /// The menu set to all windows.
menu: Option<Menu>, menu: Option<Menu>,
/// Enable macOS default menu creation.
#[allow(unused)]
enable_macos_default_menu: bool,
/// Menu event handlers that listens to all windows. /// Menu event handlers that listens to all windows.
menu_event_listeners: Vec<GlobalMenuEventListener<R>>, menu_event_listeners: Vec<GlobalMenuEventListener<R>>,
@ -902,6 +906,7 @@ impl<R: Runtime> Builder<R> {
uri_scheme_protocols: Default::default(), uri_scheme_protocols: Default::default(),
state: StateManager::new(), state: StateManager::new(),
menu: None, menu: None,
enable_macos_default_menu: true,
menu_event_listeners: Vec::new(), menu_event_listeners: Vec::new(),
window_event_listeners: Vec::new(), window_event_listeners: Vec::new(),
#[cfg(feature = "system-tray")] #[cfg(feature = "system-tray")]
@ -1332,6 +1337,11 @@ impl<R: Runtime> Builder<R> {
/// Builds the application. /// Builds the application.
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub fn build<A: Assets>(mut self, context: Context<A>) -> crate::Result<App<R>> { pub fn build<A: Assets>(mut self, context: Context<A>) -> crate::Result<App<R>> {
#[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")] #[cfg(feature = "system-tray")]
let system_tray_icon = context.system_tray_icon.clone(); let system_tray_icon = context.system_tray_icon.clone();

View File

@ -157,13 +157,7 @@ fn borrow_cmd_async(argument: &str) -> &str {
} }
fn main() { fn main() {
let context = tauri::generate_context!("../../examples/commands/tauri.conf.json");
tauri::Builder::default() tauri::Builder::default()
.menu(if cfg!(target_os = "macos") {
tauri::Menu::os_default(&context.package_info().name)
} else {
tauri::Menu::default()
})
.manage(MyState { .manage(MyState {
value: 0, value: 0,
label: "Tauri!".into(), label: "Tauri!".into(),
@ -193,6 +187,8 @@ fn main() {
future_simple_command_with_result, future_simple_command_with_result,
async_stateful_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"); .expect("error while running tauri application");
} }

View File

@ -8,13 +8,9 @@
)] )]
fn main() { fn main() {
let context = tauri::generate_context!("../../examples/helloworld/tauri.conf.json");
tauri::Builder::default() tauri::Builder::default()
.menu(if cfg!(target_os = "macos") { .run(tauri::generate_context!(
tauri::Menu::os_default(&context.package_info().name) "../../examples/helloworld/tauri.conf.json"
} else { ))
tauri::Menu::default()
})
.run(context)
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -21,14 +21,10 @@ fn main() {
#[cfg(feature = "isolation")] #[cfg(feature = "isolation")]
fn main() { fn main() {
let context = tauri::generate_context!("../../examples/isolation/tauri.conf.json");
tauri::Builder::default() 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]) .invoke_handler(tauri::generate_handler![ping])
.run(context) .run(tauri::generate_context!(
"../../examples/isolation/tauri.conf.json"
))
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -10,13 +10,7 @@
use tauri::WindowBuilder; use tauri::WindowBuilder;
fn main() { fn main() {
let context = tauri::generate_context!("../../examples/multiwindow/tauri.conf.json");
tauri::Builder::default() 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| { .on_page_load(|window, _payload| {
let label = window.label().to_string(); let label = window.label().to_string();
window.listen("clicked".to_string(), move |_payload| { window.listen("clicked".to_string(), move |_payload| {
@ -33,6 +27,8 @@ fn main() {
.build()?; .build()?;
Ok(()) Ok(())
}) })
.run(context) .run(tauri::generate_context!(
"../../examples/multiwindow/tauri.conf.json"
))
.expect("failed to run tauri application"); .expect("failed to run tauri application");
} }

View File

@ -8,13 +8,9 @@
)] )]
fn main() { fn main() {
let context = tauri::generate_context!("../../examples/navigation/tauri.conf.json");
tauri::Builder::default() tauri::Builder::default()
.menu(if cfg!(target_os = "macos") { .run(tauri::generate_context!(
tauri::Menu::os_default(&context.package_info().name) "../../examples/navigation/tauri.conf.json"
} else { ))
tauri::Menu::default()
})
.run(context)
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -24,13 +24,7 @@ async fn create_child_window(id: String, window: Window) {
} }
fn main() { fn main() {
let context = tauri::generate_context!("../../examples/parent-window/tauri.conf.json");
tauri::Builder::default() 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| { .on_page_load(|window, _payload| {
let label = window.label().to_string(); let label = window.label().to_string();
window.listen("clicked".to_string(), move |_payload| { window.listen("clicked".to_string(), move |_payload| {
@ -45,6 +39,8 @@ fn main() {
.build()?; .build()?;
Ok(()) Ok(())
}) })
.run(context) .run(tauri::generate_context!(
"../../examples/parent-window/tauri.conf.json"
))
.expect("failed to run tauri application"); .expect("failed to run tauri application");
} }

View File

@ -13,14 +13,7 @@ fn main() {
Manager, Manager,
}; };
let context = tauri::generate_context!();
tauri::Builder::default() tauri::Builder::default()
.menu(if cfg!(target_os = "macos") {
tauri::Menu::os_default(&context.package_info().name)
} else {
tauri::Menu::default()
})
.setup(move |app| { .setup(move |app| {
let window = app.get_window("main").unwrap(); let window = app.get_window("main").unwrap();
let script_path = app let script_path = app
@ -47,6 +40,6 @@ fn main() {
Ok(()) Ok(())
}) })
.run(context) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -13,13 +13,7 @@ use tauri::{
}; };
fn main() { fn main() {
let context = tauri::generate_context!();
tauri::Builder::default() tauri::Builder::default()
.menu(if cfg!(target_os = "macos") {
tauri::Menu::os_default(&context.package_info().name)
} else {
tauri::Menu::default()
})
.setup(|app| { .setup(|app| {
let window = app.get_window("main").unwrap(); let window = app.get_window("main").unwrap();
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
@ -45,6 +39,6 @@ fn main() {
Ok(()) Ok(())
}) })
.run(context) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -17,13 +17,7 @@ mod rust {
fn close_splashscreen() {} fn close_splashscreen() {}
pub fn main() { pub fn main() {
let context = tauri::generate_context!("../../examples/splashscreen/tauri.conf.json");
tauri::Builder::default() tauri::Builder::default()
.menu(if cfg!(target_os = "macos") {
tauri::Menu::os_default(&context.package_info().name)
} else {
tauri::Menu::default()
})
.setup(|app| { .setup(|app| {
let splashscreen_window = app.get_window("splashscreen").unwrap(); let splashscreen_window = app.get_window("splashscreen").unwrap();
let main_window = app.get_window("main").unwrap(); let main_window = app.get_window("main").unwrap();
@ -40,7 +34,9 @@ mod rust {
Ok(()) Ok(())
}) })
.invoke_handler(tauri::generate_handler![close_splashscreen]) .invoke_handler(tauri::generate_handler![close_splashscreen])
.run(context) .run(tauri::generate_context!(
"../../examples/splashscreen/tauri.conf.json"
))
.expect("failed to run app"); .expect("failed to run app");
} }
} }

View File

@ -69,13 +69,7 @@ fn db_read(key: String, db: State<'_, Database>) -> Option<String> {
} }
fn main() { fn main() {
let context = tauri::generate_context!("../../examples/state/tauri.conf.json");
tauri::Builder::default() 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(Counter(AtomicUsize::new(0)))
.manage(Database(Default::default())) .manage(Database(Default::default()))
.manage(Connection(Default::default())) .manage(Connection(Default::default()))
@ -87,6 +81,8 @@ fn main() {
disconnect, disconnect,
connection_send connection_send
]) ])
.run(context) .run(tauri::generate_context!(
"../../examples/state/tauri.conf.json"
))
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -38,14 +38,7 @@ fn main() {
assert!(video_file.exists()); assert!(video_file.exists());
} }
let context = tauri::generate_context!("../../examples/streaming/tauri.conf.json");
tauri::Builder::default() 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| { .register_uri_scheme_protocol("stream", move |_app, request| {
// prepare our response // prepare our response
let mut response = ResponseBuilder::new(); let mut response = ResponseBuilder::new();
@ -119,6 +112,8 @@ fn main() {
response.mimetype("video/mp4").status(status_code).body(buf) 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"); .expect("error while running tauri application");
} }

View File

@ -12,13 +12,7 @@
#[no_mangle] #[no_mangle]
pub extern "C" fn run_tauri() { pub extern "C" fn run_tauri() {
let context = tauri::generate_context!("./tauri.conf.json");
tauri::Builder::default() tauri::Builder::default()
.menu(if cfg!(target_os = "macos") { .run(tauri::generate_context!("./tauri.conf.json"))
tauri::Menu::os_default(&context.package_info().name)
} else {
tauri::Menu::default()
})
.run(context)
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -13,14 +13,8 @@ fn my_custom_command(argument: String) {
} }
fn main() { fn main() {
let context = tauri::generate_context!();
tauri::Builder::default() 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]) .invoke_handler(tauri::generate_handler![my_custom_command])
.run(context) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -4,13 +4,7 @@
)] )]
fn main() { fn main() {
let context = tauri::generate_context!();
tauri::Builder::default() tauri::Builder::default()
.menu(if cfg!(target_os = "macos") { .run(tauri::generate_context!())
tauri::Menu::os_default(&context.package_info().name)
} else {
tauri::Menu::default()
})
.run(context)
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -4,14 +4,8 @@
)] )]
fn main() { fn main() {
let context = tauri::generate_context!();
tauri::Builder::default() 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()) .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
.run(context) .run(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application");
} }

View File

@ -4,14 +4,8 @@
)] )]
fn main() { fn main() {
let context = tauri::generate_context!();
tauri::Builder::default() 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()) .plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
.run(context) .run(tauri::generate_context!())
.expect("failed to run app"); .expect("failed to run app");
} }