@@ -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
1615use tauri_macros:: default_runtime;
16+ use tauri_utils:: debug_eprintln;
1717
1818use 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]
0 commit comments