Skip to content

Commit 964926f

Browse files
authored
feat(core): enhance SystemTray::with_icon (#4849)
1 parent 52f0c8b commit 964926f

File tree

4 files changed

+100
-7
lines changed

4 files changed

+100
-7
lines changed

.changes/enhance-tray-api.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Enhance `SystemTray::with_icon` to accept `tauri::Icon`.

core/tauri/src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ impl<R: Runtime> Builder<R> {
15071507
.runtime
15081508
.as_ref()
15091509
.unwrap()
1510-
.system_tray(tray)
1510+
.system_tray(tray.into())
15111511
.expect("failed to run tray");
15121512

15131513
let tray_handle = tray::SystemTrayHandle {

core/tauri/src/app/tray.rs

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ pub use crate::{
88
MenuHash, MenuId, MenuIdRef, MenuUpdate, SystemTrayMenu, SystemTrayMenuEntry, TrayHandle,
99
},
1010
window::dpi::{PhysicalPosition, PhysicalSize},
11-
SystemTray,
1211
},
1312
Icon, Runtime,
1413
};
1514

1615
use tauri_macros::default_runtime;
16+
use tauri_utils::debug_eprintln;
1717

1818
use std::{
1919
collections::HashMap,
@@ -32,6 +32,97 @@ pub(crate) fn get_menu_ids(map: &mut HashMap<MenuHash, MenuId>, menu: &SystemTra
3232
}
3333
}
3434

35+
/// Represents a System Tray instance.
36+
#[derive(Debug, Default)]
37+
#[non_exhaustive]
38+
pub struct SystemTray {
39+
/// The tray icon.
40+
pub icon: Option<tauri_runtime::Icon>,
41+
/// The tray menu.
42+
pub menu: Option<SystemTrayMenu>,
43+
/// Whether the icon is a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc) icon or not.
44+
#[cfg(target_os = "macos")]
45+
pub icon_as_template: bool,
46+
/// Whether the menu should appear when the tray receives a left click. Defaults to `true`
47+
#[cfg(target_os = "macos")]
48+
pub menu_on_left_click: bool,
49+
}
50+
51+
impl SystemTray {
52+
/// Creates a new system tray that only renders an icon.
53+
pub fn new() -> Self {
54+
Default::default()
55+
}
56+
57+
pub(crate) fn menu(&self) -> Option<&SystemTrayMenu> {
58+
self.menu.as_ref()
59+
}
60+
61+
/// Sets the tray icon.
62+
#[must_use]
63+
pub fn with_icon<I: TryInto<tauri_runtime::Icon>>(mut self, icon: I) -> Self
64+
where
65+
I::Error: std::error::Error,
66+
{
67+
match icon.try_into() {
68+
Ok(icon) => {
69+
self.icon.replace(icon);
70+
}
71+
Err(e) => {
72+
debug_eprintln!("Failed to load tray icon: {}", e);
73+
}
74+
}
75+
self
76+
}
77+
78+
/// Sets the icon as a [template](https://developer.apple.com/documentation/appkit/nsimage/1520017-template?language=objc).
79+
///
80+
/// Images you mark as template images should consist of only black and clear colors.
81+
/// You can use the alpha channel in the image to adjust the opacity of black content.
82+
#[cfg(target_os = "macos")]
83+
#[must_use]
84+
pub fn with_icon_as_template(mut self, is_template: bool) -> Self {
85+
self.icon_as_template = is_template;
86+
self
87+
}
88+
89+
/// Sets whether the menu should appear when the tray receives a left click. Defaults to `true`.
90+
#[cfg(target_os = "macos")]
91+
#[must_use]
92+
pub fn with_menu_on_left_click(mut self, menu_on_left_click: bool) -> Self {
93+
self.menu_on_left_click = menu_on_left_click;
94+
self
95+
}
96+
97+
/// Sets the menu to show when the system tray is right clicked.
98+
#[must_use]
99+
pub fn with_menu(mut self, menu: SystemTrayMenu) -> Self {
100+
self.menu.replace(menu);
101+
self
102+
}
103+
}
104+
105+
impl From<SystemTray> for tauri_runtime::SystemTray {
106+
fn from(tray: SystemTray) -> Self {
107+
let mut t = tauri_runtime::SystemTray::new();
108+
if let Some(i) = tray.icon {
109+
t = t.with_icon(i);
110+
}
111+
112+
if let Some(menu) = tray.menu {
113+
t = t.with_menu(menu);
114+
}
115+
116+
#[cfg(target_os = "macos")]
117+
{
118+
t = t.with_icon_as_template(tray.icon_as_template);
119+
t = t.with_menu_on_left_click(tray.menu_on_left_click);
120+
}
121+
122+
t
123+
}
124+
}
125+
35126
/// System tray event.
36127
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
37128
#[non_exhaustive]

core/tauri/src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,8 @@ pub use runtime::{menu::NativeImage, ActivationPolicy};
207207
#[cfg(all(desktop, feature = "system-tray"))]
208208
#[cfg_attr(doc_cfg, doc(cfg(feature = "system-tray")))]
209209
pub use {
210-
self::app::tray::{SystemTrayEvent, SystemTrayHandle},
211-
self::runtime::{
212-
menu::{SystemTrayMenu, SystemTrayMenuItem, SystemTraySubmenu},
213-
SystemTray,
214-
},
210+
self::app::tray::{SystemTray, SystemTrayEvent, SystemTrayHandle},
211+
self::runtime::menu::{SystemTrayMenu, SystemTrayMenuItem, SystemTraySubmenu},
215212
};
216213
pub use {
217214
self::app::WindowMenuEvent,

0 commit comments

Comments
 (0)