OLD | NEW |
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 "content/browser/cocoa/system_hotkey_map.h" | 5 #import "chrome/browser/ui/cocoa/system_hotkey_map.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
6 | 8 |
7 #pragma mark - NSDictionary Helper Functions | 9 #pragma mark - NSDictionary Helper Functions |
8 | 10 |
9 namespace { | 11 namespace { |
10 | 12 |
11 // All 4 following functions return nil if the object doesn't exist, or isn't of | 13 // All 4 following functions return nil if the object doesn't exist, or isn't of |
12 // the right class. | 14 // the right class. |
13 id ObjectForKey(NSDictionary* dict, NSString* key, Class aClass) { | 15 id ObjectForKey(NSDictionary* dict, NSString* key, Class aClass) { |
14 id object = [dict objectForKey:key]; | 16 id object = [dict objectForKey:key]; |
15 if (![object isKindOfClass:aClass]) | 17 if (![object isKindOfClass:aClass]) |
(...skipping 10 matching lines...) Expand all Loading... |
26 } | 28 } |
27 | 29 |
28 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) { | 30 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) { |
29 return ObjectForKey(dict, key, [NSNumber class]); | 31 return ObjectForKey(dict, key, [NSNumber class]); |
30 } | 32 } |
31 | 33 |
32 } // namespace | 34 } // namespace |
33 | 35 |
34 #pragma mark - SystemHotkey | 36 #pragma mark - SystemHotkey |
35 | 37 |
36 namespace content { | |
37 | |
38 struct SystemHotkey { | 38 struct SystemHotkey { |
39 int key_code; | 39 int key_code; |
40 int modifiers; | 40 int modifiers; |
41 }; | 41 }; |
42 | 42 |
43 #pragma mark - SystemHotkeyMap | 43 #pragma mark - SystemHotkeyMap |
44 | 44 |
45 SystemHotkeyMap::SystemHotkeyMap() { | 45 SystemHotkeyMap::SystemHotkeyMap() { |
46 } | 46 } |
47 SystemHotkeyMap::~SystemHotkeyMap() { | 47 SystemHotkeyMap::~SystemHotkeyMap() { |
48 } | 48 } |
49 | 49 |
50 NSDictionary* SystemHotkeyMap::DictionaryFromData(NSData* data) { | 50 bool SystemHotkeyMap::ParseData(NSData* data) { |
51 if (!data) | 51 system_hotkeys_.clear(); |
52 return nil; | |
53 | 52 |
54 NSError* error = nil; | 53 NSError* error = nil; |
55 NSPropertyListFormat format; | 54 NSPropertyListFormat format; |
56 NSDictionary* dictionary = | 55 NSDictionary* dictionary = |
57 [NSPropertyListSerialization propertyListWithData:data | 56 [NSPropertyListSerialization propertyListWithData:data |
58 options:0 | 57 options:0 |
59 format:&format | 58 format:&format |
60 error:&error]; | 59 error:&error]; |
| 60 if (error) |
| 61 return false; |
61 | 62 |
62 if (![dictionary isKindOfClass:[NSDictionary class]]) | 63 if (![dictionary isKindOfClass:[NSDictionary class]]) |
63 return nil; | |
64 | |
65 return dictionary; | |
66 } | |
67 | |
68 bool SystemHotkeyMap::ParseDictionary(NSDictionary* dictionary) { | |
69 system_hotkeys_.clear(); | |
70 | |
71 if (!dictionary) | |
72 return false; | 64 return false; |
73 | 65 |
74 NSDictionary* hotkey_dictionaries = | 66 NSDictionary* hotkey_dictionaries = |
75 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys"); | 67 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys"); |
76 if (!hotkey_dictionaries) | 68 if (!hotkey_dictionaries) |
77 return false; | 69 return false; |
78 | 70 |
79 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) { | 71 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) { |
80 if (![hotkey_system_effect isKindOfClass:[NSString class]]) | 72 if (![hotkey_system_effect isKindOfClass:[NSString class]]) |
81 continue; | 73 continue; |
(...skipping 22 matching lines...) Expand all Loading... |
104 NSNumber* modifiers = [parameters objectAtIndex:2]; | 96 NSNumber* modifiers = [parameters objectAtIndex:2]; |
105 if (![modifiers isKindOfClass:[NSNumber class]]) | 97 if (![modifiers isKindOfClass:[NSNumber class]]) |
106 continue; | 98 continue; |
107 | 99 |
108 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); | 100 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); |
109 } | 101 } |
110 | 102 |
111 return true; | 103 return true; |
112 } | 104 } |
113 | 105 |
114 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const { | 106 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) { |
115 NSUInteger modifiers = | 107 std::vector<SystemHotkey>::iterator it; |
116 NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask; | |
117 return IsHotkeyReserved(event.keyCode, event.modifierFlags & modifiers); | |
118 } | |
119 | |
120 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) const { | |
121 std::vector<SystemHotkey>::const_iterator it; | |
122 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { | 108 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { |
123 if (it->key_code == key_code && it->modifiers == modifiers) | 109 if (it->key_code == key_code && it->modifiers == modifiers) |
124 return true; | 110 return true; |
125 } | 111 } |
126 return false; | 112 return false; |
127 } | 113 } |
128 | 114 |
129 void SystemHotkeyMap::ReserveHotkey(int key_code, | 115 void SystemHotkeyMap::ReserveHotkey(int key_code, |
130 int modifiers, | 116 int modifiers, |
131 NSString* system_effect) { | 117 NSString* system_effect) { |
132 ReserveHotkey(key_code, modifiers); | 118 ReserveHotkey(key_code, modifiers); |
133 | 119 |
134 // If a hotkey exists for toggling through the windows of an application, then | 120 // If a hotkey exists for toggling through the windows of an application, then |
135 // adding shift to that hotkey toggles through the windows backwards. | 121 // adding shift to that hotkey toggles through the windows backwards. |
136 if ([system_effect isEqualToString:@"27"]) | 122 if ([system_effect isEqualToString:@"27"]) |
137 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); | 123 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); |
138 } | 124 } |
139 | 125 |
140 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { | 126 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { |
141 SystemHotkey hotkey; | 127 SystemHotkey hotkey; |
142 hotkey.key_code = key_code; | 128 hotkey.key_code = key_code; |
143 hotkey.modifiers = modifiers; | 129 hotkey.modifiers = modifiers; |
144 system_hotkeys_.push_back(hotkey); | 130 system_hotkeys_.push_back(hotkey); |
145 } | 131 } |
146 | |
147 } // namespace content | |
OLD | NEW |