Redispatch events to handle native shortcuts
authorKevin Sawicki <kevinsawicki@gmail.com>
Wed, 15 Jun 2016 20:55:19 +0000 (13:55 -0700)
committerKevin Sawicki <kevinsawicki@gmail.com>
Fri, 17 Jun 2016 16:12:59 +0000 (09:12 -0700)
atom/browser/common_web_contents_delegate_mac.mm
atom/browser/native_window_mac.h
atom/browser/native_window_mac.mm

index 69117f1..eeb87f2 100644 (file)
@@ -6,6 +6,7 @@
 
 #import <Cocoa/Cocoa.h>
 
+#include "atom/browser/native_window_mac.h"
 #include "content/public/browser/native_web_keyboard_event.h"
 #include "ui/events/keycodes/keyboard_codes.h"
 
@@ -22,17 +23,9 @@ void CommonWebContentsDelegate::HandleKeyboardEvent(
   if (event.windowsKeyCode == ui::VKEY_ESCAPE && is_html_fullscreen())
     ExitFullscreenModeForTab(source);
 
-  BOOL handled = [[NSApp mainMenu] performKeyEquivalent:event.os_event];
-  if (!handled && event.os_event.window) {
-    // Handle the cmd+~ shortcut.
-    if ((event.os_event.modifierFlags & NSCommandKeyMask) /* cmd */ &&
-        (event.os_event.keyCode == 50  /* ~ */)) {
-      if (event.os_event.modifierFlags & NSShiftKeyMask) {
-        [NSApp sendAction:@selector(_cycleWindowsReversed:) to:nil from:nil];
-      } else {
-        [NSApp sendAction:@selector(_cycleWindows:) to:nil from:nil];
-      }
-    }
+  if (event.os_event.window) {
+    AtomNSWindow* native_window = static_cast<AtomNSWindow*>(event.os_event.window);
+    [native_window redispatchKeyEvent:event.os_event];
   }
 }
 
index 899043f..bf8125f 100644 (file)
@@ -152,4 +152,21 @@ class NativeWindowMac : public NativeWindow {
 
 }  // namespace atom
 
+@interface AtomNSWindow : NSWindow {
+ @private
+  atom::NativeWindowMac* shell_;
+  bool enable_larger_than_screen_;
+  BOOL redispatchingEvent_;
+  BOOL eventHandled_;
+}
+@property BOOL acceptsFirstMouse;
+@property BOOL disableAutoHideCursor;
+@property BOOL disableKeyOrMainWindow;
+
+- (void)setShell:(atom::NativeWindowMac*)shell;
+- (void)setEnableLargerThanScreen:(bool)enable;
+- (BOOL)redispatchKeyEvent:(NSEvent*)event;
+- (BOOL)performKeyEquivalent:(NSEvent*)theEvent;
+@end
+
 #endif  // ATOM_BROWSER_NATIVE_WINDOW_MAC_H_
index 0a64302..fe4cba4 100644 (file)
@@ -270,19 +270,6 @@ bool ScopedDisableResize::disable_resize_ = false;
 
 @end
 
-@interface AtomNSWindow : NSWindow {
- @private
-  atom::NativeWindowMac* shell_;
-  bool enable_larger_than_screen_;
-}
-@property BOOL acceptsFirstMouse;
-@property BOOL disableAutoHideCursor;
-@property BOOL disableKeyOrMainWindow;
-
-- (void)setShell:(atom::NativeWindowMac*)shell;
-- (void)setEnableLargerThanScreen:(bool)enable;
-@end
-
 @implementation AtomNSWindow
 
 - (void)setShell:(atom::NativeWindowMac*)shell {
@@ -347,6 +334,38 @@ bool ScopedDisableResize::disable_resize_ = false;
   return !self.disableKeyOrMainWindow;
 }
 
+- (void)sendEvent:(NSEvent*)event {
+  if (!redispatchingEvent_)
+    [super sendEvent:event];
+  else
+    eventHandled_ = NO;
+}
+
+- (BOOL)performKeyEquivalent:(NSEvent*)event {
+  if (redispatchingEvent_)
+    return NO;
+
+  if ([super performKeyEquivalent:event])
+    return YES;
+
+  return NO;
+ }
+
+- (BOOL)redispatchKeyEvent:(NSEvent*)event {
+  NSEventType eventType = [event type];
+  if (eventType != NSKeyDown && eventType != NSKeyUp &&
+      eventType != NSFlagsChanged) {
+    return YES;
+  }
+
+  // Redispatch the event.
+  eventHandled_ = YES;
+  redispatchingEvent_ = YES;
+  [NSApp sendEvent:event];
+  redispatchingEvent_ = NO;
+
+  return eventHandled_;
+}
 @end
 
 @interface ControlRegionView : NSView