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

Unified Diff: chrome/browser/ui/cocoa/chrome_command_dispatcher_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: Separate CommandDispatcher and CommandDispatcherDelegate. 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/cocoa/chrome_command_dispatcher_delegate.mm
diff --git a/chrome/browser/ui/cocoa/chrome_command_dispatcher_delegate.mm b/chrome/browser/ui/cocoa/chrome_command_dispatcher_delegate.mm
new file mode 100644
index 0000000000000000000000000000000000000000..e8282445df9879393a19e3546bcf3634aa4f7b16
--- /dev/null
+++ b/chrome/browser/ui/cocoa/chrome_command_dispatcher_delegate.mm
@@ -0,0 +1,107 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "chrome/browser/ui/cocoa/chrome_command_dispatcher_delegate.h"
+
+#include "base/logging.h"
+#include "chrome/browser/global_keyboard_shortcuts_mac.h"
+#include "chrome/browser/ui/browser_commands.h"
+#include "chrome/browser/ui/browser_finder.h"
+#import "chrome/browser/ui/cocoa/browser_window_controller_private.h"
+#import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
+
+namespace {
+
+// Type of functions listed in global_keyboard_shortcuts_mac.h.
+typedef int (*KeyToCommandMapper)(bool, bool, bool, bool, int, unichar);
+
+// If the event is for a Browser window, and the key combination has an
+// associated command, execute the command.
+bool HandleExtraKeyboardShortcut(
+ NSEvent* event,
+ NSWindow* window,
+ KeyToCommandMapper command_for_keyboard_shortcut) {
+ // Extract info from |event|.
+ NSUInteger modifers = [event modifierFlags];
+ const bool command = modifers & NSCommandKeyMask;
+ const bool shift = modifers & NSShiftKeyMask;
+ const bool control = modifers & NSControlKeyMask;
+ const bool option = modifers & NSAlternateKeyMask;
+ const int key_code = [event keyCode];
+ const unichar key_char = KeyCharacterForEvent(event);
+
+ int cmd = command_for_keyboard_shortcut(command, shift, control, option,
+ key_code, key_char);
+
+ if (cmd == -1)
+ return false;
+
+ // Only handle event if this is a browser window.
+ Browser* browser = chrome::FindBrowserWithWindow(window);
+ if (!browser)
+ return false;
+
+ chrome::ExecuteCommand(browser, cmd);
+ return true;
+}
+
+bool HandleExtraWindowKeyboardShortcut(NSEvent* event, NSWindow* window) {
+ return HandleExtraKeyboardShortcut(event, window,
+ CommandForWindowKeyboardShortcut);
+}
+
+bool HandleDelayedWindowKeyboardShortcut(NSEvent* event, NSWindow* window) {
+ return HandleExtraKeyboardShortcut(event, window,
+ CommandForDelayedWindowKeyboardShortcut);
+}
+
+bool HandleExtraBrowserKeyboardShortcut(NSEvent* event, NSWindow* window) {
+ return HandleExtraKeyboardShortcut(event, window,
+ CommandForBrowserKeyboardShortcut);
+}
+
+} // namespace
+
+@implementation ChromeCommandDispatcherDelegate
+
+- (BOOL)handleExtraKeyboardShortcut:(NSEvent*)event window:(NSWindow*)window {
+ return HandleExtraBrowserKeyboardShortcut(event, window) ||
+ HandleExtraWindowKeyboardShortcut(event, window) ||
+ HandleDelayedWindowKeyboardShortcut(event, window);
+}
+
+- (BOOL)handledByExtensionCommand:(NSEvent*)event
+ isRedispatch:(BOOL)isRedispatch {
+ // Some extension commands have higher priority than web content, and some
+ // have lower priority. Regardless of whether the event is being redispatched,
+ // let the extension system try to handle the event. In case this is a
+ // redispatched event, |event.window| gives the correct window.
tapted 2015/08/18 07:22:12 What about if it isn't redispatched? (is event.win
jackhou1 2015/08/25 06:31:12 I think it should be fine, pretty sure the logic i
+ if (event.window) {
+ BrowserWindowController* controller = [event.window windowController];
+ if ([controller respondsToSelector:@selector(handledByExtensionCommand:
+ priority:)]) {
+ ui::AcceleratorManager::HandlerPriority priority =
+ isRedispatch ? ui::AcceleratorManager::kNormalPriority
+ : ui::AcceleratorManager::kHighPriority;
+ if ([controller handledByExtensionCommand:event priority:priority])
+ return YES;
+ }
+ }
+ return NO;
+}
+
+- (BOOL)prePerformKeyEquivalent:(NSEvent*)event window:(NSWindow*)window {
+ // Handle per-window shortcuts like cmd-1, but do not handle browser-level
+ // shortcuts like cmd-left (else, cmd-left would do history navigation even
+ // if e.g. the Omnibox has focus).
+ return HandleExtraWindowKeyboardShortcut(event, window);
+}
+
+- (BOOL)postPerformKeyEquivalent:(NSEvent*)event window:(NSWindow*)window {
+ // Handle per-window shortcuts like Esc after giving everybody else a chance
+ // to handle them
+ return HandleDelayedWindowKeyboardShortcut(event, window);
+}
+
+@end // ChromeCommandDispatchDelegate

Powered by Google App Engine
This is Rietveld 408576698