Skip to content

Commit 0e0dd64

Browse files
fix: emit click events with tray context menu (electron#24235)
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
1 parent b464589 commit 0e0dd64

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

shell/browser/ui/tray_icon_cocoa.mm

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,26 @@ - (void)setMenuController:(ElectronMenuController*)menu {
130130
[statusItem_ setMenu:[menuController_ menu]];
131131
}
132132

133+
- (void)handleClickNotifications:(NSEvent*)event {
134+
// If we are ignoring double click events, we should ignore the `clickCount`
135+
// value and immediately emit a click event.
136+
BOOL shouldBeHandledAsASingleClick =
137+
(event.clickCount == 1) || ignoreDoubleClickEvents_;
138+
if (shouldBeHandledAsASingleClick)
139+
trayIcon_->NotifyClicked(
140+
gfx::ScreenRectFromNSRect(event.window.frame),
141+
gfx::ScreenPointFromNSPoint([event locationInWindow]),
142+
ui::EventFlagsFromModifiers([event modifierFlags]));
143+
144+
// Double click event.
145+
BOOL shouldBeHandledAsADoubleClick =
146+
(event.clickCount == 2) && !ignoreDoubleClickEvents_;
147+
if (shouldBeHandledAsADoubleClick)
148+
trayIcon_->NotifyDoubleClicked(
149+
gfx::ScreenRectFromNSRect(event.window.frame),
150+
ui::EventFlagsFromModifiers([event modifierFlags]));
151+
}
152+
133153
- (void)mouseDown:(NSEvent*)event {
134154
trayIcon_->NotifyMouseDown(
135155
gfx::ScreenPointFromNSPoint([event locationInWindow]),
@@ -138,6 +158,7 @@ - (void)mouseDown:(NSEvent*)event {
138158
// Pass click to superclass to show menu. Custom mouseUp handler won't be
139159
// invoked.
140160
if (menuController_) {
161+
[self handleClickNotifications:event];
141162
[super mouseDown:event];
142163
} else {
143164
[[statusItem_ button] highlight:YES];
@@ -151,23 +172,7 @@ - (void)mouseUp:(NSEvent*)event {
151172
gfx::ScreenPointFromNSPoint([event locationInWindow]),
152173
ui::EventFlagsFromModifiers([event modifierFlags]));
153174

154-
// If we are ignoring double click events, we should ignore the `clickCount`
155-
// value and immediately emit a click event.
156-
BOOL shouldBeHandledAsASingleClick =
157-
(event.clickCount == 1) || ignoreDoubleClickEvents_;
158-
if (shouldBeHandledAsASingleClick)
159-
trayIcon_->NotifyClicked(
160-
gfx::ScreenRectFromNSRect(event.window.frame),
161-
gfx::ScreenPointFromNSPoint([event locationInWindow]),
162-
ui::EventFlagsFromModifiers([event modifierFlags]));
163-
164-
// Double click event.
165-
BOOL shouldBeHandledAsADoubleClick =
166-
(event.clickCount == 2) && !ignoreDoubleClickEvents_;
167-
if (shouldBeHandledAsADoubleClick)
168-
trayIcon_->NotifyDoubleClicked(
169-
gfx::ScreenRectFromNSRect(event.window.frame),
170-
ui::EventFlagsFromModifiers([event modifierFlags]));
175+
[self handleClickNotifications:event];
171176
}
172177

173178
- (void)popUpContextMenu:(electron::ElectronMenuModel*)menu_model {

0 commit comments

Comments
 (0)