| 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 "chrome/browser/ui/cocoa/system_hotkey_map.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | |
| 8 | |
| 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; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 43 #pragma mark - SystemHotkeyMap | 41 #pragma mark - SystemHotkeyMap |
| 44 | 42 |
| 45 SystemHotkeyMap::SystemHotkeyMap() { | 43 SystemHotkeyMap::SystemHotkeyMap() { |
| 46 } | 44 } |
| 47 SystemHotkeyMap::~SystemHotkeyMap() { | 45 SystemHotkeyMap::~SystemHotkeyMap() { |
| 48 } | 46 } |
| 49 | 47 |
| 50 bool SystemHotkeyMap::ParseData(NSData* data) { | 48 bool SystemHotkeyMap::ParseData(NSData* data) { |
| 51 system_hotkeys_.clear(); | 49 system_hotkeys_.clear(); |
| 52 | 50 |
| 51 if (!data) |
| 52 return false; |
| 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 if (error) |
| 61 return false; | 62 return false; |
| 62 | 63 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 NSNumber* modifiers = [parameters objectAtIndex:2]; | 97 NSNumber* modifiers = [parameters objectAtIndex:2]; |
| 97 if (![modifiers isKindOfClass:[NSNumber class]]) | 98 if (![modifiers isKindOfClass:[NSNumber class]]) |
| 98 continue; | 99 continue; |
| 99 | 100 |
| 100 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); | 101 ReserveHotkey(key_code.intValue, modifiers.intValue, hotkey_system_effect); |
| 101 } | 102 } |
| 102 | 103 |
| 103 return true; | 104 return true; |
| 104 } | 105 } |
| 105 | 106 |
| 106 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) { | 107 bool SystemHotkeyMap::IsEventReserved(NSEvent* event) const { |
| 107 std::vector<SystemHotkey>::iterator it; | 108 NSUInteger modifiers = |
| 109 NSShiftKeyMask | NSControlKeyMask | NSCommandKeyMask | NSAlternateKeyMask; |
| 110 return IsHotkeyReserved(event.keyCode, event.modifierFlags & modifiers); |
| 111 } |
| 112 |
| 113 bool SystemHotkeyMap::IsHotkeyReserved(int key_code, int modifiers) const { |
| 114 std::vector<SystemHotkey>::const_iterator it; |
| 108 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { | 115 for (it = system_hotkeys_.begin(); it != system_hotkeys_.end(); ++it) { |
| 109 if (it->key_code == key_code && it->modifiers == modifiers) | 116 if (it->key_code == key_code && it->modifiers == modifiers) |
| 110 return true; | 117 return true; |
| 111 } | 118 } |
| 112 return false; | 119 return false; |
| 113 } | 120 } |
| 114 | 121 |
| 115 void SystemHotkeyMap::ReserveHotkey(int key_code, | 122 void SystemHotkeyMap::ReserveHotkey(int key_code, |
| 116 int modifiers, | 123 int modifiers, |
| 117 NSString* system_effect) { | 124 NSString* system_effect) { |
| 118 ReserveHotkey(key_code, modifiers); | 125 ReserveHotkey(key_code, modifiers); |
| 119 | 126 |
| 120 // If a hotkey exists for toggling through the windows of an application, then | 127 // If a hotkey exists for toggling through the windows of an application, then |
| 121 // adding shift to that hotkey toggles through the windows backwards. | 128 // adding shift to that hotkey toggles through the windows backwards. |
| 122 if ([system_effect isEqualToString:@"27"]) | 129 if ([system_effect isEqualToString:@"27"]) |
| 123 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); | 130 ReserveHotkey(key_code, modifiers | NSShiftKeyMask); |
| 124 } | 131 } |
| 125 | 132 |
| 126 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { | 133 void SystemHotkeyMap::ReserveHotkey(int key_code, int modifiers) { |
| 127 SystemHotkey hotkey; | 134 SystemHotkey hotkey; |
| 128 hotkey.key_code = key_code; | 135 hotkey.key_code = key_code; |
| 129 hotkey.modifiers = modifiers; | 136 hotkey.modifiers = modifiers; |
| 130 system_hotkeys_.push_back(hotkey); | 137 system_hotkeys_.push_back(hotkey); |
| 131 } | 138 } |
| OLD | NEW |