Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Side by Side Diff: content/browser/cocoa/system_hotkey_map.mm

Issue 408973002: mac: Load the system hotkeys after launch. (reland) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from rsesek. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "chrome/browser/ui/cocoa/system_hotkey_map.h" 5 #import "content/browser/cocoa/system_hotkey_map.h"
6
7 #import <Cocoa/Cocoa.h>
8 6
9 #pragma mark - NSDictionary Helper Functions 7 #pragma mark - NSDictionary Helper Functions
10 8
11 namespace { 9 namespace {
12 10
13 // All 4 following functions return nil if the object doesn't exist, or isn't of 11 // All 4 following functions return nil if the object doesn't exist, or isn't of
14 // the right class. 12 // the right class.
15 id ObjectForKey(NSDictionary* dict, NSString* key, Class aClass) { 13 id ObjectForKey(NSDictionary* dict, NSString* key, Class aClass) {
16 id object = [dict objectForKey:key]; 14 id object = [dict objectForKey:key];
17 if (![object isKindOfClass:aClass]) 15 if (![object isKindOfClass:aClass])
18 return nil; 16 return nil;
19 return object; 17 return object;
20 } 18 }
21 19
22 NSDictionary* DictionaryForKey(NSDictionary* dict, NSString* key) { 20 NSDictionary* DictionaryForKey(NSDictionary* dict, NSString* key) {
23 return ObjectForKey(dict, key, [NSDictionary class]); 21 return ObjectForKey(dict, key, [NSDictionary class]);
24 } 22 }
25 23
26 NSArray* ArrayForKey(NSDictionary* dict, NSString* key) { 24 NSArray* ArrayForKey(NSDictionary* dict, NSString* key) {
27 return ObjectForKey(dict, key, [NSArray class]); 25 return ObjectForKey(dict, key, [NSArray class]);
28 } 26 }
29 27
30 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) { 28 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) {
31 return ObjectForKey(dict, key, [NSNumber class]); 29 return ObjectForKey(dict, key, [NSNumber class]);
32 } 30 }
33 31
32 NSString* StringForKey(NSDictionary* dict, NSString* key) {
33 return ObjectForKey(dict, key, [NSString class]);
34 }
35
34 } // namespace 36 } // namespace
35 37
36 #pragma mark - SystemHotkey 38 #pragma mark - SystemHotkey
37 39
40 namespace content {
41
38 struct SystemHotkey { 42 struct SystemHotkey {
39 int key_code; 43 unsigned short key_code;
40 int modifiers; 44 NSUInteger modifiers;
41 }; 45 };
42 46
43 #pragma mark - SystemHotkeyMap 47 #pragma mark - SystemHotkeyMap
44 48
45 SystemHotkeyMap::SystemHotkeyMap() { 49 SystemHotkeyMap::SystemHotkeyMap() {
46 } 50 }
47 SystemHotkeyMap::~SystemHotkeyMap() { 51 SystemHotkeyMap::~SystemHotkeyMap() {
48 } 52 }
49 53
50 bool SystemHotkeyMap::ParseData(NSData* data) { 54 NSDictionary* SystemHotkeyMap::DictionaryFromData(NSData* data) {
51 system_hotkeys_.clear(); 55 if (!data)
56 return nil;
52 57
53 NSError* error = nil; 58 NSError* error = nil;
54 NSPropertyListFormat format; 59 NSPropertyListFormat format;
55 NSDictionary* dictionary = 60 NSDictionary* dictionary =
56 [NSPropertyListSerialization propertyListWithData:data 61 [NSPropertyListSerialization propertyListWithData:data
57 options:0 62 options:0
58 format:&format 63 format:&format
59 error:&error]; 64 error:&error];
60 if (error)
61 return false;
62 65
63 if (![dictionary isKindOfClass:[NSDictionary class]]) 66 if (![dictionary isKindOfClass:[NSDictionary class]])
67 return nil;
68
69 return dictionary;
70 }
71
72 bool SystemHotkeyMap::ParseDictionary(NSDictionary* dictionary) {
73 system_hotkeys_.clear();
74
75 if (!dictionary)
64 return false; 76 return false;
65 77
66 NSDictionary* hotkey_dictionaries = 78 NSDictionary* hotkey_dictionaries =
67 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys"); 79 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys");
68 if (!hotkey_dictionaries) 80 if (!hotkey_dictionaries)
69 return false; 81 return false;
70 82
71 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) { 83 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) {
72 if (![hotkey_system_effect isKindOfClass:[NSString class]]) 84 if (![hotkey_system_effect isKindOfClass:[NSString class]])
73 continue; 85 continue;
74 86
75 NSDictionary* hotkey_dictionary = 87 NSDictionary* hotkey_dictionary =
76 [hotkey_dictionaries objectForKey:hotkey_system_effect]; 88 [hotkey_dictionaries objectForKey:hotkey_system_effect];
77 if (![hotkey_dictionary isKindOfClass:[NSDictionary class]]) 89 if (![hotkey_dictionary isKindOfClass:[NSDictionary class]])
78 continue; 90 continue;
79 91
80 NSNumber* enabled = NumberForKey(hotkey_dictionary, @"enabled"); 92 NSNumber* enabled = NumberForKey(hotkey_dictionary, @"enabled");
81 if (!enabled || enabled.boolValue == NO) 93 if (!enabled || enabled.boolValue == NO)
82 continue; 94 continue;
83 95
84 NSDictionary* value = DictionaryForKey(hotkey_dictionary, @"value"); 96 NSDictionary* value = DictionaryForKey(hotkey_dictionary, @"value");
85 if (!value) 97 if (!value)
86 continue; 98 continue;
87 99
100 NSString* type = StringForKey(value, @"type");
101 if (!type || ![type isEqualToString:@"standard"])
102 continue;
103
88 NSArray* parameters = ArrayForKey(value, @"parameters"); 104 NSArray* parameters = ArrayForKey(value, @"parameters");
89 if (!parameters || [parameters count] != 3) 105 if (!parameters || [parameters count] != 3)
90 continue; 106 continue;
91 107
92 NSNumber* key_code = [parameters objectAtIndex:1]; 108 NSNumber* key_code = [parameters objectAtIndex:1];
93 if (![key_code isKindOfClass:[NSNumber class]]) 109 if (![key_code isKindOfClass:[NSNumber class]])
94 continue; 110 continue;
95 111
96 NSNumber* modifiers = [parameters objectAtIndex:2]; 112 NSNumber* modifiers = [parameters objectAtIndex:2];
97 if (![modifiers isKindOfClass:[NSNumber class]]) 113 if (![modifiers isKindOfClass:[NSNumber class]])
98 continue; 114 continue;
99 115
100 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); 116 ReserveHotkey(key_code.unsignedShortValue,
117 modifiers.unsignedIntegerValue,
118 hotkey_system_effect);
101 } 119 }
102 120
103 return true; 121 return true;
104 } 122 }
105 123
106 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) { 124 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const {
107 std::vector<SystemHotkey>::iterator it; 125 NSUInteger modifiers =
126 NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask;
127 return IsHotkeyReserved(event.keyCode, event.modifierFlags & modifiers);
128 }
129
130 bool SystemHotkeyMap::IsHotkeyReserved(unsigned short key_code,
131 NSUInteger modifiers) const {
132 std::vector<SystemHotkey>::const_iterator it;
108 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { 133 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) {
109 if (it->key_code == key_code && it->modifiers == modifiers) 134 if (it->key_code == key_code && it->modifiers == modifiers)
110 return true; 135 return true;
111 } 136 }
112 return false; 137 return false;
113 } 138 }
114 139
115 void SystemHotkeyMap::ReserveHotkey(int key_code, 140 void SystemHotkeyMap::ReserveHotkey(unsigned short key_code,
116 int modifiers, 141 NSUInteger modifiers,
117 NSString* system_effect) { 142 NSString* system_effect) {
118 ReserveHotkey(key_code, modifiers); 143 ReserveHotkey(key_code, modifiers);
119 144
120 // If a hotkey exists for toggling through the windows of an application, then 145 // If a hotkey exists for toggling through the windows of an application, then
121 // adding shift to that hotkey toggles through the windows backwards. 146 // adding shift to that hotkey toggles through the windows backwards.
122 if ([system_effect isEqualToString:@"27"]) 147 if ([system_effect isEqualToString:@"27"])
123 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); 148 ReserveHotkey(key_code, modifiers | NSShiftKeyMask);
124 } 149 }
125 150
126 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { 151 void SystemHotkeyMap::ReserveHotkey(unsigned short key_code,
152 NSUInteger modifiers) {
153 // Hotkeys require at least one of control, command, or alternate keys to be
154 // down.
155 NSUInteger required_modifiers =
156 NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask;
157 if ((modifiers & required_modifiers) == 0)
158 return;
159
127 SystemHotkey hotkey; 160 SystemHotkey hotkey;
128 hotkey.key_code = key_code; 161 hotkey.key_code = key_code;
129 hotkey.modifiers = modifiers; 162 hotkey.modifiers = modifiers;
130 system_hotkeys_.push_back(hotkey); 163 system_hotkeys_.push_back(hotkey);
131 } 164 }
165
166 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698