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 | |
5 /** | 4 /** |
6 * @constructor | 5 * @unrestricted |
7 * @param {!WebInspector.ActionRegistry} actionRegistry | |
8 * @param {!Document} document | |
9 */ | 6 */ |
10 WebInspector.ShortcutRegistry = function(actionRegistry, document) | 7 WebInspector.ShortcutRegistry = class { |
11 { | 8 /** |
| 9 * @param {!WebInspector.ActionRegistry} actionRegistry |
| 10 * @param {!Document} document |
| 11 */ |
| 12 constructor(actionRegistry, document) { |
12 this._actionRegistry = actionRegistry; | 13 this._actionRegistry = actionRegistry; |
13 /** @type {!Multimap.<string, string>} */ | 14 /** @type {!Multimap.<string, string>} */ |
14 this._defaultKeyToActions = new Multimap(); | 15 this._defaultKeyToActions = new Multimap(); |
15 /** @type {!Multimap.<string, !WebInspector.KeyboardShortcut.Descriptor>} */ | 16 /** @type {!Multimap.<string, !WebInspector.KeyboardShortcut.Descriptor>} */ |
16 this._defaultActionToShortcut = new Multimap(); | 17 this._defaultActionToShortcut = new Multimap(); |
17 this._registerBindings(document); | 18 this._registerBindings(document); |
| 19 } |
| 20 |
| 21 /** |
| 22 * @param {number} key |
| 23 * @return {!Array.<!WebInspector.Action>} |
| 24 */ |
| 25 _applicableActions(key) { |
| 26 return this._actionRegistry.applicableActions(this._defaultActionsForKey(key
).valuesArray(), WebInspector.context); |
| 27 } |
| 28 |
| 29 /** |
| 30 * @param {number} key |
| 31 * @return {!Set.<string>} |
| 32 */ |
| 33 _defaultActionsForKey(key) { |
| 34 return this._defaultKeyToActions.get(String(key)); |
| 35 } |
| 36 |
| 37 /** |
| 38 * @param {string} actionId |
| 39 * @return {!Array.<!WebInspector.KeyboardShortcut.Descriptor>} |
| 40 */ |
| 41 shortcutDescriptorsForAction(actionId) { |
| 42 return this._defaultActionToShortcut.get(actionId).valuesArray(); |
| 43 } |
| 44 |
| 45 /** |
| 46 * @param {!Array.<string>} actionIds |
| 47 * @return {!Array.<number>} |
| 48 */ |
| 49 keysForActions(actionIds) { |
| 50 var result = []; |
| 51 for (var i = 0; i < actionIds.length; ++i) { |
| 52 var descriptors = this.shortcutDescriptorsForAction(actionIds[i]); |
| 53 for (var j = 0; j < descriptors.length; ++j) |
| 54 result.push(descriptors[j].key); |
| 55 } |
| 56 return result; |
| 57 } |
| 58 |
| 59 /** |
| 60 * @param {string} actionId |
| 61 * @return {string|undefined} |
| 62 */ |
| 63 shortcutTitleForAction(actionId) { |
| 64 var descriptors = this.shortcutDescriptorsForAction(actionId); |
| 65 if (descriptors.length) |
| 66 return descriptors[0].name; |
| 67 } |
| 68 |
| 69 /** |
| 70 * @param {!KeyboardEvent} event |
| 71 */ |
| 72 handleShortcut(event) { |
| 73 this.handleKey(WebInspector.KeyboardShortcut.makeKeyFromEvent(event), event.
key, event); |
| 74 } |
| 75 |
| 76 /** |
| 77 * @param {number} key |
| 78 * @param {string} domKey |
| 79 * @param {!KeyboardEvent=} event |
| 80 */ |
| 81 handleKey(key, domKey, event) { |
| 82 var keyModifiers = key >> 8; |
| 83 var actions = this._applicableActions(key); |
| 84 if (!actions.length) |
| 85 return; |
| 86 if (WebInspector.Dialog.hasInstance()) { |
| 87 if (event && !isPossiblyInputKey()) |
| 88 event.consume(true); |
| 89 return; |
| 90 } |
| 91 |
| 92 if (!isPossiblyInputKey()) { |
| 93 if (event) |
| 94 event.consume(true); |
| 95 processNextAction.call(this, false); |
| 96 } else { |
| 97 this._pendingActionTimer = setTimeout(processNextAction.bind(this, false),
0); |
| 98 } |
| 99 |
| 100 /** |
| 101 * @param {boolean} handled |
| 102 * @this {WebInspector.ShortcutRegistry} |
| 103 */ |
| 104 function processNextAction(handled) { |
| 105 delete this._pendingActionTimer; |
| 106 var action = actions.shift(); |
| 107 if (!action || handled) |
| 108 return; |
| 109 |
| 110 action.execute().then(processNextAction.bind(this)); |
| 111 } |
| 112 |
| 113 /** |
| 114 * @return {boolean} |
| 115 */ |
| 116 function isPossiblyInputKey() { |
| 117 if (!event || !WebInspector.isEditing() || /^F\d+|Control|Shift|Alt|Meta|E
scape|Win|U\+001B$/.test(domKey)) |
| 118 return false; |
| 119 |
| 120 if (!keyModifiers) |
| 121 return true; |
| 122 |
| 123 var modifiers = WebInspector.KeyboardShortcut.Modifiers; |
| 124 if ((keyModifiers & (modifiers.Ctrl | modifiers.Alt)) === (modifiers.Ctrl
| modifiers.Alt)) |
| 125 return WebInspector.isWin(); |
| 126 |
| 127 return !hasModifier(modifiers.Ctrl) && !hasModifier(modifiers.Alt) && !has
Modifier(modifiers.Meta); |
| 128 } |
| 129 |
| 130 /** |
| 131 * @param {number} mod |
| 132 * @return {boolean} |
| 133 */ |
| 134 function hasModifier(mod) { |
| 135 return !!(keyModifiers & mod); |
| 136 } |
| 137 } |
| 138 |
| 139 /** |
| 140 * @param {string} actionId |
| 141 * @param {string} shortcut |
| 142 */ |
| 143 registerShortcut(actionId, shortcut) { |
| 144 var descriptor = WebInspector.KeyboardShortcut.makeDescriptorFromBindingShor
tcut(shortcut); |
| 145 if (!descriptor) |
| 146 return; |
| 147 this._defaultActionToShortcut.set(actionId, descriptor); |
| 148 this._defaultKeyToActions.set(String(descriptor.key), actionId); |
| 149 } |
| 150 |
| 151 dismissPendingShortcutAction() { |
| 152 if (this._pendingActionTimer) { |
| 153 clearTimeout(this._pendingActionTimer); |
| 154 delete this._pendingActionTimer; |
| 155 } |
| 156 } |
| 157 |
| 158 /** |
| 159 * @param {!Document} document |
| 160 */ |
| 161 _registerBindings(document) { |
| 162 document.addEventListener('input', this.dismissPendingShortcutAction.bind(th
is), true); |
| 163 var extensions = self.runtime.extensions(WebInspector.ActionDelegate); |
| 164 extensions.forEach(registerExtension, this); |
| 165 |
| 166 /** |
| 167 * @param {!Runtime.Extension} extension |
| 168 * @this {WebInspector.ShortcutRegistry} |
| 169 */ |
| 170 function registerExtension(extension) { |
| 171 var descriptor = extension.descriptor(); |
| 172 var bindings = descriptor['bindings']; |
| 173 for (var i = 0; bindings && i < bindings.length; ++i) { |
| 174 if (!platformMatches(bindings[i].platform)) |
| 175 continue; |
| 176 var shortcuts = bindings[i]['shortcut'].split(/\s+/); |
| 177 shortcuts.forEach(this.registerShortcut.bind(this, descriptor['actionId'
])); |
| 178 } |
| 179 } |
| 180 |
| 181 /** |
| 182 * @param {string=} platformsString |
| 183 * @return {boolean} |
| 184 */ |
| 185 function platformMatches(platformsString) { |
| 186 if (!platformsString) |
| 187 return true; |
| 188 var platforms = platformsString.split(','); |
| 189 var isMatch = false; |
| 190 var currentPlatform = WebInspector.platform(); |
| 191 for (var i = 0; !isMatch && i < platforms.length; ++i) |
| 192 isMatch = platforms[i] === currentPlatform; |
| 193 return isMatch; |
| 194 } |
| 195 } |
18 }; | 196 }; |
19 | 197 |
20 WebInspector.ShortcutRegistry.prototype = { | |
21 /** | |
22 * @param {number} key | |
23 * @return {!Array.<!WebInspector.Action>} | |
24 */ | |
25 _applicableActions: function(key) | |
26 { | |
27 return this._actionRegistry.applicableActions(this._defaultActionsForKey
(key).valuesArray(), WebInspector.context); | |
28 }, | |
29 | |
30 /** | |
31 * @param {number} key | |
32 * @return {!Set.<string>} | |
33 */ | |
34 _defaultActionsForKey: function(key) | |
35 { | |
36 return this._defaultKeyToActions.get(String(key)); | |
37 }, | |
38 | |
39 /** | |
40 * @param {string} actionId | |
41 * @return {!Array.<!WebInspector.KeyboardShortcut.Descriptor>} | |
42 */ | |
43 shortcutDescriptorsForAction: function(actionId) | |
44 { | |
45 return this._defaultActionToShortcut.get(actionId).valuesArray(); | |
46 }, | |
47 | |
48 /** | |
49 * @param {!Array.<string>} actionIds | |
50 * @return {!Array.<number>} | |
51 */ | |
52 keysForActions: function(actionIds) | |
53 { | |
54 var result = []; | |
55 for (var i = 0; i < actionIds.length; ++i) { | |
56 var descriptors = this.shortcutDescriptorsForAction(actionIds[i]); | |
57 for (var j = 0; j < descriptors.length; ++j) | |
58 result.push(descriptors[j].key); | |
59 } | |
60 return result; | |
61 }, | |
62 | |
63 /** | |
64 * @param {string} actionId | |
65 * @return {string|undefined} | |
66 */ | |
67 shortcutTitleForAction: function(actionId) | |
68 { | |
69 var descriptors = this.shortcutDescriptorsForAction(actionId); | |
70 if (descriptors.length) | |
71 return descriptors[0].name; | |
72 }, | |
73 | |
74 /** | |
75 * @param {!KeyboardEvent} event | |
76 */ | |
77 handleShortcut: function(event) | |
78 { | |
79 this.handleKey(WebInspector.KeyboardShortcut.makeKeyFromEvent(event), ev
ent.key, event); | |
80 }, | |
81 | |
82 /** | |
83 * @param {number} key | |
84 * @param {string} domKey | |
85 * @param {!KeyboardEvent=} event | |
86 */ | |
87 handleKey: function(key, domKey, event) | |
88 { | |
89 var keyModifiers = key >> 8; | |
90 var actions = this._applicableActions(key); | |
91 if (!actions.length) | |
92 return; | |
93 if (WebInspector.Dialog.hasInstance()) { | |
94 if (event && !isPossiblyInputKey()) | |
95 event.consume(true); | |
96 return; | |
97 } | |
98 | |
99 if (!isPossiblyInputKey()) { | |
100 if (event) | |
101 event.consume(true); | |
102 processNextAction.call(this, false); | |
103 } else { | |
104 this._pendingActionTimer = setTimeout(processNextAction.bind(this, f
alse), 0); | |
105 } | |
106 | |
107 /** | |
108 * @param {boolean} handled | |
109 * @this {WebInspector.ShortcutRegistry} | |
110 */ | |
111 function processNextAction(handled) | |
112 { | |
113 delete this._pendingActionTimer; | |
114 var action = actions.shift(); | |
115 if (!action || handled) | |
116 return; | |
117 | |
118 action.execute().then(processNextAction.bind(this)); | |
119 } | |
120 | |
121 /** | |
122 * @return {boolean} | |
123 */ | |
124 function isPossiblyInputKey() | |
125 { | |
126 if (!event || !WebInspector.isEditing() || /^F\d+|Control|Shift|Alt|
Meta|Escape|Win|U\+001B$/.test(domKey)) | |
127 return false; | |
128 | |
129 if (!keyModifiers) | |
130 return true; | |
131 | |
132 var modifiers = WebInspector.KeyboardShortcut.Modifiers; | |
133 if ((keyModifiers & (modifiers.Ctrl | modifiers.Alt)) === (modifiers
.Ctrl | modifiers.Alt)) | |
134 return WebInspector.isWin(); | |
135 | |
136 return !hasModifier(modifiers.Ctrl) && !hasModifier(modifiers.Alt) &
& !hasModifier(modifiers.Meta); | |
137 } | |
138 | |
139 /** | |
140 * @param {number} mod | |
141 * @return {boolean} | |
142 */ | |
143 function hasModifier(mod) | |
144 { | |
145 return !!(keyModifiers & mod); | |
146 } | |
147 }, | |
148 | |
149 /** | |
150 * @param {string} actionId | |
151 * @param {string} shortcut | |
152 */ | |
153 registerShortcut: function(actionId, shortcut) | |
154 { | |
155 var descriptor = WebInspector.KeyboardShortcut.makeDescriptorFromBinding
Shortcut(shortcut); | |
156 if (!descriptor) | |
157 return; | |
158 this._defaultActionToShortcut.set(actionId, descriptor); | |
159 this._defaultKeyToActions.set(String(descriptor.key), actionId); | |
160 }, | |
161 | |
162 dismissPendingShortcutAction: function() | |
163 { | |
164 if (this._pendingActionTimer) { | |
165 clearTimeout(this._pendingActionTimer); | |
166 delete this._pendingActionTimer; | |
167 } | |
168 }, | |
169 | |
170 /** | |
171 * @param {!Document} document | |
172 */ | |
173 _registerBindings: function(document) | |
174 { | |
175 document.addEventListener("input", this.dismissPendingShortcutAction.bin
d(this), true); | |
176 var extensions = self.runtime.extensions(WebInspector.ActionDelegate); | |
177 extensions.forEach(registerExtension, this); | |
178 | |
179 /** | |
180 * @param {!Runtime.Extension} extension | |
181 * @this {WebInspector.ShortcutRegistry} | |
182 */ | |
183 function registerExtension(extension) | |
184 { | |
185 var descriptor = extension.descriptor(); | |
186 var bindings = descriptor["bindings"]; | |
187 for (var i = 0; bindings && i < bindings.length; ++i) { | |
188 if (!platformMatches(bindings[i].platform)) | |
189 continue; | |
190 var shortcuts = bindings[i]["shortcut"].split(/\s+/); | |
191 shortcuts.forEach(this.registerShortcut.bind(this, descriptor["a
ctionId"])); | |
192 } | |
193 } | |
194 | |
195 /** | |
196 * @param {string=} platformsString | |
197 * @return {boolean} | |
198 */ | |
199 function platformMatches(platformsString) | |
200 { | |
201 if (!platformsString) | |
202 return true; | |
203 var platforms = platformsString.split(","); | |
204 var isMatch = false; | |
205 var currentPlatform = WebInspector.platform(); | |
206 for (var i = 0; !isMatch && i < platforms.length; ++i) | |
207 isMatch = platforms[i] === currentPlatform; | |
208 return isMatch; | |
209 } | |
210 } | |
211 }; | |
212 | |
213 /** | 198 /** |
214 * @constructor | 199 * @unrestricted |
215 */ | 200 */ |
216 WebInspector.ShortcutRegistry.ForwardedShortcut = function() | 201 WebInspector.ShortcutRegistry.ForwardedShortcut = class {}; |
217 { | |
218 }; | |
219 | 202 |
220 WebInspector.ShortcutRegistry.ForwardedShortcut.instance = new WebInspector.Shor
tcutRegistry.ForwardedShortcut(); | 203 WebInspector.ShortcutRegistry.ForwardedShortcut.instance = new WebInspector.Shor
tcutRegistry.ForwardedShortcut(); |
221 | 204 |
222 /** @type {!WebInspector.ShortcutRegistry} */ | 205 /** @type {!WebInspector.ShortcutRegistry} */ |
223 WebInspector.shortcutRegistry; | 206 WebInspector.shortcutRegistry; |
OLD | NEW |