]> khamidou — GitHub mirrors - electron.git/commitdiff
mac: Remove duplicate code of Popup
authorCheng Zhao <zcbenz@gmail.com>
Fri, 22 Jan 2016 18:17:12 +0000 (11:17 -0700)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 22 Jan 2016 18:17:12 +0000 (11:17 -0700)
atom/browser/api/atom_api_menu_mac.mm

index d87617a5fc65d8680b8012ee06d884704a959b2f..cfd3f0a89c34ac78e865fa04cdaa9907cb83caae 100644 (file)
@@ -19,35 +19,7 @@ MenuMac::MenuMac() {
 }
 
 void MenuMac::Popup(Window* window) {
-  NativeWindow* native_window = window->window();
-  if (!native_window)
-    return;
-  content::WebContents* web_contents = native_window->web_contents();
-  if (!web_contents)
-    return;
-
-  NSWindow* nswindow = native_window->GetNativeWindow();
-  base::scoped_nsobject<AtomMenuController> menu_controller(
-      [[AtomMenuController alloc] initWithModel:model_.get()]);
-
-  // Fake out a context menu event.
-  NSEvent* currentEvent = [NSApp currentEvent];
-  NSPoint position = [nswindow mouseLocationOutsideOfEventStream];
-  NSTimeInterval eventTime = [currentEvent timestamp];
-  NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown
-                                           location:position
-                                      modifierFlags:NSRightMouseDownMask
-                                          timestamp:eventTime
-                                       windowNumber:[nswindow windowNumber]
-                                            context:nil
-                                        eventNumber:0
-                                         clickCount:1
-                                           pressure:1.0];
-
-  // Show the menu.
-  [NSMenu popUpContextMenu:[menu_controller menu]
-                 withEvent:clickEvent
-                   forView:web_contents->GetContentNativeView()];
+  PopupAt(window, -1, -1, 0);
 }
 
 void MenuMac::PopupAt(Window* window, int x, int y, int positioning_item) {
@@ -63,16 +35,23 @@ void MenuMac::PopupAt(Window* window, int x, int y, int positioning_item) {
   NSMenu* menu = [menu_controller menu];
   NSView* view = web_contents->GetContentNativeView();
 
-  // Show the menu.
-  if (positioning_item >= [menu numberOfItems]) {
-    positioning_item = [menu numberOfItems] - 1;
-  }
-  if (positioning_item < 0) {
-    positioning_item = 0;
+  // Which menu item to show.
+  NSMenuItem* item = nil;
+  if (positioning_item < [menu numberOfItems] && positioning_item >= 0)
+    item = [menu itemAtIndex:positioning_item];
+
+  // (-1, -1) means showing on mouse location.
+  NSPoint position;
+  if (x == -1 || y == -1) {
+    NSWindow* nswindow = native_window->GetNativeWindow();
+    position = [view convertPoint:[nswindow mouseLocationOutsideOfEventStream]
+                         fromView:nil];
+  } else {
+    position = NSMakePoint(x, [view frame].size.height - y);
   }
-  [menu popUpMenupositioning_item:[menu itemAtIndex:positioning_item]
-                      atLocation:NSMakePoint(x, [view frame].size.height - y)
-                          inView:view];
+
+  // Show the menu.
+  [menu popUpMenuPositioningItem:item atLocation:position inView:view];
 }
 
 // static