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

Side by Side 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: Address comments. Created 5 years, 3 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
tapted 2015/08/26 03:04:48 Is there a way to mess with --similarity so that w
jackhou1 2015/08/26 06:24:42 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 "chrome/browser/ui/cocoa/chrome_command_dispatcher_delegate.h"
6
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
14 namespace {
15
16 // Type of functions listed in global_keyboard_shortcuts_mac.h.
17 typedef int (*KeyToCommandMapper)(bool, bool, bool, bool, int, unichar);
18
19 // If the event is for a Browser window, and the key combination has an
20 // associated command, execute the command.
21 bool HandleExtraKeyboardShortcut(
22 NSEvent* event,
23 NSWindow* window,
24 KeyToCommandMapper command_for_keyboard_shortcut) {
25 // Extract info from |event|.
26 NSUInteger modifers = [event modifierFlags];
27 const bool command = modifers & NSCommandKeyMask;
28 const bool shift = modifers & NSShiftKeyMask;
29 const bool control = modifers & NSControlKeyMask;
30 const bool option = modifers & NSAlternateKeyMask;
31 const int key_code = [event keyCode];
32 const unichar key_char = KeyCharacterForEvent(event);
33
34 int cmd = command_for_keyboard_shortcut(command, shift, control, option,
35 key_code, key_char);
36
37 if (cmd == -1)
38 return false;
39
40 // Only handle event if this is a browser window.
41 Browser* browser = chrome::FindBrowserWithWindow(window);
42 if (!browser)
43 return false;
44
45 chrome::ExecuteCommand(browser, cmd);
46 return true;
47 }
48
49 bool HandleExtraWindowKeyboardShortcut(NSEvent* event, NSWindow* window) {
50 return HandleExtraKeyboardShortcut(event, window,
51 CommandForWindowKeyboardShortcut);
52 }
53
54 bool HandleDelayedWindowKeyboardShortcut(NSEvent* event, NSWindow* window) {
55 return HandleExtraKeyboardShortcut(event, window,
56 CommandForDelayedWindowKeyboardShortcut);
57 }
58
59 bool HandleExtraBrowserKeyboardShortcut(NSEvent* event, NSWindow* window) {
60 return HandleExtraKeyboardShortcut(event, window,
61 CommandForBrowserKeyboardShortcut);
62 }
63
64 } // namespace
65
66 @implementation ChromeCommandDispatcherDelegate
67
68 - (BOOL)handleExtraKeyboardShortcut:(NSEvent*)event window:(NSWindow*)window {
69 return HandleExtraBrowserKeyboardShortcut(event, window) ||
70 HandleExtraWindowKeyboardShortcut(event, window) ||
71 HandleDelayedWindowKeyboardShortcut(event, window);
72 }
73
74 - (BOOL)handledByExtensionCommand:(NSEvent*)event
75 isRedispatch:(BOOL)isRedispatch {
76 // Some extension commands have higher priority than web content, and some
77 // have lower priority. Regardless of whether the event is being redispatched,
78 // let the extension system try to handle the event. In case this is a
79 // redispatched event, |event.window| gives the correct window.
80 if (event.window) {
tapted 2015/08/26 03:04:48 nit: event.window -> [event window] (also above an
jackhou1 2015/08/26 06:24:42 Done.
81 BrowserWindowController* controller = [event.window windowController];
82 if ([controller respondsToSelector:@selector(handledByExtensionCommand:
83 priority:)]) {
84 ui::AcceleratorManager::HandlerPriority priority =
85 isRedispatch ? ui::AcceleratorManager::kNormalPriority
86 : ui::AcceleratorManager::kHighPriority;
87 if ([controller handledByExtensionCommand:event priority:priority])
88 return YES;
89 }
90 }
91 return NO;
92 }
93
94 - (BOOL)prePerformKeyEquivalent:(NSEvent*)event window:(NSWindow*)window {
95 // Handle per-window shortcuts like cmd-1, but do not handle browser-level
96 // shortcuts like cmd-left (else, cmd-left would do history navigation even
97 // if e.g. the Omnibox has focus).
98 return HandleExtraWindowKeyboardShortcut(event, window);
99 }
100
101 - (BOOL)postPerformKeyEquivalent:(NSEvent*)event window:(NSWindow*)window {
102 // Handle per-window shortcuts like Esc after giving everybody else a chance
103 // to handle them
104 return HandleDelayedWindowKeyboardShortcut(event, window);
105 }
106
107 @end // ChromeCommandDispatchDelegate
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698