@@ -9,35 +9,57 @@ pub fn update_tray(app_handle: tauri::AppHandle, title: Option<String>, new_icon
9
9
}
10
10
if let Some ( new_icon) = new_icon {
11
11
if new_icon {
12
- tray_handle
13
- . set_icon ( tauri:: Icon :: Raw (
14
- include_bytes ! ( "../icons/tray-new.png" ) . to_vec ( ) ,
15
- ) )
16
- . unwrap ( ) ;
12
+ tray_handle. set_icon ( get_raw_tray_icon ( "new" ) ) . unwrap ( ) ;
17
13
} else {
18
- tray_handle
19
- . set_icon ( tauri:: Icon :: Raw (
20
- include_bytes ! ( "../icons/tray-base.png" ) . to_vec ( ) ,
21
- ) )
22
- . unwrap ( ) ;
14
+ tray_handle. set_icon ( get_raw_tray_icon ( "base" ) ) . unwrap ( ) ;
23
15
}
24
16
}
25
17
}
26
18
27
19
#[ tauri:: command]
28
20
pub fn toggle_tray ( app_handle : tauri:: AppHandle , show : bool ) {
29
- let tray_handle = app_handle. tray_handle_by_id ( "tray" ) . unwrap ( ) ;
30
- tray_handle. destroy ( ) . unwrap ( ) ;
21
+ let tray_handle = app_handle. tray_handle_by_id ( "tray" ) ;
22
+ if let Some ( tray_handle) = tray_handle {
23
+ tray_handle. destroy ( ) . unwrap ( ) ;
24
+ }
31
25
if show {
32
- SystemTray :: new ( )
26
+ let mut system_tray = SystemTray :: new ( )
33
27
. with_id ( "tray" )
28
+ . with_icon ( get_raw_tray_icon ( "base" ) )
34
29
. with_menu (
35
30
SystemTrayMenu :: new ( )
36
31
. add_item ( CustomMenuItem :: new ( "dashboard" . to_string ( ) , "Dashboard..." ) )
37
32
. add_native_item ( SystemTrayMenuItem :: Separator )
38
33
. add_item ( CustomMenuItem :: new ( "quit" . to_string ( ) , "Quit" ) ) ,
39
- )
40
- . build ( & app_handle)
41
- . unwrap ( ) ;
34
+ ) ;
35
+ #[ cfg( target_os = "macos" ) ]
36
+ {
37
+ system_tray = system_tray
38
+ . with_icon_as_template ( true )
39
+ . with_menu_on_left_click ( false )
40
+ }
41
+ system_tray. build ( & app_handle) . unwrap ( ) ;
42
42
}
43
43
}
44
+
45
+ fn get_raw_tray_icon ( image : & str ) -> tauri:: Icon {
46
+ let is_macos = cfg ! ( target_os = "macos" ) ;
47
+ let bytes = match image {
48
+ "base" => {
49
+ if is_macos {
50
+ include_bytes ! ( "../icons/tray-base-macos.png" ) . to_vec ( )
51
+ } else {
52
+ include_bytes ! ( "../icons/tray-base.png" ) . to_vec ( )
53
+ }
54
+ }
55
+ "new" => {
56
+ if is_macos {
57
+ include_bytes ! ( "../icons/tray-new-macos.png" ) . to_vec ( )
58
+ } else {
59
+ include_bytes ! ( "../icons/tray-new.png" ) . to_vec ( )
60
+ }
61
+ }
62
+ _ => panic ! ( "Unknown tray icon" ) ,
63
+ } ;
64
+ tauri:: Icon :: Raw ( bytes)
65
+ }
0 commit comments