Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
|
tapted
2015/08/26 03:04:49
Maybe we can even trick `git cl upload` to diff th
jackhou1
2015/08/26 06:24:43
Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "ui/base/cocoa/command_dispatcher.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 namespace { | |
| 10 | |
| 11 // Duplicate the given key event, but changing the associated window. | |
| 12 NSEvent* KeyEventForWindow(NSWindow* window, NSEvent* event) { | |
| 13 NSEventType event_type = [event type]; | |
| 14 | |
| 15 // Convert the event's location from the original window's coordinates into | |
| 16 // our own. | |
| 17 NSPoint location = [event locationInWindow]; | |
| 18 location = [[event window] convertBaseToScreen:location]; | |
| 19 location = [window convertScreenToBase:location]; | |
| 20 | |
| 21 // Various things *only* apply to key down/up. | |
| 22 bool is_a_repeat = false; | |
| 23 NSString* characters = nil; | |
| 24 NSString* charactors_ignoring_modifiers = nil; | |
| 25 if (event_type == NSKeyDown || event_type == NSKeyUp) { | |
| 26 is_a_repeat = [event isARepeat]; | |
| 27 characters = [event characters]; | |
| 28 charactors_ignoring_modifiers = [event charactersIgnoringModifiers]; | |
| 29 } | |
| 30 | |
| 31 // This synthesis may be slightly imperfect: we provide nil for the context, | |
| 32 // since I (viettrungluu) am sceptical that putting in the original context | |
| 33 // (if one is given) is valid. | |
| 34 return [NSEvent keyEventWithType:event_type | |
| 35 location:location | |
| 36 modifierFlags:[event modifierFlags] | |
| 37 timestamp:[event timestamp] | |
| 38 windowNumber:[window windowNumber] | |
| 39 context:nil | |
| 40 characters:characters | |
| 41 charactersIgnoringModifiers:charactors_ignoring_modifiers | |
| 42 isARepeat:is_a_repeat | |
| 43 keyCode:[event keyCode]]; | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 @implementation CommandDispatcherImpl | |
| 49 | |
| 50 - (id)initWithOwner:(NSWindow<CommandDispatcher>*)owner { | |
| 51 if ((self = [super init])) { | |
| 52 owner_ = owner; | |
| 53 } | |
| 54 return self; | |
| 55 } | |
| 56 | |
| 57 - (void)setDelegate:(id<CommandDispatcherDelegate>)delegate { | |
| 58 delegate_.reset([delegate retain]); | |
| 59 } | |
| 60 | |
| 61 - (BOOL)performKeyEquivalent:(NSEvent*)event { | |
| 62 if ([delegate_ handledByExtensionCommand:event | |
| 63 isRedispatch:redispatchingEvent_]) | |
| 64 return YES; | |
| 65 | |
| 66 if (redispatchingEvent_) | |
| 67 return NO; | |
| 68 | |
| 69 // Give the web site a chance to handle the event. If it doesn't want to | |
|
tapted
2015/08/26 03:04:49
the web site -> a CommandDispatcherTarget (e.g. a
jackhou1
2015/08/26 06:24:43
Done.
| |
| 70 // handle it, it will call us back with one of the |handle*| methods above. | |
| 71 NSResponder* r = [owner_ firstResponder]; | |
| 72 if ([r conformsToProtocol:@protocol(CommandDispatcherTarget)]) | |
| 73 return [r performKeyEquivalent:event]; | |
| 74 | |
| 75 if ([delegate_ prePerformKeyEquivalent:event window:owner_]) | |
| 76 return YES; | |
| 77 | |
| 78 if ([owner_ defaultPerformKeyEquivalent:event]) | |
| 79 return YES; | |
| 80 | |
| 81 return [delegate_ postPerformKeyEquivalent:event window:owner_]; | |
| 82 } | |
| 83 | |
| 84 - (BOOL)redispatchKeyEvent:(NSEvent*)event { | |
| 85 DCHECK(event); | |
| 86 NSEventType eventType = [event type]; | |
| 87 if (eventType != NSKeyDown && eventType != NSKeyUp && | |
| 88 eventType != NSFlagsChanged) { | |
| 89 NOTREACHED(); | |
| 90 return YES; // Pretend it's been handled in an effort to limit damage. | |
| 91 } | |
| 92 | |
| 93 // Ordinarily, the event's window should be this window. However, when | |
|
tapted
2015/08/26 03:04:49
this window -> |owner_|
jackhou1
2015/08/26 06:24:43
Done.
| |
| 94 // switching between normal and fullscreen mode, we switch out the window, and | |
| 95 // the event's window might be the previous window (or even an earlier one if | |
| 96 // the renderer is running slowly and several mode switches occur). In this | |
| 97 // rare case, we synthesize a new key event so that its associate window | |
| 98 // (number) is our own. | |
|
tapted
2015/08/26 03:04:49
our own -> our |owner_|'s
jackhou1
2015/08/26 06:24:43
Done.
| |
| 99 if ([event window] != owner_) | |
| 100 event = KeyEventForWindow(owner_, event); | |
| 101 | |
| 102 // Redispatch the event. | |
| 103 eventHandled_ = YES; | |
| 104 redispatchingEvent_ = YES; | |
| 105 [NSApp sendEvent:event]; | |
| 106 redispatchingEvent_ = NO; | |
| 107 | |
| 108 // If the event was not handled by [NSApp sendEvent:], the sendEvent: | |
| 109 // method below will be called, and because |redispatchingEvent_| is YES, | |
| 110 // |eventHandled_| will be set to NO. | |
| 111 return eventHandled_; | |
| 112 } | |
| 113 | |
| 114 - (BOOL)preSendEvent:(NSEvent*)event { | |
| 115 if (redispatchingEvent_) { | |
| 116 // If we get here, then the event was not handled by NSApplication. | |
| 117 eventHandled_ = NO; | |
| 118 // Return YES to stop native -sendEvent handling. | |
| 119 return YES; | |
| 120 } | |
| 121 | |
| 122 return NO; | |
| 123 } | |
| 124 | |
| 125 @end | |
| OLD | NEW |