Skip to content
Browse files

fix mac native menu event

  • Loading branch information...
1 parent 39b426d commit b98a301b04b0cefe11db82a3d4567f2880f866cf @redmunds redmunds committed Jun 19, 2013
Showing with 63 additions and 33 deletions.
  1. +1 −0 appshell/appshell_extensions_mac.mm
  2. +57 −31 appshell/cefclient_mac.mm
  3. +0 −2 appshell/native_menu_model.cpp
  4. +5 −0 appshell/native_menu_model.h
View
1 appshell/appshell_extensions_mac.mm
@@ -830,6 +830,7 @@ int32 AddMenu(CefRefPtr<CefBrowser> browser, ExtensionString itemTitle, Extensio
if (subMenu == nil) {
subMenu = [[[NSMenu alloc] initWithTitle:itemTitleStr] autorelease];
[testItem setSubmenu:subMenu];
+ [subMenu setDelegate:[[NSApp mainMenu] delegate]];
}
// Positioning hack. If position and relativeId are both "", put the menu
View
88 appshell/cefclient_mac.mm
@@ -74,8 +74,52 @@ - (void)sendEvent:(NSEvent*)event {
CefScopedSendingEvent sendingEventScoper;
[super sendEvent:event];
}
+
+// Find a window that can handle an openfile command.
+- (NSWindow *) findTargetWindow {
+ NSApplication * nsApp = (NSApplication *)self;
+ NSWindow* result = [nsApp keyWindow];
+ if (!result) {
+ result = [nsApp mainWindow];
+ if (!result) {
+ // the app might be inactive or hidden. Look for the main window on the window list.
+ NSArray* windows = [nsApp windows];
+ for (NSUInteger i = 0; i < [windows count]; i++) {
+ NSWindow* window = [windows objectAtIndex:i];
+
+ // Note: this only finds the main (first) appshell window. If additional
+ // windows are open, they will _not_ be found. If the main (first) window
+ // is closed, it may be found, but will be hidden.
+ if ([[window frameAutosaveName] isEqualToString: APP_NAME @"MainWindow"]) {
+ result = window;
+ break;
+ }
+ }
+ }
+ }
+ return result;
+}
+
@end
+@interface ClientMenuDelegate : NSObject <NSMenuDelegate> {
+}
+- (void)menuWillOpen:(NSMenu *)menu;
+@end
+
+@implementation ClientMenuDelegate
+
+- (void)menuWillOpen:(NSMenu *)menu {
+ // Notify that menu is being popped up
+ NSWindow* targetWindow = [NSApp findTargetWindow];
+ if (targetWindow) {
+ CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow(targetWindow);
+ if (browser)
+ g_handler->SendJSCommand(browser, APP_BEFORE_MENUPOPUP);
+ }
+}
+
+@end
// Receives notifications from controls and the browser window. Will delete
// itself when done.
@@ -128,9 +172,6 @@ - (IBAction)handleMenuAction:(id)sender {
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
- // Notify that menu is being popped up
- g_handler->SendJSCommand(g_handler->GetBrowser(), APP_BEFORE_MENUPOPUP);
-
NSInteger menuState = NSOffState;
NSUInteger tag = [menuItem tag];
NativeMenuModel menus = NativeMenuModel::getInstance(getMenuParent(g_handler->GetBrowser()));
@@ -323,7 +364,14 @@ - (void)createApp:(id)object {
// Create the delegate for control and browser window events.
ClientWindowDelegate* delegate = [[ClientWindowDelegate alloc] init];
-
+
+ // Create the delegate for menu events.
+ ClientMenuDelegate* menuDelegate = [[ClientMenuDelegate alloc] init];
+
+ [[NSApp mainMenu] setDelegate:menuDelegate];
+ [[[[NSApp mainMenu] itemWithTag: BRACKETS_MENUITEMTAG] submenu] setDelegate:menuDelegate];
+ [[[[NSApp mainMenu] itemWithTag: WINDOW_MENUITEMTAG] submenu] setDelegate:menuDelegate];
+
// Create the main application window.
NSUInteger styleMask = (NSTitledWindowMask |
NSClosableWindowMask |
@@ -485,33 +533,10 @@ - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)theAp
return NSTerminateNow;
}
-// Find a window that can handle an openfile command.
-- (NSWindow *) findTargetWindow:(NSApplication *)theApplication {
- NSWindow* result = [theApplication keyWindow];
- if (!result) {
- result = [theApplication mainWindow];
- if (!result) {
- // the app might be inactive or hidden. Look for the main window on the window list.
- NSArray* windows = [theApplication windows];
- for (NSUInteger i = 0; i < [windows count]; i++) {
- NSWindow* window = [windows objectAtIndex:i];
-
- // Note: this only finds the main (first) appshell window. If additional
- // windows are open, they will _not_ be found. If the main (first) window
- // is closed, it may be found, but will be hidden.
- if ([[window frameAutosaveName] isEqualToString: APP_NAME @"MainWindow"]) {
- result = window;
- break;
- }
- }
- }
- }
- return result;
-}
-
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename {
if (!pendingOpenFiles) {
- NSWindow* targetWindow = [self findTargetWindow:theApplication];
+ ClientApplication * clientApp = (ClientApplication *)theApplication;
+ NSWindow* targetWindow = [clientApp findTargetWindow];
if (targetWindow) {
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow(targetWindow);
g_handler->SendOpenFileCommand(browser, CefString([filename UTF8String]));
@@ -525,7 +550,8 @@ - (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filenam
- (BOOL)application:(NSApplication *)theApplication openFiles:(NSArray *)filenames {
if (!pendingOpenFiles) {
- NSWindow* targetWindow = [self findTargetWindow:theApplication];
+ ClientApplication * clientApp = (ClientApplication *)theApplication;
+ NSWindow* targetWindow = [clientApp findTargetWindow];
if (targetWindow) {
CefRefPtr<CefBrowser> browser = ClientHandler::GetBrowserForNativeWindow(targetWindow);
for (NSUInteger i = 0; i < [filenames count]; i++) {
@@ -574,7 +600,7 @@ int main(int argc, char* argv[]) {
[ClientApplication sharedApplication];
NSObject* delegate = [[ClientAppDelegate alloc] init];
[NSApp setDelegate:delegate];
-
+
// Parse command line arguments.
AppInitCommandLine(argc, argv);
View
2 appshell/native_menu_model.cpp
@@ -22,8 +22,6 @@
#include "native_menu_model.h"
#include "command_callbacks.h"
-//id of the existing window menu item
-#define WINDOW_MENUITEMTAG 4999
#if defined(OS_WIN)
const ExtensionString WINDOW_COMMAND = L"window";
#else
View
5 appshell/native_menu_model.h
@@ -27,6 +27,11 @@
#include <string>
#include "appshell_extensions_platform.h"
+// IDs of static menuitems
+#define BRACKETS_MENUITEMTAG 0
+#define WINDOW_MENUITEMTAG 4999
+
+
class NativeMenuItemModel
{
public:

0 comments on commit b98a301

Please sign in to comment.
Something went wrong with that request. Please try again.