]> khamidou — GitHub mirrors - electron.git/commitdiff
win: Implement menu parameter
authorCheng Zhao <zcbenz@gmail.com>
Wed, 2 Dec 2015 11:58:10 +0000 (19:58 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 2 Dec 2015 11:58:10 +0000 (19:58 +0800)
atom/browser/ui/win/notify_icon.cc
atom/browser/ui/win/notify_icon.h
atom/browser/ui/win/notify_icon_host.cc

index 133ab068da7be52cacf6d84536298e9ff45fde55..1ac29f1360cb2144e0affb8705d80659cb3af3b9 100644 (file)
@@ -13,6 +13,7 @@
 #include "ui/gfx/image/image.h"
 #include "ui/gfx/geometry/point.h"
 #include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/screen.h"
 #include "ui/views/controls/menu/menu_runner.h"
 
 namespace atom {
@@ -45,8 +46,7 @@ NotifyIcon::~NotifyIcon() {
   Shell_NotifyIcon(NIM_DELETE, &icon_data);
 }
 
-void NotifyIcon::HandleClickEvent(const gfx::Point& cursor_pos,
-                                  int modifiers,
+void NotifyIcon::HandleClickEvent(int modifiers,
                                   bool left_mouse_click,
                                   bool double_button_click) {
   NOTIFYICONIDENTIFIER icon_id;
@@ -66,7 +66,7 @@ void NotifyIcon::HandleClickEvent(const gfx::Point& cursor_pos,
     return;
   } else if (!double_button_click) {  // single right click
     if (menu_model_)
-      PopUpContextMenu(cursor_pos, menu_model_);
+      PopUpContextMenu(gfx::Point(), menu_model_);
     else
       NotifyRightClicked(gfx::Rect(rect), modifiers);
   }
@@ -145,22 +145,23 @@ void NotifyIcon::DisplayBalloon(const gfx::Image& icon,
 void NotifyIcon::PopUpContextMenu(const gfx::Point& pos,
                                   ui::SimpleMenuModel* menu_model) {
   // Returns if context menu isn't set.
-  if (!menu_model_)
+  if (!menu_model)
     return;
   // Set our window as the foreground window, so the context menu closes when
   // we click away from it.
   if (!SetForegroundWindow(window_))
     return;
 
+  // Show menu at mouse's position by default.
+  gfx::Rect rect(pos, gfx::Size());
+  if (pos.IsOrigin())
+    rect.set_origin(gfx::Screen::GetNativeScreen()->GetCursorScreenPoint());
+
   views::MenuRunner menu_runner(
-      menu_model_,
+      menu_model,
       views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS);
   ignore_result(menu_runner.RunMenuAt(
-      NULL,
-      NULL,
-      gfx::Rect(pos, gfx::Size()),
-      views::MENU_ANCHOR_TOPLEFT,
-      ui::MENU_SOURCE_MOUSE));
+      NULL, NULL, rect, views::MENU_ANCHOR_TOPLEFT, ui::MENU_SOURCE_MOUSE));
 }
 
 void NotifyIcon::SetContextMenu(ui::SimpleMenuModel* menu_model) {
index 8ee600033e946baafb25f054a2d0a7e7e7063a21..23608c7c7ab6591d19754f9ed7d7d55fcfc9f400 100644 (file)
@@ -33,8 +33,7 @@ class NotifyIcon : public TrayIcon {
   // Handles a click event from the user - if |left_button_click| is true and
   // there is a registered observer, passes the click event to the observer,
   // otherwise displays the context menu if there is one.
-  void HandleClickEvent(const gfx::Point& cursor_pos,
-                        int modifiers,
+  void HandleClickEvent(int modifiers,
                         bool left_button_click,
                         bool double_button_click);
 
index 2c84837e714d62ba22ff553628cfd6234204a4ba..a0d4287ff61453743bf0347a7ec86572ec64dfa0 100644 (file)
@@ -15,7 +15,6 @@
 #include "base/win/win_util.h"
 #include "base/win/wrapped_window_proc.h"
 #include "ui/events/event_constants.h"
-#include "ui/gfx/screen.h"
 #include "ui/gfx/win/hwnd_util.h"
 
 namespace atom {
@@ -172,10 +171,7 @@ LRESULT CALLBACK NotifyIconHost::WndProc(HWND hwnd,
       case WM_CONTEXTMENU:
         // Walk our icons, find which one was clicked on, and invoke its
         // HandleClickEvent() method.
-        gfx::Point cursor_pos(
-            gfx::Screen::GetNativeScreen()->GetCursorScreenPoint());
         win_icon->HandleClickEvent(
-            cursor_pos,
             GetKeyboardModifers(),
             (lparam == WM_LBUTTONDOWN || lparam == WM_LBUTTONDBLCLK),
             (lparam == WM_LBUTTONDBLCLK || lparam == WM_RBUTTONDBLCLK));