Skip to content

Commit

Permalink
feat: implement Default for Menu, closes #2398 (#4291)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
amrbashir and lucasfernog committed Jun 15, 2022
1 parent 2bb4faa commit 4c4acc3
Show file tree
Hide file tree
Showing 24 changed files with 223 additions and 72 deletions.
6 changes: 6 additions & 0 deletions .changes/default-menu-on-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Use the default window menu in the app template.
6 changes: 6 additions & 0 deletions .changes/default-menu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": "patch"
"tauri-runtime": "patch"
---

Add `Menu::os_default` which will create a menu filled with default menu items and submenus.
125 changes: 125 additions & 0 deletions core/tauri-runtime/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,131 @@ impl Menu {
Default::default()
}

/// Creates a menu filled with default menu items and submenus.
///
/// ## Platform-specific:
///
/// - **Windows**:
/// - File
/// - CloseWindow
/// - Quit
/// - Edit
/// - Cut
/// - Copy
/// - Paste
/// - Window
/// - Minimize
/// - CloseWindow
///
/// - **Linux**:
/// - File
/// - CloseWindow
/// - Quit
/// - Window
/// - Minimize
/// - CloseWindow
///
/// - **macOS**:
/// - App
/// - About
/// - Separator
/// - Services
/// - Separator
/// - Hide
/// - HideOthers
/// - ShowAll
/// - Separator
/// - Quit
/// - File
/// - CloseWindow
/// - Edit
/// - Undo
/// - Redo
/// - Separator
/// - Cut
/// - Copy
/// - Paste
/// - SelectAll
/// - View
/// - EnterFullScreen
/// - Window
/// - Minimize
/// - Zoom
/// - Separator
/// - CloseWindow
pub fn os_default(#[allow(unused)] app_name: &str) -> Self {
let mut menu = Menu::new();
#[cfg(target_os = "macos")]
{
menu = menu.add_submenu(Submenu::new(
app_name,
Menu::new()
.add_native_item(MenuItem::About(
app_name.to_string(),
AboutMetadata::default(),
))
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Services)
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Hide)
.add_native_item(MenuItem::HideOthers)
.add_native_item(MenuItem::ShowAll)
.add_native_item(MenuItem::Separator)
.add_native_item(MenuItem::Quit),
));
}

let mut file_menu = Menu::new();
file_menu = file_menu.add_native_item(MenuItem::CloseWindow);
#[cfg(not(target_os = "macos"))]
{
file_menu = file_menu.add_native_item(MenuItem::Quit);
}
menu = menu.add_submenu(Submenu::new("File", file_menu));

#[cfg(not(target_os = "linux"))]
let mut edit_menu = Menu::new();
#[cfg(target_os = "macos")]
{
edit_menu = edit_menu.add_native_item(MenuItem::Undo);
edit_menu = edit_menu.add_native_item(MenuItem::Redo);
edit_menu = edit_menu.add_native_item(MenuItem::Separator);
}
#[cfg(not(target_os = "linux"))]
{
edit_menu = edit_menu.add_native_item(MenuItem::Cut);
edit_menu = edit_menu.add_native_item(MenuItem::Copy);
edit_menu = edit_menu.add_native_item(MenuItem::Paste);
}
#[cfg(target_os = "macos")]
{
edit_menu = edit_menu.add_native_item(MenuItem::SelectAll);
}
#[cfg(not(target_os = "linux"))]
{
menu = menu.add_submenu(Submenu::new("Edit", edit_menu));
}
#[cfg(target_os = "macos")]
{
menu = menu.add_submenu(Submenu::new(
"View",
Menu::new().add_native_item(MenuItem::EnterFullScreen),
));
}

let mut window_menu = Menu::new();
window_menu = window_menu.add_native_item(MenuItem::Minimize);
#[cfg(target_os = "macos")]
{
window_menu = window_menu.add_native_item(MenuItem::Zoom);
window_menu = window_menu.add_native_item(MenuItem::Separator);
}
window_menu = window_menu.add_native_item(MenuItem::CloseWindow);
menu = menu.add_submenu(Submenu::new("Window", window_menu));

