@@ -15,6 +15,7 @@ use crate::{
1515} ;
1616use crate :: { ResourceId , UnsafeSend } ;
1717use serde:: Serialize ;
18+ use std:: mem:: ManuallyDrop ;
1819use std:: path:: Path ;
1920pub use tray_icon:: TrayIconId ;
2021
@@ -369,7 +370,7 @@ impl<R: Runtime> TrayIconBuilder<R> {
369370
370371 let icon = TrayIcon {
371372 id,
372- inner : unsafe_tray. take ( ) ,
373+ inner : ManuallyDrop :: new ( unsafe_tray. take ( ) ) ,
373374 app_handle : app_handle. clone ( ) ,
374375 } ;
375376
@@ -393,11 +394,12 @@ impl<R: Runtime> TrayIconBuilder<R> {
393394///
394395/// This type is reference-counted and the icon is removed when the last instance is dropped.
395396///
396- /// See [TrayIconBuilder] to construct this type.
397+ /// See [` TrayIconBuilder` ] to construct this type.
397398#[ tauri_macros:: default_runtime( crate :: Wry , wry) ]
398399pub struct TrayIcon < R : Runtime > {
399400 id : TrayIconId ,
400- inner : tray_icon:: TrayIcon ,
401+ // SAFETY: we only call `ManuallyDrop::take` in [`Self::drop`] to drop it on main thread
402+ inner : ManuallyDrop < tray_icon:: TrayIcon > ,
401403 app_handle : AppHandle < R > ,
402404}
403405
@@ -411,6 +413,17 @@ impl<R: Runtime> Clone for TrayIcon<R> {
411413 }
412414}
413415
416+ impl < R : Runtime > Drop for TrayIcon < R > {
417+ fn drop ( & mut self ) {
418+ let inner = unsafe { ManuallyDrop :: take ( & mut self . inner ) } ;
419+ // SAFETY: inner was created on main thread and is being dropped on main thread
420+ let inner = UnsafeSend ( inner) ;
421+ let _ = self . app_handle . run_on_main_thread ( move || {
422+ drop ( inner. take ( ) ) ;
423+ } ) ;
424+ }
425+ }
426+
414427/// # Safety
415428///
416429/// We make sure it always runs on the main thread.
0 commit comments