| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "chrome/browser/ui/cocoa/system_hotkey_map.h" | |
| 6 | |
| 7 #import <Cocoa/Cocoa.h> | |
| 8 | |
| 9 #pragma mark - NSDictionary Helper Functions | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // All 4 following functions return nil if the object doesn't exist, or isn't of | |
| 14 // the right class. | |
| 15 id ObjectForKey(NSDictionary* dict, NSString* key, Class aClass) { | |
| 16 id object = [dict objectForKey:key]; | |
| 17 if (![object isKindOfClass:aClass]) | |
| 18 return nil; | |
| 19 return object; | |
| 20 } | |
| 21 | |
| 22 NSDictionary* DictionaryForKey(NSDictionary* dict, NSString* key) { | |
| 23 return ObjectForKey(dict, key, [NSDictionary class]); | |
| 24 } | |
| 25 | |
| 26 NSArray* ArrayForKey(NSDictionary* dict, NSString* key) { | |
| 27 return ObjectForKey(dict, key, [NSArray class]); | |
| 28 } | |
| 29 | |
| 30 NSNumber* NumberForKey(NSDictionary* dict, NSString* key) { | |
| 31 return ObjectForKey(dict, key, [NSNumber class]); | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 #pragma mark - SystemHotkey | |
| 37 | |
| 38 struct SystemHotkey { | |
| 39 int key_code; | |
| 40 int modifiers; | |
| 41 }; | |
| 42 | |
| 43 #pragma mark - SystemHotkeyMap | |
| 44 | |
| 45 SystemHotkeyMap::SystemHotkeyMap() { | |
| 46 } | |
| 47 SystemHotkeyMap::~SystemHotkeyMap() { | |
| 48 } | |
| 49 | |
| 50 bool SystemHotkeyMap::ParseData(NSData* data) { | |
| 51 system_hotkeys_.clear(); | |
| 52 | |
| 53 NSError* error = nil; | |
| 54 NSPropertyListFormat format; | |
| 55 NSDictionary* dictionary = | |
| 56 [NSPropertyListSerialization propertyListWithData:data | |
| 57 options:0 | |
| 58 format:&format | |
| 59 error:&error]; | |
| 60 if (error) | |
| 61 return false; | |
| 62 | |
| 63 if (![dictionary isKindOfClass:[NSDictionary class]]) | |
| 64 return false; | |
| 65 | |
| 66 NSDictionary* hotkey_dictionaries = | |
| 67 DictionaryForKey(dictionary, @"AppleSymbolicHotKeys"); | |
| 68 if (!hotkey_dictionaries) | |
| 69 return false; | |
| 70 | |
| 71 for (NSString* hotkey_system_effect in [hotkey_dictionaries allKeys]) { | |
| 72 if (![hotkey_system_effect isKindOfClass:[NSString class]]) | |
| 73 continue; | |
| 74 | |
| 75 NSDictionary* hotkey_dictionary = | |
| 76 [hotkey_dictionaries objectForKey:hotkey_system_effect]; | |
| 77 if (![hotkey_dictionary isKindOfClass:[NSDictionary class]]) | |
| 78 continue; | |
| 79 | |
| 80 NSNumber* enabled = NumberForKey(hotkey_dictionary, @"enabled"); | |
| 81 if (!enabled || enabled.boolValue == NO) | |
| 82 continue; | |
| 83 | |
| 84 NSDictionary* value = DictionaryForKey(hotkey_dictionary, @"value"); | |
| 85 if (!value) | |
| 86 continue; | |
| 87 | |
| 88 NSArray* parameters = ArrayForKey(value, @"parameters"); | |
| 89 if (!parameters || [parameters count] != 3) | |
| 90 continue; | |
| 91 | |
| 92 NSNumber* key_code = [parameters objectAtIndex:1]; | |
| 93 if (![key_code isKindOfClass:[NSNumber class]]) | |
| 94 continue; | |
| 95 | |
| 96 NSNumber* modifiers = [parameters objectAtIndex:2]; | |
| 97 if (![modifiers isKindOfClass:[NSNumber class]]) | |
| 98 continue; | |
| 99 | |
| 100 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); | |
| 101 } | |
| 102 | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) { | |
| 107 std::vector<SystemHotkey>::iterator it; | |
| 108 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { | |
| 109 if (it->key_code == key_code && it->modifiers == modifiers) | |
| 110 return true; | |
| 111 } | |
| 112 return false; | |
| 113 } | |
| 114 | |
| 115 void SystemHotkeyMap::ReserveHotkey(int key_code, | |
| 116 int modifiers, | |
| 117 NSString* system_effect) { | |
| 118 ReserveHotkey(key_code, modifiers); | |
| 119 | |
| 120 // If a hotkey exists for toggling through the windows of an application, then | |
| 121 // adding shift to that hotkey toggles through the windows backwards. | |
| 122 if ([system_effect isEqualToString:@"27"]) | |
| 123 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); | |
| 124 } | |
| 125 | |
| 126 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { | |
| 127 SystemHotkey hotkey; | |
| 128 hotkey.key_code = key_code; | |
| 129 hotkey.modifiers = modifiers; | |
| 130 system_hotkeys_.push_back(hotkey); | |
| 131 } | |
| OLD | NEW |