menu
}

/// Creates a new window menu with the given items.
///
/// # Examples
Expand Down
16 changes: 8 additions & 8 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions examples/commands/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ fn borrow_cmd_async(argument: &str) -> &str {
}

fn main() {
let context = tauri::generate_context!("../../examples/commands/tauri.conf.json");
tauri::Builder::default()
.menu(tauri::Menu::os_default(&context.package_info().name))
.manage(MyState {
value: 0,
label: "Tauri!".into(),
Expand Down Expand Up @@ -187,8 +189,6 @@ fn main() {
future_simple_command_with_result,
async_stateful_command_with_result,
])
.run(tauri::generate_context!(
"../../examples/commands/tauri.conf.json"
))
.run(context)
.expect("error while running tauri application");
}
6 changes: 3 additions & 3 deletions examples/helloworld/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
)]

fn main() {
let context = tauri::generate_context!("../../examples/helloworld/tauri.conf.json");
tauri::Builder::default()
.run(tauri::generate_context!(
"../../examples/helloworld/tauri.conf.json"
))
.menu(tauri::Menu::os_default(&context.package_info().name))
.run(context)
.expect("error while running tauri application");
}
6 changes: 3 additions & 3 deletions examples/isolation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ fn main() {

#[cfg(feature = "isolation")]
fn main() {
let context = tauri::generate_context!("../../examples/isolation/tauri.conf.json");
tauri::Builder::default()
.menu(tauri::Menu::os_default(&context.package_info().name))
.invoke_handler(tauri::generate_handler![ping])
.run(tauri::generate_context!(
"../../examples/isolation/tauri.conf.json"
))
.run(context)
.expect("error while running tauri application");
}
6 changes: 3 additions & 3 deletions examples/multiwindow/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use tauri::WindowBuilder;

fn main() {
let context = tauri::generate_context!("../../examples/multiwindow/tauri.conf.json");
tauri::Builder::default()
.menu(tauri::Menu::os_default(&context.package_info().name))
.on_page_load(|window, _payload| {
let label = window.label().to_string();
window.listen("clicked".to_string(), move |_payload| {
Expand All @@ -27,8 +29,6 @@ fn main() {
.build()?;
Ok(())
})
.run(tauri::generate_context!(
"../../examples/multiwindow/tauri.conf.json"
))
.run(context)
.expect("failed to run tauri application");
}
6 changes: 3 additions & 3 deletions examples/navigation/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
)]

fn main() {
let context = tauri::generate_context!("../../examples/navigation/tauri.conf.json");
tauri::Builder::default()
.run(tauri::generate_context!(
"../../examples/navigation/tauri.conf.json"
))
.menu(tauri::Menu::os_default(&context.package_info().name))
.run(context)
.expect("error while running tauri application");
}
6 changes: 3 additions & 3 deletions examples/parent-window/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ 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(tauri::Menu::os_default(&context.package_info().name))
.on_page_load(|window, _payload| {
let label = window.label().to_string();
window.listen("clicked".to_string(), move |_payload| {
Expand All @@ -39,8 +41,6 @@ fn main() {
.build()?;
Ok(())
})
.run(tauri::generate_context!(
"../../examples/parent-window/tauri.conf.json"
))
.run(context)
.expect("failed to run tauri application");
}
12 changes: 6 additions & 6 deletions examples/resources/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/resources/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ fn main() {
Manager,
};

let context = tauri::generate_context!();

tauri::Builder::default()
.menu(tauri::Menu::os_default(&context.package_info().name))
.setup(move |app| {
let window = app.get_window("main").unwrap();
let script_path = app
Expand All @@ -40,6 +43,6 @@ fn main() {

Ok(())
})
.run(tauri::generate_context!())
.run(context)
.expect("error while running tauri application");
}
Loading

0 comments on commit 4c4acc3

Please sign in to comment.