Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(41)

Side by Side Diff: chrome/browser/ui/cocoa/chrome_command_dispatch_delegate.mm

Issue 1255783002: [Mac] Factor out keyboard shortcut handling from ChromeEventProcessingWindow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@execute
Patch Set: Fix a few things. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/ui/cocoa/chrome_command_dispatch_delegate.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "chrome/browser/global_keyboard_shortcuts_mac.h" 8 #include "chrome/browser/global_keyboard_shortcuts_mac.h"
9 #include "chrome/browser/ui/browser_commands.h" 9 #include "chrome/browser/ui/browser_commands.h"
10 #include "chrome/browser/ui/browser_finder.h" 10 #include "chrome/browser/ui/browser_finder.h"
11 #import "chrome/browser/ui/cocoa/browser_window_controller_private.h" 11 #import "chrome/browser/ui/cocoa/browser_window_controller_private.h"
12 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" 12 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
13 #import "content/public/browser/render_widget_host_view_mac_base.h" 13 #import "content/public/browser/render_widget_host_view_mac_base.h"
14 14
15 namespace { 15 namespace {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 windowNumber:[window windowNumber] 92 windowNumber:[window windowNumber]
93 context:nil 93 context:nil
94 characters:characters 94 characters:characters
95 charactersIgnoringModifiers:charactors_ignoring_modifiers 95 charactersIgnoringModifiers:charactors_ignoring_modifiers
96 isARepeat:is_a_repeat 96 isARepeat:is_a_repeat
97 keyCode:[event keyCode]]; 97 keyCode:[event keyCode]];
98 } 98 }
99 99
100 } // namespace 100 } // namespace
101 101
102 @implementation ChromeEventProcessingWindow 102 @implementation ChromeCommandDispatchDelegate
103 103
104 - (BOOL)handleExtraKeyboardShortcut:(NSEvent*)event { 104 - (BOOL)handleExtraKeyboardShortcut:(NSEvent*)event window:(NSWindow*)window {
105 return HandleExtraBrowserKeyboardShortcut(event, self) || 105 return HandleExtraBrowserKeyboardShortcut(event, window) ||
106 HandleExtraWindowKeyboardShortcut(event, self) || 106 HandleExtraWindowKeyboardShortcut(event, window) ||
107 HandleDelayedWindowKeyboardShortcut(event, self); 107 HandleDelayedWindowKeyboardShortcut(event, window);
108 } 108 }
109 109
110 - (BOOL)performKeyEquivalent:(NSEvent*)event { 110 - (BOOL)performKeyEquivalent:(NSEvent*)event
111 window:(NSWindow*)window
tapted 2015/07/31 03:06:49 Could the delegate simply have a (weak) NSWindow*
jackhou1 2015/08/04 01:03:58 Done.
112 nativeHandler:(BOOL (^)(NSEvent*))nativePerformKeyEquivalent {
111 // Some extension commands have higher priority than web content, and some 113 // Some extension commands have higher priority than web content, and some
112 // have lower priority. Regardless of whether the event is being 114 // have lower priority. Regardless of whether the event is being
113 // redispatched, let the extension system try to handle the event. 115 // redispatched, let the extension system try to handle the event.
114 NSWindow* window = event.window; 116 if (event.window) {
tapted 2015/07/31 03:06:49 event.window -> window? (or, owner_?) (or, comment
jackhou1 2015/08/04 01:03:58 Done.
115 if (window) { 117 BrowserWindowController* controller = [event.window windowController];
tapted 2015/07/31 03:06:49 ObjCCastStrict? And.. that might actually fail. I
tapted 2015/07/31 03:06:50 event.window -> window?
jackhou1 2015/08/04 01:03:58 Done.
116 BrowserWindowController* controller = [window windowController];
117 if ([controller respondsToSelector:@selector(handledByExtensionCommand: 118 if ([controller respondsToSelector:@selector(handledByExtensionCommand:
118 priority:)]) { 119 priority:)]) {
119 ui::AcceleratorManager::HandlerPriority priority = 120 ui::AcceleratorManager::HandlerPriority priority =
120 redispatchingEvent_ ? ui::AcceleratorManager::kNormalPriority 121 redispatchingEvent_ ? ui::AcceleratorManager::kNormalPriority
121 : ui::AcceleratorManager::kHighPriority; 122 : ui::AcceleratorManager::kHighPriority;
122 if ([controller handledByExtensionCommand:event priority:priority]) 123 if ([controller handledByExtensionCommand:event priority:priority])
123 return YES; 124 return YES;
124 } 125 }
125 } 126 }
126 127
127 if (redispatchingEvent_) 128 if (redispatchingEvent_)
128 return NO; 129 return NO;
129 130
130 // Give the web site a chance to handle the event. If it doesn't want to 131 // Give the web site a chance to handle the event. If it doesn't want to
131 // handle it, it will call us back with one of the |handle*| methods above. 132 // handle it, it will call us back with one of the |handle*| methods above.
132 NSResponder* r = [self firstResponder]; 133 NSResponder* r = [window firstResponder];
133 if ([r conformsToProtocol:@protocol(RenderWidgetHostViewMacBase)]) 134 if ([r conformsToProtocol:@protocol(RenderWidgetHostViewMacBase)])
134 return [r performKeyEquivalent:event]; 135 return [r performKeyEquivalent:event];
135 136
136 // Handle per-window shortcuts like cmd-1, but do not handle browser-level 137 // Handle per-window shortcuts like cmd-1, but do not handle browser-level
137 // shortcuts like cmd-left (else, cmd-left would do history navigation even 138 // shortcuts like cmd-left (else, cmd-left would do history navigation even
138 // if e.g. the Omnibox has focus). 139 // if e.g. the Omnibox has focus).
139 if (HandleExtraWindowKeyboardShortcut(event, self)) 140 if (HandleExtraWindowKeyboardShortcut(event, window))
140 return YES; 141 return YES;
141 142
142 if ([super performKeyEquivalent:event]) 143 if (nativePerformKeyEquivalent(event))
tapted 2015/07/31 03:06:50 e.g. this could become [owner_ defaultPerformKeyEq
jackhou1 2015/08/04 01:03:58 Done.
143 return YES; 144 return YES;
144 145
145 // Handle per-window shortcuts like Esc after giving everybody else a chance 146 // Handle per-window shortcuts like Esc after giving everybody else a chance
146 // to handle them 147 // to handle them
147 return HandleDelayedWindowKeyboardShortcut(event, self); 148 return HandleDelayedWindowKeyboardShortcut(event, window);
148 } 149 }
149 150
150 - (BOOL)redispatchKeyEvent:(NSEvent*)event { 151 - (BOOL)redispatchKeyEvent:(NSEvent*)event window:(NSWindow*)window {
151 DCHECK(event); 152 DCHECK(event);
152 NSEventType eventType = [event type]; 153 NSEventType eventType = [event type];
153 if (eventType != NSKeyDown && 154 if (eventType != NSKeyDown && eventType != NSKeyUp &&
154 eventType != NSKeyUp &&
155 eventType != NSFlagsChanged) { 155 eventType != NSFlagsChanged) {
156 NOTREACHED(); 156 NOTREACHED();
157 return YES; // Pretend it's been handled in an effort to limit damage. 157 return YES; // Pretend it's been handled in an effort to limit damage.
158 } 158 }
159 159
160 // Ordinarily, the event's window should be this window. However, when 160 // Ordinarily, the event's window should be this window. However, when
161 // switching between normal and fullscreen mode, we switch out the window, and 161 // switching between normal and fullscreen mode, we switch out the window, and
162 // the event's window might be the previous window (or even an earlier one if 162 // the event's window might be the previous window (or even an earlier one if
163 // the renderer is running slowly and several mode switches occur). In this 163 // the renderer is running slowly and several mode switches occur). In this
164 // rare case, we synthesize a new key event so that its associate window 164 // rare case, we synthesize a new key event so that its associate window
165 // (number) is our own. 165 // (number) is our own.
166 if ([event window] != self) 166 if ([event window] != window)
167 event = KeyEventForWindow(self, event); 167 event = KeyEventForWindow(window, event);
168 168
169 // Redispatch the event. 169 // Redispatch the event.
170 eventHandled_ = YES; 170 eventHandled_ = YES;
171 redispatchingEvent_ = YES; 171 redispatchingEvent_ = YES;
172 [NSApp sendEvent:event]; 172 [NSApp sendEvent:event];
173 redispatchingEvent_ = NO; 173 redispatchingEvent_ = NO;
174 174
175 // If the event was not handled by [NSApp sendEvent:], the sendEvent: 175 // If the event was not handled by [NSApp sendEvent:], the sendEvent:
176 // method below will be called, and because |redispatchingEvent_| is YES, 176 // method below will be called, and because |redispatchingEvent_| is YES,
177 // |eventHandled_| will be set to NO. 177 // |eventHandled_| will be set to NO.
178 return eventHandled_; 178 return eventHandled_;
179 } 179 }
180 180
181 - (void)sendEvent:(NSEvent*)event { 181 - (BOOL)preSendEvent:(NSEvent*)event {
182 if (!redispatchingEvent_) 182 if (redispatchingEvent_) {
183 [super sendEvent:event];
184 else
185 eventHandled_ = NO; 183 eventHandled_ = NO;
184 return NO;
185 }
186
187 return YES;
186 } 188 }
187 189
188 @end // ChromeEventProcessingWindow 190 @end // ChromeCommandDispatchDelegate
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698