From 7cc95e10ec66d8b155e9bb7f89cf73df56d1f107 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Mon, 27 Dec 2021 10:48:13 -0300 Subject: [PATCH] feat(core): add `Menu::with_items`, closes #2807 (#2966) --- .changes/menu-with-items-constructor.md | 6 ++++ core/tauri-runtime/src/menu.rs | 37 +++++++++++++++++++++++++ core/tauri/src/lib.rs | 4 +-- docs/usage/guides/visual/menu.md | 6 ++++ examples/api/src-tauri/src/menu.rs | 2 +- 5 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 .changes/menu-with-items-constructor.md diff --git a/.changes/menu-with-items-constructor.md b/.changes/menu-with-items-constructor.md new file mode 100644 index 000000000..1c5a43f0f --- /dev/null +++ b/.changes/menu-with-items-constructor.md @@ -0,0 +1,6 @@ +--- +"tauri-runtime": patch +"tauri": patch +--- + +Add `Menu::with_items` constructor, taking an iterator of `MenuEntry`. diff --git a/core/tauri-runtime/src/menu.rs b/core/tauri-runtime/src/menu.rs index 747ed5c03..f3ea9d3a7 100644 --- a/core/tauri-runtime/src/menu.rs +++ b/core/tauri-runtime/src/menu.rs @@ -186,6 +186,25 @@ impl Menu { Default::default() } + /// Creates a new window menu with the given items. + /// + /// # Example + /// ``` + /// # use tauri_runtime::menu::{Menu, MenuItem, CustomMenuItem, Submenu}; + /// Menu::with_items([ + /// MenuItem::SelectAll.into(), + /// #[cfg(target_os = "macos")] + /// MenuItem::Redo.into(), + /// CustomMenuItem::new("toggle", "Toggle visibility").into(), + /// Submenu::new("View", Menu::new()).into(), + /// ]); + /// ``` + pub fn with_items>(items: I) -> Self { + Self { + items: items.into_iter().collect(), + } + } + /// Adds the custom menu item to the menu. pub fn add_item(mut self, item: CustomMenuItem) -> Self { self.items.push(MenuEntry::CustomItem(item)); @@ -349,6 +368,24 @@ pub enum MenuEntry { Submenu(Submenu), } +impl From for MenuEntry { + fn from(item: CustomMenuItem) -> Self { + Self::CustomItem(item) + } +} + +impl From for MenuEntry { + fn from(item: MenuItem) -> Self { + Self::NativeItem(item) + } +} + +impl From for MenuEntry { + fn from(submenu: Submenu) -> Self { + Self::Submenu(submenu) + } +} + /// A menu item, bound to a pre-defined action or `Custom` emit an event. Note that status bar only /// supports `Custom` menu item variants. And on the menu bar, some platforms might not support some /// of the variants. Unsupported variant will be no-op on such platform. diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index ba6f36a7e..166b7d9b8 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -65,7 +65,7 @@ use serde::Serialize; use std::{collections::HashMap, fmt, sync::Arc}; // Export types likely to be used by the application. -pub use runtime::{http, menu::CustomMenuItem}; +pub use runtime::http; #[cfg(target_os = "macos")] #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))] @@ -82,7 +82,7 @@ pub use { }; pub use { self::app::WindowMenuEvent, - self::runtime::menu::{Menu, MenuItem, Submenu}, + self::runtime::menu::{CustomMenuItem, Menu, MenuEntry, MenuItem, Submenu}, self::window::menu::MenuEvent, }; pub use { diff --git a/docs/usage/guides/visual/menu.md b/docs/usage/guides/visual/menu.md index b82a2dcc8..f995c40ef 100644 --- a/docs/usage/guides/visual/menu.md +++ b/docs/usage/guides/visual/menu.md @@ -25,6 +25,12 @@ let menu = Menu::new() .add_native_item(MenuItem::Copy) .add_item(CustomMenuItem::new("hide", "Hide")) .add_submenu(submenu); +// alternatively, using the `with_items` constructor, useful if you end up using conditional compilation +let menu = Menu::with_items([ + MenuItem::Copy.into(), + CustomMenuItem::new("hide", "Hide").into(), + submenu.into(), +]) ``` ### Adding the menu to all windows diff --git a/examples/api/src-tauri/src/menu.rs b/examples/api/src-tauri/src/menu.rs index a6cc9a62a..026f3b7a4 100644 --- a/examples/api/src-tauri/src/menu.rs +++ b/examples/api/src-tauri/src/menu.rs @@ -17,7 +17,7 @@ pub fn get_menu() -> Menu { } // create a submenu - let my_sub_menu = Menu::new().add_item(disable_item); + let my_sub_menu = Menu::with_items([disable_item.into()]); let my_app_menu = Menu::new() .add_native_item(MenuItem::Copy)