Skip to content

Commit

Permalink
feat: add tauri::Builder::on_tray_icon_event (#11742)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Nov 21, 2024
1 parent fc30b20 commit f884bae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/core-builder-on-tray-icon-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": "minor:feat"
---

Add `tauri::Builder::on_tray_icon_event` handler.

31 changes: 31 additions & 0 deletions crates/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,10 @@ pub struct Builder<R: Runtime> {
#[cfg(desktop)]
menu_event_listeners: Vec<GlobalMenuEventListener<AppHandle<R>>>,

/// Tray event listeners for any tray icon event.
#[cfg(all(desktop, feature = "tray-icon"))]
tray_icon_event_listeners: Vec<GlobalTrayIconEventListener<AppHandle<R>>>,

/// Enable macOS default menu creation.
#[allow(unused)]
enable_macos_default_menu: bool,
Expand Down Expand Up @@ -1321,6 +1325,8 @@ impl<R: Runtime> Builder<R> {
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(),
Expand Down Expand Up @@ -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<F: Fn(&AppHandle<R>, 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
Expand Down Expand Up @@ -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)]
Expand Down
4 changes: 4 additions & 0 deletions crates/tauri/src/ipc/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
7 changes: 6 additions & 1 deletion crates/tauri/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ impl<R: Runtime> AppManager<R> {
uri_scheme_protocols: HashMap<String, Arc<webview::UriSchemeProtocol<R>>>,
state: StateManager,
#[cfg(desktop)] menu_event_listener: Vec<crate::app::GlobalMenuEventListener<AppHandle<R>>>,
#[cfg(all(desktop, feature = "tray-icon"))] tray_icon_event_listeners: Vec<
crate::app::GlobalTrayIconEventListener<AppHandle<R>>,
>,
window_event_listeners: Vec<GlobalWindowEventListener<R>>,
webiew_event_listeners: Vec<GlobalWebviewEventListener<R>>,
#[cfg(desktop)] window_menu_event_listeners: HashMap<
Expand Down Expand Up @@ -290,7 +293,7 @@ impl<R: Runtime> AppManager<R> {
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)]
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit f884bae

Please sign in to comment.