OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 <Cocoa/Cocoa.h> | |
6 | |
7 #include "chrome/browser/ui/views/frame/browser_view_platform.h" | |
8 | |
9 #import "base/mac/foundation_util.h" | |
10 #include "chrome/browser/global_keyboard_shortcuts_mac.h" | |
11 #include "chrome/browser/ui/browser.h" | |
12 #include "chrome/browser/ui/browser_command_controller.h" | |
13 #include "chrome/browser/ui/browser_commands.h" | |
14 #include "chrome/browser/ui/browser_finder.h" | |
15 #include "chrome/browser/ui/browser_window.h" | |
16 | |
17 namespace { | |
18 | |
19 // Type of functions listed in global_keyboard_shortcuts_mac.h. | |
20 typedef int (*KeyToCommandMapper)(bool, bool, bool, bool, int, unichar); | |
21 | |
22 // If the key combination has an associated command, execute the command. | |
23 bool HandleExtraKeyboardShortcut( | |
tapted
2016/10/17 07:02:47
We shouldn't copy all this stuff. Is there a way t
themblsha
2016/10/20 16:41:05
Made this function public in chrome_command_dispat
| |
24 NSEvent* event, | |
25 Browser* browser, | |
26 KeyToCommandMapper command_for_keyboard_shortcut) { | |
27 if (!browser) | |
28 return false; | |
29 | |
30 // Extract info from |event|. | |
31 NSUInteger modifers = [event modifierFlags]; | |
32 const bool command = modifers & NSCommandKeyMask; | |
33 const bool shift = modifers & NSShiftKeyMask; | |
34 const bool control = modifers & NSControlKeyMask; | |
35 const bool option = modifers & NSAlternateKeyMask; | |
36 const int key_code = [event keyCode]; | |
37 const unichar key_char = KeyCharacterForEvent(event); | |
38 | |
39 int cmd = command_for_keyboard_shortcut(command, shift, control, option, | |
40 key_code, key_char); | |
41 | |
42 if (cmd == -1) | |
43 return false; | |
44 | |
45 chrome::ExecuteCommand(browser, cmd); | |
46 return true; | |
47 } | |
48 | |
49 bool HandleExtraWindowKeyboardShortcut(NSEvent* event, Browser* browser) { | |
50 return HandleExtraKeyboardShortcut(event, browser, | |
51 CommandForWindowKeyboardShortcut); | |
52 } | |
53 | |
54 bool HandleDelayedWindowKeyboardShortcut(NSEvent* event, Browser* browser) { | |
55 return HandleExtraKeyboardShortcut(event, browser, | |
56 CommandForDelayedWindowKeyboardShortcut); | |
57 } | |
58 | |
59 bool HandleExtraBrowserKeyboardShortcut(NSEvent* event, Browser* browser) { | |
60 return HandleExtraKeyboardShortcut(event, browser, | |
61 CommandForBrowserKeyboardShortcut); | |
62 } | |
63 | |
64 bool ShouldHandleKeyboardEvent(const content::NativeWebKeyboardEvent& event) { | |
65 // Do not fire shortcuts on key up. | |
66 if ([event type] != NSKeyDown) | |
67 return false; | |
68 | |
69 if (event.skip_in_browser || | |
70 event.type == content::NativeWebKeyboardEvent::Char) | |
71 return false; | |
72 | |
73 DCHECK(event.os_event != NULL); | |
74 return true; | |
75 } | |
76 | |
77 // Copied from chrome_command_dispatcher_delegate.mm's | |
78 // HandleExtraBrowserKeyboardShortcut. | |
79 bool HandleExtraKeyboardShortcut(NSEvent* event, Browser* browser) { | |
80 // Send the event to the menu before sending it to the browser/window | |
81 // shortcut handling, so that if a user configures cmd-left to mean | |
82 // "previous tab", it takes precedence over the built-in "history back" | |
83 // binding. Other than that, the |-redispatchKeyEvent:| call would take care | |
84 // of invoking the original menu item shortcut as well. | |
85 | |
86 if ([[NSApp mainMenu] performKeyEquivalent:event]) | |
87 return true; | |
88 | |
89 return HandleExtraBrowserKeyboardShortcut(event, browser) || | |
90 HandleExtraWindowKeyboardShortcut(event, browser) || | |
91 HandleDelayedWindowKeyboardShortcut(event, browser); | |
92 } | |
93 | |
94 } // namespace | |
95 | |
96 // static. Copied from BrowserWindowCocoa::PreHandleKeyboardEvent | |
tapted
2016/10/17 07:02:47
nit: Move Copied from.. into the function body.
themblsha
2016/10/20 16:41:05
Done.
| |
97 // implementation. | |
98 bool BrowserViewPlatform::PreHandleKeyboardEvent( | |
99 const content::NativeWebKeyboardEvent& event, | |
100 Browser* browser) { | |
101 if (!ShouldHandleKeyboardEvent(event)) | |
102 return false; | |
103 | |
104 // CommandForKeyEvent consults the [NSApp mainMenu] and | |
105 // CommandForWindowKeyboardShortcut + CommandForBrowserKeyboardShortcut | |
106 // accelerator tables internally. | |
107 int command_id = CommandForKeyEvent(event.os_event); | |
108 if (command_id == -1) | |
109 return false; | |
110 | |
111 if (!browser->command_controller()->IsReservedCommandOrKey(command_id, event)) | |
112 return false; | |
113 | |
114 return HandleExtraKeyboardShortcut(event.os_event, browser); | |
115 } | |
116 | |
117 // static. Copied from BrowserWindowCocoa::HandleKeyboardEvent implementation. | |
118 bool BrowserViewPlatform::HandleKeyboardEvent( | |
119 const content::NativeWebKeyboardEvent& event, | |
120 Browser* browser) { | |
121 if (!ShouldHandleKeyboardEvent(event)) | |
122 return false; | |
123 | |
124 return HandleExtraKeyboardShortcut(event.os_event, browser); | |
125 } | |
OLD | NEW |