Browse Source

Merge branch 'feature/modifiers-click-tray' of https://github.com/nishanths/electron into nishanths-feature/modifiers-click-tray

Cheng Zhao 9 years ago
parent
commit
edde653d60

+ 6 - 6
atom/browser/api/atom_api_tray.cc

@@ -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) {

+ 3 - 3
atom/browser/api/atom_api_tray.h

@@ -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:

+ 4 - 49
atom/browser/ui/cocoa/atom_menu_controller.mm

@@ -11,55 +11,9 @@
 #include "ui/base/accelerators/platform_accelerator_cocoa.h"
 #include "ui/base/l10n/l10n_util_mac.h"
 #include "ui/base/models/simple_menu_model.h"
+#include "ui/events/cocoa/cocoa_event_utils.h"
 #include "ui/gfx/image/image.h"
 
-namespace {
-
-bool isLeftButtonEvent(NSEvent* event) {
-  NSEventType type = [event type];
-  return type == NSLeftMouseDown ||
-    type == NSLeftMouseDragged ||
-    type == NSLeftMouseUp;
-}
-
-bool isRightButtonEvent(NSEvent* event) {
-  NSEventType type = [event type];
-  return type == NSRightMouseDown ||
-    type == NSRightMouseDragged ||
-    type == NSRightMouseUp;
-}
-
-bool isMiddleButtonEvent(NSEvent* event) {
-  if ([event buttonNumber] != 2)
-    return false;
-
-  NSEventType type = [event type];
-  return type == NSOtherMouseDown ||
-    type == NSOtherMouseDragged ||
-    type == NSOtherMouseUp;
-}
-
-int EventFlagsFromNSEventWithModifiers(NSEvent* event, NSUInteger modifiers) {
-  int flags = 0;
-  flags |= (modifiers & NSAlphaShiftKeyMask) ? ui::EF_CAPS_LOCK_DOWN : 0;
-  flags |= (modifiers & NSShiftKeyMask) ? ui::EF_SHIFT_DOWN : 0;
-  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;
-  return flags;
-}
-
-// Retrieves a bitsum of ui::EventFlags from NSEvent.
-int EventFlagsFromNSEvent(NSEvent* event) {
-  NSUInteger modifiers = [event modifierFlags];
-  return EventFlagsFromNSEventWithModifiers(event, modifiers);
-}
-
-}  // namespace
-
 @interface AtomMenuController (Private)
 - (void)addSeparatorToMenu:(NSMenu*)menu
                    atIndex:(int)index;
@@ -246,8 +200,9 @@ int EventFlagsFromNSEvent(NSEvent* event) {
           [[sender representedObject] pointerValue]);
   DCHECK(model);
   if (model) {
-    int event_flags = EventFlagsFromNSEvent([NSApp currentEvent]);
-    model->ActivatedAt(modelIndex, event_flags);
+    NSEvent* event = [NSApp currentEvent];
+    model->ActivatedAt(modelIndex,
+                       ui::EventFlagsFromModifiers([event modifierFlags]));
   }
 }
 

+ 8 - 6
atom/browser/ui/tray_icon.cc

@@ -29,12 +29,13 @@ 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 +50,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) {

+ 4 - 3
atom/browser/ui/tray_icon.h

@@ -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:

+ 13 - 3
atom/browser/ui/tray_icon_cocoa.mm

@@ -6,6 +6,7 @@
 
 #include "atom/browser/ui/cocoa/atom_menu_controller.h"
 #include "base/strings/sys_string_conversions.h"
+#include "ui/events/cocoa/cocoa_event_utils.h"
 #include "ui/gfx/image/image.h"
 #include "ui/gfx/screen.h"
 
@@ -147,16 +148,23 @@ 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(
+        [self getBoundsFromEvent:event],
+        ui::EventFlagsFromModifiers([event modifierFlags]));
   }
 
+  // Double click
   if (event.clickCount == 2 && !menuController_) {
-    trayIcon_->NotifyDoubleClicked([self getBoundsFromEvent:event]);
+    trayIcon_->NotifyDoubleClicked(
+        [self getBoundsFromEvent:event],
+        ui::EventFlagsFromModifiers([event modifierFlags]));
   }
   [self setNeedsDisplay:YES];
 }
@@ -173,7 +181,9 @@ const CGFloat kMargin = 3;
 }
 
 - (void)rightMouseUp:(NSEvent*)event {
-  trayIcon_->NotifyRightClicked([self getBoundsFromEvent:event]);
+  trayIcon_->NotifyRightClicked(
+      [self getBoundsFromEvent:event],
+      ui::EventFlagsFromModifiers([event modifierFlags]));
 }
 
 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {

+ 3 - 3
atom/browser/ui/tray_icon_observer.h

@@ -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:

+ 4 - 0
docs/api/tray.md

@@ -53,6 +53,7 @@ Creates a new tray icon associated with the `image`.
   * `y` Integer
   * `width` Integer
   * `height` Integer
+* [`modifiers`][modifiers] Integer - 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`][modifiers] Integer - bitsum of keyboard modifiers and mouse keys
 
 Emitted when the tray icon is right clicked.
 
@@ -80,6 +82,7 @@ will be emitted if the tray icon has context menu.
   * `y` Integer
   * `width` Integer
   * `height` Integer
+* [`modifiers`][modifiers] Integer - bitsum of keyboard modifiers and mouse keys
 
 Emitted when the tray icon is double clicked.
 
@@ -178,3 +181,4 @@ The `position` is only available on Windows, and it is (0, 0) by default.
 Sets the context menu for this icon.
 
 [event-emitter]: http://nodejs.org/api/events.html#events_class_events_eventemitter
+[modifiers]: https://code.google.com/p/chromium/codesearch#chromium/src/ui/events/event_constants.h&l=77