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.
This commit is contained in:
Nathan Sobo
2011-12-15 19:10:58 -08:00
parent b3ce89d294
commit ca9e26882a
3 changed files with 21 additions and 25 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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;