OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/browser_window_utils.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/app/chrome_command_ids.h" |
| 9 #include "chrome/browser/global_keyboard_shortcuts_mac.h" |
| 10 #include "chrome/browser/ui/browser.h" |
| 11 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h" |
| 12 #import "chrome/browser/ui/cocoa/nsmenuitem_additions.h" |
| 13 #include "content/common/native_web_keyboard_event.h" |
| 14 |
| 15 @interface MenuWalker : NSObject |
| 16 + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key |
| 17 menu:(NSMenu*)menu; |
| 18 @end |
| 19 |
| 20 @implementation MenuWalker |
| 21 + (NSMenuItem*)itemForKeyEquivalent:(NSEvent*)key |
| 22 menu:(NSMenu*)menu { |
| 23 NSMenuItem* result = nil; |
| 24 |
| 25 for (NSMenuItem* item in [menu itemArray]) { |
| 26 NSMenu* submenu = [item submenu]; |
| 27 if (submenu) { |
| 28 if (submenu != [NSApp servicesMenu]) |
| 29 result = [self itemForKeyEquivalent:key |
| 30 menu:submenu]; |
| 31 } else if ([item cr_firesForKeyEventIfEnabled:key]) { |
| 32 result = item; |
| 33 } |
| 34 |
| 35 if (result) |
| 36 break; |
| 37 } |
| 38 |
| 39 return result; |
| 40 } |
| 41 @end |
| 42 |
| 43 @implementation BrowserWindowUtils |
| 44 + (BOOL)shouldHandleKeyboardEvent:(const NativeWebKeyboardEvent&)event { |
| 45 if (event.skip_in_browser || event.type == NativeWebKeyboardEvent::Char) |
| 46 return NO; |
| 47 DCHECK(event.os_event != NULL); |
| 48 return YES; |
| 49 } |
| 50 |
| 51 + (int)getCommandId:(const NativeWebKeyboardEvent&)event { |
| 52 if ([event.os_event type] != NSKeyDown) |
| 53 return -1; |
| 54 |
| 55 // Look in menu. |
| 56 NSMenuItem* item = [MenuWalker itemForKeyEquivalent:event.os_event |
| 57 menu:[NSApp mainMenu]]; |
| 58 |
| 59 if (item && [item action] == @selector(commandDispatch:) && [item tag] > 0) |
| 60 return [item tag]; |
| 61 |
| 62 // "Close window" doesn't use the |commandDispatch:| mechanism. Menu items |
| 63 // that do not correspond to IDC_ constants need no special treatment however, |
| 64 // as they can't be blacklisted in |Browser::IsReservedCommandOrKey()| anyhow. |
| 65 if (item && [item action] == @selector(performClose:)) |
| 66 return IDC_CLOSE_WINDOW; |
| 67 |
| 68 // "Exit" doesn't use the |commandDispatch:| mechanism either. |
| 69 if (item && [item action] == @selector(terminate:)) |
| 70 return IDC_EXIT; |
| 71 |
| 72 // Look in secondary keyboard shortcuts. |
| 73 NSUInteger modifiers = [event.os_event modifierFlags]; |
| 74 const bool cmdKey = (modifiers & NSCommandKeyMask) != 0; |
| 75 const bool shiftKey = (modifiers & NSShiftKeyMask) != 0; |
| 76 const bool cntrlKey = (modifiers & NSControlKeyMask) != 0; |
| 77 const bool optKey = (modifiers & NSAlternateKeyMask) != 0; |
| 78 const int keyCode = [event.os_event keyCode]; |
| 79 const unichar keyChar = KeyCharacterForEvent(event.os_event); |
| 80 |
| 81 int cmdNum = CommandForWindowKeyboardShortcut( |
| 82 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); |
| 83 if (cmdNum != -1) |
| 84 return cmdNum; |
| 85 |
| 86 cmdNum = CommandForBrowserKeyboardShortcut( |
| 87 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); |
| 88 if (cmdNum != -1) |
| 89 return cmdNum; |
| 90 |
| 91 return -1; |
| 92 } |
| 93 |
| 94 + (BOOL)handleKeyboardEvent:(NSEvent*)event |
| 95 inWindow:(NSWindow*)window { |
| 96 ChromeEventProcessingWindow* event_window = |
| 97 static_cast<ChromeEventProcessingWindow*>(window); |
| 98 DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]); |
| 99 |
| 100 // Do not fire shortcuts on key up. |
| 101 if ([event type] == NSKeyDown) { |
| 102 // Send the event to the menu before sending it to the browser/window |
| 103 // shortcut handling, so that if a user configures cmd-left to mean |
| 104 // "previous tab", it takes precedence over the built-in "history back" |
| 105 // binding. Other than that, the |-redispatchKeyEvent:| call would take care |
| 106 // of invoking the original menu item shortcut as well. |
| 107 |
| 108 if ([[NSApp mainMenu] performKeyEquivalent:event]) |
| 109 return true; |
| 110 |
| 111 if ([event_window handleExtraBrowserKeyboardShortcut:event]) |
| 112 return true; |
| 113 |
| 114 if ([event_window handleExtraWindowKeyboardShortcut:event]) |
| 115 return true; |
| 116 |
| 117 if ([event_window handleDelayedWindowKeyboardShortcut:event]) |
| 118 return true; |
| 119 } |
| 120 |
| 121 return [event_window redispatchKeyEvent:event]; |
| 122 } |
| 123 |
| 124 @end |
OLD | NEW |