Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 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. | |
|
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
| |
| 80 if (event.window) { | |
| 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 | |
| OLD | NEW |