Browse Source

Implement 'right-clicked' tray event on OS X.

Haojian Wu 9 years ago
parent
commit
cca4f4abd5

+ 4 - 0
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_;
 }

+ 1 - 0
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;

+ 4 - 0
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

+ 1 - 0
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();

+ 13 - 6
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 {

+ 1 - 0
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() {}