Skip to content

Commit

Permalink
fix(macos): retain tray to prevent segfault when event loop is running (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 20, 2022
1 parent 7e5556e commit 759b7db
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-macos-tray-creation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tao": patch
---

Fix system tray creation after event loop starts on macOS.
23 changes: 15 additions & 8 deletions examples/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
#[cfg(target_os = "linux")]
use tao::platform::linux::SystemTrayBuilderExtLinux;
use tao::{
event::Event,
event::{Event, StartCause},
event_loop::{ControlFlow, EventLoop},
menu::{ContextMenu as Menu, MenuItemAttributes, MenuType},
system_tray::SystemTrayBuilder,
Expand Down Expand Up @@ -56,15 +56,22 @@ fn main() {

let mut second_tray_menu = Menu::new();
let log = second_tray_menu.add_item(MenuItemAttributes::new("Log"));
let second_system_tray = SystemTrayBuilder::new(icon, Some(second_tray_menu))
.with_id(second_tray_id)
.build(&event_loop)
.unwrap();
let mut second_tray_menu = Some(second_tray_menu);

event_loop.run(move |event, _event_loop, control_flow| {
let mut system_tray = Some(system_tray);
let mut second_system_tray = None;

event_loop.run(move |event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;

match event {
Event::NewEvents(StartCause::Init) => {
let tray = SystemTrayBuilder::new(icon.clone(), second_tray_menu.take())
.with_id(second_tray_id)
.build(&event_loop)
.unwrap();
second_system_tray.replace(tray);
}
Event::MenuEvent {
menu_id,
// specify only context menu's
Expand All @@ -73,8 +80,8 @@ fn main() {
} => {
if menu_id == quit.clone().id() {
// drop the system tray before exiting to remove the icon from system tray on Windows
drop(&system_tray);
drop(&second_system_tray);
system_tray.take();
second_system_tray.take();
*control_flow = ControlFlow::Exit;
} else if menu_id == log.clone().id() {
println!("Log clicked");
Expand Down
9 changes: 5 additions & 4 deletions src/platform_impl/macos/system_tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use cocoa::{
NSStatusBar, NSStatusItem, NSWindow,
},
base::{id, nil, NO, YES},
foundation::{NSAutoreleasePool, NSData, NSPoint, NSSize, NSString},
foundation::{NSData, NSPoint, NSSize, NSString},
};
use objc::{
declare::ClassDecl,
Expand All @@ -38,9 +38,9 @@ impl SystemTrayBuilder {
#[inline]
pub fn new(icon: Icon, tray_menu: Option<Menu>) -> Self {
unsafe {
let ns_status_bar = NSStatusBar::systemStatusBar(nil)
.statusItemWithLength_(NSSquareStatusItemLength)
.autorelease();
let ns_status_bar =
NSStatusBar::systemStatusBar(nil).statusItemWithLength_(NSSquareStatusItemLength);
let _: () = msg_send![ns_status_bar, retain];

Self {
system_tray: SystemTray {
Expand Down Expand Up @@ -119,6 +119,7 @@ impl Drop for SystemTray {
fn drop(&mut self) {
unsafe {
NSStatusBar::systemStatusBar(nil).removeStatusItem_(self.ns_status_bar);
let _: () = msg_send![self.ns_status_bar, release];
}
}
}
Expand Down

0 comments on commit 759b7db

Please sign in to comment.