Redispatch events to handle native shortcuts

This commit is contained in:
Kevin Sawicki 2016-06-15 13:55:19 -07:00
parent 66fe1e48e8
commit a2bbfea9e0
3 changed files with 53 additions and 24 deletions

View 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];
}
}

View 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_

View 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