@@ -10,10 +10,10 @@ use crate::app::{GlobalMenuEventListener, GlobalTrayIconEventListener};
1010use crate :: menu:: ContextMenu ;
1111use crate :: menu:: MenuEvent ;
1212use crate :: resources:: Resource ;
13- use crate :: UnsafeSend ;
1413use crate :: {
1514 image:: Image , menu:: run_item_main_thread, AppHandle , Manager , PhysicalPosition , Rect , Runtime ,
1615} ;
16+ use crate :: { ResourceId , UnsafeSend } ;
1717use serde:: Serialize ;
1818use std:: path:: Path ;
1919pub use tray_icon:: TrayIconId ;
@@ -358,8 +358,10 @@ impl<R: Runtime> TrayIconBuilder<R> {
358358 self . inner . id ( )
359359 }
360360
361- /// Builds and adds a new [`TrayIcon`] to the system tray.
362- pub fn build < M : Manager < R > > ( self , manager : & M ) -> crate :: Result < TrayIcon < R > > {
361+ pub ( crate ) fn build_inner (
362+ self ,
363+ app_handle : & AppHandle < R > ,
364+ ) -> crate :: Result < ( TrayIcon < R > , ResourceId ) > {
363365 let id = self . id ( ) . clone ( ) ;
364366
365367 // SAFETY:
@@ -368,8 +370,7 @@ impl<R: Runtime> TrayIconBuilder<R> {
368370 let unsafe_builder = UnsafeSend ( self . inner ) ;
369371
370372 let ( tx, rx) = std:: sync:: mpsc:: channel ( ) ;
371- let unsafe_tray = manager
372- . app_handle ( )
373+ let unsafe_tray = app_handle
373374 . run_on_main_thread ( move || {
374375 // SAFETY: will only be accessed on main thread
375376 let _ = tx. send ( unsafe_builder. take ( ) . build ( ) . map ( UnsafeSend ) ) ;
@@ -379,15 +380,21 @@ impl<R: Runtime> TrayIconBuilder<R> {
379380 let icon = TrayIcon {
380381 id,
381382 inner : unsafe_tray. take ( ) ,
382- app_handle : manager . app_handle ( ) . clone ( ) ,
383+ app_handle : app_handle. clone ( ) ,
383384 } ;
384385
385- icon. register (
386+ let rid = icon. register (
386387 & icon. app_handle ,
387388 self . on_menu_event ,
388389 self . on_tray_icon_event ,
389390 ) ;
390391
392+ Ok ( ( icon, rid) )
393+ }
394+
395+ /// Builds and adds a new [`TrayIcon`] to the system tray.
396+ pub fn build < M : Manager < R > > ( self , manager : & M ) -> crate :: Result < TrayIcon < R > > {
397+ let ( icon, _rid) = self . build_inner ( manager. app_handle ( ) ) ?;
391398 Ok ( icon)
392399 }
393400}
@@ -426,7 +433,7 @@ impl<R: Runtime> TrayIcon<R> {
426433 app_handle : & AppHandle < R > ,
427434 on_menu_event : Option < GlobalMenuEventListener < AppHandle < R > > > ,
428435 on_tray_icon_event : Option < GlobalTrayIconEventListener < TrayIcon < R > > > ,
429- ) {
436+ ) -> ResourceId {
430437 if let Some ( handler) = on_menu_event {
431438 app_handle
432439 . manager
@@ -447,13 +454,15 @@ impl<R: Runtime> TrayIcon<R> {
447454 . insert ( self . id . clone ( ) , handler) ;
448455 }
449456
457+ let rid = app_handle. resources_table ( ) . add ( self . clone ( ) ) ;
450458 app_handle
451459 . manager
452460 . tray
453461 . icons
454462 . lock ( )
455463 . unwrap ( )
456- . push ( self . clone ( ) ) ;
464+ . push ( ( self . id ( ) . clone ( ) , rid) ) ;
465+ rid
457466 }
458467
459468 /// The application handle associated with this type.
@@ -598,14 +607,13 @@ impl<R: Runtime> TrayIcon<R> {
598607
599608impl < R : Runtime > Resource for TrayIcon < R > {
600609 fn close ( self : std:: sync:: Arc < Self > ) {
601- self
602- . app_handle
603- . state :: < plugin:: TrayIcons > ( )
604- . icons
605- . lock ( )
606- . unwrap ( )
607- . remove ( & self . id . 0 ) ;
608- self . app_handle . remove_tray_by_id ( & self . id ) ;
610+ let mut icons = self . app_handle . manager . tray . icons . lock ( ) . unwrap ( ) ;
611+ for ( i, ( tray_icon_id, _rid) ) in icons. iter_mut ( ) . enumerate ( ) {
612+ if tray_icon_id == & self . id {
613+ icons. swap_remove ( i) ;
614+ return ;
615+ }
616+ }
609617 }
610618}
611619
0 commit comments