Skip to content

Commit

Permalink
fix: macOS tray button selection with VoiceOver (electron#39352)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored and MrHuangJser committed Dec 11, 2023
1 parent 4f4bf2f commit a6e5d7f
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion shell/browser/ui/tray_icon_cocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ - (id)initWithIcon:(electron::TrayIconCocoa*)icon {
NSStatusItem* item = [[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength];
statusItem_ = item;
[[statusItem_ button] addSubview:self]; // inject custom view
[[statusItem_ button] addSubview:self];

// We need to set the target and action on the button, otherwise
// VoiceOver doesn't know where to send the select action.
[[statusItem_ button] setTarget:self];
[[statusItem_ button] setAction:@selector(mouseDown:)];
[self updateDimensions];
}
return self;
Expand Down Expand Up @@ -190,6 +195,25 @@ - (void)handleClickNotifications:(NSEvent*)event {
}

- (void)mouseDown:(NSEvent*)event {
// If |event| does not respond to locationInWindow, we've
// arrived here from VoiceOver, which does not pass an event.
// Create a synthetic event to pass to the click handler.
if (![event respondsToSelector:@selector(locationInWindow)]) {
event = [NSEvent mouseEventWithType:NSEventTypeRightMouseDown
location:NSMakePoint(0, 0)
modifierFlags:0
timestamp:NSApp.currentEvent.timestamp
windowNumber:0
context:nil
eventNumber:0
clickCount:1
pressure:1.0];

// We also need to explicitly call the click handler here, since
// VoiceOver won't trigger mouseUp.
[self handleClickNotifications:event];
}

trayIcon_->NotifyMouseDown(
gfx::ScreenPointFromNSPoint([event locationInWindow]),
ui::EventFlagsFromModifiers([event modifierFlags]));
Expand Down

0 comments on commit a6e5d7f

Please sign in to comment.