From f884bae75b3ad8f8debfd29bb05ac9cbc9748c89 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Thu, 21 Nov 2024 15:06:57 +0200 Subject: [PATCH] feat: add `tauri::Builder::on_tray_icon_event` (#11742) --- .changes/core-builder-on-tray-icon-event.md | 6 ++++ crates/tauri/src/app.rs | 31 +++++++++++++++++++++ crates/tauri/src/ipc/protocol.rs | 4 +++ crates/tauri/src/manager/mod.rs | 7 ++++- 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 .changes/core-builder-on-tray-icon-event.md diff --git a/.changes/core-builder-on-tray-icon-event.md b/.changes/core-builder-on-tray-icon-event.md new file mode 100644 index 000000000000..8a4115049bd3 --- /dev/null +++ b/.changes/core-builder-on-tray-icon-event.md @@ -0,0 +1,6 @@ +--- +"tauri": "minor:feat" +--- + +Add `tauri::Builder::on_tray_icon_event` handler. + diff --git a/crates/tauri/src/app.rs b/crates/tauri/src/app.rs index b4a535e1c93d..3881e336dc82 100644 --- a/crates/tauri/src/app.rs +++ b/crates/tauri/src/app.rs @@ -1249,6 +1249,10 @@ pub struct Builder { #[cfg(desktop)] menu_event_listeners: Vec>>, + /// Tray event listeners for any tray icon event. + #[cfg(all(desktop, feature = "tray-icon"))] + tray_icon_event_listeners: Vec>>, + /// Enable macOS default menu creation. #[allow(unused)] enable_macos_default_menu: bool, @@ -1321,6 +1325,8 @@ impl Builder { menu: None, #[cfg(desktop)] menu_event_listeners: Vec::new(), + #[cfg(all(desktop, feature = "tray-icon"))] + tray_icon_event_listeners: Vec::new(), enable_macos_default_menu: true, window_event_listeners: Vec::new(), webview_event_listeners: Vec::new(), @@ -1682,6 +1688,29 @@ tauri::Builder::default() self } + /// Registers an event handler for any tray icon event. + /// + /// # Examples + /// ``` + /// use tauri::Manager; + /// + /// tauri::Builder::default() + /// .on_tray_icon_event(|app, event| { + /// let tray = app.tray_by_id(event.id()).expect("can't find tray icon"); + /// let _ = tray.set_visible(false); + /// }); + /// ``` + #[must_use] + #[cfg(all(desktop, feature = "tray-icon"))] + #[cfg_attr(docsrs, doc(cfg(all(desktop, feature = "tray-icon"))))] + pub fn on_tray_icon_event, TrayIconEvent) + Send + Sync + 'static>( + mut self, + f: F, + ) -> Self { + self.tray_icon_event_listeners.push(Box::new(f)); + self + } + /// Enable or disable the default menu on macOS. Enabled by default. /// /// # Examples @@ -1889,6 +1918,8 @@ tauri::Builder::default() self.state, #[cfg(desktop)] self.menu_event_listeners, + #[cfg(all(desktop, feature = "tray-icon"))] + self.tray_icon_event_listeners, self.window_event_listeners, self.webview_event_listeners, #[cfg(desktop)] diff --git a/crates/tauri/src/ipc/protocol.rs b/crates/tauri/src/ipc/protocol.rs index 90a6d0a6ff49..a8351bd5ec2f 100644 --- a/crates/tauri/src/ipc/protocol.rs +++ b/crates/tauri/src/ipc/protocol.rs @@ -586,6 +586,8 @@ mod tests { Default::default(), StateManager::new(), Default::default(), + #[cfg(all(desktop, feature = "tray-icon"))] + Default::default(), Default::default(), Default::default(), Default::default(), @@ -701,6 +703,8 @@ mod tests { Default::default(), StateManager::new(), Default::default(), + #[cfg(all(desktop, feature = "tray-icon"))] + Default::default(), Default::default(), Default::default(), Default::default(), diff --git a/crates/tauri/src/manager/mod.rs b/crates/tauri/src/manager/mod.rs index 08c26b242fbf..440ed3a28170 100644 --- a/crates/tauri/src/manager/mod.rs +++ b/crates/tauri/src/manager/mod.rs @@ -254,6 +254,9 @@ impl AppManager { uri_scheme_protocols: HashMap>>, state: StateManager, #[cfg(desktop)] menu_event_listener: Vec>>, + #[cfg(all(desktop, feature = "tray-icon"))] tray_icon_event_listeners: Vec< + crate::app::GlobalTrayIconEventListener>, + >, window_event_listeners: Vec>, webiew_event_listeners: Vec>, #[cfg(desktop)] window_menu_event_listeners: HashMap< @@ -290,7 +293,7 @@ impl AppManager { tray: tray::TrayManager { icon: context.tray_icon, icons: Default::default(), - global_event_listeners: Default::default(), + global_event_listeners: Mutex::new(tray_icon_event_listeners), event_listeners: Default::default(), }, #[cfg(desktop)] @@ -768,6 +771,8 @@ mod test { Default::default(), StateManager::new(), Default::default(), + #[cfg(all(desktop, feature = "tray-icon"))] + Default::default(), Default::default(), Default::default(), Default::default(),