Skip to content

Commit 7cc95e1

Browse files
authored
feat(core): add Menu::with_items, closes #2807 (#2966)
1 parent 3a04c03 commit 7cc95e1

5 files changed

Lines changed: 52 additions & 3 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": patch
3+
"tauri": patch
4+
---
5+
6+
Add `Menu::with_items` constructor, taking an iterator of `MenuEntry`.

core/tauri-runtime/src/menu.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@ impl Menu {
186186
Default::default()
187187
}
188188

189+
/// Creates a new window menu with the given items.
190+
///
191+
/// # Example
192+
/// ```
193+
/// # use tauri_runtime::menu::{Menu, MenuItem, CustomMenuItem, Submenu};
194+
/// Menu::with_items([
195+
/// MenuItem::SelectAll.into(),
196+
/// #[cfg(target_os = "macos")]
197+
/// MenuItem::Redo.into(),
198+
/// CustomMenuItem::new("toggle", "Toggle visibility").into(),
199+
/// Submenu::new("View", Menu::new()).into(),
200+
/// ]);
201+
/// ```
202+
pub fn with_items<I: IntoIterator<Item = MenuEntry>>(items: I) -> Self {
203+
Self {
204+
items: items.into_iter().collect(),
205+
}
206+
}
207+
189208
/// Adds the custom menu item to the menu.
190209
pub fn add_item(mut self, item: CustomMenuItem) -> Self {
191210
self.items.push(MenuEntry::CustomItem(item));
@@ -349,6 +368,24 @@ pub enum MenuEntry {
349368
Submenu(Submenu),
350369
}
351370

371+
impl From<CustomMenuItem> for MenuEntry {
372+
fn from(item: CustomMenuItem) -> Self {
373+
Self::CustomItem(item)
374+
}
375+
}
376+
377+
impl From<MenuItem> for MenuEntry {
378+
fn from(item: MenuItem) -> Self {
379+
Self::NativeItem(item)
380+
}
381+
}
382+
383+
impl From<Submenu> for MenuEntry {
384+
fn from(submenu: Submenu) -> Self {
385+
Self::Submenu(submenu)
386+
}
387+
}
388+
352389
/// A menu item, bound to a pre-defined action or `Custom` emit an event. Note that status bar only
353390
/// supports `Custom` menu item variants. And on the menu bar, some platforms might not support some
354391
/// of the variants. Unsupported variant will be no-op on such platform.

core/tauri/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use serde::Serialize;
6565
use std::{collections::HashMap, fmt, sync::Arc};
6666

6767
// Export types likely to be used by the application.
68-
pub use runtime::{http, menu::CustomMenuItem};
68+
pub use runtime::http;
6969

7070
#[cfg(target_os = "macos")]
7171
#[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
@@ -82,7 +82,7 @@ pub use {
8282
};
8383
pub use {
8484
self::app::WindowMenuEvent,
85-
self::runtime::menu::{Menu, MenuItem, Submenu},
85+
self::runtime::menu::{CustomMenuItem, Menu, MenuEntry, MenuItem, Submenu},
8686
self::window::menu::MenuEvent,
8787
};
8888
pub use {

docs/usage/guides/visual/menu.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ let menu = Menu::new()
2525
.add_native_item(MenuItem::Copy)
2626
.add_item(CustomMenuItem::new("hide", "Hide"))
2727
.add_submenu(submenu);
28+
// alternatively, using the `with_items` constructor, useful if you end up using conditional compilation
29+
let menu = Menu::with_items([
30+
MenuItem::Copy.into(),
31+
CustomMenuItem::new("hide", "Hide").into(),
32+
submenu.into(),
33+
])
2834
```
2935

3036
### Adding the menu to all windows

examples/api/src-tauri/src/menu.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn get_menu() -> Menu {
1717
}
1818

1919
// create a submenu
20-
let my_sub_menu = Menu::new().add_item(disable_item);
20+
let my_sub_menu = Menu::with_items([disable_item.into()]);
2121

2222
let my_app_menu = Menu::new()
2323
.add_native_item(MenuItem::Copy)

0 commit comments

Comments
 (0)