]> khamidou — GitHub mirrors - electron.git/commitdiff
Allow positioning the context menu
authorJoao Moreno <mail@joaomoreno.com>
Tue, 25 Nov 2014 15:47:41 +0000 (16:47 +0100)
committerJoao Moreno <mail@joaomoreno.com>
Wed, 26 Nov 2014 12:54:05 +0000 (13:54 +0100)
atom/browser/api/atom_api_menu.cc
atom/browser/api/atom_api_menu.h
atom/browser/api/atom_api_menu_mac.h
atom/browser/api/atom_api_menu_mac.mm
atom/browser/api/atom_api_menu_views.cc
atom/browser/api/atom_api_menu_views.h
atom/browser/api/lib/menu.coffee

index 7841ea4d76a153ace895df9e3a0a233d416ee9de..6c3bd89c2836200eca8e87c2ebd0e084028fedd6 100644 (file)
@@ -246,7 +246,8 @@ void Menu::BuildPrototype(v8::Isolate* isolate,
       .SetMethod("isEnabledAt", &Menu::IsEnabledAt)
       .SetMethod("isVisibleAt", &Menu::IsVisibleAt)
       .SetMethod("attachToWindow", &Menu::AttachToWindow)
-      .SetMethod("_popup", &Menu::Popup);
+      .SetMethod("_popup", &Menu::Popup)
+      .SetMethod("_popupAt", &Menu::PopupAt);
 }
 
 }  // namespace api
index 10777ba6813dd0b23ec5611426e3c0fb63dbbde6..7b8e68d01a1a19a85e9bf673ff0502fd5c727aba 100644 (file)
@@ -55,6 +55,7 @@ class Menu : public mate::Wrappable,
 
   virtual void AttachToWindow(Window* window);
   virtual void Popup(Window* window) = 0;
+  virtual void PopupAt(Window* window, int x, int y) = 0;
 
   scoped_ptr<ui::SimpleMenuModel> model_;
   Menu* parent_;
index 7ccacdc32328b755af93f6ff2fac3ad566d748ac..f055d92cde3d5b6f6ca841d7b90844dd30486066 100644 (file)
@@ -20,6 +20,7 @@ class MenuMac : public Menu {
   MenuMac();
 
   virtual void Popup(Window* window) OVERRIDE;
+  virtual void PopupAt(Window* window, int x, int y) OVERRIDE;
 
   base::scoped_nsobject<AtomMenuController> menu_controller_;
 
index 7480498287ff9a30f1f604e6d446883f34234163..9b1652939378f1ab9a7bd2389561aef5253239bb 100644 (file)
@@ -46,6 +46,21 @@ void MenuMac::Popup(Window* window) {
                    forView:web_contents->GetContentNativeView()];
 }
 
+void MenuMac::PopupAt(Window* window, int x, int y) {
+  base::scoped_nsobject<AtomMenuController> menu_controller(
+      [[AtomMenuController alloc] initWithModel:model_.get()]);
+
+  NativeWindow* native_window = window->window();
+  content::WebContents* web_contents = native_window->GetWebContents();
+  NSView* view = web_contents->GetContentNativeView();
+  NSMenu* menu = [menu_controller menu];
+
+  // Show the menu.
+  [menu popUpMenuPositioningItem:[menu itemAtIndex:0]
+                      atLocation:NSMakePoint(x, [view frame].size.height - y)
+                          inView:view];
+}
+
 // static
 void Menu::SetApplicationMenu(Menu* base_menu) {
   MenuMac* menu = static_cast<MenuMac*>(base_menu);
index e94830b30dc280e9b714b61fd902a3f94537f994..ed751e8dc03009c9fcd3e60f7deac8d45faf6f58 100644 (file)
@@ -5,6 +5,7 @@
 #include "atom/browser/api/atom_api_menu_views.h"
 
 #include "atom/browser/native_window_views.h"
+#include "content/public/browser/render_widget_host_view.h"
 #include "ui/gfx/screen.h"
 #include "ui/views/controls/menu/menu_runner.h"
 
@@ -16,14 +17,24 @@ MenuViews::MenuViews() {
 }
 
 void MenuViews::Popup(Window* window) {
-  gfx::Point cursor = gfx::Screen::GetNativeScreen()->GetCursorScreenPoint();
+  PopupAtPoint(window, gfx::Screen::GetNativeScreen()->GetCursorScreenPoint());
+}
+
+void MenuViews::PopupAt(Window* window, int x, int y) {
+  NativeWindow* nativeWindowViews = static_cast<NativeWindow*>(window->window());
+  content::RenderWidgetHostView* view = nativeWindowViews->GetWebContents()->GetRenderWidgetHostView();
+  gfx::Point viewOrigin = view->GetViewBounds().origin();
+  PopupAtPoint(window, gfx::Point(viewOrigin.x() + x, viewOrigin.y() + y));
+}
+
+void MenuViews::PopupAtPoint(Window* window, gfx::Point point) {
   views::MenuRunner menu_runner(
       model(),
       views::MenuRunner::CONTEXT_MENU | views::MenuRunner::HAS_MNEMONICS);
   ignore_result(menu_runner.RunMenuAt(
       static_cast<NativeWindowViews*>(window->window())->widget(),
       NULL,
-      gfx::Rect(cursor, gfx::Size()),
+      gfx::Rect(point, gfx::Size()),
       views::MENU_ANCHOR_TOPLEFT,
       ui::MENU_SOURCE_MOUSE));
 }
index 2256e71c5ce9d225c8d48ade67a0ad0a74252614..fa375fcba1723722621bc9cdbef5046722496572 100644 (file)
@@ -6,6 +6,7 @@
 #define ATOM_BROWSER_API_ATOM_API_MENU_VIEWS_H_
 
 #include "atom/browser/api/atom_api_menu.h"
+#include "ui/gfx/screen.h"
 
 namespace atom {
 
@@ -17,8 +18,10 @@ class MenuViews : public Menu {
 
  protected:
   virtual void Popup(Window* window) OVERRIDE;
+  virtual void PopupAt(Window* window, int x, int y) OVERRIDE;
 
  private:
+  void PopupAtPoint(Window* window, gfx::Point point);
   DISALLOW_COPY_AND_ASSIGN(MenuViews);
 };
 
index 722c56eb609a783e8b668fda556175add9e5405a..bde4e91522dccdcc1ed594e0e2c7a4c66486c3c0 100644 (file)
@@ -45,9 +45,12 @@ Menu::_init = ->
           break
         v8Util.setHiddenValue group[0], 'checked', true unless checked
 
-Menu::popup = (window) ->
+Menu::popup = (window, x, y) ->
   throw new TypeError('Invalid window') unless window?.constructor is BrowserWindow
-  @_popup window
+  if x? and y?
+    @_popupAt(window, x, y)
+  else
+    @_popup window
 
 Menu::append = (item) ->
   @insert @getItemCount(), item