Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 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. |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 4 | |
Tim van der Lippe | ee97fa3 | 2020-04-23 15:20:56 | [diff] [blame] | 5 | // @ts-nocheck |
| 6 | // TODO(crbug.com/1011811): Enable TypeScript compiler checks |
| 7 | |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 8 | import * as Host from '../host/host.js'; |
Tim van der Lippe | ee97fa3 | 2020-04-23 15:20:56 | [diff] [blame] | 9 | import * as Platform from '../platform/platform.js'; |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame] | 10 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 11 | import {Action} from './Action.js'; // eslint-disable-line no-unused-vars |
| 12 | import {ActionRegistry} from './ActionRegistry.js'; // eslint-disable-line no-unused-vars |
| 13 | import {Context} from './Context.js'; |
| 14 | import {Dialog} from './Dialog.js'; |
Jack Lynch | 94a9b0c | 2020-03-11 21:45:14 | [diff] [blame] | 15 | import {Descriptor, KeyboardShortcut, Modifiers, Type} from './KeyboardShortcut.js'; // eslint-disable-line no-unused-vars |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 16 | import {isEditing} from './UIUtils.js'; |
| 17 | |
Tim van der Lippe | 9c9fb12 | 2020-09-08 15:06:17 | [diff] [blame^] | 18 | /** @type {!ShortcutRegistry} */ |
| 19 | let shortcutRegistryInstance; |
Sigurd Schneider | 46da7db | 2020-05-20 13:45:11 | [diff] [blame] | 20 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 21 | export class ShortcutRegistry { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 22 | /** |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 23 | * @param {!ActionRegistry} actionRegistry |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 24 | */ |
Jack Lynch | 94a9b0c | 2020-03-11 21:45:14 | [diff] [blame] | 25 | constructor(actionRegistry) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 26 | this._actionRegistry = actionRegistry; |
Jack Lynch | 94a9b0c | 2020-03-11 21:45:14 | [diff] [blame] | 27 | /** @type {!Platform.Multimap.<number, !KeyboardShortcut>} */ |
| 28 | this._keyToShortcut = new Platform.Multimap(); |
| 29 | /** @type {!Platform.Multimap.<string, !KeyboardShortcut>} */ |
| 30 | this._actionToShortcut = new Platform.Multimap(); |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 31 | this._keyMap = new ShortcutTreeNode(0, 0); |
| 32 | /** @type {?ShortcutTreeNode} */ |
| 33 | this._activePrefixKey = null; |
| 34 | /** @type {?number} */ |
| 35 | this._activePrefixTimeout = null; |
| 36 | /** @type {?function():Promise<void>} */ |
| 37 | this._consumePrefix = null; |
Jack Lynch | 42297a7 | 2020-06-21 01:54:30 | [diff] [blame] | 38 | /** @type {!Set.<string>} */ |
| 39 | this._devToolsDefaultShortcutActions = new Set(); |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 40 | const keybindSetSetting = self.Common.settings.moduleSetting('activeKeybindSet'); |
| 41 | if (!Root.Runtime.experiments.isEnabled('customKeyboardShortcuts') && |
| 42 | keybindSetSetting.get() !== DefaultShortcutSetting) { |
| 43 | keybindSetSetting.set(DefaultShortcutSetting); |
| 44 | } |
Jack Lynch | 9de6dbb | 2020-06-04 18:22:12 | [diff] [blame] | 45 | keybindSetSetting.addChangeListener(event => { |
| 46 | Host.userMetrics.keybindSetSettingChanged(event.data); |
| 47 | this._registerBindings(); |
| 48 | }); |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 49 | |
Jack Lynch | 94a9b0c | 2020-03-11 21:45:14 | [diff] [blame] | 50 | this._registerBindings(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
Tim van der Lippe | 9c9fb12 | 2020-09-08 15:06:17 | [diff] [blame^] | 54 | * @param {{forceNew: ?boolean, actionRegistry: ?ActionRegistry}} opts |
| 55 | */ |
| 56 | static instance(opts = {forceNew: null, actionRegistry: null}) { |
| 57 | const {forceNew, actionRegistry} = opts; |
| 58 | if (!shortcutRegistryInstance || forceNew) { |
| 59 | if (!actionRegistry) { |
| 60 | throw new Error('Missing actionRegistry for shortcutRegistry'); |
| 61 | } |
| 62 | shortcutRegistryInstance = new ShortcutRegistry(actionRegistry); |
| 63 | } |
| 64 | |
| 65 | return shortcutRegistryInstance; |
| 66 | } |
| 67 | |
| 68 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 69 | * @param {number} key |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 70 | * @param {!Object.<string, function():Promise.<boolean>>=} handlers |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 71 | * @return {!Array.<!Action>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 72 | */ |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 73 | _applicableActions(key, handlers = {}) { |
| 74 | let actions = []; |
| 75 | const keyMap = this._activePrefixKey || this._keyMap; |
| 76 | const keyNode = keyMap.getNode(key); |
| 77 | if (keyNode) { |
| 78 | actions = keyNode.actions(); |
| 79 | } |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 80 | const applicableActions = this._actionRegistry.applicableActions(actions, Context.instance()); |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 81 | if (keyNode) { |
| 82 | for (const actionId of Object.keys(handlers)) { |
| 83 | if (keyNode.actions().indexOf(actionId) >= 0) { |
| 84 | const action = this._actionRegistry.action(actionId); |
| 85 | if (action) { |
| 86 | applicableActions.push(action); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | return applicableActions; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /** |
Jack Lynch | 575e9fb | 2020-03-26 22:20:51 | [diff] [blame] | 95 | * @param {string} action |
| 96 | * @return {!Array.<!KeyboardShortcut>} |
| 97 | */ |
| 98 | shortcutsForAction(action) { |
| 99 | return [...this._actionToShortcut.get(action)]; |
| 100 | } |
| 101 | |
| 102 | /** |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 103 | * @return {!Array<number>} |
| 104 | */ |
| 105 | globalShortcutKeys() { |
| 106 | const keys = []; |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 107 | for (const node of this._keyMap.chords().values()) { |
| 108 | const actions = node.actions(); |
Tim van der Lippe | d1a00aa | 2020-08-19 16:03:56 | [diff] [blame] | 109 | const applicableActions = this._actionRegistry.applicableActions(actions, Context.instance()); |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 110 | if (applicableActions.length || node.hasChords()) { |
| 111 | keys.push(node.key()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 112 | } |
Joel Einbinder | 67f28fb | 2018-08-02 00:33:47 | [diff] [blame] | 113 | } |
| 114 | return keys; |
| 115 | } |
| 116 | |
| 117 | /** |
Jack Lynch | b8fb3c7 | 2020-04-21 05:36:16 | [diff] [blame] | 118 | * @deprecated this function is obsolete and will be removed in the |
| 119 | * future along with the legacy shortcuts settings tab |
| 120 | * crbug.com/174309 |
| 121 | * |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 122 | * @param {string} actionId |
Tim van der Lippe | aa76aa2 | 2020-02-14 14:38:24 | [diff] [blame] | 123 | * @return {!Array.<!Descriptor>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 124 | */ |
| 125 | shortcutDescriptorsForAction(actionId) { |
Jack Lynch | b8fb3c7 | 2020-04-21 05:36:16 | [diff] [blame] | 126 | return [...this._actionToShortcut.get(actionId)].map(shortcut => shortcut.descriptors[0]); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param {!Array.<string>} actionIds |
| 131 | * @return {!Array.<number>} |
| 132 | */ |
| 133 | keysForActions(actionIds) { |
Jack Lynch | b8fb3c7 | 2020-04-21 05:36:16 | [diff] [blame] | 134 | const keys = actionIds.flatMap( |
| 135 | action => [...this._actionToShortcut.get(action)].flatMap( |
| 136 | shortcut => shortcut.descriptors.map(descriptor => descriptor.key))); |
| 137 | return [...(new Set(keys))]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param {string} actionId |
| 142 | * @return {string|undefined} |
| 143 | */ |
| 144 | shortcutTitleForAction(actionId) { |
Jack Lynch | b8fb3c7 | 2020-04-21 05:36:16 | [diff] [blame] | 145 | const shortcuts = this._actionToShortcut.get(actionId); |
| 146 | if (shortcuts.size) { |
| 147 | return shortcuts.firstValue().title(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 148 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @param {!KeyboardEvent} event |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 153 | * @param {!Object.<string, function():Promise.<boolean>>=} handlers |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 154 | */ |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 155 | handleShortcut(event, handlers) { |
| 156 | this.handleKey(KeyboardShortcut.makeKeyFromEvent(event), event.key, event, handlers); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Jack Lynch | 42297a7 | 2020-06-21 01:54:30 | [diff] [blame] | 160 | * @param {string} actionId |
| 161 | * return {boolean} |
| 162 | */ |
| 163 | actionHasDefaultShortcut(actionId) { |
| 164 | return this._devToolsDefaultShortcutActions.has(actionId); |
| 165 | } |
| 166 | |
| 167 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 168 | * @param {!Element} element |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 169 | * @param {!Object.<string, function():Promise.<boolean>>} handlers |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 170 | */ |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 171 | addShortcutListener(element, handlers) { |
| 172 | // We only want keys for these specific actions to get handled this |
Mathias Bynens | 5165a7a | 2020-06-10 05:51:43 | [diff] [blame] | 173 | // way; all others should be allowed to bubble up. |
| 174 | const allowlistKeyMap = new ShortcutTreeNode(0, 0); |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 175 | const shortcuts = Object.keys(handlers).flatMap(action => [...this._actionToShortcut.get(action)]); |
| 176 | shortcuts.forEach(shortcut => { |
Mathias Bynens | 5165a7a | 2020-06-10 05:51:43 | [diff] [blame] | 177 | allowlistKeyMap.addKeyMapping(shortcut.descriptors.map(descriptor => descriptor.key), shortcut.action); |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 178 | }); |
| 179 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 180 | element.addEventListener('keydown', event => { |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 181 | const key = KeyboardShortcut.makeKeyFromEvent(/** @type {!KeyboardEvent} */ (event)); |
Mathias Bynens | 5165a7a | 2020-06-10 05:51:43 | [diff] [blame] | 182 | let keyMap = allowlistKeyMap; |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 183 | if (this._activePrefixKey) { |
| 184 | keyMap = keyMap.getNode(this._activePrefixKey.key()); |
| 185 | if (!keyMap) { |
| 186 | return; |
| 187 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 188 | } |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 189 | if (keyMap.getNode(key)) { |
| 190 | this.handleShortcut(/** @type {!KeyboardEvent} */ (event), handlers); |
| 191 | } |
| 192 | }); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * @param {number} key |
| 197 | * @param {string} domKey |
| 198 | * @param {!KeyboardEvent=} event |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 199 | * @param {!Object.<string, function():Promise.<boolean>>=} handlers |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 200 | */ |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 201 | async handleKey(key, domKey, event, handlers) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 202 | const keyModifiers = key >> 8; |
Jack Lynch | a0dd1f0 | 2020-05-04 23:33:35 | [diff] [blame] | 203 | const hasHandlersOrPrefixKey = !!handlers || !!this._activePrefixKey; |
Jack Lynch | 4a6f753 | 2020-05-07 00:48:09 | [diff] [blame] | 204 | const keyMapNode = this._keyMap.getNode(key); |
| 205 | const maybeHasActions = this._applicableActions(key, handlers).length > 0 || (keyMapNode && keyMapNode.hasChords()); |
Jack Lynch | a0dd1f0 | 2020-05-04 23:33:35 | [diff] [blame] | 206 | if ((!hasHandlersOrPrefixKey && isPossiblyInputKey()) || !maybeHasActions || |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 207 | KeyboardShortcut.isModifier(KeyboardShortcut.keyCodeAndModifiersFromKey(key).keyCode)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 208 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 209 | } |
Jack Lynch | a0dd1f0 | 2020-05-04 23:33:35 | [diff] [blame] | 210 | if (event) { |
| 211 | event.consume(true); |
| 212 | } |
| 213 | if (!hasHandlersOrPrefixKey && Dialog.hasInstance()) { |
| 214 | return; |
| 215 | } |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 216 | |
| 217 | if (this._activePrefixTimeout) { |
| 218 | clearTimeout(this._activePrefixTimeout); |
| 219 | const handled = await maybeExecuteActionForKey.call(this); |
| 220 | this._activePrefixKey = null; |
| 221 | this._activePrefixTimeout = null; |
| 222 | if (handled) { |
| 223 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 224 | } |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 225 | if (this._consumePrefix) { |
| 226 | await this._consumePrefix(); |
| 227 | } |
| 228 | } |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 229 | if (keyMapNode && keyMapNode.hasChords()) { |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 230 | this._activePrefixKey = keyMapNode; |
| 231 | this._consumePrefix = async () => { |
| 232 | this._activePrefixKey = null; |
| 233 | this._activePrefixTimeout = null; |
| 234 | await maybeExecuteActionForKey.call(this); |
| 235 | }; |
| 236 | this._activePrefixTimeout = setTimeout(this._consumePrefix, KeyTimeout); |
| 237 | } else { |
| 238 | await maybeExecuteActionForKey.call(this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /** |
| 242 | * @return {boolean} |
| 243 | */ |
| 244 | function isPossiblyInputKey() { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 245 | if (!event || !isEditing() || /^F\d+|Control|Shift|Alt|Meta|Escape|Win|U\+001B$/.test(domKey)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 246 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 247 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 248 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 249 | if (!keyModifiers) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 250 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 251 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 252 | |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 253 | const modifiers = Modifiers; |
Joel Einbinder | a66e5bf | 2018-05-31 01:26:37 | [diff] [blame] | 254 | // Undo/Redo will also cause input, so textual undo should take precedence over DevTools undo when editing. |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 255 | if (Host.Platform.isMac()) { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 256 | if (KeyboardShortcut.makeKey('z', modifiers.Meta) === key) { |
Joel Einbinder | a66e5bf | 2018-05-31 01:26:37 | [diff] [blame] | 257 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 258 | } |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 259 | if (KeyboardShortcut.makeKey('z', modifiers.Meta | modifiers.Shift) === key) { |
Joel Einbinder | a66e5bf | 2018-05-31 01:26:37 | [diff] [blame] | 260 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 261 | } |
Joel Einbinder | a66e5bf | 2018-05-31 01:26:37 | [diff] [blame] | 262 | } else { |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 263 | if (KeyboardShortcut.makeKey('z', modifiers.Ctrl) === key) { |
Joel Einbinder | a66e5bf | 2018-05-31 01:26:37 | [diff] [blame] | 264 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 265 | } |
Paul Lewis | 9950e18 | 2019-12-16 16:06:07 | [diff] [blame] | 266 | if (KeyboardShortcut.makeKey('y', modifiers.Ctrl) === key) { |
Joel Einbinder | a66e5bf | 2018-05-31 01:26:37 | [diff] [blame] | 267 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 268 | } |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 269 | if (!Host.Platform.isWin() && KeyboardShortcut.makeKey('z', modifiers.Ctrl | modifiers.Shift) === key) { |
Joel Einbinder | a66e5bf | 2018-05-31 01:26:37 | [diff] [blame] | 270 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 271 | } |
Joel Einbinder | a66e5bf | 2018-05-31 01:26:37 | [diff] [blame] | 272 | } |
| 273 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 274 | if ((keyModifiers & (modifiers.Ctrl | modifiers.Alt)) === (modifiers.Ctrl | modifiers.Alt)) { |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 275 | return Host.Platform.isWin(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 276 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 277 | |
| 278 | return !hasModifier(modifiers.Ctrl) && !hasModifier(modifiers.Alt) && !hasModifier(modifiers.Meta); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * @param {number} mod |
| 283 | * @return {boolean} |
| 284 | */ |
| 285 | function hasModifier(mod) { |
| 286 | return !!(keyModifiers & mod); |
| 287 | } |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * @return {!Promise.<boolean>}; |
| 291 | * @this {!ShortcutRegistry} |
| 292 | */ |
| 293 | async function maybeExecuteActionForKey() { |
| 294 | const actions = this._applicableActions(key, handlers); |
| 295 | if (!actions.length) { |
| 296 | return false; |
| 297 | } |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 298 | for (const action of actions) { |
| 299 | let handled; |
| 300 | if (handlers && handlers[action.id()]) { |
| 301 | handled = await handlers[action.id()](); |
| 302 | } |
| 303 | if (!handlers) { |
| 304 | handled = await action.execute(); |
| 305 | } |
| 306 | if (handled) { |
| 307 | Host.userMetrics.keyboardShortcutFired(action.id()); |
| 308 | return true; |
| 309 | } |
| 310 | } |
| 311 | return false; |
| 312 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
Jack Lynch | 94a9b0c | 2020-03-11 21:45:14 | [diff] [blame] | 316 | * @param {!KeyboardShortcut} shortcut |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 317 | */ |
Jack Lynch | 94a9b0c | 2020-03-11 21:45:14 | [diff] [blame] | 318 | _registerShortcut(shortcut) { |
| 319 | this._actionToShortcut.set(shortcut.action, shortcut); |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 320 | this._keyMap.addKeyMapping(shortcut.descriptors.map(descriptor => descriptor.key), shortcut.action); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 321 | } |
| 322 | |
Jack Lynch | 94a9b0c | 2020-03-11 21:45:14 | [diff] [blame] | 323 | _registerBindings() { |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 324 | this._keyToShortcut.clear(); |
| 325 | this._actionToShortcut.clear(); |
| 326 | this._keyMap.clear(); |
| 327 | const keybindSet = self.Common.settings.moduleSetting('activeKeybindSet').get(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 328 | const extensions = self.runtime.extensions('action'); |
Jack Lynch | 10d7055 | 2020-06-10 20:30:36 | [diff] [blame] | 329 | const forwardedKeys = []; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 330 | extensions.forEach(registerExtension, this); |
Jack Lynch | 10d7055 | 2020-06-10 20:30:36 | [diff] [blame] | 331 | Host.InspectorFrontendHost.InspectorFrontendHostInstance.setWhitelistedShortcuts(JSON.stringify(forwardedKeys)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 332 | |
| 333 | /** |
Tim van der Lippe | 99e59b8 | 2019-09-30 20:00:59 | [diff] [blame] | 334 | * @param {!Root.Runtime.Extension} extension |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 335 | * @this {ShortcutRegistry} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 336 | */ |
| 337 | function registerExtension(extension) { |
| 338 | const descriptor = extension.descriptor(); |
Jack Lynch | 94a9b0c | 2020-03-11 21:45:14 | [diff] [blame] | 339 | const bindings = descriptor.bindings; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 340 | for (let i = 0; bindings && i < bindings.length; ++i) { |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 341 | const keybindSets = bindings[i].keybindSets; |
| 342 | if (!platformMatches(bindings[i].platform) || !keybindSetsMatch(keybindSets)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 343 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 344 | } |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 345 | const keys = bindings[i].shortcut.split(/\s+/); |
| 346 | const shortcutDescriptors = keys.map(KeyboardShortcut.makeDescriptorFromBindingShortcut); |
| 347 | if (shortcutDescriptors.length > 0) { |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 348 | const actionId = /** @type {string} */ (descriptor.actionId); |
Jack Lynch | 10d7055 | 2020-06-10 20:30:36 | [diff] [blame] | 349 | if (ForwardedActions.has(actionId)) { |
| 350 | forwardedKeys.push( |
| 351 | ...shortcutDescriptors.map(shortcut => KeyboardShortcut.keyCodeAndModifiersFromKey(shortcut.key))); |
| 352 | } |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 353 | if (!keybindSets) { |
Jack Lynch | 42297a7 | 2020-06-21 01:54:30 | [diff] [blame] | 354 | this._devToolsDefaultShortcutActions.add(actionId); |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 355 | this._registerShortcut(new KeyboardShortcut(shortcutDescriptors, actionId, Type.DefaultShortcut)); |
| 356 | } else { |
Jack Lynch | 42297a7 | 2020-06-21 01:54:30 | [diff] [blame] | 357 | if (keybindSets.includes(DefaultShortcutSetting)) { |
| 358 | this._devToolsDefaultShortcutActions.add(actionId); |
| 359 | } |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 360 | this._registerShortcut( |
Jack Lynch | 42297a7 | 2020-06-21 01:54:30 | [diff] [blame] | 361 | new KeyboardShortcut(shortcutDescriptors, actionId, Type.KeybindSetShortcut, new Set(keybindSets))); |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 362 | } |
Jack Lynch | f0d1424 | 2020-04-07 22:42:04 | [diff] [blame] | 363 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * @param {string=} platformsString |
| 369 | * @return {boolean} |
| 370 | */ |
| 371 | function platformMatches(platformsString) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 372 | if (!platformsString) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 373 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 374 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 375 | const platforms = platformsString.split(','); |
| 376 | let isMatch = false; |
Paul Lewis | 0fd4371 | 2020-01-08 17:07:36 | [diff] [blame] | 377 | const currentPlatform = Host.Platform.platform(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 378 | for (let i = 0; !isMatch && i < platforms.length; ++i) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 379 | isMatch = platforms[i] === currentPlatform; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 380 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 381 | return isMatch; |
| 382 | } |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 383 | |
| 384 | /** |
| 385 | * @param {!Array<string>=} keybindSets |
| 386 | */ |
| 387 | function keybindSetsMatch(keybindSets) { |
| 388 | if (!keybindSets) { |
| 389 | return true; |
| 390 | } |
| 391 | return keybindSets.includes(keybindSet); |
| 392 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 393 | } |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 394 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 395 | |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 396 | export class ShortcutTreeNode { |
| 397 | /** |
| 398 | * @param {number} key |
| 399 | * @param {number=} depth |
| 400 | */ |
| 401 | constructor(key, depth = 0) { |
| 402 | this._key = key; |
| 403 | /** @type {!Array.<string>} */ |
| 404 | this._actions = []; |
| 405 | this._chords = new Map(); |
| 406 | this._depth = depth; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * @param {string} action |
| 411 | */ |
| 412 | addAction(action) { |
| 413 | this._actions.push(action); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * @return {number} |
| 418 | */ |
| 419 | key() { |
| 420 | return this._key; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * @return {!Map.<number, !ShortcutTreeNode>} |
| 425 | */ |
| 426 | chords() { |
| 427 | return this._chords; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * @return {boolean} |
| 432 | */ |
| 433 | hasChords() { |
| 434 | return this._chords.size > 0; |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * @param {!Array.<number>} keys |
| 439 | * @param {string} action |
| 440 | */ |
| 441 | addKeyMapping(keys, action) { |
| 442 | if (keys.length < this._depth) { |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | if (keys.length === this._depth) { |
| 447 | this.addAction(action); |
| 448 | } else { |
| 449 | const key = keys[this._depth]; |
| 450 | if (!this._chords.has(key)) { |
| 451 | this._chords.set(key, new ShortcutTreeNode(key, this._depth + 1)); |
| 452 | } |
| 453 | this._chords.get(key).addKeyMapping(keys, action); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * @param {number} key |
| 459 | * @return {?ShortcutTreeNode} |
| 460 | */ |
| 461 | getNode(key) { |
| 462 | return this._chords.get(key) || null; |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * @return {!Array.<string>} |
| 467 | */ |
| 468 | actions() { |
| 469 | return this._actions; |
| 470 | } |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 471 | |
| 472 | clear() { |
| 473 | this._actions = []; |
| 474 | this._chords = new Map(); |
| 475 | } |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 476 | } |
| 477 | |
Sigurd Schneider | 46da7db | 2020-05-20 13:45:11 | [diff] [blame] | 478 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 479 | export class ForwardedShortcut {} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 480 | |
Tim van der Lippe | 0830b3d | 2019-10-03 13:20:07 | [diff] [blame] | 481 | ForwardedShortcut.instance = new ForwardedShortcut(); |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 482 | |
Jack Lynch | 10d7055 | 2020-06-10 20:30:36 | [diff] [blame] | 483 | export const ForwardedActions = new Set([ |
| 484 | 'main.toggle-dock', 'debugger.toggle-breakpoints-active', 'debugger.toggle-pause', 'commandMenu.show', 'console.show' |
| 485 | ]); |
Jack Lynch | 966040b | 2020-04-23 20:11:44 | [diff] [blame] | 486 | export const KeyTimeout = 1000; |
Jack Lynch | f1f00fa | 2020-05-01 22:16:12 | [diff] [blame] | 487 | export const DefaultShortcutSetting = 'devToolsDefault'; |