feat: add tauri::Builder::on_tray_icon_event (#11742)

This commit is contained in:
Amr Bashir 2024-11-21 15:06:57 +02:00 committed by GitHub
parent fc30b20bea
commit f884bae75b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,6 @@
---
"tauri": "minor:feat"
---
Add `tauri::Builder::on_tray_icon_event` handler.

View File

@ -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,
@ -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(),
@ -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
@ -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)]

View File

@ -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(),

View File

@ -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<
@ -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)]
@ -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(),