Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(502)

Side by Side Diff: content/browser/cocoa/system_hotkey_map.mm

Issue 370293004: mac: Load the system hotkeys after launch. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments from rsesek, round 3. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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;
Robert Sesek 2014/07/14 19:37:56 It occurs to me that doing a linear scan of this v
erikchen 2014/07/14 19:54:14 When I wrote this, I deemed the minor performance
Robert Sesek 2014/07/14 19:59:45 What do you mean another container? You could just
erikchen 2014/07/14 20:13:33 Yes, I could replace the vector with a map and a c
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698