From 8dbb8ccbabe50bb7c60d94dd55d58e6542b3e22d Mon Sep 17 00:00:00 2001 From: Shubham Date: Wed, 14 Jun 2017 18:00:29 -0400 Subject: [PATCH] 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 4e9b86dbb2d..bc903da7253 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 61ee72bedf9..52f04b0752e 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 882a2ba27a0..d08cef7a541 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 90e81657aae..599ed9ec6ad 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 1ecd51fe566..38be918dcad 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 1782e913d54..53587a0f391 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 141b24bbbff..1f530170f28 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: