OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 #include "chrome/browser/profiles/profile.h" | |
6 #include "chrome/browser/ui/cocoa/accelerators_cocoa.h" | |
7 #import "chrome/browser/ui/cocoa/browser_window_utils.h" | |
8 #include "content/public/browser/native_web_keyboard_event.h" | |
9 #include "ui/base/accelerators/accelerator.h" | |
10 #import "ui/base/accelerators/platform_accelerator_cocoa.h" | |
11 #import "ui/base/keycodes/keyboard_code_conversion_mac.h" | |
12 | |
13 namespace chrome { | |
14 | |
15 bool IsChromeAccelerator(const ui::Accelerator& accelerator, Profile* profile) { | |
16 // The |accelerator| passed in contains a Windows key code but no platform | |
17 // accelerator info. The Accelerator list is the opposite: It has accelerators | |
18 // that have key_code() == VKEY_UNKNOWN but they contain a platform | |
19 // accelerator. We find common ground by converting the passed in Windows key | |
20 // code to a character and use that when comparing against the Accelerator | |
21 // list. | |
22 unichar character; | |
23 unichar characterIgnoringModifiers; | |
24 ui::MacKeyCodeForWindowsKeyCode(accelerator.key_code(), | |
25 0, | |
26 &character, | |
27 &characterIgnoringModifiers); | |
28 NSString* characters = | |
29 [[[NSString alloc] initWithCharacters:&character length:1] autorelease]; | |
30 | |
31 NSUInteger modifiers = | |
32 (accelerator.IsCtrlDown() ? NSControlKeyMask : 0) | | |
33 (accelerator.IsCmdDown() ? NSCommandKeyMask : 0) | | |
34 (accelerator.IsAltDown() ? NSAlternateKeyMask : 0) | | |
35 (accelerator.IsShiftDown() ? NSShiftKeyMask : 0); | |
36 | |
37 NSEvent* event = [NSEvent keyEventWithType:NSKeyDown | |
38 location:NSZeroPoint | |
39 modifierFlags:modifiers | |
40 timestamp:0 | |
41 windowNumber:0 | |
42 context:nil | |
43 characters:characters | |
44 charactersIgnoringModifiers:characters | |
45 isARepeat:NO | |
46 keyCode:accelerator.key_code()]; | |
47 | |
48 content::NativeWebKeyboardEvent keyboard_event(event); | |
49 int id = [BrowserWindowUtils getCommandId:keyboard_event]; | |
50 if (id != -1) | |
51 return true; | |
52 | |
53 AcceleratorsCocoa* accelerators = AcceleratorsCocoa::GetInstance(); | |
54 for (AcceleratorsCocoa::const_iterator iter = accelerators->begin(); | |
55 iter != accelerators->end(); ++iter) { | |
Nico
2013/04/16 21:44:54
I don't know if the secondary shortcuts should win
Finnur
2013/04/17 14:28:07
Good point. We should probably be consistent here.
| |
56 const ui::PlatformAcceleratorCocoa* platform_accelerator = | |
57 static_cast<const ui::PlatformAcceleratorCocoa*>( | |
58 iter->second.platform_accelerator()); | |
59 if ([characters isEqualToString:platform_accelerator->characters()] && | |
60 modifiers == platform_accelerator->modifier_mask()) | |
61 return true; | |
62 } | |
63 | |
64 return false; | |
65 } | |
66 | |
67 } // namespace chrome | |
OLD | NEW |