]> khamidou — GitHub mirrors - electron.git/commitdiff
Add popup method for Menu.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 14 May 2013 08:50:56 +0000 (16:50 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 14 May 2013 08:50:56 +0000 (16:50 +0800)
browser/api/atom_api_menu.cc
browser/api/atom_api_menu.h
browser/api/atom_api_menu_mac.h
browser/api/atom_api_menu_mac.mm

index 64bfb4a1f1d2319e416d5aad251da42b4e953b84..3699acc0a56bdf4bb3365b7e5e2daa2be3e10edf 100644 (file)
@@ -44,11 +44,27 @@ bool Menu::IsCommandIdEnabled(int command_id) const {
   return true;
 }
 
+bool Menu::IsCommandIdVisible(int command_id) const {
+  return true;
+}
+
 bool Menu::GetAcceleratorForCommandId(int command_id,
                                     ui::Accelerator* accelerator) {
   return false;
 }
 
+bool Menu::IsItemForCommandIdDynamic(int command_id) const {
+  return false;
+}
+
+string16 Menu::GetLabelForCommandId(int command_id) const {
+  return string16();
+}
+
+string16 Menu::GetSublabelForCommandId(int command_id) const {
+  return string16();
+}
+
 void Menu::ExecuteCommand(int command_id, int event_flags) {
 }
 
@@ -256,6 +272,22 @@ v8::Handle<v8::Value> Menu::IsVisibleAt(const v8::Arguments &args) {
   return v8::Boolean::New(self->model_->IsVisibleAt(args[0]->IntegerValue()));
 }
 
+// static
+v8::Handle<v8::Value> Menu::Popup(const v8::Arguments &args) {
+  UNWRAP_MEMNU_AND_CHECK;
+
+  if (!args[0]->IsObject() || !args[1]->IsNumber() || !args[2]->IsNumber())
+    return node::ThrowTypeError("Bad argument");
+
+  NativeWindow* window = Unwrap<NativeWindow>(args[0]->ToObject());
+  if (!window)
+    return node::ThrowTypeError("Invalid window");
+
+  self->Popup(window, args[1]->IntegerValue(), args[2]->IntegerValue());
+
+  return v8::Undefined();
+}
+
 // static
 void Menu::Initialize(v8::Handle<v8::Object> target) {
   v8::HandleScope scope;
@@ -269,9 +301,12 @@ void Menu::Initialize(v8::Handle<v8::Object> target) {
   NODE_SET_PROTOTYPE_METHOD(t, "insertRadioItem", InsertRadioItem);
   NODE_SET_PROTOTYPE_METHOD(t, "insertSeparator", InsertSeparator);
   NODE_SET_PROTOTYPE_METHOD(t, "insertSubMenu", InsertSubMenu);
+
   NODE_SET_PROTOTYPE_METHOD(t, "setIcon", SetIcon);
   NODE_SET_PROTOTYPE_METHOD(t, "setSublabel", SetSublabel);
+
   NODE_SET_PROTOTYPE_METHOD(t, "clear", Clear);
+
   NODE_SET_PROTOTYPE_METHOD(t, "getIndexOfCommandId", GetIndexOfCommandId);
   NODE_SET_PROTOTYPE_METHOD(t, "getItemCount", GetItemCount);
   NODE_SET_PROTOTYPE_METHOD(t, "getCommandIdAt", GetCommandIdAt);
@@ -281,6 +316,8 @@ void Menu::Initialize(v8::Handle<v8::Object> target) {
   NODE_SET_PROTOTYPE_METHOD(t, "isEnabledAt", IsEnabledAt);
   NODE_SET_PROTOTYPE_METHOD(t, "isVisibleAt", IsVisibleAt);
 
+  NODE_SET_PROTOTYPE_METHOD(t, "popup", Popup);
+
   target->Set(v8::String::NewSymbol("Menu"), t->GetFunction());
 }
 
index 96514ab71e180ff1551de0a9616b06645ecb9190..26ac768741faedd9fd1156b1b010d6368d203da4 100644 (file)
@@ -11,6 +11,8 @@
 
 namespace atom {
 
+class NativeWindow;
+
 namespace api {
 
 class Menu : public EventEmitter,
@@ -28,11 +30,17 @@ class Menu : public EventEmitter,
   // ui::SimpleMenuModel::Delegate implementations:
   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
+  virtual bool IsCommandIdVisible(int command_id) const OVERRIDE;
   virtual bool GetAcceleratorForCommandId(
       int command_id,
       ui::Accelerator* accelerator) OVERRIDE;
+  virtual bool IsItemForCommandIdDynamic(int command_id) const OVERRIDE;
+  virtual string16 GetLabelForCommandId(int command_id) const OVERRIDE;
+  virtual string16 GetSublabelForCommandId(int command_id) const OVERRIDE;
   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
 
+  virtual void Popup(NativeWindow* window, int x, int y) = 0;
+
   scoped_ptr<ui::SimpleMenuModel> model_;
 
  private:
@@ -58,6 +66,8 @@ class Menu : public EventEmitter,
   static v8::Handle<v8::Value> IsEnabledAt(const v8::Arguments &args);
   static v8::Handle<v8::Value> IsVisibleAt(const v8::Arguments &args);
 
+  static v8::Handle<v8::Value> Popup(const v8::Arguments &args);
+
   DISALLOW_COPY_AND_ASSIGN(Menu);
 };
 
index 1181ddaeb75c2b6ab364b364fc7f7a0d4a0065a4..efaccd4bd06723ddc067ebeaf9f63147a6a8126f 100644 (file)
@@ -19,6 +19,8 @@ class MenuMac : public Menu {
   virtual ~MenuMac();
 
  protected:
+  virtual void Popup(NativeWindow* window, int x, int y) OVERRIDE;
+
   scoped_nsobject<MenuController> controller_;
 
  private:
index 166890383854ff66d7e7d6be028d961669c211d1..7ad2b68ad4f2f53a457d13b38765cbf032a33fa7 100644 (file)
@@ -4,6 +4,12 @@
 
 #import "browser/api/atom_api_menu_mac.h"
 
+#include "base/message_loop.h"
+#include "base/mac/scoped_sending_event.h"
+#include "browser/native_window.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_contents_view.h"
+
 namespace atom {
 
 namespace api {
@@ -17,6 +23,46 @@ MenuMac::MenuMac(v8::Handle<v8::Object> wrapper)
 MenuMac::~MenuMac() {
 }
 
+void MenuMac::Popup(NativeWindow* native_window, int ox, int oy) {
+  NSWindow* window = native_window->GetNativeWindow();
+  content::WebContents* web_contents = native_window->GetWebContents();
+
+  CGFloat x = ox;
+  CGFloat y = oy;
+
+  // Fake out a context menu event.
+  NSEvent* currentEvent = [NSApp currentEvent];
+  NSView* web_view = web_contents->GetView()->GetNativeView();
+  NSPoint position = { x, web_view.bounds.size.height - y };
+  NSTimeInterval eventTime = [currentEvent timestamp];
+  NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown
+                                           location:position
+                                      modifierFlags:NSRightMouseDownMask
+                                          timestamp:eventTime
+                                       windowNumber:[window windowNumber]
+                                            context:nil
+                                        eventNumber:0
+                                         clickCount:1
+                                           pressure:1.0];
+
+  {
+    // Make sure events can be pumped while the menu is up.
+    MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current());
+
+    // One of the events that could be pumped is |window.close()|.
+    // User-initiated event-tracking loops protect against this by
+    // setting flags in -[CrApplication sendEvent:], but since
+    // web-content menus are initiated by IPC message the setup has to
+    // be done manually.
+    base::mac::ScopedSendingEvent sendingEventScoper;
+
+    // Show the menu.
+    [NSMenu popUpContextMenu:[controller_ menu]
+                   withEvent:clickEvent
+                     forView:web_view];
+  }
+}
+
 // static
 Menu* Menu::Create(v8::Handle<v8::Object> wrapper) {
   return new MenuMac(wrapper);