From 0b205019b6ff068bfff9508434e7c8bf7379c261 Mon Sep 17 00:00:00 2001 From: Sean Bannigan Date: Sat, 26 Aug 2017 14:04:58 -0400 Subject: [PATCH] add mouse-move event and click event position for tray --- atom/browser/api/atom_api_tray.cc | 8 ++++++-- atom/browser/api/atom_api_tray.h | 3 ++- atom/browser/ui/tray_icon.cc | 9 +++++++-- atom/browser/ui/tray_icon.h | 4 +++- atom/browser/ui/tray_icon_cocoa.mm | 11 +++++++++-- atom/browser/ui/tray_icon_observer.h | 3 ++- docs/api/tray.md | 12 ++++++++++++ 7 files changed, 41 insertions(+), 9 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 0a5ff4907c0a..d8cc54ecf6de 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -86,8 +86,8 @@ mate::WrappableBase* Tray::New(mate::Handle image, return new Tray(args->isolate(), args->GetThis(), image); } -void Tray::OnClicked(const gfx::Rect& bounds, int modifiers) { - EmitWithFlags("click", modifiers, bounds); +void Tray::OnClicked(const gfx::Rect& bounds, const gfx::Point& location, int modifiers) { + EmitWithFlags("click", modifiers, bounds, location); } void Tray::OnDoubleClicked(const gfx::Rect& bounds, int modifiers) { @@ -130,6 +130,10 @@ void Tray::OnMouseExited(const gfx::Point& location, int modifiers) { EmitWithFlags("mouse-leave", modifiers, location); } +void Tray::OnMouseMoved(const gfx::Point& location, int modifiers) { + EmitWithFlags("mouse-move", modifiers, location); +} + 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 301b3c5e873a..9e722cb4e603 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -47,7 +47,7 @@ class Tray : public mate::TrackableObject, ~Tray() override; // TrayIconObserver: - void OnClicked(const gfx::Rect& bounds, int modifiers) override; + void OnClicked(const gfx::Rect& bounds, const gfx::Point& location, int modifiers) override; void OnDoubleClicked(const gfx::Rect& bounds, int modifiers) override; void OnRightClicked(const gfx::Rect& bounds, int modifiers) override; void OnBalloonShow() override; @@ -61,6 +61,7 @@ class Tray : public mate::TrackableObject, void OnDragEnded() override; void OnMouseEntered(const gfx::Point& location, int modifiers) override; void OnMouseExited(const gfx::Point& location, int modifiers) override; + void OnMouseMoved(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 bcaf10e4e486..f4a72c8cc250 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -34,9 +34,9 @@ gfx::Rect TrayIcon::GetBounds() { return gfx::Rect(); } -void TrayIcon::NotifyClicked(const gfx::Rect& bounds, int modifiers) { +void TrayIcon::NotifyClicked(const gfx::Rect& bounds, const gfx::Point& location, int modifiers) { for (TrayIconObserver& observer : observers_) - observer.OnClicked(bounds, modifiers); + observer.OnClicked(bounds, location, modifiers); } void TrayIcon::NotifyDoubleClicked(const gfx::Rect& bounds, int modifiers) { @@ -89,6 +89,11 @@ void TrayIcon::NotifyMouseExited(const gfx::Point& location, int modifiers) { observer.OnMouseExited(location, modifiers); } +void TrayIcon::NotifyMouseMoved(const gfx::Point& location, int modifiers) { + for (TrayIconObserver& observer : observers_) + observer.OnMouseMoved(location, modifiers); +} + 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 c3482864628c..f5d8887d674f 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -70,7 +70,7 @@ class TrayIcon { void AddObserver(TrayIconObserver* obs) { observers_.AddObserver(obs); } void RemoveObserver(TrayIconObserver* obs) { observers_.RemoveObserver(obs); } - void NotifyClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0); + void NotifyClicked(const gfx::Rect& = gfx::Rect(), const gfx::Point& location = gfx::Point(), int modifiers = 0); void NotifyDoubleClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0); void NotifyBalloonShow(); void NotifyBalloonClicked(); @@ -87,6 +87,8 @@ class TrayIcon { int modifiers = 0); void NotifyMouseExited(const gfx::Point& location = gfx::Point(), int modifiers = 0); + void NotifyMouseMoved(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 629180478e16..cac3ca0abdc4 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -57,10 +57,10 @@ const CGFloat kVerticalTitleMargin = 2; // Finalize setup by sizing our views [self updateDimensions]; - // Add NSTrackingArea for listening to mouseEnter and mouseExit events + // Add NSTrackingArea for listening to mouseEnter, mouseExit, and mouseMove events auto trackingArea = [[[NSTrackingArea alloc] initWithRect:[self bounds] - options:NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways + options:NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveAlways owner:self userInfo:nil] autorelease]; [self addTrackingArea:trackingArea]; @@ -249,6 +249,7 @@ const CGFloat kVerticalTitleMargin = 2; if (event.clickCount == 1) trayIcon_->NotifyClicked( gfx::ScreenRectFromNSRect(event.window.frame), + gfx::ScreenPointFromNSPoint([event locationInWindow]), ui::EventFlagsFromModifiers([event modifierFlags])); // Double click event. @@ -307,6 +308,12 @@ const CGFloat kVerticalTitleMargin = 2; ui::EventFlagsFromModifiers([event modifierFlags])); } +- (void)mouseMoved:(NSEvent*)event { + trayIcon_->NotifyMouseMoved( + gfx::ScreenPointFromNSPoint([event locationInWindow]), + ui::EventFlagsFromModifiers([event modifierFlags])); +} + - (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 8d21ee0f2751..34a7ff369409 100644 --- a/atom/browser/ui/tray_icon_observer.h +++ b/atom/browser/ui/tray_icon_observer.h @@ -17,7 +17,7 @@ namespace atom { class TrayIconObserver { public: - virtual void OnClicked(const gfx::Rect& bounds, int modifiers) {} + virtual void OnClicked(const gfx::Rect& bounds, const gfx::Point& location, int modifiers) {} virtual void OnDoubleClicked(const gfx::Rect& bounds, int modifiers) {} virtual void OnBalloonShow() {} virtual void OnBalloonClicked() {} @@ -31,6 +31,7 @@ class TrayIconObserver { virtual void OnDragEnded() {} virtual void OnMouseEntered(const gfx::Point& location, int modifiers) {} virtual void OnMouseExited(const gfx::Point& location, int modifiers) {} + virtual void OnMouseMoved(const gfx::Point& location, int modifiers) {} protected: virtual ~TrayIconObserver() {} diff --git a/docs/api/tray.md b/docs/api/tray.md index cf578494f226..53efaa712011 100644 --- a/docs/api/tray.md +++ b/docs/api/tray.md @@ -76,6 +76,7 @@ The `Tray` module emits the following events: * `ctrlKey` Boolean * `metaKey` Boolean * `bounds` [Rectangle](structures/rectangle.md) - The bounds of tray icon +* `position` [Point](structures/point.md) - The position of the event Emitted when the tray icon is clicked. @@ -166,6 +167,17 @@ Emitted when the mouse enters the tray icon. Emitted when the mouse exits the tray icon. +#### Event: 'mouse-move' _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 moves in the tray icon. + ### Instance Methods The `Tray` class has the following methods: