Index: ui/views/cocoa/native_widget_mac_nswindow.mm |
diff --git a/ui/views/cocoa/native_widget_mac_nswindow.mm b/ui/views/cocoa/native_widget_mac_nswindow.mm |
index 7a3d3dcb931c95767957dc5fc4541fb579ecbfcb..9a6fa2ef8f4cd543b28a267046a9659a5db65e21 100644 |
--- a/ui/views/cocoa/native_widget_mac_nswindow.mm |
+++ b/ui/views/cocoa/native_widget_mac_nswindow.mm |
@@ -6,11 +6,13 @@ |
#include "base/mac/foundation_util.h" |
#import "ui/views/cocoa/views_nswindow_delegate.h" |
+#include "ui/views/controls/menu/menu_controller.h" |
#include "ui/views/widget/native_widget_mac.h" |
@interface NativeWidgetMacNSWindow () |
- (ViewsNSWindowDelegate*)viewsNSWindowDelegate; |
- (views::Widget*)viewsWidget; |
+- (BOOL)hasViewsMenuActive; |
@end |
@implementation NativeWidgetMacNSWindow |
@@ -23,6 +25,12 @@ |
return [[self viewsNSWindowDelegate] nativeWidgetMac]->GetWidget(); |
} |
+- (BOOL)hasViewsMenuActive { |
+ views::MenuController* menuController = |
+ views::MenuController::GetActiveInstance(); |
+ return menuController && menuController->owner() == [self viewsWidget]; |
+} |
+ |
// Ignore [super canBecome{Key,Main}Window]. The default is NO for windows with |
// NSBorderlessWindowMask, which is not the desired behavior. |
// Note these can be called via -[NSWindow close] while the widget is being torn |
@@ -35,6 +43,24 @@ |
return [self delegate] && [self viewsWidget]->CanActivate(); |
} |
+// Override sendEvent to allow key events to be forwarded to a toolkit-views |
+// menu while it is active, and while still allowing the native subview to |
+// retain firstResponder status. |
+- (void)sendEvent:(NSEvent*)event { |
+ NSEventType type = [event type]; |
+ if ((type != NSKeyDown && type != NSKeyUp) || ![self hasViewsMenuActive]) { |
+ [super sendEvent:event]; |
+ return; |
+ } |
+ |
+ // Send to the menu, after converting the event into an action message using |
+ // the content view. |
+ if (type == NSKeyDown) |
+ [[self contentView] keyDown:event]; |
+ else |
+ [[self contentView] keyUp:event]; |
+} |
+ |
// Override display, since this is the first opportunity Cocoa gives to detect |
// a visibility change in some cases. For example, restoring from the dock first |
// calls -[NSWindow display] before any NSWindowDelegate functions and before |