Skip to content

Commit 61603ee

Browse files
authored
fix(mac): always run tray_update() on the main thread (#157)
1 parent f449b4a commit 61603ee

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

src/tray_darwin.m

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,24 @@ int tray_loop(int blocking) {
114114
}
115115

116116
void tray_update(struct tray *tray) {
117-
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[NSString stringWithUTF8String:tray->icon]];
118-
NSSize size = NSMakeSize(16, 16);
119-
[image setSize:NSMakeSize(16, 16)];
120-
statusItem.button.image = image;
121-
[statusItem setMenu:_tray_menu(tray->menu)];
122-
123-
// Set tooltip if provided
124-
if (tray->tooltip != NULL) {
125-
statusItem.button.toolTip = [NSString stringWithUTF8String:tray->tooltip];
117+
// This must run on the main thread
118+
void (^updateBlock)(void) = ^{
119+
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[NSString stringWithUTF8String:tray->icon]];
120+
NSSize size = NSMakeSize(16, 16);
121+
[image setSize:NSMakeSize(16, 16)];
122+
statusItem.button.image = image;
123+
[statusItem setMenu:_tray_menu(tray->menu)];
124+
125+
// Set tooltip if provided
126+
if (tray->tooltip != NULL) {
127+
statusItem.button.toolTip = [NSString stringWithUTF8String:tray->tooltip];
128+
}
129+
};
130+
131+
if ([NSThread isMainThread]) {
132+
updateBlock();
133+
} else {
134+
dispatch_sync(dispatch_get_main_queue(), updateBlock);
126135
}
127136
}
128137

0 commit comments

Comments
 (0)