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 | |
| 5 | import * as Common from '../common/common.js'; |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame^] | 6 | import * as Platform from '../platform/platform.js'; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 7 | import {ls} from '../platform/platform.js'; |
| 8 | import * as Root from '../root/root.js'; |
| 9 | |
| 10 | import {Context} from './Context.js'; |
| 11 | |
| 12 | class ActionRuntimeExtensionDescriptor extends Root.Runtime.RuntimeExtensionDescriptor { |
| 13 | iconClass?: string; |
| 14 | toggledIconClass?: string; |
| 15 | toggleWithRedColor?: boolean; |
| 16 | toggleable?: boolean; |
| 17 | |
| 18 | constructor() { |
| 19 | super(); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | export interface Action extends Common.EventTarget.EventTarget { |
| 24 | id(): string; |
| 25 | |
| 26 | execute(): Promise<boolean>; |
| 27 | |
| 28 | icon(): string|undefined; |
| 29 | |
| 30 | toggledIcon(): string|undefined; |
| 31 | |
| 32 | toggleWithRedColor(): boolean; |
| 33 | |
| 34 | setEnabled(_enabled: boolean): void; |
| 35 | |
| 36 | enabled(): boolean; |
| 37 | |
| 38 | category(): string; |
| 39 | |
Andres Olivares | 344120f | 2020-12-07 17:43:28 | [diff] [blame] | 40 | tags(): string|void; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 41 | |
| 42 | toggleable(): boolean; |
| 43 | |
| 44 | title(): string; |
| 45 | |
| 46 | toggled(): boolean; |
| 47 | |
| 48 | setToggled(_toggled: boolean): void |
| 49 | } |
| 50 | |
| 51 | export class LegacyActionRegistration extends Common.ObjectWrapper.ObjectWrapper implements Action { |
| 52 | _extension: Root.Runtime.Extension; |
| 53 | _enabled: boolean; |
| 54 | _toggled: boolean; |
| 55 | |
| 56 | constructor(extension: Root.Runtime.Extension) { |
| 57 | super(); |
| 58 | this._extension = extension; |
| 59 | this._enabled = true; |
| 60 | this._toggled = false; |
| 61 | } |
| 62 | |
| 63 | id(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 64 | return this.actionDescriptor().actionId || ''; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | extension(): Root.Runtime.Extension { |
| 68 | return this._extension; |
| 69 | } |
| 70 | |
| 71 | async execute(): Promise<boolean> { |
| 72 | if (!this._extension.canInstantiate()) { |
| 73 | return false; |
| 74 | } |
| 75 | const delegate = await this._extension.instance() as ActionDelegate; |
| 76 | const actionId = this.id(); |
| 77 | return delegate.handleAction(Context.instance(), actionId); |
| 78 | } |
| 79 | |
| 80 | icon(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 81 | return this.actionDescriptor().iconClass || ''; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | toggledIcon(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 85 | return this.actionDescriptor().toggledIconClass || ''; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | toggleWithRedColor(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 89 | return Boolean(this.actionDescriptor().toggleWithRedColor); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 90 | } |
| 91 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 92 | setEnabled(enabled: boolean): void { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 93 | if (this._enabled === enabled) { |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | this._enabled = enabled; |
| 98 | this.dispatchEventToListeners(Events.Enabled, enabled); |
| 99 | } |
| 100 | |
| 101 | enabled(): boolean { |
| 102 | return this._enabled; |
| 103 | } |
| 104 | |
| 105 | category(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 106 | return ls`${this.actionDescriptor().category || ''}`; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | tags(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 110 | const keys = this.actionDescriptor().tags || ''; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 111 | // Get localized keys and separate by null character to prevent fuzzy matching from matching across them. |
| 112 | const keyList = keys.split(','); |
| 113 | let key = ''; |
| 114 | keyList.forEach(k => { |
| 115 | key += (ls(k.trim()) + '\0'); |
| 116 | }); |
| 117 | return key; |
| 118 | } |
| 119 | |
| 120 | toggleable(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 121 | return Boolean(this.actionDescriptor().toggleable); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | title(): string { |
| 125 | let title = this._extension.title() || ''; |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 126 | const options = this.actionDescriptor().options; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 127 | if (options) { |
| 128 | for (const pair of options) { |
| 129 | if (pair.value !== this._toggled) { |
| 130 | title = ls`${pair.title}`; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | return title; |
| 135 | } |
| 136 | |
| 137 | toggled(): boolean { |
| 138 | return this._toggled; |
| 139 | } |
| 140 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 141 | setToggled(toggled: boolean): void { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 142 | console.assert(this.toggleable(), 'Shouldn\'t be toggling an untoggleable action', this.id()); |
| 143 | if (this._toggled === toggled) { |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | this._toggled = toggled; |
| 148 | this.dispatchEventToListeners(Events.Toggled, toggled); |
| 149 | } |
| 150 | |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 151 | private actionDescriptor(): ActionRuntimeExtensionDescriptor { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 152 | return this._extension.descriptor() as ActionRuntimeExtensionDescriptor; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | export interface ActionDelegate { |
| 157 | handleAction(_context: Context, _actionId: string): boolean; |
| 158 | } |
| 159 | |
| 160 | export class PreRegisteredAction extends Common.ObjectWrapper.ObjectWrapper implements Action { |
| 161 | _enabled = true; |
| 162 | _toggled = false; |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 163 | private actionRegistration: ActionRegistration; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 164 | constructor(actionRegistration: ActionRegistration) { |
| 165 | super(); |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 166 | this.actionRegistration = actionRegistration; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | id(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 170 | return this.actionRegistration.actionId; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | async execute(): Promise<boolean> { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 174 | if (!this.actionRegistration.loadActionDelegate) { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 175 | return false; |
| 176 | } |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 177 | const delegate = await this.actionRegistration.loadActionDelegate(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 178 | const actionId = this.id(); |
| 179 | return delegate.handleAction(Context.instance(), actionId); |
| 180 | } |
| 181 | |
| 182 | icon(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 183 | return this.actionRegistration.iconClass; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | toggledIcon(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 187 | return this.actionRegistration.toggledIconClass; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | toggleWithRedColor(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 191 | return Boolean(this.actionRegistration.toggleWithRedColor); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 192 | } |
| 193 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 194 | setEnabled(enabled: boolean): void { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 195 | if (this._enabled === enabled) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | this._enabled = enabled; |
| 200 | this.dispatchEventToListeners(Events.Enabled, enabled); |
| 201 | } |
| 202 | |
| 203 | enabled(): boolean { |
| 204 | return this._enabled; |
| 205 | } |
| 206 | |
| 207 | category(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 208 | return this.actionRegistration.category; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 209 | } |
| 210 | |
Andres Olivares | 344120f | 2020-12-07 17:43:28 | [diff] [blame] | 211 | tags(): string|void { |
| 212 | if (this.actionRegistration.tags) { |
| 213 | // 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^] | 214 | return this.actionRegistration.tags.map(tag => tag()).join('\0'); |
Andres Olivares | 344120f | 2020-12-07 17:43:28 | [diff] [blame] | 215 | } |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | toggleable(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 219 | return Boolean(this.actionRegistration.toggleable); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | title(): string { |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame^] | 223 | let title = this.actionRegistration.title ? this.actionRegistration.title() : ''; |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 224 | const options = this.actionRegistration.options; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 225 | if (options) { |
| 226 | // Actions with an 'options' property don't have a title field. Instead, the displayed |
| 227 | // title is taken from the 'title' property of the option that is not active. Only one of the |
| 228 | // two options can be active at a given moment and the 'toggled' property of the action along |
| 229 | // with the 'value' of the options are used to determine which one it is. |
| 230 | |
| 231 | for (const pair of options) { |
| 232 | if (pair.value !== this._toggled) { |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame^] | 233 | title = pair.title(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | return title; |
| 238 | } |
| 239 | |
| 240 | toggled(): boolean { |
| 241 | return this._toggled; |
| 242 | } |
| 243 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 244 | setToggled(toggled: boolean): void { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 245 | console.assert(this.toggleable(), 'Shouldn\'t be toggling an untoggleable action', this.id()); |
| 246 | if (this._toggled === toggled) { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | this._toggled = toggled; |
| 251 | this.dispatchEventToListeners(Events.Toggled, toggled); |
| 252 | } |
| 253 | |
| 254 | options(): undefined|Array<ExtensionOption> { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 255 | return this.actionRegistration.options; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | contextTypes(): undefined|Array<unknown> { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 259 | if (this.actionRegistration.contextTypes) { |
| 260 | return this.actionRegistration.contextTypes(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 261 | } |
| 262 | return undefined; |
| 263 | } |
| 264 | |
| 265 | canInstantiate(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 266 | return Boolean(this.actionRegistration.loadActionDelegate); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | bindings(): Array<Binding>|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 270 | return this.actionRegistration.bindings; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | experiment(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 274 | return this.actionRegistration.experiment; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | condition(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 278 | return this.actionRegistration.condition; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | const registeredActionExtensions: Array<PreRegisteredAction> = []; |
| 283 | |
| 284 | const actionIdSet = new Set<string>(); |
| 285 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 286 | export function registerActionExtension(registration: ActionRegistration): void { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 287 | const actionId = registration.actionId; |
| 288 | if (actionIdSet.has(actionId)) { |
| 289 | throw new Error(`Duplicate Action id '${actionId}': ${new Error().stack}`); |
| 290 | } |
| 291 | actionIdSet.add(actionId); |
| 292 | registeredActionExtensions.push(new PreRegisteredAction(registration)); |
| 293 | } |
| 294 | |
| 295 | export function getRegisteredActionExtensions(): Array<PreRegisteredAction> { |
| 296 | return registeredActionExtensions.filter( |
| 297 | action => |
| 298 | Root.Runtime.Runtime.isDescriptorEnabled({experiment: action.experiment(), condition: action.condition()})); |
| 299 | } |
| 300 | |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame^] | 301 | export const enum Platforms { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 302 | All = 'All platforms', |
| 303 | Mac = 'mac', |
| 304 | WindowsLinux = 'windows,linux', |
| 305 | Android = 'Android', |
| 306 | } |
| 307 | |
| 308 | export const Events = { |
| 309 | Enabled: Symbol('Enabled'), |
| 310 | Toggled: Symbol('Toggled'), |
| 311 | }; |
| 312 | |
| 313 | export const ActionCategory = { |
| 314 | ELEMENTS: ls`Elements`, |
| 315 | SCREENSHOT: ls`Screenshot`, |
Andres Olivares | d7b94d6 | 2020-12-14 21:01:38 | [diff] [blame] | 316 | NETWORK: ls`Network`, |
Andres Olivares | 37a2177 | 2020-12-22 14:10:09 | [diff] [blame] | 317 | MEMORY: ls`Memory`, |
| 318 | JAVASCRIPT_PROFILER: ls`JavaScript Profiler`, |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 319 | }; |
| 320 | |
| 321 | type ActionCategory = typeof ActionCategory[keyof typeof ActionCategory]; |
| 322 | |
| 323 | export const enum IconClass { |
| 324 | LARGEICON_NODE_SEARCH = 'largeicon-node-search', |
Andres Olivares | d7b94d6 | 2020-12-14 21:01:38 | [diff] [blame] | 325 | LARGEICON_START_RECORDING = 'largeicon-start-recording', |
| 326 | LARGEICON_STOP_RECORDING = 'largeicon-stop-recording', |
Andres Olivares | 37a2177 | 2020-12-22 14:10:09 | [diff] [blame] | 327 | LARGEICON_REFRESH = 'largeicon-refresh', |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | export const enum KeybindSet { |
| 331 | DEVTOOLS_DEFAULT = 'devToolsDefault', |
| 332 | VS_CODE = 'vsCode', |
| 333 | } |
| 334 | |
| 335 | export interface ExtensionOption { |
| 336 | value: boolean; |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame^] | 337 | title: () => Platform.UIString.LocalizedString; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 338 | text?: string; |
| 339 | } |
| 340 | |
| 341 | export interface Binding { |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame^] | 342 | platform?: Platforms; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 343 | shortcut: string; |
| 344 | keybindSets?: Array<KeybindSet>; |
| 345 | } |
| 346 | |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 347 | /** |
| 348 | * The representation of an action extension to be registered. |
| 349 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 350 | export interface ActionRegistration { |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 351 | /** |
| 352 | * The unique id of an Action extension. |
| 353 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 354 | actionId: string; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 355 | /** |
| 356 | * The category with which the action is displayed in the UI. |
| 357 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 358 | category: ActionCategory; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 359 | /** |
| 360 | * The title with which the action is displayed in the UI. |
| 361 | */ |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame^] | 362 | title?: () => Platform.UIString.LocalizedString; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 363 | /** |
| 364 | * The type of the icon used to trigger the action. |
| 365 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 366 | iconClass?: string; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 367 | /** |
| 368 | * Whether the style of the icon toggles on interaction. |
| 369 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 370 | toggledIconClass?: string; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 371 | /** |
| 372 | * Whether the class 'toolbar-toggle-with-red-color' is toggled on the icon on interaction. |
| 373 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 374 | toggleWithRedColor?: boolean; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 375 | /** |
| 376 | * Words used to find an action in the Command Menu. |
| 377 | */ |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame^] | 378 | tags?: Array<() => Platform.UIString.LocalizedString>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 379 | /** |
| 380 | * Whether the action is toggleable. |
| 381 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 382 | toggleable?: boolean; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 383 | /** |
| 384 | * Loads the class that handles the action when it is triggered. |
| 385 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 386 | loadActionDelegate?: () => Promise<ActionDelegate>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 387 | /** |
| 388 | * Returns the classes that represent the 'context flavors' under which the action is available for triggering. |
| 389 | * The context of the application is described in 'flavors' that are usually views added and removed to the context |
| 390 | * as the user interacts with the application (e.g when the user moves across views). (See UI.Context) |
| 391 | * When the action is supposed to be available globally, that is, it does not depend on the application to have |
| 392 | * a specific context, the value of this property should be undefined. |
| 393 | * |
| 394 | * Because the method is synchronous, context types should be already loaded when the method is invoked. |
| 395 | * In the case that an action has context types it depends on, and they haven't been loaded yet, the function should |
| 396 | * return an empty array. Once the context types have been loaded, the function should return an array with all types |
| 397 | * that it depends on. |
| 398 | * |
| 399 | * The common pattern for implementing this function is relying on having the module with the corresponding context |
| 400 | * types loaded and stored when the related 'view' extension is loaded asynchronously. As an example: |
| 401 | * |
| 402 | * ```js |
| 403 | * let loadedElementsModule; |
| 404 | * |
| 405 | * async function loadElementsModule() { |
| 406 | * |
| 407 | * if (!loadedElementsModule) { |
| 408 | * loadedElementsModule = await import('./elements.js'); |
| 409 | * } |
| 410 | * return loadedElementsModule; |
| 411 | * } |
| 412 | * function maybeRetrieveContextTypes(getClassCallBack: (elementsModule: typeof Elements) => unknown[]): unknown[] { |
| 413 | * |
| 414 | * if (loadedElementsModule === undefined) { |
| 415 | * return []; |
| 416 | * } |
| 417 | * return getClassCallBack(loadedElementsModule); |
| 418 | * } |
| 419 | * UI.ActionRegistration.registerActionExtension({ |
| 420 | * |
| 421 | * contextTypes() { |
| 422 | * return maybeRetrieveContextTypes(Elements => [Elements.ElementsPanel.ElementsPanel]); |
| 423 | * } |
| 424 | * <...> |
| 425 | * }); |
| 426 | * ``` |
| 427 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 428 | contextTypes?: () => Array<unknown>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 429 | /** |
| 430 | * The descriptions for each of the two states in which toggleable can be. |
| 431 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 432 | options?: Array<ExtensionOption>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 433 | /** |
| 434 | * The description of the variables (e.g. platform, keys and keybind sets) under which a keyboard shortcut triggers the action. |
| 435 | * If a keybind must be available on all platforms, its 'platform' property must be undefined. The same applies to keybind sets |
| 436 | * and the keybindSet property. |
| 437 | * |
| 438 | * Keybinds also depend on the context types of their corresponding action, and so they will only be available when such context types |
| 439 | * are flavors of the current appliaction context. |
| 440 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 441 | bindings?: Array<Binding>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 442 | /** |
| 443 | * The name of the experiment an action is associated with. |
| 444 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 445 | experiment?: Root.Runtime.ExperimentName; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 446 | /** |
| 447 | * A condition represented as a string the action's availability depends on. |
| 448 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 449 | condition?: Root.Runtime.ConditionName; |
| 450 | } |