@@ -6,7 +6,7 @@ use std::{path::PathBuf, sync::Mutex};
66use tauri:: {
77 image:: Image ,
88 menu:: { Menu , MenuItem } ,
9- tray:: { TrayIcon , TrayIconBuilder } ,
9+ tray:: { MouseButton , MouseButtonState , TrayIcon , TrayIconBuilder , TrayIconEvent } ,
1010 AppHandle , Emitter , Manager , RunEvent , WindowEvent , Wry ,
1111} ;
1212#[ cfg( target_os = "macos" ) ]
@@ -176,12 +176,22 @@ fn setup_tray(app: &mut tauri::App) -> tauri::Result<()> {
176176 let show = MenuItem :: with_id ( app, TRAY_SHOW_ID , "显示窗口" , true , None :: < & str > ) ?;
177177 let quit = MenuItem :: with_id ( app, TRAY_QUIT_ID , "退出" , true , None :: < & str > ) ?;
178178 let menu = Menu :: with_items ( app, & [ & show, & toggle_share, & check_update, & quit] ) ?;
179- let icon = tray_icon ( ) . or_else ( || app. default_window_icon ( ) . cloned ( ) ) ;
179+ let icon = platform_tray_icon ( ) . or_else ( || app. default_window_icon ( ) . cloned ( ) ) ;
180180
181181 let mut tray = TrayIconBuilder :: new ( )
182182 . menu ( & menu)
183183 . tooltip ( "FileShare" )
184- . show_menu_on_left_click ( true )
184+ . show_menu_on_left_click ( false )
185+ . on_tray_icon_event ( |tray, event| {
186+ if let TrayIconEvent :: Click {
187+ button : MouseButton :: Left ,
188+ button_state : MouseButtonState :: Up ,
189+ ..
190+ } = event
191+ {
192+ show_main_window ( tray. app_handle ( ) ) ;
193+ }
194+ } )
185195 . on_menu_event ( |app, event| match event. id ( ) . as_ref ( ) {
186196 TRAY_TOGGLE_SHARE_ID => {
187197 let app = app. clone ( ) ;
@@ -236,6 +246,18 @@ fn tray_icon() -> Option<Image<'static>> {
236246 . map ( Image :: to_owned)
237247}
238248
249+ #[ cfg( target_os = "windows" ) ]
250+ fn platform_tray_icon ( ) -> Option < Image < ' static > > {
251+ Image :: from_bytes ( include_bytes ! ( "../icons/32x32.png" ) )
252+ . ok ( )
253+ . map ( Image :: to_owned)
254+ }
255+
256+ #[ cfg( not( target_os = "windows" ) ) ]
257+ fn platform_tray_icon ( ) -> Option < Image < ' static > > {
258+ tray_icon ( )
259+ }
260+
239261fn inactive_tray_icon ( ) -> Option < Image < ' static > > {
240262 tray_icon ( ) . map ( |icon| {
241263 let rgba = icon
@@ -246,7 +268,7 @@ fn inactive_tray_icon() -> Option<Image<'static>> {
246268 pixel[ 0 ] ,
247269 pixel[ 1 ] ,
248270 pixel[ 2 ] ,
249- ( ( pixel[ 3 ] as f32 ) * 0.38 ) . round ( ) as u8 ,
271+ ( ( pixel[ 3 ] as f32 ) * 0.70 ) . round ( ) as u8 ,
250272 ]
251273 } )
252274 . collect :: < Vec < _ > > ( ) ;
@@ -363,11 +385,10 @@ fn set_tray_share_running(app: &AppHandle, running: bool) {
363385 } else {
364386 "启动分享"
365387 } ) ;
366- let icon = if running {
367- tray_icon ( )
368- } else {
369- inactive_tray_icon ( )
370- } ;
388+ #[ cfg( target_os = "macos" ) ]
389+ let icon = if running { tray_icon ( ) } else { inactive_tray_icon ( ) } ;
390+ #[ cfg( not( target_os = "macos" ) ) ]
391+ let icon = platform_tray_icon ( ) ;
371392 let _ = tray. set_icon ( icon) ;
372393 #[ cfg( target_os = "macos" ) ]
373394 {
0 commit comments