@@ -47,6 +47,8 @@ use tauri_plugin_deep_link::DeepLinkExt;
4747use tauri_plugin_dialog:: { DialogExt , MessageDialogButtons , MessageDialogKind } ;
4848
4949use std:: sync:: Arc ;
50+ #[ cfg( target_os = "macos" ) ]
51+ use tauri:: image:: Image ;
5052use tauri:: tray:: { TrayIconBuilder , TrayIconEvent } ;
5153#[ cfg( target_os = "macos" ) ]
5254use tauri:: RunEvent ;
@@ -133,6 +135,19 @@ async fn update_tray_menu(
133135 }
134136}
135137
138+ #[ cfg( target_os = "macos" ) ]
139+ fn macos_tray_icon ( ) -> Option < Image < ' static > > {
140+ const ICON_BYTES : & [ u8 ] = include_bytes ! ( "../icons/tray/macos/statusbar_template_3x.png" ) ;
141+
142+ match Image :: from_bytes ( ICON_BYTES ) {
143+ Ok ( icon) => Some ( icon) ,
144+ Err ( err) => {
145+ log:: warn!( "Failed to load macOS tray icon: {err}" ) ;
146+ None
147+ }
148+ }
149+ }
150+
136151#[ cfg_attr( mobile, tauri:: mobile_entry_point) ]
137152pub fn run ( ) {
138153 let mut builder = tauri:: Builder :: default ( ) ;
@@ -500,11 +515,26 @@ pub fn run() {
500515 } )
501516 . show_menu_on_left_click ( true ) ;
502517
503- // 统一使用应用默认图标;待托盘模板图标就绪后再启用
504- if let Some ( icon) = app. default_window_icon ( ) {
505- tray_builder = tray_builder. icon ( icon. clone ( ) ) ;
506- } else {
507- log:: warn!( "Failed to get default window icon for tray" ) ;
518+ // 使用平台对应的托盘图标(macOS 使用模板图标适配深浅色)
519+ #[ cfg( target_os = "macos" ) ]
520+ {
521+ if let Some ( icon) = macos_tray_icon ( ) {
522+ tray_builder = tray_builder. icon ( icon) . icon_as_template ( true ) ;
523+ } else if let Some ( icon) = app. default_window_icon ( ) {
524+ log:: warn!( "Falling back to default window icon for tray" ) ;
525+ tray_builder = tray_builder. icon ( icon. clone ( ) ) ;
526+ } else {
527+ log:: warn!( "Failed to load macOS tray icon for tray" ) ;
528+ }
529+ }
530+
531+ #[ cfg( not( target_os = "macos" ) ) ]
532+ {
533+ if let Some ( icon) = app. default_window_icon ( ) {
534+ tray_builder = tray_builder. icon ( icon. clone ( ) ) ;
535+ } else {
536+ log:: warn!( "Failed to get default window icon for tray" ) ;
537+ }
508538 }
509539
510540 let _tray = tray_builder. build ( app) ?;
0 commit comments