| 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 bool SystemHotkeyMap::ParseData(NSData* data) { |
| 51 system_hotkeys_.clear(); | 51 system_hotkeys_.clear(); |
| 52 | 52 |
| 53 if (!data) |
| 54 return false; |
| 55 |
| 53 NSError* error = nil; | 56 NSError* error = nil; |
| 54 NSPropertyListFormat format; | 57 NSPropertyListFormat format; |
| 55 NSDictionary* dictionary = | 58 NSDictionary* dictionary = |
| 56 [NSPropertyListSerialization propertyListWithData:data | 59 [NSPropertyListSerialization propertyListWithData:data |
| 57 options:0 | 60 options:0 |
| 58 format:&format | 61 format:&format |
| 59 error:&error]; | 62 error:&error]; |
| 60 if (error) | 63 if (error) |
| 61 return false; | 64 return false; |
| 62 | 65 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 NSNumber* modifiers = [parameters objectAtIndex:2]; | 99 NSNumber* modifiers = [parameters objectAtIndex:2]; |
| 97 if (![modifiers isKindOfClass:[NSNumber class]]) | 100 if (![modifiers isKindOfClass:[NSNumber class]]) |
| 98 continue; | 101 continue; |
| 99 | 102 |
| 100 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); | 103 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); |
| 101 } | 104 } |
| 102 | 105 |
| 103 return true; | 106 return true; |
| 104 } | 107 } |
| 105 | 108 |
| 106 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) { | 109 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const { |
| 107 std::vector<SystemHotkey>::iterator it; | 110 NSUInteger modifiers = |
| 111 NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask; |
| 112 return IsHotkeyReserved(event.keyCode, event.modifierFlags & modifiers); |
| 113 } |
| 114 |
| 115 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) const { |
| 116 std::vector<SystemHotkey>::const_iterator it; |
| 108 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { | 117 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { |
| 109 if (it->key_code == key_code && it->modifiers == modifiers) | 118 if (it->key_code == key_code && it->modifiers == modifiers) |
| 110 return true; | 119 return true; |
| 111 } | 120 } |
| 112 return false; | 121 return false; |
| 113 } | 122 } |
| 114 | 123 |
| 115 void SystemHotkeyMap::ReserveHotkey(int key_code, | 124 void SystemHotkeyMap::ReserveHotkey(int key_code, |
| 116 int modifiers, | 125 int modifiers, |
| 117 NSString* system_effect) { | 126 NSString* system_effect) { |
| 118 ReserveHotkey(key_code, modifiers); | 127 ReserveHotkey(key_code, modifiers); |
| 119 | 128 |
| 120 // If a hotkey exists for toggling through the windows of an application, then | 129 // If a hotkey exists for toggling through the windows of an application, then |
| 121 // adding shift to that hotkey toggles through the windows backwards. | 130 // adding shift to that hotkey toggles through the windows backwards. |
| 122 if ([system_effect isEqualToString:@"27"]) | 131 if ([system_effect isEqualToString:@"27"]) |
| 123 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); | 132 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); |
| 124 } | 133 } |
| 125 | 134 |
| 126 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { | 135 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { |
| 127 SystemHotkey hotkey; | 136 SystemHotkey hotkey; |
| 128 hotkey.key_code = key_code; | 137 hotkey.key_code = key_code; |
| 129 hotkey.modifiers = modifiers; | 138 hotkey.modifiers = modifiers; |
| 130 system_hotkeys_.push_back(hotkey); | 139 system_hotkeys_.push_back(hotkey); |
| 131 } | 140 } |
| 141 |
| 142 } // namespace content |
| OLD | NEW |