Skip to content

Commit

Permalink
feat: implement Default for Menu, closes #2398
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Jun 7, 2022
1 parent 3455244 commit c94019a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .changes/default-menu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"tauri": "patch"
"tauri-runtime": "patch"
---

* Change `Menu::default` to create a default menu filled with default menu items and menus. Previously, it returned an empty menu and now you can do:
```diff
tauri::Builder::default()
+ .menu(tauri::Menu::default())
.run(tauri::generate_context!())
.expect("error while running tauri application");
```
68 changes: 66 additions & 2 deletions core/tauri-runtime/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub trait TrayHandle: fmt::Debug + Clone + Send + Sync {
}

/// A window menu.
#[derive(Debug, Default, Clone)]
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Menu {
pub items: Vec<MenuEntry>,
Expand Down Expand Up @@ -183,7 +183,7 @@ impl Submenu {
impl Menu {
/// Creates a new window menu.
pub fn new() -> Self {
Default::default()
Self { items: Vec::new() }
}

/// Creates a new window menu with the given items.
Expand Down Expand Up @@ -227,6 +227,70 @@ impl Menu {
}
}

impl Default for Menu {
fn default() -> Self {
let mut menu = Menu::new();
#[cfg(target_os = "macos")]
{
menu = menu.add_submenu(Submenu::new(
"app_name",
Menu::new()
.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));

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);
edit_menu = edit_menu.add_native_item(MenuItem::SelectAll);
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
}
}

/// A custom menu item.
#[derive(Debug, Clone)]
#[non_exhaustive]
Expand Down

0 comments on commit c94019a

Please sign in to comment.