From cca4f4abd588d7fe3970d3cca6d7f0f5d6b86282 Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Wed, 15 Jul 2015 19:23:12 +0800 Subject: [PATCH] Implement 'right-clicked' tray event on OS X. --- atom/browser/api/atom_api_tray.cc | 4 ++++ atom/browser/api/atom_api_tray.h | 1 + atom/browser/ui/tray_icon.cc | 4 ++++ atom/browser/ui/tray_icon.h | 1 + atom/browser/ui/tray_icon_cocoa.mm | 19 +++++++++++++------ atom/browser/ui/tray_icon_observer.h | 1 + 6 files changed, 24 insertions(+), 6 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index 649967a2b1eb..7c1bfdfac95a 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -60,6 +60,10 @@ void Tray::OnBalloonClosed() { Emit("balloon-closed"); } +void Tray::OnRightClicked(const gfx::Rect& bounds) { + Emit("right-clicked", bounds); +} + bool Tray::IsDestroyed() const { return !tray_icon_; } diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 1a4a498d16b9..366a847ac602 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -46,6 +46,7 @@ class Tray : public mate::EventEmitter, void OnBalloonShow() override; void OnBalloonClicked() override; void OnBalloonClosed() override; + void OnRightClicked(const gfx::Rect&) override; // mate::Wrappable: bool IsDestroyed() const override; diff --git a/atom/browser/ui/tray_icon.cc b/atom/browser/ui/tray_icon.cc index a3878f718a62..4f801faf862e 100644 --- a/atom/browser/ui/tray_icon.cc +++ b/atom/browser/ui/tray_icon.cc @@ -46,4 +46,8 @@ void TrayIcon::NotifyBalloonClosed() { FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnBalloonClosed()); } +void TrayIcon::NotifyRightClicked(const gfx::Rect& bounds) { + FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnRightClicked(bounds)); +} + } // namespace atom diff --git a/atom/browser/ui/tray_icon.h b/atom/browser/ui/tray_icon.h index 7dc67da1bac4..4e097e0ac84a 100644 --- a/atom/browser/ui/tray_icon.h +++ b/atom/browser/ui/tray_icon.h @@ -56,6 +56,7 @@ class TrayIcon { void NotifyBalloonShow(); void NotifyBalloonClicked(); void NotifyBalloonClosed(); + void NotifyRightClicked(const gfx::Rect& bounds = gfx::Rect()); protected: TrayIcon(); diff --git a/atom/browser/ui/tray_icon_cocoa.mm b/atom/browser/ui/tray_icon_cocoa.mm index 11efbe0aee90..2d0acdd6aaf4 100644 --- a/atom/browser/ui/tray_icon_cocoa.mm +++ b/atom/browser/ui/tray_icon_cocoa.mm @@ -75,7 +75,7 @@ const CGFloat kMargin = 3; title_width + kStatusItemLength, [[statusItem_ statusBar] thickness]); [title_ drawInRect:title_rect - withAttributes:[self titleAttributes]]; + withAttributes:[self titleAttributes]]; [statusItem_ setLength:title_width + kStatusItemLength]; } } @@ -129,11 +129,7 @@ const CGFloat kMargin = 3; [statusItem_ popUpStatusItemMenu:[menu_controller_ menu]]; } - NSRect frame = event.window.frame; - gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame)); - NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; - bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame)); - trayIcon_->NotifyClicked(bounds); + trayIcon_->NotifyClicked([self getBoundsFromEvent:event]); } if (event.clickCount == 2 && !menu_controller_) { @@ -142,11 +138,22 @@ const CGFloat kMargin = 3; [self setNeedsDisplay:YES]; } +- (void)rightMouseUp:(NSEvent*)event { + trayIcon_->NotifyRightClicked([self getBoundsFromEvent:event]); +} + -(BOOL) shouldHighlight { BOOL is_menu_open = [menu_controller_ isMenuOpen]; return isHighlightEnable_ && (inMouseEventSequence_ || is_menu_open); } +-(gfx::Rect) getBoundsFromEvent:(NSEvent*)event { + NSRect frame = event.window.frame; + gfx::Rect bounds(frame.origin.x, 0, NSWidth(frame), NSHeight(frame)); + NSScreen* screen = [[NSScreen screens] objectAtIndex:0]; + bounds.set_y(NSHeight([screen frame]) - NSMaxY(frame)); + return bounds; +} @end namespace atom { diff --git a/atom/browser/ui/tray_icon_observer.h b/atom/browser/ui/tray_icon_observer.h index 3a34888b5318..061115efdc64 100644 --- a/atom/browser/ui/tray_icon_observer.h +++ b/atom/browser/ui/tray_icon_observer.h @@ -18,6 +18,7 @@ class TrayIconObserver { virtual void OnBalloonShow() {} virtual void OnBalloonClicked() {} virtual void OnBalloonClosed() {} + virtual void OnRightClicked(const gfx::Rect&) {} protected: virtual ~TrayIconObserver() {}