Skip to content

Commit 2b47f5e

Browse files
committed
fix: drop tray icon on main thread
1 parent 6f6ab12 commit 2b47f5e

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

crates/tauri/src/tray/mod.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
};
1616
use crate::{ResourceId, UnsafeSend};
1717
use serde::Serialize;
18+
use std::mem::ManuallyDrop;
1819
use std::path::Path;
1920
pub 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)]
398399
pub 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

Comments
 (0)