]> khamidou — GitHub mirrors - electron.git/commitdiff
Add keyboard modifiers payload to tray click events
authorNishanth Shanmugham <nishanth.gerrard@gmail.com>
Mon, 27 Jul 2015 10:15:51 +0000 (03:15 -0700)
committerNishanth Shanmugham <nishanth.gerrard@gmail.com>
Mon, 27 Jul 2015 10:15:51 +0000 (03:15 -0700)
* Add keyboard and mouse button bitsum to Tray click events payload
* Move getBoundsFromRect: to common event_util file
* Update documentation

atom/browser/api/atom_api_tray.cc
atom/browser/api/atom_api_tray.h
atom/browser/ui/event_util.h
atom/browser/ui/event_util_mac.mm
atom/browser/ui/tray_icon.cc
atom/browser/ui/tray_icon.h
atom/browser/ui/tray_icon_cocoa.mm
atom/browser/ui/tray_icon_observer.h
docs/api/tray.md

index 82b2d01c613b5365742ee89c59977e124ed18644..a849c03f59a0acf88d325211de1845040793faba 100644 (file)
@@ -40,12 +40,12 @@ mate::Wrappable* Tray::New(v8::Isolate* isolate, const gfx::Image& image) {
   return new Tray(image);
 }
 
-void Tray::OnClicked(const gfx::Rect& bounds) {
-  Emit("clicked", bounds);
+void Tray::OnClicked(const gfx::Rect& bounds, int modifiers) {
+  Emit("clicked", bounds, modifiers);
 }
 
-void Tray::OnDoubleClicked(const gfx::Rect& bounds) {
-  Emit("double-clicked", bounds);
+void Tray::OnDoubleClicked(const gfx::Rect& bounds, int modifiers) {
+  Emit("double-clicked", bounds, modifiers);
 }
 
 void Tray::OnBalloonShow() {
@@ -60,8 +60,8 @@ void Tray::OnBalloonClosed() {
   Emit("balloon-closed");
 }
 
-void Tray::OnRightClicked(const gfx::Rect& bounds) {
-  Emit("right-clicked", bounds);
+void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) {
+  Emit("right-clicked", bounds, modifiers);
 }
 
 void Tray::OnDropFiles(const std::vector<std::string>& files) {
index f3b91f00c0f0d8b07c8ee784eb9c788a7b67f3d2..10340677813fec03b9e3adda542b81560cb8105d 100644 (file)
@@ -42,12 +42,12 @@ class Tray : public mate::EventEmitter,
   virtual ~Tray();
 
   // TrayIconObserver:
-  void OnClicked(const gfx::Rect& bounds) override;
-  void OnDoubleClicked(const gfx::Rect& bounds) override;
+  void OnClicked(const gfx::Rect& bounds, int modifiers) override;
+  void OnDoubleClicked(const gfx::Rect& bounds, int modifiers) override;
   void OnBalloonShow() override;
   void OnBalloonClicked() override;
   void OnBalloonClosed() override;
-  void OnRightClicked(const gfx::Rect& bounds) override;
+  void OnRightClicked(const gfx::Rect& bounds, int modifiers) override;
   void OnDropFiles(const std::vector<std::string>& files) override;
 
   // mate::Wrappable:
index 59a624d6bf9653f3f60b3b877e72514c1ced2063..b7278edaee82d381235d65d496813a8a5fa8233c 100644 (file)
@@ -2,16 +2,19 @@
 #define ATOM_BROWSER_UI_EVENT_UTIL_H_
 
 #import <Cocoa/Cocoa.h>
+#include "ui/gfx/geometry/rect.h"
 
 namespace event_util {
 
-bool isLeftButtonEvent(NSEvent* event);
-bool isRightButtonEvent(NSEvent* event);
-bool isMiddleButtonEvent(NSEvent* event);
+bool IsLeftButtonEvent(NSEvent* event);
+bool IsRightButtonEvent(NSEvent* event);
+bool IsMiddleButtonEvent(NSEvent* event);
 
 // Retrieves a bitsum of ui::EventFlags from NSEvent.
 int EventFlagsFromNSEvent(NSEvent* event);
 
+gfx::Rect GetBoundsFromEvent(NSEvent* event);
+
 } // namespace event_util
 
 #endif // ATOM_BROWSER_UI_EVENT_UTIL_H_
index 5226b155b74e120ba59946536f49e80299b7da7a..13188fdcf823c2e2342f03d414fd4623dd088795 100644 (file)
@@ -1,22 +1,24 @@
 #include "atom/browser/ui/event_util.h"
-#include "ui/base/accelerators/accelerator.h"
+
+#include "ui/events/event_constants.h"
 
 namespace event_util {
-    bool isLeftButtonEvent(NSEvent* event) {
+
+    bool IsLeftButtonEvent(NSEvent* event) {
       NSEventType type = [event type];
       return type == NSLeftMouseDown ||
         type == NSLeftMouseDragged ||
         type == NSLeftMouseUp;
     }
 
-    bool isRightButtonEvent(NSEvent* event) {
+    bool IsRightButtonEvent(NSEvent* event) {
       NSEventType type = [event type];
       return type == NSRightMouseDown ||
         type == NSRightMouseDragged ||
         type == NSRightMouseUp;
     }
 
-    bool isMiddleButtonEvent(NSEvent* event) {
+    bool IsMiddleButtonEvent(NSEvent* event) {
       if ([event buttonNumber] != 2)
         return false;
 
@@ -33,9 +35,9 @@ namespace event_util {
       flags |= (modifiers & NSControlKeyMask) ? ui::EF_CONTROL_DOWN : 0;
       flags |= (modifiers & NSAlternateKeyMask) ? ui::EF_ALT_DOWN : 0;
       flags |= (modifiers & NSCommandKeyMask) ? ui::EF_COMMAND_DOWN : 0;
-      flags |= isLeftButtonEvent(event) ? ui::EF_LEFT_MOUSE_BUTTON : 0;
-      flags |= isRightButtonEvent(event) ? ui::EF_RIGHT_MOUSE_BUTTON : 0;
-      flags |= isMiddleButtonEvent(event) ? ui::EF_MIDDLE_MOUSE_BUTTON : 0;
+      flags |= IsLeftButtonEvent(event) ? ui::EF_LEFT_MOUSE_BUTTON : 0;
+      flags |= IsRightButtonEvent(event) ? ui::EF_RIGHT_MOUSE_BUTTON : 0;
+      flags |= IsMiddleButtonEvent(event) ? ui::EF_MIDDLE_MOUSE_BUTTON : 0;
       return flags;
     }
 
@@ -44,4 +46,20 @@ namespace event_util {
       NSUInteger modifiers = [event modifierFlags];
       return EventFlagsFromNSEventWithModifiers(event, modifiers);
     }
+
+    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;
+    }
+
 } // namespace event_util
index b22250d35c8f6978f21f406a54e6e46dd0d58b93..d706d81868ebfe6abe6f143614a7dcdb07a49890 100644 (file)
@@ -29,12 +29,14 @@ void TrayIcon::DisplayBalloon(const gfx::Image& icon,
 void TrayIcon::PopContextMenu(const gfx::Point& pos) {
 }
 
-void TrayIcon::NotifyClicked(const gfx::Rect& bounds) {
-  FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnClicked(bounds));
+void TrayIcon::NotifyClicked(const gfx::Rect& bounds, int modifiers) {
+  FOR_EACH_OBSERVER(TrayIconObserver, observers_,
+    OnClicked(bounds, modifiers));
 }
 
-void TrayIcon::NotifyDoubleClicked(const gfx::Rect& bounds) {
-  FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnDoubleClicked(bounds));
+void TrayIcon::NotifyDoubleClicked(const gfx::Rect& bounds, int modifiers) {
+  FOR_EACH_OBSERVER(TrayIconObserver, observers_,
+    OnDoubleClicked(bounds, modifiers));
 }
 
 void TrayIcon::NotifyBalloonShow() {
@@ -49,8 +51,9 @@ void TrayIcon::NotifyBalloonClosed() {
   FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnBalloonClosed());
 }
 
-void TrayIcon::NotifyRightClicked(const gfx::Rect& bounds) {
-  FOR_EACH_OBSERVER(TrayIconObserver, observers_, OnRightClicked(bounds));
+void TrayIcon::NotifyRightClicked(const gfx::Rect& bounds, int modifiers) {
+  FOR_EACH_OBSERVER(TrayIconObserver, observers_,
+    OnRightClicked(bounds, modifiers));
 }
 
 void TrayIcon::NotfiyDropFiles(const std::vector<std::string>& files) {
index 5e362806f5d61b0f4ac1e20b5aae7c32a422abf8..a60622db9505dd2e3a2210927320b11fa4426700 100644 (file)
@@ -54,12 +54,13 @@ class TrayIcon {
 
   void AddObserver(TrayIconObserver* obs) { observers_.AddObserver(obs); }
   void RemoveObserver(TrayIconObserver* obs) { observers_.RemoveObserver(obs); }
-  void NotifyClicked(const gfx::Rect& = gfx::Rect());
-  void NotifyDoubleClicked(const gfx::Rect& = gfx::Rect());
+  void NotifyClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0);
+  void NotifyDoubleClicked(const gfx::Rect& = gfx::Rect(), int modifiers = 0);
   void NotifyBalloonShow();
   void NotifyBalloonClicked();
   void NotifyBalloonClosed();
-  void NotifyRightClicked(const gfx::Rect& bounds = gfx::Rect());
+  void NotifyRightClicked(const gfx::Rect& bounds = gfx::Rect(),
+    int modifiers = 0);
   void NotfiyDropFiles(const std::vector<std::string>& files);
 
  protected:
index 59386ff8eb4108d20f1ffc2f166d1e9de089cd54..3329619c99a74ca517a5fefcfcf14dc358d98845 100644 (file)
@@ -5,6 +5,7 @@
 #include "atom/browser/ui/tray_icon_cocoa.h"
 
 #include "atom/browser/ui/cocoa/atom_menu_controller.h"
+#include "atom/browser/ui/event_util.h"
 #include "base/strings/sys_string_conversions.h"
 #include "ui/gfx/image/image.h"
 #include "ui/gfx/screen.h"
@@ -147,16 +148,21 @@ const CGFloat kMargin = 3;
      return;
   }
   inMouseEventSequence_ = NO;
+
+  // Single click
   if (event.clickCount == 1) {
     if (menuController_) {
       [statusItem_ popUpStatusItemMenu:[menuController_ menu]];
     }
 
-    trayIcon_->NotifyClicked([self getBoundsFromEvent:event]);
+    trayIcon_->NotifyClicked(event_util::GetBoundsFromEvent(event),
+      event_util::EventFlagsFromNSEvent(event));
   }
 
+  // Double click
   if (event.clickCount == 2 && !menuController_) {
-    trayIcon_->NotifyDoubleClicked([self getBoundsFromEvent:event]);
+    trayIcon_->NotifyDoubleClicked(event_util::GetBoundsFromEvent(event),
+      event_util::EventFlagsFromNSEvent(event));
   }
   [self setNeedsDisplay:YES];
 }
@@ -173,7 +179,8 @@ const CGFloat kMargin = 3;
 }
 
 - (void)rightMouseUp:(NSEvent*)event {
-  trayIcon_->NotifyRightClicked([self getBoundsFromEvent:event]);
+  trayIcon_->NotifyRightClicked(event_util::GetBoundsFromEvent(event),
+    event_util::EventFlagsFromNSEvent(event));
 }
 
 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
@@ -199,13 +206,6 @@ const CGFloat kMargin = 3;
   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 {
index 474c6a771934ba28db2d92cd7b17d0819b341be3..fa8090d7d6c5f69b51575ae2207295e8dc18496f 100644 (file)
@@ -16,12 +16,12 @@ namespace atom {
 
 class TrayIconObserver {
  public:
-  virtual void OnClicked(const gfx::Rect& bounds) {}
-  virtual void OnDoubleClicked(const gfx::Rect& bounds) {}
+  virtual void OnClicked(const gfx::Rect& bounds, int modifiers) {}
+  virtual void OnDoubleClicked(const gfx::Rect& bounds, int modifiers) {}
   virtual void OnBalloonShow() {}
   virtual void OnBalloonClicked() {}
   virtual void OnBalloonClosed() {}
-  virtual void OnRightClicked(const gfx::Rect& bounds) {}
+  virtual void OnRightClicked(const gfx::Rect& bounds, int modifiers) {}
   virtual void OnDropFiles(const std::vector<std::string>& files) {}
 
  protected:
index 9332822a32a8138db5dba8536ba02e4044ffe817..e35a43587331df06c20ce55d1fe785132ba8cccf 100644 (file)
@@ -53,6 +53,7 @@ Creates a new tray icon associated with the `image`.
   * `y` Integer
   * `width` Integer
   * `height` Integer
+* [`modifiers`](https://code.google.com/p/chromium/codesearch#chromium/src/ui/events/event_constants.h&l=77) number - bitsum of keyboard modifiers and mouse keys
 
 Emitted when the tray icon is clicked.
 
@@ -66,6 +67,7 @@ __Note:__ The `bounds` payload is only implemented on OS X and Windows 7 or newe
   * `y` Integer
   * `width` Integer
   * `height` Integer
+* [`modifiers`](https://code.google.com/p/chromium/codesearch#chromium/src/ui/events/event_constants.h&l=77) number - bitsum of keyboard modifiers and mouse keys
 
 Emitted when the tray icon is right clicked.
 
@@ -82,7 +84,8 @@ Emitted when the tray icon is double clicked.
   * `y` Integer
   * `width` Integer
   * `height` Integer
-  
+* [`modifiers`](https://code.google.com/p/chromium/codesearch#chromium/src/ui/events/event_constants.h&l=77) number - bitsum of keyboard modifiers and mouse keys
+
 __Note:__ This is only implemented on OS X.
 
 ### Event: 'balloon-show'