Andres Olivares | 6490c00 | 2020-12-02 16:03:35 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tim van der Lippe | aa61faf | 2021-04-07 15:32:07 | [diff] [blame] | 5 | import * as Common from '../../core/common/common.js'; |
Douglas Chiang | 5f52fb6 | 2023-04-05 08:43:10 | [diff] [blame] | 6 | import * as i18n from '../../core/i18n/i18n.js'; |
Benedikt Meurer | e0b4b92 | 2024-01-15 13:33:53 | [diff] [blame] | 7 | import * as Platform from '../../core/platform/platform.js'; |
Tim van der Lippe | aa61faf | 2021-04-07 15:32:07 | [diff] [blame] | 8 | import * as Root from '../../core/root/root.js'; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 9 | |
| 10 | import {Context} from './Context.js'; |
| 11 | |
Douglas Chiang | 5f52fb6 | 2023-04-05 08:43:10 | [diff] [blame] | 12 | const UIStrings = { |
| 13 | /** |
| 14 | *@description Title of the keybind category 'Elements' in Settings' Shortcuts pannel. |
| 15 | */ |
| 16 | elements: 'Elements', |
| 17 | /** |
| 18 | *@description Title of the keybind category 'Screenshot' in Settings' Shortcuts pannel. |
| 19 | */ |
| 20 | screenshot: 'Screenshot', |
| 21 | /** |
| 22 | *@description Title of the keybind category 'Network' in Settings' Shortcuts pannel. |
| 23 | */ |
| 24 | network: 'Network', |
| 25 | /** |
| 26 | *@description Title of the keybind category 'Memory' in Settings' Shortcuts pannel. |
| 27 | */ |
| 28 | memory: 'Memory', |
| 29 | /** |
| 30 | *@description Title of the keybind category 'JavaScript Profiler' in Settings' Shortcuts pannel. |
| 31 | */ |
| 32 | javascript_profiler: 'JavaScript Profiler', |
| 33 | /** |
| 34 | *@description Title of the keybind category 'Console' in Settings' Shortcuts pannel. |
| 35 | */ |
| 36 | console: 'Console', |
| 37 | /** |
| 38 | *@description Title of the keybind category 'Performance' in Settings' Shortcuts pannel. |
| 39 | */ |
| 40 | performance: 'Performance', |
| 41 | /** |
| 42 | *@description Title of the keybind category 'Mobile' in Settings' Shortcuts pannel. |
| 43 | */ |
| 44 | mobile: 'Mobile', |
| 45 | /** |
| 46 | *@description Title of the keybind category 'Help' in Settings' Shortcuts pannel. |
| 47 | */ |
| 48 | help: 'Help', |
| 49 | /** |
| 50 | *@description Title of the keybind category 'Layers' in Settings' Shortcuts pannel. |
| 51 | */ |
| 52 | layers: 'Layers', |
| 53 | /** |
| 54 | *@description Title of the keybind category 'Navigation' in Settings' Shortcuts pannel. |
| 55 | */ |
| 56 | navigation: 'Navigation', |
| 57 | /** |
| 58 | *@description Title of the keybind category 'Drawer' in Settings' Shortcuts pannel. |
| 59 | */ |
| 60 | drawer: 'Drawer', |
| 61 | /** |
| 62 | *@description Title of the keybind category 'Global' in Settings' Shortcuts pannel. |
| 63 | */ |
| 64 | global: 'Global', |
| 65 | /** |
| 66 | *@description Title of the keybind category 'Resources' in Settings' Shortcuts pannel. |
| 67 | */ |
| 68 | resources: 'Resources', |
| 69 | /** |
| 70 | *@description Title of the keybind category 'Background Services' in Settings' Shortcuts pannel. |
| 71 | */ |
| 72 | background_services: 'Background Services', |
| 73 | /** |
| 74 | *@description Title of the keybind category 'Settings' in Settings' Shortcuts pannel. |
| 75 | */ |
| 76 | settings: 'Settings', |
| 77 | /** |
| 78 | *@description Title of the keybind category 'Debugger' in Settings' Shortcuts pannel. |
| 79 | */ |
| 80 | debugger: 'Debugger', |
| 81 | /** |
| 82 | *@description Title of the keybind category 'Sources' in Settings' Shortcuts pannel. |
| 83 | */ |
| 84 | sources: 'Sources', |
| 85 | /** |
| 86 | *@description Title of the keybind category 'Rendering' in Settings' Shortcuts pannel. |
| 87 | */ |
| 88 | rendering: 'Rendering', |
Nikolay Vitkov | 8f7bda5 | 2023-11-10 16:46:13 | [diff] [blame] | 89 | /** |
| 90 | *@description Title of the keybind category 'Recorder' in Settings' Shortcuts pannel. |
| 91 | */ |
| 92 | recorder: 'Recorder', |
Benedikt Meurer | 01fabc6 | 2023-11-16 10:24:16 | [diff] [blame] | 93 | /** |
| 94 | *@description Title of the keybind category 'Changes' in Settings' Shortcuts pannel. |
| 95 | */ |
| 96 | changes: 'Changes', |
Ergun Erdogmus | 5efc7e9 | 2025-02-21 11:36:50 | [diff] [blame] | 97 | } as const; |
Douglas Chiang | 5f52fb6 | 2023-04-05 08:43:10 | [diff] [blame] | 98 | const str_ = i18n.i18n.registerUIStrings('ui/legacy/ActionRegistration.ts', UIStrings); |
| 99 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
| 100 | |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 101 | export interface ActionDelegate { |
Nikolay Vitkov | b31abae | 2024-05-28 15:52:27 | [diff] [blame] | 102 | handleAction(context: Context, actionId: string): boolean; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 103 | } |
| 104 | |
Kateryna Prokopenko | a72448b | 2021-08-31 14:16:16 | [diff] [blame] | 105 | export class Action extends Common.ObjectWrapper.ObjectWrapper<EventTypes> { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 106 | private enabledInternal = true; |
| 107 | private toggledInternal = false; |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 108 | private actionRegistration: ActionRegistration; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 109 | constructor(actionRegistration: ActionRegistration) { |
| 110 | super(); |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 111 | this.actionRegistration = actionRegistration; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | id(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 115 | return this.actionRegistration.actionId; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | async execute(): Promise<boolean> { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 119 | if (!this.actionRegistration.loadActionDelegate) { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 120 | return false; |
| 121 | } |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 122 | const delegate = await this.actionRegistration.loadActionDelegate(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 123 | const actionId = this.id(); |
| 124 | return delegate.handleAction(Context.instance(), actionId); |
| 125 | } |
| 126 | |
| 127 | icon(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 128 | return this.actionRegistration.iconClass; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | toggledIcon(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 132 | return this.actionRegistration.toggledIconClass; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | toggleWithRedColor(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 136 | return Boolean(this.actionRegistration.toggleWithRedColor); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 137 | } |
| 138 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 139 | setEnabled(enabled: boolean): void { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 140 | if (this.enabledInternal === enabled) { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 144 | this.enabledInternal = enabled; |
Benedikt Meurer | f08ab4a | 2024-09-03 09:37:35 | [diff] [blame] | 145 | this.dispatchEventToListeners(Events.ENABLED, enabled); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | enabled(): boolean { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 149 | return this.enabledInternal; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 150 | } |
| 151 | |
Douglas Chiang | 5f52fb6 | 2023-04-05 08:43:10 | [diff] [blame] | 152 | category(): ActionCategory { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 153 | return this.actionRegistration.category; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 154 | } |
| 155 | |
Andres Olivares | 344120f | 2020-12-07 17:43:28 | [diff] [blame] | 156 | tags(): string|void { |
| 157 | if (this.actionRegistration.tags) { |
| 158 | // Get localized keys and separate by null character to prevent fuzzy matching from matching across them. |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 159 | return this.actionRegistration.tags.map(tag => tag()).join('\0'); |
Andres Olivares | 344120f | 2020-12-07 17:43:28 | [diff] [blame] | 160 | } |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | toggleable(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 164 | return Boolean(this.actionRegistration.toggleable); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 165 | } |
| 166 | |
Simon ZĂĽnd | 6eb94e3 | 2023-04-11 05:28:22 | [diff] [blame] | 167 | title(): Common.UIString.LocalizedString { |
| 168 | let title = this.actionRegistration.title ? this.actionRegistration.title() : i18n.i18n.lockedString(''); |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 169 | const options = this.actionRegistration.options; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 170 | if (options) { |
| 171 | // Actions with an 'options' property don't have a title field. Instead, the displayed |
| 172 | // title is taken from the 'title' property of the option that is not active. Only one of the |
| 173 | // two options can be active at a given moment and the 'toggled' property of the action along |
| 174 | // with the 'value' of the options are used to determine which one it is. |
| 175 | |
| 176 | for (const pair of options) { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 177 | if (pair.value !== this.toggledInternal) { |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 178 | title = pair.title(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | } |
| 182 | return title; |
| 183 | } |
| 184 | |
| 185 | toggled(): boolean { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 186 | return this.toggledInternal; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 187 | } |
| 188 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 189 | setToggled(toggled: boolean): void { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 190 | console.assert(this.toggleable(), 'Shouldn\'t be toggling an untoggleable action', this.id()); |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 191 | if (this.toggledInternal === toggled) { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 195 | this.toggledInternal = toggled; |
Benedikt Meurer | f08ab4a | 2024-09-03 09:37:35 | [diff] [blame] | 196 | this.dispatchEventToListeners(Events.TOGGLED, toggled); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 197 | } |
| 198 | |
Benedikt Meurer | 84b0fcc | 2025-02-12 13:57:23 | [diff] [blame] | 199 | options(): undefined|ExtensionOption[] { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 200 | return this.actionRegistration.options; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 201 | } |
| 202 | |
Nikolay Vitkov | b30f3b2 | 2025-03-04 13:38:33 | [diff] [blame] | 203 | contextTypes(): undefined|Array<Platform.Constructor.Constructor<unknown>> { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 204 | if (this.actionRegistration.contextTypes) { |
| 205 | return this.actionRegistration.contextTypes(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 206 | } |
| 207 | return undefined; |
| 208 | } |
| 209 | |
| 210 | canInstantiate(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 211 | return Boolean(this.actionRegistration.loadActionDelegate); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 212 | } |
| 213 | |
Benedikt Meurer | 84b0fcc | 2025-02-12 13:57:23 | [diff] [blame] | 214 | bindings(): Binding[]|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 215 | return this.actionRegistration.bindings; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | experiment(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 219 | return this.actionRegistration.experiment; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 220 | } |
| 221 | |
Alex Rudenko | 43ebf02 | 2024-01-31 18:43:40 | [diff] [blame] | 222 | setting(): string|undefined { |
| 223 | return this.actionRegistration.setting; |
| 224 | } |
| 225 | |
Alex Rudenko | 6329ebd | 2024-02-08 14:39:59 | [diff] [blame] | 226 | condition(): Root.Runtime.Condition|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 227 | return this.actionRegistration.condition; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 228 | } |
Andres Olivares | 4dba130 | 2021-01-28 23:17:32 | [diff] [blame] | 229 | |
| 230 | order(): number|undefined { |
| 231 | return this.actionRegistration.order; |
| 232 | } |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 233 | } |
| 234 | |
Benedikt Meurer | c9fb2be | 2024-01-15 09:06:42 | [diff] [blame] | 235 | const registeredActions = new Map<string, Action>(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 236 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 237 | export function registerActionExtension(registration: ActionRegistration): void { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 238 | const actionId = registration.actionId; |
Benedikt Meurer | c9fb2be | 2024-01-15 09:06:42 | [diff] [blame] | 239 | if (registeredActions.has(actionId)) { |
Benedikt Meurer | e0b4b92 | 2024-01-15 13:33:53 | [diff] [blame] | 240 | throw new Error(`Duplicate action ID '${actionId}'`); |
| 241 | } |
| 242 | if (!Platform.StringUtilities.isExtendedKebabCase(actionId)) { |
| 243 | throw new Error(`Invalid action ID '${actionId}'`); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 244 | } |
Benedikt Meurer | c9fb2be | 2024-01-15 09:06:42 | [diff] [blame] | 245 | registeredActions.set(actionId, new Action(registration)); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 246 | } |
| 247 | |
Andrés Olivares | b0fcd18 | 2023-02-21 10:13:28 | [diff] [blame] | 248 | export function reset(): void { |
Benedikt Meurer | c9fb2be | 2024-01-15 09:06:42 | [diff] [blame] | 249 | registeredActions.clear(); |
Andrés Olivares | b0fcd18 | 2023-02-21 10:13:28 | [diff] [blame] | 250 | } |
| 251 | |
Benedikt Meurer | 84b0fcc | 2025-02-12 13:57:23 | [diff] [blame] | 252 | export function getRegisteredActionExtensions(): Action[] { |
Benedikt Meurer | c9fb2be | 2024-01-15 09:06:42 | [diff] [blame] | 253 | return Array.from(registeredActions.values()) |
Alex Rudenko | 43ebf02 | 2024-01-31 18:43:40 | [diff] [blame] | 254 | .filter(action => { |
| 255 | const settingName = action.setting(); |
Alex Rudenko | 3c5b6b3 | 2024-02-09 09:59:25 | [diff] [blame] | 256 | try { |
| 257 | if (settingName && !Common.Settings.moduleSetting(settingName).get()) { |
| 258 | return false; |
| 259 | } |
| 260 | } catch (err) { |
| 261 | if (err.message.startsWith('No setting registered')) { |
| 262 | return false; |
| 263 | } |
Alex Rudenko | 43ebf02 | 2024-01-31 18:43:40 | [diff] [blame] | 264 | } |
| 265 | |
Benedikt Meurer | ea9d286 | 2025-02-12 12:06:16 | [diff] [blame] | 266 | return Root.Runtime.Runtime.isDescriptorEnabled({ |
| 267 | experiment: action.experiment(), |
| 268 | condition: action.condition(), |
| 269 | }); |
Alex Rudenko | 43ebf02 | 2024-01-31 18:43:40 | [diff] [blame] | 270 | }) |
Andres Olivares | 4dba130 | 2021-01-28 23:17:32 | [diff] [blame] | 271 | .sort((firstAction, secondAction) => { |
| 272 | const order1 = firstAction.order() || 0; |
| 273 | const order2 = secondAction.order() || 0; |
| 274 | return order1 - order2; |
| 275 | }); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 276 | } |
| 277 | |
Andres Olivares | 975c302 | 2021-03-29 13:45:24 | [diff] [blame] | 278 | export function maybeRemoveActionExtension(actionId: string): boolean { |
Benedikt Meurer | c9fb2be | 2024-01-15 09:06:42 | [diff] [blame] | 279 | return registeredActions.delete(actionId); |
Andres Olivares | 975c302 | 2021-03-29 13:45:24 | [diff] [blame] | 280 | } |
| 281 | |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 282 | export const enum Platforms { |
Benedikt Meurer | f08ab4a | 2024-09-03 09:37:35 | [diff] [blame] | 283 | ALL = 'All platforms', |
| 284 | MAC = 'mac', |
| 285 | WINDOWS_LINUX = 'windows,linux', |
| 286 | ANDROID = 'Android', |
| 287 | WINDOWS = 'windows', |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 288 | } |
| 289 | |
Kateryna Prokopenko | a72448b | 2021-08-31 14:16:16 | [diff] [blame] | 290 | export const enum Events { |
Benedikt Meurer | f08ab4a | 2024-09-03 09:37:35 | [diff] [blame] | 291 | ENABLED = 'Enabled', |
| 292 | TOGGLED = 'Toggled', |
Kateryna Prokopenko | a72448b | 2021-08-31 14:16:16 | [diff] [blame] | 293 | } |
| 294 | |
Nikolay Vitkov | 44e3006 | 2025-01-07 14:33:04 | [diff] [blame] | 295 | export interface EventTypes { |
| 296 | [Events.ENABLED]: boolean; |
| 297 | [Events.TOGGLED]: boolean; |
| 298 | } |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 299 | |
Benedikt Meurer | 624312d | 2024-01-22 09:24:09 | [diff] [blame] | 300 | export const enum ActionCategory { |
Douglas Chiang | 5f52fb6 | 2023-04-05 08:43:10 | [diff] [blame] | 301 | NONE = '', // `NONE` must be a falsy value. Legacy code uses if-checks for the category. |
| 302 | ELEMENTS = 'ELEMENTS', |
| 303 | SCREENSHOT = 'SCREENSHOT', |
| 304 | NETWORK = 'NETWORK', |
| 305 | MEMORY = 'MEMORY', |
| 306 | JAVASCRIPT_PROFILER = 'JAVASCRIPT_PROFILER', |
| 307 | CONSOLE = 'CONSOLE', |
| 308 | PERFORMANCE = 'PERFORMANCE', |
| 309 | MOBILE = 'MOBILE', |
| 310 | HELP = 'HELP', |
| 311 | LAYERS = 'LAYERS', |
| 312 | NAVIGATION = 'NAVIGATION', |
| 313 | DRAWER = 'DRAWER', |
| 314 | GLOBAL = 'GLOBAL', |
| 315 | RESOURCES = 'RESOURCES', |
| 316 | BACKGROUND_SERVICES = 'BACKGROUND_SERVICES', |
| 317 | SETTINGS = 'SETTINGS', |
| 318 | DEBUGGER = 'DEBUGGER', |
| 319 | SOURCES = 'SOURCES', |
| 320 | RENDERING = 'RENDERING', |
Nikolay Vitkov | 8f7bda5 | 2023-11-10 16:46:13 | [diff] [blame] | 321 | RECORDER = 'RECORDER', |
Benedikt Meurer | 01fabc6 | 2023-11-16 10:24:16 | [diff] [blame] | 322 | CHANGES = 'CHANGES', |
Douglas Chiang | 5f52fb6 | 2023-04-05 08:43:10 | [diff] [blame] | 323 | } |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 324 | |
Douglas Chiang | 5f52fb6 | 2023-04-05 08:43:10 | [diff] [blame] | 325 | export function getLocalizedActionCategory(category: ActionCategory): Platform.UIString.LocalizedString { |
| 326 | switch (category) { |
| 327 | case ActionCategory.ELEMENTS: |
| 328 | return i18nString(UIStrings.elements); |
| 329 | case ActionCategory.SCREENSHOT: |
| 330 | return i18nString(UIStrings.screenshot); |
| 331 | case ActionCategory.NETWORK: |
| 332 | return i18nString(UIStrings.network); |
| 333 | case ActionCategory.MEMORY: |
| 334 | return i18nString(UIStrings.memory); |
| 335 | case ActionCategory.JAVASCRIPT_PROFILER: |
| 336 | return i18nString(UIStrings.javascript_profiler); |
| 337 | case ActionCategory.CONSOLE: |
| 338 | return i18nString(UIStrings.console); |
| 339 | case ActionCategory.PERFORMANCE: |
| 340 | return i18nString(UIStrings.performance); |
| 341 | case ActionCategory.MOBILE: |
| 342 | return i18nString(UIStrings.mobile); |
| 343 | case ActionCategory.HELP: |
| 344 | return i18nString(UIStrings.help); |
| 345 | case ActionCategory.LAYERS: |
| 346 | return i18nString(UIStrings.layers); |
| 347 | case ActionCategory.NAVIGATION: |
| 348 | return i18nString(UIStrings.navigation); |
| 349 | case ActionCategory.DRAWER: |
| 350 | return i18nString(UIStrings.drawer); |
| 351 | case ActionCategory.GLOBAL: |
| 352 | return i18nString(UIStrings.global); |
| 353 | case ActionCategory.RESOURCES: |
| 354 | return i18nString(UIStrings.resources); |
| 355 | case ActionCategory.BACKGROUND_SERVICES: |
| 356 | return i18nString(UIStrings.background_services); |
| 357 | case ActionCategory.SETTINGS: |
| 358 | return i18nString(UIStrings.settings); |
| 359 | case ActionCategory.DEBUGGER: |
| 360 | return i18nString(UIStrings.debugger); |
| 361 | case ActionCategory.SOURCES: |
| 362 | return i18nString(UIStrings.sources); |
| 363 | case ActionCategory.RENDERING: |
| 364 | return i18nString(UIStrings.rendering); |
Nikolay Vitkov | 8f7bda5 | 2023-11-10 16:46:13 | [diff] [blame] | 365 | case ActionCategory.RECORDER: |
| 366 | return i18nString(UIStrings.recorder); |
Benedikt Meurer | 01fabc6 | 2023-11-16 10:24:16 | [diff] [blame] | 367 | case ActionCategory.CHANGES: |
| 368 | return i18nString(UIStrings.changes); |
Douglas Chiang | 5f52fb6 | 2023-04-05 08:43:10 | [diff] [blame] | 369 | case ActionCategory.NONE: |
| 370 | return i18n.i18n.lockedString(''); |
| 371 | } |
Simon ZĂĽnd | d2132fb | 2023-04-05 11:39:15 | [diff] [blame] | 372 | // Not all categories are cleanly typed yet. Return the category as-is in this case. |
| 373 | return i18n.i18n.lockedString(category); |
Douglas Chiang | 5f52fb6 | 2023-04-05 08:43:10 | [diff] [blame] | 374 | } |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 375 | |
| 376 | export const enum IconClass { |
Kateryna Prokopenko | 910cb53 | 2023-03-28 09:36:46 | [diff] [blame] | 377 | LARGEICON_NODE_SEARCH = 'select-element', |
Kateryna Prokopenko | 75da971 | 2023-03-31 11:56:38 | [diff] [blame] | 378 | START_RECORDING = 'record-start', |
| 379 | STOP_RECORDING = 'record-stop', |
Kateryna Prokopenko | 551f9c8 | 2023-04-04 14:34:21 | [diff] [blame] | 380 | REFRESH = 'refresh', |
Kateryna Prokopenko | 05ade9a | 2023-03-31 10:57:05 | [diff] [blame] | 381 | CLEAR = 'clear', |
| 382 | EYE = 'eye', |
Kateryna Prokopenko | 910cb53 | 2023-03-28 09:36:46 | [diff] [blame] | 383 | LARGEICON_PHONE = 'devices', |
Benedikt Meurer | 58fea3a | 2023-04-13 05:48:47 | [diff] [blame] | 384 | PLAY = 'play', |
Benedikt Meurer | c4ad9d9 | 2023-04-11 07:25:02 | [diff] [blame] | 385 | DOWNLOAD = 'download', |
Benedikt Meurer | 4878f6a | 2023-03-29 12:01:44 | [diff] [blame] | 386 | LARGEICON_PAUSE = 'pause', |
| 387 | LARGEICON_RESUME = 'resume', |
Muhammad Mahad | 8823710 | 2023-12-10 10:24:03 | [diff] [blame] | 388 | MOP = 'mop', |
Benedikt Meurer | 178c0c3 | 2023-03-31 04:50:06 | [diff] [blame] | 389 | BIN = 'bin', |
Kateryna Prokopenko | 910cb53 | 2023-03-28 09:36:46 | [diff] [blame] | 390 | LARGEICON_SETTINGS_GEAR = 'gear', |
Benedikt Meurer | 4878f6a | 2023-03-29 12:01:44 | [diff] [blame] | 391 | LARGEICON_STEP_OVER = 'step-over', |
| 392 | LARGE_ICON_STEP_INTO = 'step-into', |
| 393 | LARGE_ICON_STEP = 'step', |
| 394 | LARGE_ICON_STEP_OUT = 'step-out', |
Benedikt Meurer | 32c2a1b | 2023-03-30 07:09:59 | [diff] [blame] | 395 | BREAKPOINT_CROSSED_FILLED = 'breakpoint-crossed-filled', |
| 396 | BREAKPOINT_CROSSED = 'breakpoint-crossed', |
Benedikt Meurer | 7d5cc37 | 2023-04-12 12:20:41 | [diff] [blame] | 397 | PLUS = 'plus', |
Benedikt Meurer | 01fabc6 | 2023-11-16 10:24:16 | [diff] [blame] | 398 | UNDO = 'undo', |
| 399 | COPY = 'copy', |
Benedikt Meurer | 9279563 | 2023-11-30 14:00:57 | [diff] [blame] | 400 | IMPORT = 'import', |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | export const enum KeybindSet { |
| 404 | DEVTOOLS_DEFAULT = 'devToolsDefault', |
| 405 | VS_CODE = 'vsCode', |
| 406 | } |
| 407 | |
| 408 | export interface ExtensionOption { |
| 409 | value: boolean; |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 410 | title: () => Platform.UIString.LocalizedString; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 411 | text?: string; |
| 412 | } |
| 413 | |
| 414 | export interface Binding { |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 415 | platform?: Platforms; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 416 | shortcut: string; |
Benedikt Meurer | 84b0fcc | 2025-02-12 13:57:23 | [diff] [blame] | 417 | keybindSets?: KeybindSet[]; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 418 | } |
| 419 | |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 420 | /** |
| 421 | * The representation of an action extension to be registered. |
| 422 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 423 | export interface ActionRegistration { |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 424 | /** |
| 425 | * The unique id of an Action extension. |
| 426 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 427 | actionId: string; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 428 | /** |
| 429 | * The category with which the action is displayed in the UI. |
| 430 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 431 | category: ActionCategory; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 432 | /** |
| 433 | * The title with which the action is displayed in the UI. |
| 434 | */ |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 435 | title?: () => Platform.UIString.LocalizedString; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 436 | /** |
| 437 | * The type of the icon used to trigger the action. |
| 438 | */ |
Andres Olivares | 844c58d | 2021-01-26 13:09:07 | [diff] [blame] | 439 | iconClass?: IconClass; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 440 | /** |
| 441 | * Whether the style of the icon toggles on interaction. |
| 442 | */ |
Andres Olivares | 844c58d | 2021-01-26 13:09:07 | [diff] [blame] | 443 | toggledIconClass?: IconClass; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 444 | /** |
| 445 | * Whether the class 'toolbar-toggle-with-red-color' is toggled on the icon on interaction. |
| 446 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 447 | toggleWithRedColor?: boolean; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 448 | /** |
| 449 | * Words used to find an action in the Command Menu. |
| 450 | */ |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 451 | tags?: Array<() => Platform.UIString.LocalizedString>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 452 | /** |
| 453 | * Whether the action is toggleable. |
| 454 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 455 | toggleable?: boolean; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 456 | /** |
Andres Olivares | a0cdb38 | 2021-01-21 15:44:33 | [diff] [blame] | 457 | * Loads the class that handles the action when it is triggered. The common pattern for implementing |
| 458 | * this function relies on having the module that contains the action’s handler lazily loaded. For example: |
| 459 | * ```js |
| 460 | * let loadedElementsModule; |
| 461 | * |
| 462 | * async function loadElementsModule() { |
| 463 | * |
| 464 | * if (!loadedElementsModule) { |
| 465 | * loadedElementsModule = await import('./elements.js'); |
| 466 | * } |
| 467 | * return loadedElementsModule; |
| 468 | * } |
| 469 | * UI.ActionRegistration.registerActionExtension({ |
| 470 | * <...> |
| 471 | * async loadActionDelegate() { |
| 472 | * const Elements = await loadElementsModule(); |
Benedikt Meurer | 64d1fec | 2023-11-24 10:32:08 | [diff] [blame] | 473 | * return new Elements.ElementsPanel.ElementsActionDelegate(); |
Andres Olivares | a0cdb38 | 2021-01-21 15:44:33 | [diff] [blame] | 474 | * }, |
| 475 | * <...> |
| 476 | * }); |
| 477 | * ``` |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 478 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 479 | loadActionDelegate?: () => Promise<ActionDelegate>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 480 | /** |
| 481 | * Returns the classes that represent the 'context flavors' under which the action is available for triggering. |
| 482 | * The context of the application is described in 'flavors' that are usually views added and removed to the context |
| 483 | * as the user interacts with the application (e.g when the user moves across views). (See UI.Context) |
| 484 | * When the action is supposed to be available globally, that is, it does not depend on the application to have |
| 485 | * a specific context, the value of this property should be undefined. |
| 486 | * |
| 487 | * Because the method is synchronous, context types should be already loaded when the method is invoked. |
| 488 | * In the case that an action has context types it depends on, and they haven't been loaded yet, the function should |
| 489 | * return an empty array. Once the context types have been loaded, the function should return an array with all types |
| 490 | * that it depends on. |
| 491 | * |
| 492 | * The common pattern for implementing this function is relying on having the module with the corresponding context |
| 493 | * types loaded and stored when the related 'view' extension is loaded asynchronously. As an example: |
| 494 | * |
| 495 | * ```js |
| 496 | * let loadedElementsModule; |
| 497 | * |
| 498 | * async function loadElementsModule() { |
| 499 | * |
| 500 | * if (!loadedElementsModule) { |
| 501 | * loadedElementsModule = await import('./elements.js'); |
| 502 | * } |
| 503 | * return loadedElementsModule; |
| 504 | * } |
| 505 | * function maybeRetrieveContextTypes(getClassCallBack: (elementsModule: typeof Elements) => unknown[]): unknown[] { |
| 506 | * |
| 507 | * if (loadedElementsModule === undefined) { |
| 508 | * return []; |
| 509 | * } |
| 510 | * return getClassCallBack(loadedElementsModule); |
| 511 | * } |
| 512 | * UI.ActionRegistration.registerActionExtension({ |
| 513 | * |
| 514 | * contextTypes() { |
| 515 | * return maybeRetrieveContextTypes(Elements => [Elements.ElementsPanel.ElementsPanel]); |
| 516 | * } |
| 517 | * <...> |
| 518 | * }); |
| 519 | * ``` |
| 520 | */ |
Nikolay Vitkov | b30f3b2 | 2025-03-04 13:38:33 | [diff] [blame] | 521 | contextTypes?: () => Array<Platform.Constructor.Constructor<unknown>>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 522 | /** |
Andres Olivares | a0cdb38 | 2021-01-21 15:44:33 | [diff] [blame] | 523 | * The descriptions for each of the two states in which a toggleable action can be. |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 524 | */ |
Benedikt Meurer | 84b0fcc | 2025-02-12 13:57:23 | [diff] [blame] | 525 | options?: ExtensionOption[]; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 526 | /** |
| 527 | * The description of the variables (e.g. platform, keys and keybind sets) under which a keyboard shortcut triggers the action. |
| 528 | * If a keybind must be available on all platforms, its 'platform' property must be undefined. The same applies to keybind sets |
| 529 | * and the keybindSet property. |
| 530 | * |
| 531 | * Keybinds also depend on the context types of their corresponding action, and so they will only be available when such context types |
| 532 | * are flavors of the current appliaction context. |
| 533 | */ |
Benedikt Meurer | 84b0fcc | 2025-02-12 13:57:23 | [diff] [blame] | 534 | bindings?: Binding[]; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 535 | /** |
Andres Olivares | a0cdb38 | 2021-01-21 15:44:33 | [diff] [blame] | 536 | * The name of the experiment an action is associated with. Enabling and disabling the declared |
| 537 | * experiment will enable and disable the action respectively. |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 538 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 539 | experiment?: Root.Runtime.ExperimentName; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 540 | /** |
Alex Rudenko | 43ebf02 | 2024-01-31 18:43:40 | [diff] [blame] | 541 | * The name of the setting an action is associated with. Enabling and |
| 542 | * disabling the declared setting will enable and disable the action |
| 543 | * respectively. Note that changing the setting requires a reload for it to |
| 544 | * apply to action registration. |
| 545 | */ |
| 546 | setting?: string; |
| 547 | /** |
Alex Rudenko | 6329ebd | 2024-02-08 14:39:59 | [diff] [blame] | 548 | * A condition is a function that will make the action available if it |
| 549 | * returns true, and not available, otherwise. Make sure that objects you |
| 550 | * access from inside the condition function are ready at the time when the |
| 551 | * setting conditions are checked. |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 552 | */ |
Alex Rudenko | 6329ebd | 2024-02-08 14:39:59 | [diff] [blame] | 553 | condition?: Root.Runtime.Condition; |
Andres Olivares | 4dba130 | 2021-01-28 23:17:32 | [diff] [blame] | 554 | /** |
| 555 | * Used to sort actions when all registered actions are queried. |
| 556 | */ |
| 557 | order?: number; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 558 | } |