Fix the menu popup on OS X.

This commit is contained in:
Cheng Zhao 2013-05-14 19:25:14 +08:00
parent d6103dd639
commit 9ef9f6bf95
2 changed files with 11 additions and 16 deletions

View file

@ -7,8 +7,6 @@
#include "browser/api/atom_api_menu.h" #include "browser/api/atom_api_menu.h"
#import "chrome/browser/ui/cocoa/menu_controller.h"
namespace atom { namespace atom {
namespace api { namespace api {
@ -19,9 +17,7 @@ class MenuMac : public Menu {
virtual ~MenuMac(); virtual ~MenuMac();
protected: protected:
virtual void Popup(NativeWindow* window, int x, int y) OVERRIDE; virtual void Popup(NativeWindow* window) OVERRIDE;
scoped_nsobject<MenuController> controller_;
private: private:
DISALLOW_COPY_AND_ASSIGN(MenuMac); DISALLOW_COPY_AND_ASSIGN(MenuMac);

View file

@ -7,6 +7,7 @@
#include "base/message_loop.h" #include "base/message_loop.h"
#include "base/mac/scoped_sending_event.h" #include "base/mac/scoped_sending_event.h"
#include "browser/native_window.h" #include "browser/native_window.h"
#import "chrome/browser/ui/cocoa/menu_controller.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h" #include "content/public/browser/web_contents_view.h"
@ -15,25 +16,23 @@ namespace atom {
namespace api { namespace api {
MenuMac::MenuMac(v8::Handle<v8::Object> wrapper) MenuMac::MenuMac(v8::Handle<v8::Object> wrapper)
: Menu(wrapper), : Menu(wrapper) {
controller_([[MenuController alloc] initWithModel:model_.get()
useWithPopUpButtonCell:NO]) {
} }
MenuMac::~MenuMac() { MenuMac::~MenuMac() {
} }
void MenuMac::Popup(NativeWindow* native_window, int ox, int oy) { void MenuMac::Popup(NativeWindow* native_window) {
scoped_nsobject<MenuController> menu_controller(
[[MenuController alloc] initWithModel:model_.get()
useWithPopUpButtonCell:NO]);
NSWindow* window = native_window->GetNativeWindow(); NSWindow* window = native_window->GetNativeWindow();
content::WebContents* web_contents = native_window->GetWebContents(); content::WebContents* web_contents = native_window->GetWebContents();
CGFloat x = ox;
CGFloat y = oy;
// Fake out a context menu event. // Fake out a context menu event.
NSEvent* currentEvent = [NSApp currentEvent]; NSEvent* currentEvent = [NSApp currentEvent];
NSView* web_view = web_contents->GetView()->GetNativeView(); NSPoint position = [window mouseLocationOutsideOfEventStream];
NSPoint position = { x, web_view.bounds.size.height - y };
NSTimeInterval eventTime = [currentEvent timestamp]; NSTimeInterval eventTime = [currentEvent timestamp];
NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown
location:position location:position
@ -57,9 +56,9 @@ void MenuMac::Popup(NativeWindow* native_window, int ox, int oy) {
base::mac::ScopedSendingEvent sendingEventScoper; base::mac::ScopedSendingEvent sendingEventScoper;
// Show the menu. // Show the menu.
[NSMenu popUpContextMenu:[controller_ menu] [NSMenu popUpContextMenu:[menu_controller menu]
withEvent:clickEvent withEvent:clickEvent
forView:web_view]; forView:web_contents->GetView()->GetContentNativeView()];
} }
} }