From ca9e26882a123db3db7fe39ac558b5a09ad62922 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 15 Dec 2011 19:10:58 -0800 Subject: [PATCH] Bypass key equivalents on keydown until web view has a chance to process events. Move refresh handling to keyDown: method on AtomController. Eliminate custom handleInputEvent method. NSApplication-sendEvent calls performKeyEquivalents on keydown events. Here I override sendEvent for keydown and forward the event to its window. This allows the web view to receive Command-H etc without key equivalents interfering. If the web view short-circuits the event, then the key equivalent will not be invoked. But it can let the event pass through and it ends up invoking the key equivalent anyway via a mechanism I don't fully understand. Apparently Cocoa tries to execute key equivalents after the web view finishes with the event? But it works great. --- Atom/Classes/AtomApp.m | 24 +++++++++++++++--------- Atom/Classes/AtomController.h | 1 - Atom/Classes/AtomController.m | 21 ++++++--------------- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/Atom/Classes/AtomApp.m b/Atom/Classes/AtomApp.m index 492e63c8b..aa28e76ec 100644 --- a/Atom/Classes/AtomApp.m +++ b/Atom/Classes/AtomApp.m @@ -33,24 +33,26 @@ } #pragma mark Overrides -- (void)sendEvent:(NSEvent *)event { - if ([event type] != NSKeyUp) { +- (void) sendEvent: (NSEvent *)event { + // Default implementation for key down tries key equivalents first + // We want to wait until the web view handles the event, then allow key equivalents to be tried + if ([event type] != NSKeyDown) { [super sendEvent:event]; return; } - BOOL shouldRunSpecs = [event modifierFlags] & (NSAlternateKeyMask | NSControlKeyMask | NSCommandKeyMask) && [[event charactersIgnoringModifiers] hasPrefix:@"s"]; + // TODO(NS): Make running specs a menu command with a key equivalent, so we can delete this code + BOOL shouldRunSpecs = + ([event type] == NSKeyDown) && + ([event modifierFlags] & (NSAlternateKeyMask | NSControlKeyMask | NSCommandKeyMask)) && + [[event charactersIgnoringModifiers] hasPrefix:@"s"]; + if (shouldRunSpecs) { [self createSpecController]; return; } - - AtomController *controller = [[self keyWindow] windowController]; - if ([controller isKindOfClass:[AtomController class]]) { // ensure its not a dialog - if ([controller handleInputEvent:event]) return; - } - [super sendEvent:event]; + [[event window] sendEvent:event]; } - (void)terminate:(id)sender { @@ -69,4 +71,8 @@ [[NSUserDefaults standardUserDefaults] registerDefaults:defaults]; } +- (void)applicationDidFinishLaunching:(NSNotification *)notification { + [self createSpecController]; +} + @end diff --git a/Atom/Classes/AtomController.h b/Atom/Classes/AtomController.h index d519c2b36..68c19341f 100644 --- a/Atom/Classes/AtomController.h +++ b/Atom/Classes/AtomController.h @@ -15,7 +15,6 @@ struct JSGlobalContextRef; - (id)initForSpecs; - (id)initWithURL:(NSString *)url; -- (BOOL)handleInputEvent:(NSEvent *)event; - (void)triggerAtomEventWithName:(NSString *)name data:(id)data; - (void)reload; - (JSValueRefAndContextRef)jsWindow; diff --git a/Atom/Classes/AtomController.m b/Atom/Classes/AtomController.m index 2c18b6506..646a94a25 100644 --- a/Atom/Classes/AtomController.m +++ b/Atom/Classes/AtomController.m @@ -66,21 +66,6 @@ [self createWebView]; } -- (BOOL)handleInputEvent:(NSEvent *)event { - BOOL shouldReload = [event modifierFlags] & NSCommandKeyMask && [[event charactersIgnoringModifiers] hasPrefix:@"r"]; - if (shouldReload) { - [self reload]; - return YES; - } - - if ([self.jscocoa hasJSFunctionNamed:@"handleKeyEvent"]) { - JSValueRef handled = [self.jscocoa callJSFunctionNamed:@"handleKeyEvent" withArguments:event, nil]; - return [self.jscocoa toBool:handled]; - } - - return NO; -} - - (void)triggerAtomEventWithName:(NSString *)name data:(id)data { [self.jscocoa callJSFunctionNamed:@"triggerEvent" withArguments:name, data, false, nil]; } @@ -140,6 +125,12 @@ return YES; } +- (void)keyDown:(NSEvent *)event { + if ([event modifierFlags] & NSCommandKeyMask && [[event charactersIgnoringModifiers] hasPrefix:@"r"]) { + [self reload]; + } +} + #pragma mark WebUIDelegate - (NSArray *)webView:(WebView *)sender contextMenuItemsForElement:(NSDictionary *)element defaultMenuItems:(NSArray *)defaultMenuItems { return defaultMenuItems;