| 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 "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]) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 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 |
| 34 } // namespace | 32 } // namespace |
| 35 | 33 |
| 36 #pragma mark - SystemHotkey | 34 #pragma mark - SystemHotkey |
| 37 | 35 |
| 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 bool SystemHotkeyMap::ParseData(NSData* data) { | 50 NSDictionary* SystemHotkeyMap::DictionaryFromData(NSData* data) { |
| 51 system_hotkeys_.clear(); | 51 if (!data) |
| 52 return nil; |
| 52 | 53 |
| 53 NSError* error = nil; | 54 NSError* error = nil; |
| 54 NSPropertyListFormat format; | 55 NSPropertyListFormat format; |
| 55 NSDictionary* dictionary = | 56 NSDictionary* dictionary = |
| 56 [NSPropertyListSerialization propertyListWithData:data | 57 [NSPropertyListSerialization propertyListWithData:data |
| 57 options:0 | 58 options:0 |
| 58 format:&format | 59 format:&format |
| 59 error:&error]; | 60 error:&error]; |
| 60 if (error) | |
| 61 return false; | |
| 62 | 61 |
| 63 if (![dictionary isKindOfClass:[NSDictionary class]]) | 62 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) |
| 64 return false; | 72 return false; |
| 65 | 73 |
| 66 NSDictionary* hotkey_dictionaries = | 74 NSDictionary* hotkey_dictionaries = |
| 67 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys"); | 75 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys"); |
| 68 if (!hotkey_dictionaries) | 76 if (!hotkey_dictionaries) |
| 69 return false; | 77 return false; |
| 70 | 78 |
| 71 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) { | 79 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) { |
| 72 if (![hotkey_system_effect isKindOfClass:[NSString class]]) | 80 if (![hotkey_system_effect isKindOfClass:[NSString class]]) |
| 73 continue; | 81 continue; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 96 NSNumber* modifiers = [parameters objectAtIndex:2]; | 104 NSNumber* modifiers = [parameters objectAtIndex:2]; |
| 97 if (![modifiers isKindOfClass:[NSNumber class]]) | 105 if (![modifiers isKindOfClass:[NSNumber class]]) |
| 98 continue; | 106 continue; |
| 99 | 107 |
| 100 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); | 108 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); |
| 101 } | 109 } |
| 102 | 110 |
| 103 return true; | 111 return true; |
| 104 } | 112 } |
| 105 | 113 |
| 106 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) { | 114 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const { |
| 107 std::vector<SystemHotkey>::iterator it; | 115 NSUInteger modifiers = |
| 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; |
| 108 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { | 122 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { |
| 109 if (it->key_code == key_code && it->modifiers == modifiers) | 123 if (it->key_code == key_code && it->modifiers == modifiers) |
| 110 return true; | 124 return true; |
| 111 } | 125 } |
| 112 return false; | 126 return false; |
| 113 } | 127 } |
| 114 | 128 |
| 115 void SystemHotkeyMap::ReserveHotkey(int key_code, | 129 void SystemHotkeyMap::ReserveHotkey(int key_code, |
| 116 int modifiers, | 130 int modifiers, |
| 117 NSString* system_effect) { | 131 NSString* system_effect) { |
| 118 ReserveHotkey(key_code, modifiers); | 132 ReserveHotkey(key_code, modifiers); |
| 119 | 133 |
| 120 // If a hotkey exists for toggling through the windows of an application, then | 134 // If a hotkey exists for toggling through the windows of an application, then |
| 121 // adding shift to that hotkey toggles through the windows backwards. | 135 // adding shift to that hotkey toggles through the windows backwards. |
| 122 if ([system_effect isEqualToString:@"27"]) | 136 if ([system_effect isEqualToString:@"27"]) |
| 123 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); | 137 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); |
| 124 } | 138 } |
| 125 | 139 |
| 126 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { | 140 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { |
| 127 SystemHotkey hotkey; | 141 SystemHotkey hotkey; |
| 128 hotkey.key_code = key_code; | 142 hotkey.key_code = key_code; |
| 129 hotkey.modifiers = modifiers; | 143 hotkey.modifiers = modifiers; |
| 130 system_hotkeys_.push_back(hotkey); | 144 system_hotkeys_.push_back(hotkey); |
| 131 } | 145 } |
| 146 |
| 147 } // namespace content |
| OLD | NEW |