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 // "Close window" doesn't use the |commandDispatch:| mechanism. Menu items | |
60 // that do not correspond to IDC_ constants need no special treatment however, | |
61 // as they can't be blacklisted in |Browser::IsReservedCommandOrKey()| anyhow. | |
62 if (item && [item action] == @selector(performClose:)) | |
63 return IDC_CLOSE_WINDOW; | |
64 | |
65 // "Exit" doesn't use the |commandDispatch:| mechanism either. | |
66 if (item && [item action] == @selector(terminate:)) | |
67 return IDC_EXIT; | |
68 | |
69 // Look in secondary keyboard shortcuts. | |
70 NSUInteger modifiers = [event.os_event modifierFlags]; | |
71 const bool cmdKey = (modifiers & NSCommandKeyMask) != 0; | |
72 const bool shiftKey = (modifiers & NSShiftKeyMask) != 0; | |
73 const bool cntrlKey = (modifiers & NSControlKeyMask) != 0; | |
74 const bool optKey = (modifiers & NSAlternateKeyMask) != 0; | |
75 const int keyCode = [event.os_event keyCode]; | |
76 const unichar keyChar = KeyCharacterForEvent(event.os_event); | |
77 | |
78 int cmdNum = CommandForWindowKeyboardShortcut( | |
79 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); | |
80 if (cmdNum != -1) | |
81 return cmdNum; | |
82 | |
83 cmdNum = CommandForBrowserKeyboardShortcut( | |
84 cmdKey, shiftKey, cntrlKey, optKey, keyCode, keyChar); | |
85 if (cmdNum != -1) | |
86 return cmdNum; | |
87 | |
88 return -1; | |
89 } | |
90 | |
91 + (BOOL)handleKeyboardEvent:(NSEvent*)event | |
92 inWindow:(NSWindow*)window { | |
93 ChromeEventProcessingWindow* event_window = | |
94 static_cast<ChromeEventProcessingWindow*>(window); | |
95 DCHECK([event_window isKindOfClass:[ChromeEventProcessingWindow class]]); | |
96 | |
97 // Do not fire shortcuts on key up. | |
98 if ([event type] == NSKeyDown) { | |
99 // Send the event to the menu before sending it to the browser/window | |
100 // shortcut handling, so that if a user configures cmd-left to mean | |
101 // "previous tab", it takes precedence over the built-in "history back" | |
102 // binding. Other than that, the |-redispatchKeyEvent:| call would take care | |
103 // of invoking the original menu item shortcut as well. | |
104 | |
105 if ([[NSApp mainMenu] performKeyEquivalent:event]) | |
106 return true; | |
107 | |
108 if ([event_window handleExtraBrowserKeyboardShortcut:event]) | |
109 return true; | |
110 | |
111 if ([event_window handleExtraWindowKeyboardShortcut:event]) | |
112 return true; | |
113 | |
114 if ([event_window handleDelayedWindowKeyboardShortcut:event]) | |
115 return true; | |
116 } | |
117 | |
118 return [event_window redispatchKeyEvent:event]; | |
119 } | |
120 | |
121 @end | |
OLD | NEW |