Skip to content

Commit e4fc6d2

Browse files
committed
fix: make tray_exit() thread-safe
1 parent d43f4c2 commit e4fc6d2

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/tray_darwin.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ - (IBAction)menuCallback:(id)sender {
4040
static NSStatusBar *statusBar;
4141
static NSStatusItem *statusItem;
4242

43+
#define QUIT_EVENT_SUBTYPE 0x0DED ///< NSEvent subtype used to signal exit.
44+
4345
static NSMenu *_tray_menu(struct tray_menu *m) {
4446
NSMenu *menu = [[NSMenu alloc] init];
4547
[menu setAutoenablesItems:FALSE];
@@ -82,6 +84,10 @@ int tray_loop(int blocking) {
8284
inMode:[NSString stringWithUTF8String:"kCFRunLoopDefaultMode"]
8385
dequeue:TRUE];
8486
if (event) {
87+
if (event.type == NSEventTypeApplicationDefined && event.subtype == QUIT_EVENT_SUBTYPE) {
88+
return -1;
89+
}
90+
8591
[app sendEvent:event];
8692
}
8793
return 0;
@@ -96,5 +102,14 @@ void tray_update(struct tray *tray) {
96102
}
97103

98104
void tray_exit(void) {
99-
[app terminate:app];
105+
NSEvent *event = [NSEvent otherEventWithType:NSEventTypeApplicationDefined
106+
location:NSMakePoint(0, 0)
107+
modifierFlags:0
108+
timestamp:0
109+
windowNumber:0
110+
context:nil
111+
subtype:QUIT_EVENT_SUBTYPE
112+
data1:0
113+
data2:0];
114+
[app postEvent:event atStart:FALSE];
100115
}

src/tray_windows.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ void tray_update(struct tray *tray) {
317317

318318
void tray_exit(void) {
319319
Shell_NotifyIconW(NIM_DELETE, &nid);
320+
SendMessage(hwnd, WM_CLOSE, 0, 0);
320321
_destroy_icon_cache();
321322
if (hmenu != 0) {
322323
DestroyMenu(hmenu);
323324
}
324-
PostQuitMessage(0);
325325
UnregisterClass(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL));
326326
}

0 commit comments

Comments
 (0)