mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-01 10:01:07 +02:00
committed by
GitHub
parent
3a04c036ff
commit
7cc95e10ec
6
.changes/menu-with-items-constructor.md
Normal file
6
.changes/menu-with-items-constructor.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"tauri-runtime": patch
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
Add `Menu::with_items` constructor, taking an iterator of `MenuEntry`.
|
||||
@@ -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<I: IntoIterator<Item = MenuEntry>>(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<CustomMenuItem> for MenuEntry {
|
||||
fn from(item: CustomMenuItem) -> Self {
|
||||
Self::CustomItem(item)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<MenuItem> for MenuEntry {
|
||||
fn from(item: MenuItem) -> Self {
|
||||
Self::NativeItem(item)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Submenu> 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.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user