123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744 |
- // Copyright 2019-2021 Tauri Programme within The Commons Conservancy
- // SPDX-License-Identifier: Apache-2.0
- // SPDX-License-Identifier: MIT
- use std::{
- collections::hash_map::DefaultHasher,
- fmt,
- hash::{Hash, Hasher},
- };
- pub type MenuHash = u16;
- pub type MenuId = String;
- pub type MenuIdRef<'a> = &'a str;
- /// Named images defined by the system.
- #[cfg(target_os = "macos")]
- #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
- #[derive(Debug, Clone)]
- pub enum NativeImage {
- /// An add item template image.
- Add,
- /// Advanced preferences toolbar icon for the preferences window.
- Advanced,
- /// A Bluetooth template image.
- Bluetooth,
- /// Bookmarks image suitable for a template.
- Bookmarks,
- /// A caution image.
- Caution,
- /// A color panel toolbar icon.
- ColorPanel,
- /// A column view mode template image.
- ColumnView,
- /// A computer icon.
- Computer,
- /// An enter full-screen mode template image.
- EnterFullScreen,
- /// Permissions for all users.
- Everyone,
- /// An exit full-screen mode template image.
- ExitFullScreen,
- /// A cover flow view mode template image.
- FlowView,
- /// A folder image.
- Folder,
- /// A burnable folder icon.
- FolderBurnable,
- /// A smart folder icon.
- FolderSmart,
- /// A link template image.
- FollowLinkFreestanding,
- /// A font panel toolbar icon.
- FontPanel,
- /// A `go back` template image.
- GoLeft,
- /// A `go forward` template image.
- GoRight,
- /// Home image suitable for a template.
- Home,
- /// An iChat Theater template image.
- IChatTheater,
- /// An icon view mode template image.
- IconView,
- /// An information toolbar icon.
- Info,
- /// A template image used to denote invalid data.
- InvalidDataFreestanding,
- /// A generic left-facing triangle template image.
- LeftFacingTriangle,
- /// A list view mode template image.
- ListView,
- /// A locked padlock template image.
- LockLocked,
- /// An unlocked padlock template image.
- LockUnlocked,
- /// A horizontal dash, for use in menus.
- MenuMixedState,
- /// A check mark template image, for use in menus.
- MenuOnState,
- /// A MobileMe icon.
- MobileMe,
- /// A drag image for multiple items.
- MultipleDocuments,
- /// A network icon.
- Network,
- /// A path button template image.
- Path,
- /// General preferences toolbar icon for the preferences window.
- PreferencesGeneral,
- /// A Quick Look template image.
- QuickLook,
- /// A refresh template image.
- RefreshFreestanding,
- /// A refresh template image.
- Refresh,
- /// A remove item template image.
- Remove,
- /// A reveal contents template image.
- RevealFreestanding,
- /// A generic right-facing triangle template image.
- RightFacingTriangle,
- /// A share view template image.
- Share,
- /// A slideshow template image.
- Slideshow,
- /// A badge for a `smart` item.
- SmartBadge,
- /// Small green indicator, similar to iChat’s available image.
- StatusAvailable,
- /// Small clear indicator.
- StatusNone,
- /// Small yellow indicator, similar to iChat’s idle image.
- StatusPartiallyAvailable,
- /// Small red indicator, similar to iChat’s unavailable image.
- StatusUnavailable,
- /// A stop progress template image.
- StopProgressFreestanding,
- /// A stop progress button template image.
- StopProgress,
- /// An image of the empty trash can.
- TrashEmpty,
- /// An image of the full trash can.
- TrashFull,
- /// Permissions for a single user.
- User,
- /// User account toolbar icon for the preferences window.
- UserAccounts,
- /// Permissions for a group of users.
- UserGroup,
- /// Permissions for guests.
- UserGuest,
- }
- #[derive(Debug, Clone)]
- pub enum MenuUpdate {
- /// Modifies the enabled state of the menu item.
- SetEnabled(bool),
- /// Modifies the title (label) of the menu item.
- SetTitle(String),
- /// Modifies the selected state of the menu item.
- SetSelected(bool),
- /// Update native image.
- #[cfg(target_os = "macos")]
- #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
- SetNativeImage(NativeImage),
- }
- pub trait TrayHandle: fmt::Debug + Clone + Send + Sync {
- fn set_icon(&self, icon: crate::Icon) -> crate::Result<()>;
- fn set_menu(&self, menu: crate::menu::SystemTrayMenu) -> crate::Result<()>;
- fn update_item(&self, id: u16, update: MenuUpdate) -> crate::Result<()>;
- #[cfg(target_os = "macos")]
- fn set_icon_as_template(&self, is_template: bool) -> crate::Result<()>;
- fn destroy(&self) -> crate::Result<()>;
- }
- /// A window menu.
- #[derive(Debug, Default, Clone)]
- #[non_exhaustive]
- pub struct Menu {
- pub items: Vec<MenuEntry>,
- }
- #[derive(Debug, Clone)]
- #[non_exhaustive]
- pub struct Submenu {
- pub title: String,
- pub enabled: bool,
- pub inner: Menu,
- }
- impl Submenu {
- /// Creates a new submenu with the given title and menu items.
- pub fn new<S: Into<String>>(title: S, menu: Menu) -> Self {
- Self {
- title: title.into(),
- enabled: true,
- inner: menu,
- }
- }
- }
- impl Menu {
- /// Creates a new window menu.
- pub fn new() -> Self {
- 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
- /// ```
- /// # 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.
- #[must_use]
- pub fn add_item(mut self, item: CustomMenuItem) -> Self {
- self.items.push(MenuEntry::CustomItem(item));
- self
- }
- /// Adds a native item to the menu.
- #[must_use]
- pub fn add_native_item(mut self, item: MenuItem) -> Self {
- self.items.push(MenuEntry::NativeItem(item));
- self
- }
- /// Adds an entry with submenu.
- #[must_use]
- pub fn add_submenu(mut self, submenu: Submenu) -> Self {
- self.items.push(MenuEntry::Submenu(submenu));
- self
- }
- }
- /// A custom menu item.
- #[derive(Debug, Clone)]
- #[non_exhaustive]
- pub struct CustomMenuItem {
- pub id: MenuHash,
- pub id_str: MenuId,
- pub title: String,
- pub keyboard_accelerator: Option<String>,
- pub enabled: bool,
- pub selected: bool,
- #[cfg(target_os = "macos")]
- pub native_image: Option<NativeImage>,
- }
- impl CustomMenuItem {
- /// Create new custom menu item.
- pub fn new<I: Into<String>, T: Into<String>>(id: I, title: T) -> Self {
- let id_str = id.into();
- Self {
- id: Self::hash(&id_str),
- id_str,
- title: title.into(),
- keyboard_accelerator: None,
- enabled: true,
- selected: false,
- #[cfg(target_os = "macos")]
- native_image: None,
- }
- }
- /// Assign a keyboard shortcut to the menu action.
- #[must_use]
- pub fn accelerator<T: Into<String>>(mut self, accelerator: T) -> Self {
- self.keyboard_accelerator.replace(accelerator.into());
- self
- }
- #[cfg(target_os = "macos")]
- #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))]
- #[must_use]
- /// A native image do render on the menu item.
- pub fn native_image(mut self, image: NativeImage) -> Self {
- self.native_image.replace(image);
- self
- }
- /// Mark the item as disabled.
- #[must_use]
- pub fn disabled(mut self) -> Self {
- self.enabled = false;
- self
- }
- /// Mark the item as selected.
- #[must_use]
- pub fn selected(mut self) -> Self {
- self.selected = true;
- self
- }
- fn hash(id: &str) -> MenuHash {
- let mut hasher = DefaultHasher::new();
- id.hash(&mut hasher);
- hasher.finish() as MenuHash
- }
- }
- /// A system tray menu.
- #[derive(Debug, Default, Clone)]
- #[non_exhaustive]
- pub struct SystemTrayMenu {
- pub items: Vec<SystemTrayMenuEntry>,
- }
- #[derive(Debug, Clone)]
- #[non_exhaustive]
- pub struct SystemTraySubmenu {
- pub title: String,
- pub enabled: bool,
- pub inner: SystemTrayMenu,
- }
- impl SystemTraySubmenu {
- /// Creates a new submenu with the given title and menu items.
- pub fn new<S: Into<String>>(title: S, menu: SystemTrayMenu) -> Self {
- Self {
- title: title.into(),
- enabled: true,
- inner: menu,
- }
- }
- }
- impl SystemTrayMenu {
- /// Creates a new system tray menu.
- pub fn new() -> Self {
- Default::default()
- }
- /// Adds the custom menu item to the system tray menu.
- #[must_use]
- pub fn add_item(mut self, item: CustomMenuItem) -> Self {
- self.items.push(SystemTrayMenuEntry::CustomItem(item));
- self
- }
- /// Adds a native item to the system tray menu.
- #[must_use]
- pub fn add_native_item(mut self, item: SystemTrayMenuItem) -> Self {
- self.items.push(SystemTrayMenuEntry::NativeItem(item));
- self
- }
- /// Adds an entry with submenu.
- #[must_use]
- pub fn add_submenu(mut self, submenu: SystemTraySubmenu) -> Self {
- self.items.push(SystemTrayMenuEntry::Submenu(submenu));
- self
- }
- }
- /// An entry on the system tray menu.
- #[derive(Debug, Clone)]
- pub enum SystemTrayMenuEntry {
- /// A custom item.
- CustomItem(CustomMenuItem),
- /// A native item.
- NativeItem(SystemTrayMenuItem),
- /// An entry with submenu.
- Submenu(SystemTraySubmenu),
- }
- /// System tray menu item.
- #[derive(Debug, Clone)]
- #[non_exhaustive]
- pub enum SystemTrayMenuItem {
- /// A separator.
- Separator,
- }
- /// An entry on the system tray menu.
- #[derive(Debug, Clone)]
- pub enum MenuEntry {
- /// A custom item.
- CustomItem(CustomMenuItem),
- /// A native item.
- NativeItem(MenuItem),
- /// An entry with submenu.
- 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)
- }
- }
- /// Application metadata for the [`MenuItem::About`] action.
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / macOS / Android / iOS:** The metadata is ignored on these platforms.
- #[derive(Debug, Clone, Default)]
- #[non_exhaustive]
- pub struct AboutMetadata {
- /// The application name.
- pub version: Option<String>,
- /// The authors of the application.
- pub authors: Option<Vec<String>>,
- /// Application comments.
- pub comments: Option<String>,
- /// The copyright of the application.
- pub copyright: Option<String>,
- /// The license of the application.
- pub license: Option<String>,
- /// The application website.
- pub website: Option<String>,
- /// The website label.
- pub website_label: Option<String>,
- }
- impl AboutMetadata {
- /// Creates the default metadata for the [`MenuItem::About`] action, which is just empty.
- pub fn new() -> Self {
- Default::default()
- }
- /// Defines the application version.
- pub fn version(mut self, version: impl Into<String>) -> Self {
- self.version.replace(version.into());
- self
- }
- /// Defines the application authors.
- pub fn authors(mut self, authors: Vec<String>) -> Self {
- self.authors.replace(authors);
- self
- }
- /// Defines the application comments.
- pub fn comments(mut self, comments: impl Into<String>) -> Self {
- self.comments.replace(comments.into());
- self
- }
- /// Defines the application copyright.
- pub fn copyright(mut self, copyright: impl Into<String>) -> Self {
- self.copyright.replace(copyright.into());
- self
- }
- /// Defines the application license.
- pub fn license(mut self, license: impl Into<String>) -> Self {
- self.license.replace(license.into());
- self
- }
- /// Defines the application version.
- pub fn website(mut self, website: impl Into<String>) -> Self {
- self.website.replace(website.into());
- self
- }
- /// Defines the application version.
- pub fn website_label(mut self, website_label: impl Into<String>) -> Self {
- self.website_label.replace(website_label.into());
- self
- }
- }
- /// 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.
- #[derive(Debug, Clone)]
- #[non_exhaustive]
- pub enum MenuItem {
- /// Shows a standard "About" item.
- ///
- /// The first value is the application name, and the second is its metadata.
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / Android / iOS:** Unsupported
- /// - **Linux:** The metadata is only applied on Linux
- ///
- About(String, AboutMetadata),
- /// A standard "hide the app" menu item.
- ///
- /// ## Platform-specific
- ///
- /// - **Android / iOS:** Unsupported
- ///
- Hide,
- /// A standard "Services" menu item.
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / Linux / Android / iOS:** Unsupported
- ///
- Services,
- /// A "hide all other windows" menu item.
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / Linux / Android / iOS:** Unsupported
- ///
- HideOthers,
- /// A menu item to show all the windows for this app.
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / Linux / Android / iOS:** Unsupported
- ///
- ShowAll,
- /// Close the current window.
- ///
- /// ## Platform-specific
- ///
- /// - **Android / iOS:** Unsupported
- ///
- CloseWindow,
- /// A "quit this app" menu icon.
- ///
- /// ## Platform-specific
- ///
- /// - **Android / iOS:** Unsupported
- ///
- Quit,
- /// A menu item for enabling copying (often text) from responders.
- ///
- /// ## Platform-specific
- ///
- /// - **Android / iOS / Linux:** Unsupported
- ///
- Copy,
- /// A menu item for enabling cutting (often text) from responders.
- ///
- /// ## Platform-specific
- ///
- /// - **Android / iOS / Linux:** Unsupported
- ///
- Cut,
- /// An "undo" menu item; particularly useful for supporting the cut/copy/paste/undo lifecycle
- /// of events.
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / Linux / Android / iOS:** Unsupported
- ///
- Undo,
- /// An "redo" menu item; particularly useful for supporting the cut/copy/paste/undo lifecycle
- /// of events.
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / Linux / Android / iOS:** Unsupported
- ///
- Redo,
- /// A menu item for selecting all (often text) from responders.
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / Android / iOS / Linux:** Unsupported
- ///
- SelectAll,
- /// A menu item for pasting (often text) into responders.
- ///
- /// ## Platform-specific
- ///
- /// - **Android / iOS / Linux:** Unsupported
- ///
- Paste,
- /// A standard "enter full screen" item.
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / Linux / Android / iOS:** Unsupported
- ///
- EnterFullScreen,
- /// An item for minimizing the window with the standard system controls.
- ///
- /// ## Platform-specific
- ///
- /// - **Android / iOS:** Unsupported
- ///
- Minimize,
- /// An item for instructing the app to zoom
- ///
- /// ## Platform-specific
- ///
- /// - **Windows / Linux / Android / iOS:** Unsupported
- ///
- Zoom,
- /// Represents a Separator
- ///
- /// ## Platform-specific
- ///
- /// - **Android / iOS:** Unsupported
- ///
- Separator,
- }
|