From 8dbb8ccbabe50bb7c60d94dd55d58e6542b3e22d Mon Sep 17 00:00:00 2001 From: Shubham Date: Wed, 14 Jun 2017 18:00:29 -0400 Subject: [PATCH 1/4] add mouse-enter and mouse-exit events for tray --- atom/browser/api/atom_api_tray.cc | 8 ++++++++ atom/browser/api/atom_api_tray.h | 2 ++ atom/browser/ui/tray_icon.cc | 10 ++++++++++ atom/browser/ui/tray_icon.h | 2 ++ atom/browser/ui/tray_icon_cocoa.mm | 17 ++++++++++++++++- atom/browser/ui/tray_icon_observer.h | 2 ++ docs/api/tray.md | 8 ++++++++ 7 files changed, 48 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 4e9b86dbb2d0..bc903da72534 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -122,6 +122,14 @@ void Tray::OnDropText(const std::string& text) { Emit("drop-text", text); } +void Tray::OnMouseEntered() { + Emit("mouse-enter"); +} + +void Tray::OnMouseExited() { + Emit("mouse-exited"); +} + void Tray::OnDragEntered() { Emit("drag-enter"); } diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 61ee72bedf9f..52f04b0752eb 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -59,6 +59,8 @@ class Tray : public mate::TrackableObject, void OnDragEntered() override; void OnDragExited() override; void OnDragEnded() override; + void OnMouseEntered() override; + void OnMouseExited() override; void SetImage(v8::Isolate* isolate, mate::Handle image); void SetPressedImage(v8::Isolate* isolate, mate::Handle image); diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index 882a2ba27a0f..d08cef7a5418 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -79,6 +79,16 @@ void TrayIcon::NotifyDropText(const std::string& text) { observer.OnDropText(text); } +void TrayIcon::NotifyMouseEntered() { + for (TrayIconObserver& observer : observers_) + observer.OnMouseEntered(); +} + +void TrayIcon::NotifyMouseExited() { + for (TrayIconObserver& observer : observers_) + observer.OnMouseExited(); +} + void TrayIcon::NotifyDragEntered() { for (TrayIconObserver& observer : observers_) observer.OnDragEntered(); diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 90e81657aae2..599ed9ec6ad9 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -83,6 +83,8 @@ class TrayIcon { void NotifyDragEntered(); void NotifyDragExited(); void NotifyDragEnded(); + void NotifyMouseEntered(); + void NotifyMouseExited(); protected: TrayIcon(); diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 1ecd51fe566a..38be918dcad8 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -54,9 +54,16 @@ const CGFloat kVerticalTitleMargin = 2; statusItemWithLength:NSVariableStatusItemLength]; statusItem_.reset([item retain]); [statusItem_ setView:self]; - // Finalize setup by sizing our views [self updateDimensions]; + + // Add NSTrackingArea for listening to mouseEnter and mouseExit events + int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways); + NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] + options:opts + owner:self + userInfo:nil]; + [self addTrackingArea:trackingArea]; } return self; } @@ -288,6 +295,14 @@ const CGFloat kVerticalTitleMargin = 2; return NSDragOperationCopy; } +- (void)mouseExited:(NSEvent*)event { + trayIcon_->NotifyMouseExited(); +} + +- (void)mouseEntered:(NSEvent*)event { + trayIcon_->NotifyMouseEntered(); +} + - (void)draggingExited:(id )sender { trayIcon_->NotifyDragExited(); } diff --git a/atom/browser/ui/tray_icon_observer.h b/atom/browser/ui/tray_icon_observer.h index 1782e913d541..53587a0f3916 100644 --- a/atom/browser/ui/tray_icon_observer.h +++ b/atom/browser/ui/tray_icon_observer.h @@ -28,6 +28,8 @@ class TrayIconObserver { virtual void OnDragEntered() {} virtual void OnDragExited() {} virtual void OnDragEnded() {} + virtual void OnMouseEntered() {} + virtual void OnMouseExited() {} protected: virtual ~TrayIconObserver() {} diff --git a/docs/api/tray.md b/docs/api/tray.md index 141b24bbbff1..1f530170f287 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -144,6 +144,14 @@ Emitted when a drag operation exits the tray icon. Emitted when a drag operation ends on the tray or ends at another location. +#### Event: 'mouse-enter' _macOS_ + +Emitted when the mouse enters the tray icon. + +#### Event: 'mouse-exit' _macOS_ + +Emitted when the mouse exits the tray icon. + ### Instance Methods The `Tray` class has the following methods: From 2406d6c279c23793d1ddd57bccd28a74df2558d3 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 28 Jun 2017 11:40:21 -0700 Subject: [PATCH 2/4] Rename to mouse-leave for consistency with drag-leave --- atom/browser/api/atom_api_tray.cc | 2 +- docs/api/tray.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index bc903da72534..8d0ab829182e 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -127,7 +127,7 @@ void Tray::OnMouseEntered() { } void Tray::OnMouseExited() { - Emit("mouse-exited"); + Emit("mouse-leave"); } void Tray::OnDragEntered() { diff --git a/docs/api/tray.md b/docs/api/tray.md index 1f530170f287..e39ab6136af8 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -148,7 +148,7 @@ Emitted when a drag operation ends on the tray or ends at another location. Emitted when the mouse enters the tray icon. -#### Event: 'mouse-exit' _macOS_ +#### Event: 'mouse-leave' _macOS_ Emitted when the mouse exits the tray icon. From 70544440f97d8c1e423ce45756f16c45a0be7617 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 28 Jun 2017 12:09:12 -0700 Subject: [PATCH 3/4] Add location and keyboard modifiers to tray mouse events --- atom/browser/api/atom_api_tray.cc | 8 ++++---- atom/browser/api/atom_api_tray.h | 4 ++-- atom/browser/ui/tray_icon.cc | 8 ++++---- atom/browser/ui/tray_icon.h | 6 ++++-- atom/browser/ui/tray_icon_cocoa.mm | 8 ++++++-- atom/browser/ui/tray_icon_observer.h | 5 +++-- docs/api/tray.md | 14 ++++++++++++++ 7 files changed, 37 insertions(+), 16 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 8d0ab829182e..0a5ff4907c0a 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -122,12 +122,12 @@ void Tray::OnDropText(const std::string& text) { Emit("drop-text", text); } -void Tray::OnMouseEntered() { - Emit("mouse-enter"); +void Tray::OnMouseEntered(const gfx::Point& location, int modifiers) { + EmitWithFlags("mouse-enter", modifiers, location); } -void Tray::OnMouseExited() { - Emit("mouse-leave"); +void Tray::OnMouseExited(const gfx::Point& location, int modifiers) { + EmitWithFlags("mouse-leave", modifiers, location); } void Tray::OnDragEntered() { diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 52f04b0752eb..301b3c5e873a 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -59,8 +59,8 @@ class Tray : public mate::TrackableObject, void OnDragEntered() override; void OnDragExited() override; void OnDragEnded() override; - void OnMouseEntered() override; - void OnMouseExited() override; + void OnMouseEntered(const gfx::Point& location, int modifiers) override; + void OnMouseExited(const gfx::Point& location, int modifiers) override; void SetImage(v8::Isolate* isolate, mate::Handle image); void SetPressedImage(v8::Isolate* isolate, mate::Handle image); diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index d08cef7a5418..bcaf10e4e486 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -79,14 +79,14 @@ void TrayIcon::NotifyDropText(const std::string& text) { observer.OnDropText(text); } -void TrayIcon::NotifyMouseEntered() { +void TrayIcon::NotifyMouseEntered(const gfx::Point& location, int modifiers) { for (TrayIconObserver& observer : observers_) - observer.OnMouseEntered(); + observer.OnMouseEntered(location, modifiers); } -void TrayIcon::NotifyMouseExited() { +void TrayIcon::NotifyMouseExited(const gfx::Point& location, int modifiers) { for (TrayIconObserver& observer : observers_) - observer.OnMouseExited(); + observer.OnMouseExited(location, modifiers); } void TrayIcon::NotifyDragEntered() { diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 599ed9ec6ad9..c3482864628c 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -83,8 +83,10 @@ class TrayIcon { void NotifyDragEntered(); void NotifyDragExited(); void NotifyDragEnded(); - void NotifyMouseEntered(); - void NotifyMouseExited(); + void NotifyMouseEntered(const gfx::Point& location = gfx::Point(), + int modifiers = 0); + void NotifyMouseExited(const gfx::Point& location = gfx::Point(), + int modifiers = 0); protected: TrayIcon(); diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 38be918dcad8..fe6818d2c9f5 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -296,11 +296,15 @@ const CGFloat kVerticalTitleMargin = 2; } - (void)mouseExited:(NSEvent*)event { - trayIcon_->NotifyMouseExited(); + trayIcon_->NotifyMouseExited( + gfx::ScreenPointFromNSPoint([event locationInWindow]), + ui::EventFlagsFromModifiers([event modifierFlags])); } - (void)mouseEntered:(NSEvent*)event { - trayIcon_->NotifyMouseEntered(); + trayIcon_->NotifyMouseEntered( + gfx::ScreenPointFromNSPoint([event locationInWindow]), + ui::EventFlagsFromModifiers([event modifierFlags])); } - (void)draggingExited:(id )sender { diff --git a/atom/browser/ui/tray_icon_observer.h b/atom/browser/ui/tray_icon_observer.h index 53587a0f3916..8d21ee0f2751 100644 --- a/atom/browser/ui/tray_icon_observer.h +++ b/atom/browser/ui/tray_icon_observer.h @@ -10,6 +10,7 @@ namespace gfx { class Rect; +class Point; } namespace atom { @@ -28,8 +29,8 @@ class TrayIconObserver { virtual void OnDragEntered() {} virtual void OnDragExited() {} virtual void OnDragEnded() {} - virtual void OnMouseEntered() {} - virtual void OnMouseExited() {} + virtual void OnMouseEntered(const gfx::Point& location, int modifiers) {} + virtual void OnMouseExited(const gfx::Point& location, int modifiers) {} protected: virtual ~TrayIconObserver() {} diff --git a/docs/api/tray.md b/docs/api/tray.md index e39ab6136af8..cf578494f226 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -146,10 +146,24 @@ Emitted when a drag operation ends on the tray or ends at another location. #### Event: 'mouse-enter' _macOS_ +* `event` Event + * `altKey` Boolean + * `shiftKey` Boolean + * `ctrlKey` Boolean + * `metaKey` Boolean +* `position` [Point](structures/point.md) - The position of the event + Emitted when the mouse enters the tray icon. #### Event: 'mouse-leave' _macOS_ +* `event` Event + * `altKey` Boolean + * `shiftKey` Boolean + * `ctrlKey` Boolean + * `metaKey` Boolean +* `position` [Point](structures/point.md) - The position of the event + Emitted when the mouse exits the tray icon. ### Instance Methods From d53f07cc20319c328ce8c6951a039a113dc076f0 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 28 Jun 2017 12:13:12 -0700 Subject: [PATCH 4/4] :art: --- atom/browser/ui/tray_icon_cocoa.mm | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index fe6818d2c9f5..a6b5bb96b03d 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -58,11 +58,11 @@ const CGFloat kVerticalTitleMargin = 2; [self updateDimensions]; // Add NSTrackingArea for listening to mouseEnter and mouseExit events - int opts = (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways); - NSTrackingArea* trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] - options:opts - owner:self - userInfo:nil]; + NSTrackingArea* trackingArea = [[NSTrackingArea alloc] + initWithRect:[self bounds] + options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways + owner:self + userInfo:nil]; [self addTrackingArea:trackingArea]; } return self;