From cb834596a95d0e510dd0405f013dd7b0c598f66c Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Thu, 4 Aug 2022 13:22:42 -0300 Subject: [PATCH] feat(tray): remove from tray on `Drop` on macOS --- .changes/drop-tray-macos.md | 5 +++++ src/platform_impl/macos/system_tray.rs | 8 ++++++++ src/system_tray.rs | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changes/drop-tray-macos.md diff --git a/.changes/drop-tray-macos.md b/.changes/drop-tray-macos.md new file mode 100644 index 000000000..3125812bc --- /dev/null +++ b/.changes/drop-tray-macos.md @@ -0,0 +1,5 @@ +--- +"tao": patch +--- + +Remove the NSStatusItem from the menu bar when the `SystemTray` instance is dropped. \ No newline at end of file diff --git a/src/platform_impl/macos/system_tray.rs b/src/platform_impl/macos/system_tray.rs index 8153248d1..2c6ecfda6 100644 --- a/src/platform_impl/macos/system_tray.rs +++ b/src/platform_impl/macos/system_tray.rs @@ -115,6 +115,14 @@ pub struct SystemTray { pub(crate) ns_status_bar: id, } +impl Drop for SystemTray { + fn drop(&mut self) { + unsafe { + NSStatusBar::systemStatusBar(nil).removeStatusItem_(self.ns_status_bar); + } + } +} + impl SystemTray { pub fn set_icon(&mut self, icon: Icon) { // update our icon diff --git a/src/system_tray.rs b/src/system_tray.rs index 6b6b9cc24..b89908190 100644 --- a/src/system_tray.rs +++ b/src/system_tray.rs @@ -97,7 +97,7 @@ impl SystemTrayBuilder { /// **Linux:** /// - Dropping the tray too early could lead to a default icon. /// - Dropping the tray after the icon has been added to the system tray may not remove it. -/// **Windows:** Dropping the tray will effectively remove the icon from the system tray. +/// **Windows / macOS:** Dropping the tray will effectively remove the icon from the system tray. pub struct SystemTray(pub SystemTrayPlatform); impl SystemTray {