OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" | 5 #import "ui/base/cocoa/command_dispatcher.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/browser/global_keyboard_shortcuts_mac.h" | |
9 #include "chrome/browser/ui/browser_commands.h" | |
10 #include "chrome/browser/ui/browser_finder.h" | |
11 #import "chrome/browser/ui/cocoa/browser_window_controller_private.h" | |
12 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" | |
13 #import "content/public/browser/render_widget_host_view_mac_base.h" | |
14 | 8 |
15 namespace { | 9 namespace { |
16 | 10 |
17 // Type of functions listed in global_keyboard_shortcuts_mac.h. | |
18 typedef int (*KeyToCommandMapper)(bool, bool, bool, bool, int, unichar); | |
19 | |
20 // If the event is for a Browser window, and the key combination has an | |
21 // associated command, execute the command. | |
22 bool HandleExtraKeyboardShortcut( | |
23 NSEvent* event, | |
24 NSWindow* window, | |
25 KeyToCommandMapper command_for_keyboard_shortcut) { | |
26 // Extract info from |event|. | |
27 NSUInteger modifers = [event modifierFlags]; | |
28 const bool command = modifers & NSCommandKeyMask; | |
29 const bool shift = modifers & NSShiftKeyMask; | |
30 const bool control = modifers & NSControlKeyMask; | |
31 const bool option = modifers & NSAlternateKeyMask; | |
32 const int key_code = [event keyCode]; | |
33 const unichar key_char = KeyCharacterForEvent(event); | |
34 | |
35 int cmd = command_for_keyboard_shortcut(command, shift, control, option, | |
36 key_code, key_char); | |
37 | |
38 if (cmd == -1) | |
39 return false; | |
40 | |
41 // Only handle event if this is a browser window. | |
42 Browser* browser = chrome::FindBrowserWithWindow(window); | |
43 if (!browser) | |
44 return false; | |
45 | |
46 chrome::ExecuteCommand(browser, cmd); | |
47 return true; | |
48 } | |
49 | |
50 bool HandleExtraWindowKeyboardShortcut(NSEvent* event, NSWindow* window) { | |
51 return HandleExtraKeyboardShortcut(event, window, | |
52 CommandForWindowKeyboardShortcut); | |
53 } | |
54 | |
55 bool HandleDelayedWindowKeyboardShortcut(NSEvent* event, NSWindow* window) { | |
56 return HandleExtraKeyboardShortcut(event, window, | |
57 CommandForDelayedWindowKeyboardShortcut); | |
58 } | |
59 | |
60 bool HandleExtraBrowserKeyboardShortcut(NSEvent* event, NSWindow* window) { | |
61 return HandleExtraKeyboardShortcut(event, window, | |
62 CommandForBrowserKeyboardShortcut); | |
63 } | |
64 | |
65 // Duplicate the given key event, but changing the associated window. | 11 // Duplicate the given key event, but changing the associated window. |
66 NSEvent* KeyEventForWindow(NSWindow* window, NSEvent* event) { | 12 NSEvent* KeyEventForWindow(NSWindow* window, NSEvent* event) { |
67 NSEventType event_type = [event type]; | 13 NSEventType event_type = [event type]; |
68 | 14 |
69 // Convert the event's location from the original window's coordinates into | 15 // Convert the event's location from the original window's coordinates into |
70 // our own. | 16 // our own. |
71 NSPoint location = [event locationInWindow]; | 17 NSPoint location = [event locationInWindow]; |
72 location = [[event window] convertBaseToScreen:location]; | 18 location = [[event window] convertBaseToScreen:location]; |
73 location = [window convertScreenToBase:location]; | 19 location = [window convertScreenToBase:location]; |
74 | 20 |
(...skipping 17 matching lines...) Expand all Loading... | |
92 windowNumber:[window windowNumber] | 38 windowNumber:[window windowNumber] |
93 context:nil | 39 context:nil |
94 characters:characters | 40 characters:characters |
95 charactersIgnoringModifiers:charactors_ignoring_modifiers | 41 charactersIgnoringModifiers:charactors_ignoring_modifiers |
96 isARepeat:is_a_repeat | 42 isARepeat:is_a_repeat |
97 keyCode:[event keyCode]]; | 43 keyCode:[event keyCode]]; |
98 } | 44 } |
99 | 45 |
100 } // namespace | 46 } // namespace |
101 | 47 |
102 @implementation ChromeEventProcessingWindow | 48 @implementation CommandDispatcher |
103 | 49 |
104 - (BOOL)handleExtraKeyboardShortcut:(NSEvent*)event { | 50 @synthesize delegate = delegate_; |
105 return HandleExtraBrowserKeyboardShortcut(event, self) || | 51 |
106 HandleExtraWindowKeyboardShortcut(event, self) || | 52 - (id)initWithOwner:(NSWindow<CommandDispatchingWindow>*)owner { |
107 HandleDelayedWindowKeyboardShortcut(event, self); | 53 if ((self = [super init])) { |
54 owner_ = owner; | |
55 } | |
56 return self; | |
108 } | 57 } |
109 | 58 |
110 - (BOOL)performKeyEquivalent:(NSEvent*)event { | 59 - (BOOL)performKeyEquivalent:(NSEvent*)event { |
111 // Some extension commands have higher priority than web content, and some | 60 if ([delegate_ handledByExtensionCommand:event |
112 // have lower priority. Regardless of whether the event is being | 61 isRedispatch:redispatchingEvent_]) |
Robert Sesek
2015/08/26 17:46:49
nit: requires braces since multi-line if
jackhou1
2015/08/27 00:01:25
Done.
| |
113 // redispatched, let the extension system try to handle the event. | 62 return YES; |
114 NSWindow* window = event.window; | |
115 if (window) { | |
116 BrowserWindowController* controller = [window windowController]; | |
117 if ([controller respondsToSelector:@selector(handledByExtensionCommand: | |
118 priority:)]) { | |
119 ui::AcceleratorManager::HandlerPriority priority = | |
120 redispatchingEvent_ ? ui::AcceleratorManager::kNormalPriority | |
121 : ui::AcceleratorManager::kHighPriority; | |
122 if ([controller handledByExtensionCommand:event priority:priority]) | |
123 return YES; | |
124 } | |
125 } | |
126 | 63 |
127 if (redispatchingEvent_) | 64 if (redispatchingEvent_) |
128 return NO; | 65 return NO; |
129 | 66 |
130 // Give the web site a chance to handle the event. If it doesn't want to | 67 // Give a CommandDispatcherTarget (e.g. a web site) a chance to handle the |
131 // handle it, it will call us back with one of the |handle*| methods above. | 68 // event. If it doesn't want to handle it, it will call us back with one of |
132 NSResponder* r = [self firstResponder]; | 69 // the |handle*| methods above. |
133 if ([r conformsToProtocol:@protocol(RenderWidgetHostViewMacBase)]) | 70 NSResponder* r = [owner_ firstResponder]; |
71 if ([r conformsToProtocol:@protocol(CommandDispatcherTarget)]) | |
134 return [r performKeyEquivalent:event]; | 72 return [r performKeyEquivalent:event]; |
135 | 73 |
136 // Handle per-window shortcuts like cmd-1, but do not handle browser-level | 74 if ([delegate_ prePerformKeyEquivalent:event window:owner_]) |
137 // shortcuts like cmd-left (else, cmd-left would do history navigation even | |
138 // if e.g. the Omnibox has focus). | |
139 if (HandleExtraWindowKeyboardShortcut(event, self)) | |
140 return YES; | 75 return YES; |
141 | 76 |
142 if ([super performKeyEquivalent:event]) | 77 if ([owner_ defaultPerformKeyEquivalent:event]) |
143 return YES; | 78 return YES; |
144 | 79 |
145 // Handle per-window shortcuts like Esc after giving everybody else a chance | 80 return [delegate_ postPerformKeyEquivalent:event window:owner_]; |
146 // to handle them | |
147 return HandleDelayedWindowKeyboardShortcut(event, self); | |
148 } | 81 } |
149 | 82 |
150 - (BOOL)redispatchKeyEvent:(NSEvent*)event { | 83 - (BOOL)redispatchKeyEvent:(NSEvent*)event { |
151 DCHECK(event); | 84 DCHECK(event); |
152 NSEventType eventType = [event type]; | 85 NSEventType eventType = [event type]; |
153 if (eventType != NSKeyDown && | 86 if (eventType != NSKeyDown && eventType != NSKeyUp && |
154 eventType != NSKeyUp && | |
155 eventType != NSFlagsChanged) { | 87 eventType != NSFlagsChanged) { |
156 NOTREACHED(); | 88 NOTREACHED(); |
157 return YES; // Pretend it's been handled in an effort to limit damage. | 89 return YES; // Pretend it's been handled in an effort to limit damage. |
158 } | 90 } |
159 | 91 |
160 // Ordinarily, the event's window should be this window. However, when | 92 // Ordinarily, the event's window should be |owner_|. However, when switching |
161 // switching between normal and fullscreen mode, we switch out the window, and | 93 // between normal and fullscreen mode, we switch out the window, and the |
162 // the event's window might be the previous window (or even an earlier one if | 94 // event's window might be the previous window (or even an earlier one if the |
163 // the renderer is running slowly and several mode switches occur). In this | 95 // renderer is running slowly and several mode switches occur). In this rare |
164 // rare case, we synthesize a new key event so that its associate window | 96 // case, we synthesize a new key event so that its associate window (number) |
165 // (number) is our own. | 97 // is our |owner_|'s. |
166 if ([event window] != self) | 98 if ([event window] != owner_) |
167 event = KeyEventForWindow(self, event); | 99 event = KeyEventForWindow(owner_, event); |
168 | 100 |
169 // Redispatch the event. | 101 // Redispatch the event. |
170 eventHandled_ = YES; | 102 eventHandled_ = YES; |
171 redispatchingEvent_ = YES; | 103 redispatchingEvent_ = YES; |
172 [NSApp sendEvent:event]; | 104 [NSApp sendEvent:event]; |
173 redispatchingEvent_ = NO; | 105 redispatchingEvent_ = NO; |
174 | 106 |
175 // If the event was not handled by [NSApp sendEvent:], the sendEvent: | 107 // If the event was not handled by [NSApp sendEvent:], the sendEvent: |
176 // method below will be called, and because |redispatchingEvent_| is YES, | 108 // method below will be called, and because |redispatchingEvent_| is YES, |
177 // |eventHandled_| will be set to NO. | 109 // |eventHandled_| will be set to NO. |
178 return eventHandled_; | 110 return eventHandled_; |
179 } | 111 } |
180 | 112 |
181 - (void)sendEvent:(NSEvent*)event { | 113 - (BOOL)preSendEvent:(NSEvent*)event { |
182 if (!redispatchingEvent_) | 114 if (redispatchingEvent_) { |
183 [super sendEvent:event]; | 115 // If we get here, then the event was not handled by NSApplication. |
184 else | |
185 eventHandled_ = NO; | 116 eventHandled_ = NO; |
117 // Return YES to stop native -sendEvent handling. | |
118 return YES; | |
119 } | |
120 | |
121 return NO; | |
186 } | 122 } |
187 | 123 |
188 @end // ChromeEventProcessingWindow | 124 @end |
OLD | NEW |