From 1f29124d11d006af050a5db899503953d5071a87 Mon Sep 17 00:00:00 2001 From: mikeykhalil Date: Sun, 1 Apr 2018 18:07:26 -0700 Subject: [PATCH 1/6] updated Tray API to ignore double click events on macOS (#8952) --- atom/browser/api/atom_api_tray.cc | 6 ++++++ atom/browser/api/atom_api_tray.h | 1 + atom/browser/ui/tray_icon.cc | 3 +++ atom/browser/ui/tray_icon.h | 4 ++++ atom/browser/ui/tray_icon_cocoa.h | 1 + atom/browser/ui/tray_icon_cocoa.mm | 21 +++++++++++++++++++++ 6 files changed, 36 insertions(+) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 08739552f..20ed32e35 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -176,6 +176,10 @@ void Tray::SetHighlightMode(TrayIcon::HighlightMode mode) { tray_icon_->SetHighlightMode(mode); } +void Tray::SetIgnoreDoubleClickEvents(bool ignore) { + tray_icon_->SetIgnoreDoubleClickEvents(ignore); +} + void Tray::DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options) { mate::Handle icon; @@ -224,6 +228,8 @@ void Tray::BuildPrototype(v8::Isolate* isolate, .SetMethod("setToolTip", &Tray::SetToolTip) .SetMethod("setTitle", &Tray::SetTitle) .SetMethod("setHighlightMode", &Tray::SetHighlightMode) + .SetMethod("setIgnoreDoubleClickEvents", + &Tray::SetIgnoreDoubleClickEvents) .SetMethod("displayBalloon", &Tray::DisplayBalloon) .SetMethod("popUpContextMenu", &Tray::PopUpContextMenu) .SetMethod("setContextMenu", &Tray::SetContextMenu) diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 94bcd8adb..298659dda 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -70,6 +70,7 @@ class Tray : public mate::TrackableObject, public TrayIconObserver { void SetToolTip(const std::string& tool_tip); void SetTitle(const std::string& title); void SetHighlightMode(TrayIcon::HighlightMode mode); + void SetIgnoreDoubleClickEvents(bool ignore); void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options); void PopUpContextMenu(mate::Arguments* args); void SetContextMenu(v8::Isolate* isolate, mate::Handle menu); diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index b5332ac97..5a7c0c926 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -16,6 +16,9 @@ void TrayIcon::SetTitle(const std::string& title) {} void TrayIcon::SetHighlightMode(TrayIcon::HighlightMode mode) {} +void TrayIcon::SetIgnoreDoubleClickEvents(bool ignore) { +} + void TrayIcon::DisplayBalloon(ImageType icon, const base::string16& title, const base::string16& contents) {} diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index b91e6ea8a..097fb180d 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -51,6 +51,10 @@ class TrayIcon { }; virtual void SetHighlightMode(HighlightMode mode); + // Sets the flag which determines whether to ignore double click events. This + // only works on macOS. + virtual void SetIgnoreDoubleClickEvents(bool ignore); + // Displays a notification balloon with the specified contents. // Depending on the platform it might not appear by the icon tray. virtual void DisplayBalloon(ImageType icon, diff --git a/atom/browser/ui/tray_icon_cocoa.h b/atom/browser/ui/tray_icon_cocoa.h index 628634434..2f6a5f799 100644 --- a/atom/browser/ui/tray_icon_cocoa.h +++ b/atom/browser/ui/tray_icon_cocoa.h @@ -27,6 +27,7 @@ class TrayIconCocoa : public TrayIcon, public AtomMenuModel::Observer { void SetToolTip(const std::string& tool_tip) override; void SetTitle(const std::string& title) override; void SetHighlightMode(TrayIcon::HighlightMode mode) override; + void SetIgnoreDoubleClickEvents(bool ignore) override; void PopUpContextMenu(const gfx::Point& pos, AtomMenuModel* menu_model) override; void SetContextMenu(AtomMenuModel* menu_model) override; diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index c4ef1a4a5..c47121200 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -25,6 +25,7 @@ const CGFloat kVerticalTitleMargin = 2; atom::TrayIconCocoa* trayIcon_; // weak AtomMenuController* menuController_; // weak atom::TrayIcon::HighlightMode highlight_mode_; + BOOL ignoreDoubleClickEvents_; BOOL forceHighlight_; BOOL inMouseEventSequence_; BOOL ANSI_; @@ -44,6 +45,7 @@ const CGFloat kVerticalTitleMargin = 2; image_.reset([image copy]); trayIcon_ = icon; highlight_mode_ = atom::TrayIcon::HighlightMode::SELECTION; + ignoreDoubleClickEvents_ = NO; forceHighlight_ = NO; inMouseEventSequence_ = NO; @@ -204,6 +206,10 @@ const CGFloat kVerticalTitleMargin = 2; [self setNeedsDisplay:YES]; } +- (void) setIgnoreDoubleClickEvents:(BOOL)ignore { + ignoreDoubleClickEvents_ = ignore; +} + - (void)setTitle:(NSString*)title { if (title.length > 0) { title_.reset([title copy]); @@ -279,6 +285,17 @@ const CGFloat kVerticalTitleMargin = 2; if (menuController_) return; + // If we are ignoring double click events, we should ignore the `clickCount` + // value and immediately emit a click event. + if (ignoreDoubleClickEvents_ == YES) { + trayIcon_->NotifyClicked( + gfx::ScreenRectFromNSRect(event.window.frame), + gfx::ScreenPointFromNSPoint([event locationInWindow]), + ui::EventFlagsFromModifiers([event modifierFlags])); + [self setNeedsDisplay:YES]; + return; + } + // Single click event. if (event.clickCount == 1) trayIcon_->NotifyClicked( @@ -437,6 +454,10 @@ void TrayIconCocoa::SetHighlightMode(TrayIcon::HighlightMode mode) { [status_item_view_ setHighlight:mode]; } +void TrayIconCocoa::SetIgnoreDoubleClickEvents(bool ignore) { + [status_item_view_ setIgnoreDoubleClickEvents:ignore]; +} + void TrayIconCocoa::PopUpContextMenu(const gfx::Point& pos, AtomMenuModel* menu_model) { [status_item_view_ popUpContextMenu:menu_model]; From e1dcd79e48a98ae32e3c31ba3e8489872927544d Mon Sep 17 00:00:00 2001 From: mikeykhalil Date: Sun, 1 Apr 2018 18:28:44 -0700 Subject: [PATCH 2/6] updated Tray API docs to include new setIgnoreDoubleClickEvents method --- docs/api/tray.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/api/tray.md b/docs/api/tray.md index f130d1279..8daec5496 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -241,6 +241,15 @@ win.on('hide', () => { }) ``` +#### `tray.setIgnoreDoubleClickEvents(ignore)` _macOS_ + +* `ignore` Boolean + +Sets the option to ignore double click events. Ignoring these events allow you +to detect every individual click of the tray icon. + +This value is set to false by default. + #### `tray.displayBalloon(options)` _Windows_ * `options` Object From 664e14b91f20aced032bf7f7eab608d934300a18 Mon Sep 17 00:00:00 2001 From: mikeykhalil Date: Mon, 16 Apr 2018 22:00:38 -0700 Subject: [PATCH 3/6] fixed typo found in code review --- docs/api/tray.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/tray.md b/docs/api/tray.md index 8daec5496..d7ece0340 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -245,7 +245,7 @@ win.on('hide', () => { * `ignore` Boolean -Sets the option to ignore double click events. Ignoring these events allow you +Sets the option to ignore double click events. Ignoring these events allows you to detect every individual click of the tray icon. This value is set to false by default. From 208374afa4ede558693a87aaf7032eb31d757768 Mon Sep 17 00:00:00 2001 From: mikeykhalil Date: Sat, 28 Apr 2018 20:07:32 -0700 Subject: [PATCH 4/6] clean up ignore double click event implementation --- atom/browser/api/atom_api_tray.cc | 2 ++ atom/browser/ui/tray_icon_cocoa.mm | 16 ++++------------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 20ed32e35..80a4d20c9 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -177,7 +177,9 @@ void Tray::SetHighlightMode(TrayIcon::HighlightMode mode) { } void Tray::SetIgnoreDoubleClickEvents(bool ignore) { +#if defined(OS_MACOSX) tray_icon_->SetIgnoreDoubleClickEvents(ignore); +#endif } void Tray::DisplayBalloon(mate::Arguments* args, diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index c47121200..166a6fe82 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -287,24 +287,16 @@ const CGFloat kVerticalTitleMargin = 2; // If we are ignoring double click events, we should ignore the `clickCount` // value and immediately emit a click event. - if (ignoreDoubleClickEvents_ == YES) { - trayIcon_->NotifyClicked( - gfx::ScreenRectFromNSRect(event.window.frame), - gfx::ScreenPointFromNSPoint([event locationInWindow]), - ui::EventFlagsFromModifiers([event modifierFlags])); - [self setNeedsDisplay:YES]; - return; - } - - // Single click event. - if (event.clickCount == 1) + BOOL shouldBeHandledAsASingleClick = (event.clickCount == 1) || ignoreDoubleClickEvents_; + if (shouldBeHandledAsASingleClick) trayIcon_->NotifyClicked( gfx::ScreenRectFromNSRect(event.window.frame), gfx::ScreenPointFromNSPoint([event locationInWindow]), ui::EventFlagsFromModifiers([event modifierFlags])); // Double click event. - if (event.clickCount == 2) + BOOL shouldBeHandledAsADoubleClick = (event.clickCount == 2) && !ignoreDoubleClickEvents_; + if (shouldBeHandledAsADoubleClick) trayIcon_->NotifyDoubleClicked( gfx::ScreenRectFromNSRect(event.window.frame), ui::EventFlagsFromModifiers([event modifierFlags])); From 94ffd4bfc017b47641fdfe96f46cbba83196f512 Mon Sep 17 00:00:00 2001 From: mikeykhalil Date: Thu, 3 May 2018 00:43:46 -0700 Subject: [PATCH 5/6] add getter for ignoreDoubleClickEvents field --- atom/browser/api/atom_api_tray.cc | 10 ++++++++++ atom/browser/api/atom_api_tray.h | 1 + atom/browser/ui/tray_icon.cc | 3 --- atom/browser/ui/tray_icon.h | 9 ++++++--- atom/browser/ui/tray_icon_cocoa.h | 1 + atom/browser/ui/tray_icon_cocoa.mm | 10 +++++++++- 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 80a4d20c9..20416b012 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -182,6 +182,14 @@ void Tray::SetIgnoreDoubleClickEvents(bool ignore) { #endif } +bool Tray::GetIgnoreDoubleClickEvents() { +#if defined(OS_MACOSX) + return tray_icon_->GetIgnoreDoubleClickEvents(); +#else + return false; +#endif +} + void Tray::DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options) { mate::Handle icon; @@ -232,6 +240,8 @@ void Tray::BuildPrototype(v8::Isolate* isolate, .SetMethod("setHighlightMode", &Tray::SetHighlightMode) .SetMethod("setIgnoreDoubleClickEvents", &Tray::SetIgnoreDoubleClickEvents) + .SetMethod("getIgnoreDoubleClickEvents", + &Tray::GetIgnoreDoubleClickEvents) .SetMethod("displayBalloon", &Tray::DisplayBalloon) .SetMethod("popUpContextMenu", &Tray::PopUpContextMenu) .SetMethod("setContextMenu", &Tray::SetContextMenu) diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 298659dda..2ccb836d6 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -71,6 +71,7 @@ class Tray : public mate::TrackableObject, public TrayIconObserver { void SetTitle(const std::string& title); void SetHighlightMode(TrayIcon::HighlightMode mode); void SetIgnoreDoubleClickEvents(bool ignore); + bool GetIgnoreDoubleClickEvents(); void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options); void PopUpContextMenu(mate::Arguments* args); void SetContextMenu(v8::Isolate* isolate, mate::Handle menu); diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index 5a7c0c926..b5332ac97 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -16,9 +16,6 @@ void TrayIcon::SetTitle(const std::string& title) {} void TrayIcon::SetHighlightMode(TrayIcon::HighlightMode mode) {} -void TrayIcon::SetIgnoreDoubleClickEvents(bool ignore) { -} - void TrayIcon::DisplayBalloon(ImageType icon, const base::string16& title, const base::string16& contents) {} diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 097fb180d..76155000e 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -51,9 +51,12 @@ class TrayIcon { }; virtual void SetHighlightMode(HighlightMode mode); - // Sets the flag which determines whether to ignore double click events. This - // only works on macOS. - virtual void SetIgnoreDoubleClickEvents(bool ignore); + // Setter and getter for the flag which determines whether to ignore double + // click events. These only work on macOS. +#if defined(OS_MACOSX) + virtual void SetIgnoreDoubleClickEvents(bool ignore) = 0; + virtual bool GetIgnoreDoubleClickEvents() = 0; +#endif // Displays a notification balloon with the specified contents. // Depending on the platform it might not appear by the icon tray. diff --git a/atom/browser/ui/tray_icon_cocoa.h b/atom/browser/ui/tray_icon_cocoa.h index 2f6a5f799..0208b1898 100644 --- a/atom/browser/ui/tray_icon_cocoa.h +++ b/atom/browser/ui/tray_icon_cocoa.h @@ -28,6 +28,7 @@ class TrayIconCocoa : public TrayIcon, public AtomMenuModel::Observer { void SetTitle(const std::string& title) override; void SetHighlightMode(TrayIcon::HighlightMode mode) override; void SetIgnoreDoubleClickEvents(bool ignore) override; + bool GetIgnoreDoubleClickEvents() override; void PopUpContextMenu(const gfx::Point& pos, AtomMenuModel* menu_model) override; void SetContextMenu(AtomMenuModel* menu_model) override; diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 166a6fe82..f5ad0624d 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -206,10 +206,14 @@ const CGFloat kVerticalTitleMargin = 2; [self setNeedsDisplay:YES]; } -- (void) setIgnoreDoubleClickEvents:(BOOL)ignore { +- (void)setIgnoreDoubleClickEvents:(BOOL)ignore { ignoreDoubleClickEvents_ = ignore; } +- (BOOL)getIgnoreDoubleClickEvents { + return ignoreDoubleClickEvents_; +} + - (void)setTitle:(NSString*)title { if (title.length > 0) { title_.reset([title copy]); @@ -450,6 +454,10 @@ void TrayIconCocoa::SetIgnoreDoubleClickEvents(bool ignore) { [status_item_view_ setIgnoreDoubleClickEvents:ignore]; } +bool TrayIconCocoa::GetIgnoreDoubleClickEvents() { + return [status_item_view_ getIgnoreDoubleClickEvents]; +} + void TrayIconCocoa::PopUpContextMenu(const gfx::Point& pos, AtomMenuModel* menu_model) { [status_item_view_ popUpContextMenu:menu_model]; From 3aba515bbc85381741d26b982ac5ed77220b7985 Mon Sep 17 00:00:00 2001 From: mikeykhalil Date: Thu, 3 May 2018 01:02:56 -0700 Subject: [PATCH 6/6] update docs for getIgnoreDoubleClickEvents method --- docs/api/tray.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/api/tray.md b/docs/api/tray.md index d7ece0340..e4d270d01 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -250,6 +250,10 @@ to detect every individual click of the tray icon. This value is set to false by default. +### `tray.getIgnoreDoubleClickEvents()` _macOS_ + +Returns `Boolean` - Whether double click events will be ignored. + #### `tray.displayBalloon(options)` _Windows_ * `options` Object