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'; |
Jack Franklin | a75ae7c | 2021-05-11 13:22:54 | [diff] [blame] | 6 | import type * as Platform from '../../core/platform/platform.js'; |
Tim van der Lippe | aa61faf | 2021-04-07 15:32:07 | [diff] [blame] | 7 | import * as Root from '../../core/root/root.js'; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 8 | |
| 9 | import {Context} from './Context.js'; |
| 10 | |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 11 | export interface ActionDelegate { |
| 12 | handleAction(_context: Context, _actionId: string): boolean; |
| 13 | } |
| 14 | |
Kateryna Prokopenko | a72448b | 2021-08-31 14:16:16 | [diff] [blame] | 15 | export class Action extends Common.ObjectWrapper.ObjectWrapper<EventTypes> { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 16 | private enabledInternal = true; |
| 17 | private toggledInternal = false; |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 18 | private actionRegistration: ActionRegistration; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 19 | constructor(actionRegistration: ActionRegistration) { |
| 20 | super(); |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 21 | this.actionRegistration = actionRegistration; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | id(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 25 | return this.actionRegistration.actionId; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | async execute(): Promise<boolean> { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 29 | if (!this.actionRegistration.loadActionDelegate) { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 30 | return false; |
| 31 | } |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 32 | const delegate = await this.actionRegistration.loadActionDelegate(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 33 | const actionId = this.id(); |
| 34 | return delegate.handleAction(Context.instance(), actionId); |
| 35 | } |
| 36 | |
| 37 | icon(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 38 | return this.actionRegistration.iconClass; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | toggledIcon(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 42 | return this.actionRegistration.toggledIconClass; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | toggleWithRedColor(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 46 | return Boolean(this.actionRegistration.toggleWithRedColor); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 47 | } |
| 48 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 49 | setEnabled(enabled: boolean): void { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 50 | if (this.enabledInternal === enabled) { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 54 | this.enabledInternal = enabled; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 55 | this.dispatchEventToListeners(Events.Enabled, enabled); |
| 56 | } |
| 57 | |
| 58 | enabled(): boolean { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 59 | return this.enabledInternal; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | category(): string { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 63 | return this.actionRegistration.category; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 64 | } |
| 65 | |
Andres Olivares | 344120f | 2020-12-07 17:43:28 | [diff] [blame] | 66 | tags(): string|void { |
| 67 | if (this.actionRegistration.tags) { |
| 68 | // 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] | 69 | return this.actionRegistration.tags.map(tag => tag()).join('\0'); |
Andres Olivares | 344120f | 2020-12-07 17:43:28 | [diff] [blame] | 70 | } |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | toggleable(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 74 | return Boolean(this.actionRegistration.toggleable); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | title(): string { |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 78 | let title = this.actionRegistration.title ? this.actionRegistration.title() : ''; |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 79 | const options = this.actionRegistration.options; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 80 | if (options) { |
| 81 | // Actions with an 'options' property don't have a title field. Instead, the displayed |
| 82 | // title is taken from the 'title' property of the option that is not active. Only one of the |
| 83 | // two options can be active at a given moment and the 'toggled' property of the action along |
| 84 | // with the 'value' of the options are used to determine which one it is. |
| 85 | |
| 86 | for (const pair of options) { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 87 | if (pair.value !== this.toggledInternal) { |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 88 | title = pair.title(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | return title; |
| 93 | } |
| 94 | |
| 95 | toggled(): boolean { |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 96 | return this.toggledInternal; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 97 | } |
| 98 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 99 | setToggled(toggled: boolean): void { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 100 | console.assert(this.toggleable(), 'Shouldn\'t be toggling an untoggleable action', this.id()); |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 101 | if (this.toggledInternal === toggled) { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 102 | return; |
| 103 | } |
| 104 | |
Jan Scheffler | 01eab3c | 2021-08-16 17:18:07 | [diff] [blame] | 105 | this.toggledInternal = toggled; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 106 | this.dispatchEventToListeners(Events.Toggled, toggled); |
| 107 | } |
| 108 | |
| 109 | options(): undefined|Array<ExtensionOption> { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 110 | return this.actionRegistration.options; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 111 | } |
| 112 | |
Sigurd Schneider | 61fc9bd | 2021-07-14 09:01:53 | [diff] [blame] | 113 | contextTypes(): undefined|Array<Function> { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 114 | if (this.actionRegistration.contextTypes) { |
| 115 | return this.actionRegistration.contextTypes(); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 116 | } |
| 117 | return undefined; |
| 118 | } |
| 119 | |
| 120 | canInstantiate(): boolean { |
Tim van der Lippe | ba0e645 | 2021-01-07 13:46:34 | [diff] [blame] | 121 | return Boolean(this.actionRegistration.loadActionDelegate); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bindings(): Array<Binding>|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 125 | return this.actionRegistration.bindings; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | experiment(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 129 | return this.actionRegistration.experiment; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | condition(): string|undefined { |
Jack Franklin | 01d09b0 | 2020-12-02 15:15:20 | [diff] [blame] | 133 | return this.actionRegistration.condition; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 134 | } |
Andres Olivares | 4dba130 | 2021-01-28 23:17:32 | [diff] [blame] | 135 | |
| 136 | order(): number|undefined { |
| 137 | return this.actionRegistration.order; |
| 138 | } |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 139 | } |
| 140 | |
Andres Olivares | f2a2ddd | 2021-02-03 17:27:51 | [diff] [blame] | 141 | const registeredActionExtensions: Array<Action> = []; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 142 | |
| 143 | const actionIdSet = new Set<string>(); |
| 144 | |
Tim van der Lippe | d946df0 | 2020-12-14 14:35:49 | [diff] [blame] | 145 | export function registerActionExtension(registration: ActionRegistration): void { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 146 | const actionId = registration.actionId; |
| 147 | if (actionIdSet.has(actionId)) { |
| 148 | throw new Error(`Duplicate Action id '${actionId}': ${new Error().stack}`); |
| 149 | } |
| 150 | actionIdSet.add(actionId); |
Andres Olivares | f2a2ddd | 2021-02-03 17:27:51 | [diff] [blame] | 151 | registeredActionExtensions.push(new Action(registration)); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 152 | } |
| 153 | |
Andrés Olivares | b0fcd18 | 2023-02-21 10:13:28 | [diff] [blame] | 154 | export function reset(): void { |
| 155 | actionIdSet.clear(); |
| 156 | registeredActionExtensions.length = 0; |
| 157 | } |
| 158 | |
Andres Olivares | f2a2ddd | 2021-02-03 17:27:51 | [diff] [blame] | 159 | export function getRegisteredActionExtensions(): Array<Action> { |
Andres Olivares | 4dba130 | 2021-01-28 23:17:32 | [diff] [blame] | 160 | return registeredActionExtensions |
| 161 | .filter( |
| 162 | action => Root.Runtime.Runtime.isDescriptorEnabled( |
| 163 | {experiment: action.experiment(), condition: action.condition()})) |
| 164 | .sort((firstAction, secondAction) => { |
| 165 | const order1 = firstAction.order() || 0; |
| 166 | const order2 = secondAction.order() || 0; |
| 167 | return order1 - order2; |
| 168 | }); |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 169 | } |
| 170 | |
Andres Olivares | 975c302 | 2021-03-29 13:45:24 | [diff] [blame] | 171 | export function maybeRemoveActionExtension(actionId: string): boolean { |
| 172 | const actionIndex = registeredActionExtensions.findIndex(action => action.id() === actionId); |
| 173 | if (actionIndex < 0 || !actionIdSet.delete(actionId)) { |
| 174 | return false; |
| 175 | } |
| 176 | registeredActionExtensions.splice(actionIndex, 1); |
| 177 | return true; |
| 178 | } |
| 179 | |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 180 | export const enum Platforms { |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 181 | All = 'All platforms', |
| 182 | Mac = 'mac', |
| 183 | WindowsLinux = 'windows,linux', |
| 184 | Android = 'Android', |
Andres Olivares | 70556d6 | 2021-02-02 19:09:01 | [diff] [blame] | 185 | Windows = 'windows', |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 186 | } |
| 187 | |
Kateryna Prokopenko | a72448b | 2021-08-31 14:16:16 | [diff] [blame] | 188 | export const enum Events { |
| 189 | Enabled = 'Enabled', |
| 190 | Toggled = 'Toggled', |
| 191 | } |
| 192 | |
| 193 | export type EventTypes = { |
| 194 | [Events.Enabled]: boolean, |
| 195 | [Events.Toggled]: boolean, |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 196 | }; |
| 197 | |
Vidal Guillermo Diazleal Ortega | 9b0bc5b | 2021-02-22 22:12:04 | [diff] [blame] | 198 | // TODO(crbug.com/1181019) |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 199 | export const ActionCategory = { |
Vidal Guillermo Diazleal Ortega | 9b0bc5b | 2021-02-22 22:12:04 | [diff] [blame] | 200 | ELEMENTS: 'Elements', |
| 201 | SCREENSHOT: 'Screenshot', |
| 202 | NETWORK: 'Network', |
| 203 | MEMORY: 'Memory', |
| 204 | JAVASCRIPT_PROFILER: 'JavaScript Profiler', |
| 205 | CONSOLE: 'Console', |
| 206 | PERFORMANCE: 'Performance', |
| 207 | MOBILE: 'Mobile', |
| 208 | SENSORS: 'Sensors', |
| 209 | HELP: 'Help', |
| 210 | INPUTS: 'Inputs', |
| 211 | LAYERS: 'Layers', |
| 212 | NAVIGATION: 'Navigation', |
| 213 | DRAWER: 'Drawer', |
| 214 | GLOBAL: 'Global', |
| 215 | RESOURCES: 'Resources', |
| 216 | BACKGROUND_SERVICES: 'Background Services', |
| 217 | SETTINGS: 'Settings', |
| 218 | DEBUGGER: 'Debugger', |
Vidal Guillermo Diazleal Ortega | 9b0bc5b | 2021-02-22 22:12:04 | [diff] [blame] | 219 | SOURCES: 'Sources', |
Jacky Hu | 4853b34 | 2022-04-29 10:47:21 | [diff] [blame] | 220 | RENDERING: 'Rendering', |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 221 | }; |
| 222 | |
| 223 | type ActionCategory = typeof ActionCategory[keyof typeof ActionCategory]; |
| 224 | |
| 225 | export const enum IconClass { |
Kateryna Prokopenko | 910cb53 | 2023-03-28 09:36:46 | [diff] [blame^] | 226 | LARGEICON_NODE_SEARCH = 'select-element', |
Andres Olivares | d7b94d6 | 2020-12-14 21:01:38 | [diff] [blame] | 227 | LARGEICON_START_RECORDING = 'largeicon-start-recording', |
| 228 | LARGEICON_STOP_RECORDING = 'largeicon-stop-recording', |
Andres Olivares | 37a2177 | 2020-12-22 14:10:09 | [diff] [blame] | 229 | LARGEICON_REFRESH = 'largeicon-refresh', |
Andres Olivares | 844c58d | 2021-01-26 13:09:07 | [diff] [blame] | 230 | LARGEICON_CLEAR = 'largeicon-clear', |
| 231 | LARGEICON_VISIBILITY = 'largeicon-visibility', |
Kateryna Prokopenko | 910cb53 | 2023-03-28 09:36:46 | [diff] [blame^] | 232 | LARGEICON_PHONE = 'devices', |
Andres Olivares | a7b7b49 | 2021-01-28 16:12:37 | [diff] [blame] | 233 | LARGEICON_PLAY = 'largeicon-play', |
Alex Rudenko | a2ffe4a | 2021-05-17 13:36:31 | [diff] [blame] | 234 | LARGEICON_DOWNLOAD = 'largeicon-download', |
Andres Olivares | a7b7b49 | 2021-01-28 16:12:37 | [diff] [blame] | 235 | LARGEICON_PAUSE = 'largeicon-pause', |
| 236 | LARGEICON_RESUME = 'largeicon-resume', |
Andres Olivares | 27dccc0 | 2021-02-01 13:31:16 | [diff] [blame] | 237 | LARGEICON_TRASH_BIN = 'largeicon-trash-bin', |
Kateryna Prokopenko | 910cb53 | 2023-03-28 09:36:46 | [diff] [blame^] | 238 | LARGEICON_SETTINGS_GEAR = 'gear', |
Andres Olivares | 7ed9807 | 2021-02-02 18:13:58 | [diff] [blame] | 239 | LARGEICON_STEP_OVER = 'largeicon-step-over', |
| 240 | LARGE_ICON_STEP_INTO = 'largeicon-step-into', |
| 241 | LARGE_ICON_STEP = 'largeicon-step', |
| 242 | LARGE_ICON_STEP_OUT = 'largeicon-step-out', |
| 243 | LARGE_ICON_DEACTIVATE_BREAKPOINTS = 'largeicon-deactivate-breakpoints', |
Andres Olivares | 70556d6 | 2021-02-02 19:09:01 | [diff] [blame] | 244 | LARGE_ICON_ADD = 'largeicon-add', |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | export const enum KeybindSet { |
| 248 | DEVTOOLS_DEFAULT = 'devToolsDefault', |
| 249 | VS_CODE = 'vsCode', |
| 250 | } |
| 251 | |
| 252 | export interface ExtensionOption { |
| 253 | value: boolean; |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 254 | title: () => Platform.UIString.LocalizedString; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 255 | text?: string; |
| 256 | } |
| 257 | |
| 258 | export interface Binding { |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 259 | platform?: Platforms; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 260 | shortcut: string; |
| 261 | keybindSets?: Array<KeybindSet>; |
| 262 | } |
| 263 | |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 264 | /** |
| 265 | * The representation of an action extension to be registered. |
| 266 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 267 | export interface ActionRegistration { |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 268 | /** |
| 269 | * The unique id of an Action extension. |
| 270 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 271 | actionId: string; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 272 | /** |
| 273 | * The category with which the action is displayed in the UI. |
| 274 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 275 | category: ActionCategory; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 276 | /** |
| 277 | * The title with which the action is displayed in the UI. |
| 278 | */ |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 279 | title?: () => Platform.UIString.LocalizedString; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 280 | /** |
| 281 | * The type of the icon used to trigger the action. |
| 282 | */ |
Andres Olivares | 844c58d | 2021-01-26 13:09:07 | [diff] [blame] | 283 | iconClass?: IconClass; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 284 | /** |
| 285 | * Whether the style of the icon toggles on interaction. |
| 286 | */ |
Andres Olivares | 844c58d | 2021-01-26 13:09:07 | [diff] [blame] | 287 | toggledIconClass?: IconClass; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 288 | /** |
| 289 | * Whether the class 'toolbar-toggle-with-red-color' is toggled on the icon on interaction. |
| 290 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 291 | toggleWithRedColor?: boolean; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 292 | /** |
| 293 | * Words used to find an action in the Command Menu. |
| 294 | */ |
Andres Olivares | 4ce36a5 | 2021-01-18 18:35:05 | [diff] [blame] | 295 | tags?: Array<() => Platform.UIString.LocalizedString>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 296 | /** |
| 297 | * Whether the action is toggleable. |
| 298 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 299 | toggleable?: boolean; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 300 | /** |
Andres Olivares | a0cdb38 | 2021-01-21 15:44:33 | [diff] [blame] | 301 | * Loads the class that handles the action when it is triggered. The common pattern for implementing |
| 302 | * this function relies on having the module that contains the action’s handler lazily loaded. For example: |
| 303 | * ```js |
| 304 | * let loadedElementsModule; |
| 305 | * |
| 306 | * async function loadElementsModule() { |
| 307 | * |
| 308 | * if (!loadedElementsModule) { |
| 309 | * loadedElementsModule = await import('./elements.js'); |
| 310 | * } |
| 311 | * return loadedElementsModule; |
| 312 | * } |
| 313 | * UI.ActionRegistration.registerActionExtension({ |
| 314 | * <...> |
| 315 | * async loadActionDelegate() { |
| 316 | * const Elements = await loadElementsModule(); |
| 317 | * return Elements.ElementsPanel.ElementsActionDelegate.instance(); |
| 318 | * }, |
| 319 | * <...> |
| 320 | * }); |
| 321 | * ``` |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 322 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 323 | loadActionDelegate?: () => Promise<ActionDelegate>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 324 | /** |
| 325 | * Returns the classes that represent the 'context flavors' under which the action is available for triggering. |
| 326 | * The context of the application is described in 'flavors' that are usually views added and removed to the context |
| 327 | * as the user interacts with the application (e.g when the user moves across views). (See UI.Context) |
| 328 | * When the action is supposed to be available globally, that is, it does not depend on the application to have |
| 329 | * a specific context, the value of this property should be undefined. |
| 330 | * |
| 331 | * Because the method is synchronous, context types should be already loaded when the method is invoked. |
| 332 | * In the case that an action has context types it depends on, and they haven't been loaded yet, the function should |
| 333 | * return an empty array. Once the context types have been loaded, the function should return an array with all types |
| 334 | * that it depends on. |
| 335 | * |
| 336 | * The common pattern for implementing this function is relying on having the module with the corresponding context |
| 337 | * types loaded and stored when the related 'view' extension is loaded asynchronously. As an example: |
| 338 | * |
| 339 | * ```js |
| 340 | * let loadedElementsModule; |
| 341 | * |
| 342 | * async function loadElementsModule() { |
| 343 | * |
| 344 | * if (!loadedElementsModule) { |
| 345 | * loadedElementsModule = await import('./elements.js'); |
| 346 | * } |
| 347 | * return loadedElementsModule; |
| 348 | * } |
| 349 | * function maybeRetrieveContextTypes(getClassCallBack: (elementsModule: typeof Elements) => unknown[]): unknown[] { |
| 350 | * |
| 351 | * if (loadedElementsModule === undefined) { |
| 352 | * return []; |
| 353 | * } |
| 354 | * return getClassCallBack(loadedElementsModule); |
| 355 | * } |
| 356 | * UI.ActionRegistration.registerActionExtension({ |
| 357 | * |
| 358 | * contextTypes() { |
| 359 | * return maybeRetrieveContextTypes(Elements => [Elements.ElementsPanel.ElementsPanel]); |
| 360 | * } |
| 361 | * <...> |
| 362 | * }); |
| 363 | * ``` |
| 364 | */ |
Sigurd Schneider | 61fc9bd | 2021-07-14 09:01:53 | [diff] [blame] | 365 | contextTypes?: () => Array<Function>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 366 | /** |
Andres Olivares | a0cdb38 | 2021-01-21 15:44:33 | [diff] [blame] | 367 | * 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] | 368 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 369 | options?: Array<ExtensionOption>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 370 | /** |
| 371 | * The description of the variables (e.g. platform, keys and keybind sets) under which a keyboard shortcut triggers the action. |
| 372 | * If a keybind must be available on all platforms, its 'platform' property must be undefined. The same applies to keybind sets |
| 373 | * and the keybindSet property. |
| 374 | * |
| 375 | * Keybinds also depend on the context types of their corresponding action, and so they will only be available when such context types |
| 376 | * are flavors of the current appliaction context. |
| 377 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 378 | bindings?: Array<Binding>; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 379 | /** |
Andres Olivares | a0cdb38 | 2021-01-21 15:44:33 | [diff] [blame] | 380 | * The name of the experiment an action is associated with. Enabling and disabling the declared |
| 381 | * experiment will enable and disable the action respectively. |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 382 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 383 | experiment?: Root.Runtime.ExperimentName; |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 384 | /** |
Andres Olivares | a0cdb38 | 2021-01-21 15:44:33 | [diff] [blame] | 385 | * A condition represented as a string the action's availability depends on. Conditions come |
| 386 | * from the queryParamsObject defined in Runtime and just as the experiment field, they determine the availability |
| 387 | * of the setting. A condition can be negated by prepending a ‘!’ to the value of the condition |
| 388 | * property and in that case the behaviour of the action's availability will be inverted. |
Andres Olivares | 3f49f13 | 2020-12-03 13:10:27 | [diff] [blame] | 389 | */ |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 390 | condition?: Root.Runtime.ConditionName; |
Andres Olivares | 4dba130 | 2021-01-28 23:17:32 | [diff] [blame] | 391 | /** |
| 392 | * Used to sort actions when all registered actions are queried. |
| 393 | */ |
| 394 | order?: number; |
Andres Olivares | 0e3a9e8 | 2020-12-01 14:03:20 | [diff] [blame] | 395 | } |