@@ -10,10 +10,10 @@ use crate::app::{GlobalMenuEventListener, GlobalTrayIconEventListener};
10
10
use crate :: menu:: ContextMenu ;
11
11
use crate :: menu:: MenuEvent ;
12
12
use crate :: resources:: Resource ;
13
- use crate :: UnsafeSend ;
14
13
use crate :: {
15
14
image:: Image , menu:: run_item_main_thread, AppHandle , Manager , PhysicalPosition , Rect , Runtime ,
16
15
} ;
16
+ use crate :: { ResourceId , UnsafeSend } ;
17
17
use serde:: Serialize ;
18
18
use std:: path:: Path ;
19
19
pub use tray_icon:: TrayIconId ;
@@ -358,8 +358,10 @@ impl<R: Runtime> TrayIconBuilder<R> {
358
358
self . inner . id ( )
359
359
}
360
360
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 ) > {
363
365
let id = self . id ( ) . clone ( ) ;
364
366
365
367
// SAFETY:
@@ -368,8 +370,7 @@ impl<R: Runtime> TrayIconBuilder<R> {
368
370
let unsafe_builder = UnsafeSend ( self . inner ) ;
369
371
370
372
let ( tx, rx) = std:: sync:: mpsc:: channel ( ) ;
371
- let unsafe_tray = manager
372
- . app_handle ( )
373
+ let unsafe_tray = app_handle
373
374
. run_on_main_thread ( move || {
374
375
// SAFETY: will only be accessed on main thread
375
376
let _ = tx. send ( unsafe_builder. take ( ) . build ( ) . map ( UnsafeSend ) ) ;
@@ -379,15 +380,21 @@ impl<R: Runtime> TrayIconBuilder<R> {
379
380
let icon = TrayIcon {
380
381
id,
381
382
inner : unsafe_tray. take ( ) ,
382
- app_handle : manager . app_handle ( ) . clone ( ) ,
383
+ app_handle : app_handle. clone ( ) ,
383
384
} ;
384
385
385
- icon. register (
386
+ let rid = icon. register (
386
387
& icon. app_handle ,
387
388
self . on_menu_event ,
388
389
self . on_tray_icon_event ,
389
390
) ;
390
391
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 ( ) ) ?;
391
398
Ok ( icon)
392
399
}
393
400
}
@@ -426,7 +433,7 @@ impl<R: Runtime> TrayIcon<R> {
426
433
app_handle : & AppHandle < R > ,
427
434
on_menu_event : Option < GlobalMenuEventListener < AppHandle < R > > > ,
428
435
on_tray_icon_event : Option < GlobalTrayIconEventListener < TrayIcon < R > > > ,
429
- ) {
436
+ ) -> ResourceId {
430
437
if let Some ( handler) = on_menu_event {
431
438
app_handle
432
439
. manager
@@ -447,13 +454,15 @@ impl<R: Runtime> TrayIcon<R> {
447
454
. insert ( self . id . clone ( ) , handler) ;
448
455
}
449
456
457
+ let rid = app_handle. resources_table ( ) . add ( self . clone ( ) ) ;
450
458
app_handle
451
459
. manager
452
460
. tray
453
461
. icons
454
462
. lock ( )
455
463
. unwrap ( )
456
- . push ( self . clone ( ) ) ;
464
+ . push ( ( self . id ( ) . clone ( ) , rid) ) ;
465
+ rid
457
466
}
458
467
459
468
/// The application handle associated with this type.
@@ -598,14 +607,13 @@ impl<R: Runtime> TrayIcon<R> {
598
607
599
608
impl < R : Runtime > Resource for TrayIcon < R > {
600
609
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
+ }
609
617
}
610
618
}
611
619
0 commit comments