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