@foxglove/extension 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foxglove/extension",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "license": "MIT",
5
5
  "author": {
6
6
  "name": "Foxglove Technologies",
@@ -17,8 +17,5 @@
17
17
  "devDependencies": {
18
18
  "@foxglove/tsconfig": "2.0.0",
19
19
  "typescript": "5.3.3"
20
- },
21
- "dependencies": {
22
- "@foxglove/studio": "2.4.0"
23
20
  }
24
- }
21
+ }
@@ -0,0 +1,77 @@
1
+ // Immutable types borrowed from ts-essentials: https://github.com/ts-essentials/ts-essentials
2
+ // https://github.com/ts-essentials/ts-essentials/blob/master/lib/deep-readonly/README.md
3
+
4
+ /**
5
+ The MIT License
6
+
7
+ Copyright (c) 2018-2019 Chris Kaczor (github.com/krzkaczor)
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in
17
+ all copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
+ THE SOFTWARE.
26
+ */
27
+
28
+ // eslint-disable-next-line no-restricted-syntax
29
+ export type Primitive = string | number | boolean | bigint | symbol | undefined | null;
30
+
31
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
+ export type AnyArray<Type = any> = Array<Type> | ReadonlyArray<Type>;
33
+
34
+ // eslint-disable-next-line @typescript-eslint/ban-types
35
+ export type Builtin = Primitive | Function | Date | Error | RegExp;
36
+
37
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
+ export type IsTuple<Type> = Type extends readonly any[]
39
+ ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
+ any[] extends Type
41
+ ? never
42
+ : Type
43
+ : never;
44
+
45
+ export type IsUnknown<Type> =
46
+ IsAny<Type> extends true ? false : unknown extends Type ? true : false;
47
+
48
+ // https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
49
+ export type IsAny<Type> = 0 extends 1 & Type ? true : false;
50
+
51
+ export type Immutable<Type> =
52
+ Type extends Exclude<Builtin, Error>
53
+ ? Type
54
+ : Type extends Map<infer Keys, infer Values>
55
+ ? ReadonlyMap<Immutable<Keys>, Immutable<Values>>
56
+ : Type extends ReadonlyMap<infer Keys, infer Values>
57
+ ? ReadonlyMap<Immutable<Keys>, Immutable<Values>>
58
+ : Type extends WeakMap<infer Keys, infer Values>
59
+ ? WeakMap<Immutable<Keys>, Immutable<Values>>
60
+ : Type extends Set<infer Values>
61
+ ? ReadonlySet<Immutable<Values>>
62
+ : Type extends ReadonlySet<infer Values>
63
+ ? ReadonlySet<Immutable<Values>>
64
+ : Type extends WeakSet<infer Values>
65
+ ? WeakSet<Immutable<Values>>
66
+ : Type extends Promise<infer Value>
67
+ ? Promise<Immutable<Value>>
68
+ : Type extends AnyArray<infer Values>
69
+ ? Type extends IsTuple<Type>
70
+ ? { readonly [Key in keyof Type]: Immutable<Type[Key]> }
71
+ : ReadonlyArray<Immutable<Values>>
72
+ : // eslint-disable-next-line @typescript-eslint/ban-types
73
+ Type extends {}
74
+ ? { readonly [Key in keyof Type]: Immutable<Type[Key]> }
75
+ : IsUnknown<Type> extends true
76
+ ? unknown
77
+ : Readonly<Type>;
package/src/index.ts CHANGED
@@ -1 +1,832 @@
1
- export * from "@foxglove/studio";
1
+ import type { Immutable } from "./immutable";
2
+
3
+ export type { Immutable } from "./immutable";
4
+
5
+ // Valid types for parameter data (such as rosparams)
6
+ export type ParameterValue =
7
+ | undefined
8
+ | boolean
9
+ | number
10
+ | string
11
+ | Date
12
+ | Uint8Array
13
+ | ParameterValue[]
14
+ | { [key: string]: ParameterValue };
15
+
16
+ // Valid types for global variables
17
+ export type VariableValue =
18
+ | undefined
19
+ | boolean
20
+ | number
21
+ | string
22
+ | VariableValue[]
23
+ | { [key: string]: VariableValue };
24
+
25
+ export type VariableStruct = { [key: string]: VariableValue };
26
+
27
+ // Valid types for application settings
28
+ export type AppSettingValue = string | number | boolean | undefined;
29
+
30
+ export interface Time {
31
+ sec: number;
32
+ nsec: number;
33
+ }
34
+
35
+ /**
36
+ * A topic is a namespace for specific types of messages
37
+ */
38
+ export type Topic = {
39
+ /**
40
+ * topic name i.e. "/some/topic"
41
+ */
42
+ name: string;
43
+ /**
44
+ * @deprecated Renamed to `schemaName`. `datatype` will be removed in a future release.
45
+ */
46
+ datatype: string;
47
+ /**
48
+ * The schema name is an identifier for the types of messages on this topic. Typically this is the
49
+ * fully-qualified name of the message schema. The fully-qualified name depends on the data source
50
+ * and data loaded by the data source.
51
+ *
52
+ * i.e. `package.Message` in protobuf-like serialization or `pkg/Msg` in ROS systems.
53
+ */
54
+ schemaName: string;
55
+
56
+ /**
57
+ * Lists any additional schema names available for subscribers on the topic. When subscribing to
58
+ * a topic, the panel can request messages be automatically converted from schemaName into one
59
+ * of the convertibleTo schemas using the convertTo option.
60
+ */
61
+ convertibleTo?: readonly string[];
62
+ };
63
+
64
+ export type Subscription = {
65
+ topic: string;
66
+
67
+ /**
68
+ * If a topic has additional schema names, specifying a schema name will convert messages on that
69
+ * topic to the convertTo schema using a registered message converter. MessageEvents for the
70
+ * subscription will contain the converted message and an originalMessageEvent field with the
71
+ * original message event.
72
+ */
73
+ convertTo?: string;
74
+
75
+ /**
76
+ * Setting preload to _true_ hints to the data source that it should attempt to load all available
77
+ * messages for the topic. The default behavior is to only load messages for the current frame.
78
+ *
79
+ * **Only** topics with `preload: true` are available in the `allFrames` render state.
80
+ */
81
+ preload?: boolean;
82
+ };
83
+
84
+ /**
85
+ * A message event frames message data with the topic and receive time
86
+ */
87
+ export type MessageEvent<T = unknown> = {
88
+ /** The topic name this message was received on, i.e. "/some/topic" */
89
+ topic: string;
90
+ /**
91
+ * The schema name is an identifier for the schema of the message within the message event.
92
+ */
93
+ schemaName: string;
94
+ /**
95
+ * The time in nanoseconds this message was received. This may be set by the
96
+ * local system clock or the data source, depending on the data source used
97
+ * and whether time is simulated via a /clock topic or similar mechanism.
98
+ * The timestamp is often nanoseconds since the UNIX epoch, but may be
99
+ * relative to another event such as system boot time or simulation start
100
+ * time depending on the context.
101
+ */
102
+ receiveTime: Time;
103
+ /**
104
+ * The time in nanoseconds this message was originally published. This is
105
+ * only available for some data sources. The timestamp is often nanoseconds
106
+ * since the UNIX epoch, but may be relative to another event such as system
107
+ * boot time or simulation start time depending on the context.
108
+ */
109
+ publishTime?: Time;
110
+ /** The deserialized message as a JavaScript object. */
111
+ message: T;
112
+ /**
113
+ * The approximate size of this message in its serialized form. This can be
114
+ * useful for statistics tracking and cache eviction.
115
+ */
116
+ sizeInBytes: number;
117
+
118
+ /**
119
+ * When subscribing to a topic using the `convertTo` option, the message event `message`
120
+ * contains the converted message and the originalMessageEvent field contains the original
121
+ * un-converted message event.
122
+ */
123
+ originalMessageEvent?: MessageEvent;
124
+ };
125
+
126
+ export interface LayoutActions {
127
+ /** Open a new panel or update an existing panel in the layout. */
128
+ addPanel(params: {
129
+ /**
130
+ * Where to position the panel. Currently, only "sibling" is supported which indicates the
131
+ * new panel will be adjacent to the calling panel.
132
+ */
133
+ position: "sibling";
134
+
135
+ /**
136
+ * The type of panel to open. For extension panels, this `"extensionName.panelName"` where
137
+ * extensionName is the `name` field from the extension's package.json, and panelName is the
138
+ * name provided to `registerPanel()`.
139
+ */
140
+ type: string;
141
+
142
+ /**
143
+ * Whether to update an existing sibling panel of the same type, if it already exists. If false
144
+ * or omitted, a new panel will always be added.
145
+ *
146
+ * @deprecated This parameter is only supported for built-in panels at this time.
147
+ */
148
+ updateIfExists?: boolean;
149
+
150
+ /**
151
+ * A function that returns the state for the new panel. If updating an existing panel, the
152
+ * existing state will be passed in.
153
+ * @see `updateIfExists`
154
+ */
155
+ getState(existingState?: unknown): unknown;
156
+ }): void;
157
+ }
158
+
159
+ export type RenderState = {
160
+ /**
161
+ * The latest messages for the current render frame. These are new messages since the last render frame.
162
+ */
163
+ currentFrame?: MessageEvent[];
164
+
165
+ /**
166
+ * True if the data source performed a seek. This indicates that some data may have been skipped
167
+ * (never appeared in the `currentFrame`), so panels should clear out any stale state to avoid
168
+ * displaying incorrect data.
169
+ */
170
+ didSeek?: boolean;
171
+
172
+ /**
173
+ * All available messages. Best-effort list of all available messages.
174
+ */
175
+ allFrames?: MessageEvent[];
176
+
177
+ /**
178
+ * Map of current parameter values. Parameters are key/value pairs associated with the data
179
+ * source, and may not be available for all data sources. For example, ROS 1 live connections
180
+ * support parameters through the Parameter Server <http://wiki.ros.org/Parameter%20Server>.
181
+ */
182
+ parameters?: Map<string, ParameterValue>;
183
+
184
+ /**
185
+ * Transient panel state shared between panels of the same type. This can be any data a
186
+ * panel author wishes to share between panels.
187
+ */
188
+ sharedPanelState?: Record<string, unknown>;
189
+
190
+ /**
191
+ * Map of current Studio variables. Variables are key/value pairs that are globally accessible
192
+ * to panels and scripts in the current layout. See
193
+ * <https://docs.foxglove.dev/docs/visualization/variables> for more information.
194
+ */
195
+ variables?: Map<string, VariableValue>;
196
+
197
+ /**
198
+ * List of available topics. This list includes subscribed and unsubscribed topics.
199
+ */
200
+ topics?: Topic[];
201
+
202
+ /**
203
+ * A timestamp value indicating the current playback time.
204
+ */
205
+ currentTime?: Time;
206
+
207
+ /**
208
+ * The start timestamp of the playback range for the current data source. For offline files it
209
+ * is expected to be present. For live connections, the start time may or may not be present
210
+ * depending on the data source.
211
+ */
212
+ startTime?: Time;
213
+
214
+ /**
215
+ * The end timestamp of the playback range for the current data source. For offline files it
216
+ * is expected to be present. For live connections, the end time may or may not be present
217
+ * depending on the data source.
218
+ */
219
+ endTime?: Time;
220
+
221
+ /**
222
+ * A seconds value indicating a preview time. The preview time is set when a user hovers
223
+ * over the seek bar or when a panel sets the preview time explicitly. The preview time
224
+ * is a seconds value within the playback range.
225
+ *
226
+ * i.e. A plot panel may set the preview time when a user is hovering over the plot to signal
227
+ * to other panels where the user is currently hovering and allow them to render accordingly.
228
+ */
229
+ previewTime?: number | undefined;
230
+
231
+ /** The color scheme currently in use throughout the app. */
232
+ colorScheme?: "dark" | "light";
233
+
234
+ /** Application settings. This will only contain subscribed application setting key/values */
235
+ appSettings?: Map<string, AppSettingValue>;
236
+ };
237
+
238
+ export type PanelExtensionContext = {
239
+ /**
240
+ * The root element for the panel. Add your panel elements as children under this element.
241
+ */
242
+ readonly panelElement: HTMLDivElement;
243
+
244
+ /**
245
+ * Initial panel state
246
+ */
247
+ readonly initialState: unknown;
248
+
249
+ /** Actions the panel may perform related to the user's current layout. */
250
+ readonly layout: LayoutActions;
251
+
252
+ /**
253
+ * Identifies the semantics of the data being played back, such as which topics or parameters
254
+ * are semantically meaningful or normalization conventions to use. This typically maps to a
255
+ * shorthand identifier for a robotics framework such as "ros1", "ros2", or "ulog". See the MCAP
256
+ * profiles concept at <https://github.com/foxglove/mcap/blob/main/docs/specification/appendix.md#well-known-profiles>.
257
+ */
258
+ readonly dataSourceProfile?: string;
259
+
260
+ /**
261
+ * Subscribe to updates on this field within the render state. Render will only be invoked when
262
+ * this field changes.
263
+ */
264
+ watch: (field: keyof RenderState) => void;
265
+
266
+ /**
267
+ * Save arbitrary object as persisted panel state. This state is persisted for the panel
268
+ * within a layout.
269
+ *
270
+ * The state value should be JSON serializable.
271
+ */
272
+ saveState: (state: Partial<unknown>) => void;
273
+
274
+ /**
275
+ * Set the value of parameter name to value.
276
+ *
277
+ * @param name The name of the parameter to set.
278
+ * @param value The new value of the parameter.
279
+ */
280
+ setParameter: (name: string, value: ParameterValue) => void;
281
+
282
+ /**
283
+ * Set the transient state shared by panels of the same type as the caller of this function.
284
+ * This will not be persisted in the layout.
285
+ */
286
+ setSharedPanelState: (state: undefined | Record<string, unknown>) => void;
287
+
288
+ /**
289
+ * Set the value of variable name to value.
290
+ *
291
+ * @param name The name of the variable to set.
292
+ * @param value The new value of the variable.
293
+ */
294
+ setVariable: (name: string, value: VariableValue) => void;
295
+
296
+ /**
297
+ * Set the active preview time. Setting the preview time to undefined clears the preview time.
298
+ */
299
+ setPreviewTime: (time: number | undefined) => void;
300
+
301
+ /**
302
+ * Seek playback to the given time. Behaves as if the user had clicked the playback bar
303
+ * to seek.
304
+ *
305
+ * Clients can pass a number or alternatively a Time object for greater precision.
306
+ */
307
+ seekPlayback?: (time: number | Time) => void;
308
+
309
+ /**
310
+ * Subscribe to an array of topic names.
311
+ *
312
+ * Subscribe will update the current subscriptions to the list of topic names. Passing an empty
313
+ * array will unsubscribe from all topics.
314
+ *
315
+ * Calling subscribe with an empty array of topics is analagous to unsubscribeAll.
316
+ *
317
+ * @deprecated Use `subscribe` with an array of Subscription objects instead.
318
+ */
319
+ subscribe(topics: string[]): void;
320
+
321
+ /**
322
+ * Subscribe to an array of topics with additional options for each subscription.
323
+ *
324
+ * Subscribe will update the current subscriptions to the new list of Subscriptions and
325
+ * unsubscribe from any previously subscribed topics no longer in the Subscription list. Passing
326
+ * an empty array will unsubscribe from all topics.
327
+ *
328
+ * Calling subscribe with an empty array is analagous to unsubscribeAll.
329
+ */
330
+ subscribe(subscriptions: Subscription[]): void;
331
+
332
+ /**
333
+ * Unsubscribe from all topics.
334
+ *
335
+ * Note: This is analagous to calling subscribe([]) with an empty array of topics.
336
+ */
337
+ unsubscribeAll(): void;
338
+
339
+ /**
340
+ * Subscribe to any changes in application settings for an array of setting names.
341
+ */
342
+ subscribeAppSettings(settings: string[]): void;
343
+
344
+ /**
345
+ * Indicate intent to publish messages on a specific topic.
346
+ *
347
+ * @param topic The topic on which the extension will publish messages.
348
+ * @param schemaName The name of the schema that the published messages will conform to.
349
+ * @param options Options passed to the current data source for additional configuration.
350
+ */
351
+ advertise?(topic: string, schemaName: string, options?: Record<string, unknown>): void;
352
+
353
+ /**
354
+ * Indicate that you no longer want to advertise on this topic.
355
+ */
356
+ unadvertise?(topic: string): void;
357
+
358
+ /**
359
+ * Publish a message on a given topic. You must first advertise on the topic before publishing.
360
+ *
361
+ * @param topic The name of the topic to publish the message on
362
+ * @param message The message to publish
363
+ */
364
+ publish?(topic: string, message: unknown): void;
365
+
366
+ /**
367
+ * Call a service.
368
+ *
369
+ * @param service The name of the service to call
370
+ * @param request The request payload for the service call
371
+ * @returns A promise that resolves when the result is available or rejected with an error
372
+ */
373
+ callService?(service: string, request: unknown): Promise<unknown>;
374
+
375
+ /**
376
+ * Process render events for the panel. Each render event receives a render state and a done callback.
377
+ * Render events occur frequently (60hz, 30hz, etc).
378
+ *
379
+ * The done callback should be called once the panel has rendered the render state.
380
+ */
381
+ onRender?: (renderState: Immutable<RenderState>, done: () => void) => void;
382
+
383
+ /**
384
+ * Updates the panel's settings editor. Call this every time you want to update
385
+ * the representation of the panel settings in the editor.
386
+ */
387
+ updatePanelSettingsEditor(settings: Immutable<SettingsTree>): void;
388
+
389
+ /**
390
+ * Updates the panel's default title. Users can always override the default title by editing it
391
+ * manually. A value of `undefined` will display the panel's name in the title bar.
392
+ */
393
+ setDefaultPanelTitle(defaultTitle: string | undefined): void;
394
+ };
395
+
396
+ export type ExtensionPanelRegistration = {
397
+ // Unique name of the panel within your extension
398
+ //
399
+ // NOTE: Panel names within your extension must be unique. The panel name identifies this panel
400
+ // within a layout. Changing the panel name will cause layouts using the old name unable to load
401
+ // your panel.
402
+ name: string;
403
+
404
+ /**
405
+ * This function is invoked when your panel is initialized
406
+ * @return: (optional) A function which is called when the panel is removed or replaced. Typically intended for cleanup logic to gracefully teardown your panel.
407
+ */
408
+ initPanel: (context: PanelExtensionContext) => void | (() => void);
409
+ };
410
+
411
+ export type RegisterMessageConverterArgs<Src> = {
412
+ fromSchemaName: string;
413
+ toSchemaName: string;
414
+ converter: (msg: Src, event: Immutable<MessageEvent<Src>>) => unknown;
415
+ };
416
+
417
+ type BaseTopic = { name: string; schemaName?: string };
418
+ type TopicAlias = { name: string; sourceTopicName: string };
419
+
420
+ /**
421
+ * An AliasFunction takes a list of data source topics and variables and outputs
422
+ * a list of aliased topics.
423
+ */
424
+ export type TopicAliasFunction = (
425
+ args: Immutable<{
426
+ topics: BaseTopic[];
427
+ globalVariables: Readonly<Record<string, VariableValue>>;
428
+ }>,
429
+ ) => TopicAlias[];
430
+
431
+ export interface ExtensionContext {
432
+ /** The current _mode_ of the application. */
433
+ readonly mode: "production" | "development" | "test";
434
+
435
+ registerPanel(params: ExtensionPanelRegistration): void;
436
+
437
+ /**
438
+ * Register a function to convert messages from one schema to another.
439
+ *
440
+ * A converter function is invoked when a panel subscribes to a topic with the `convertTo` option.
441
+ * The return value of the converter function is the converted message and is provided to the
442
+ * panel.
443
+ *
444
+ * If the converter function invocation returns _undefined_, the output of the converter for that
445
+ * message is ignored and no message is provided to the panel. This is useful in instances where
446
+ * you might want to selectively output a converted schema depending on the input message.
447
+ */
448
+ registerMessageConverter<Src>(args: RegisterMessageConverterArgs<Src>): void;
449
+
450
+ /**
451
+ * Registers a new alias function with the extension context. The function will be
452
+ * called every time there is a new set of topics and variables and returns an array of
453
+ * topic aliases.
454
+ */
455
+ registerTopicAliases(aliasFunction: TopicAliasFunction): void;
456
+ }
457
+
458
+ export type ExtensionActivate = (extensionContext: ExtensionContext) => void;
459
+
460
+ // ExtensionModule describes the interface your extension entry level module must export
461
+ // as its default export
462
+ export interface ExtensionModule {
463
+ activate: ExtensionActivate;
464
+ }
465
+
466
+ export type SettingsIcon =
467
+ | "Add"
468
+ | "Addchart"
469
+ | "AutoAwesome"
470
+ | "Background"
471
+ | "Camera"
472
+ | "Cells"
473
+ | "Check"
474
+ | "Circle"
475
+ | "Clear"
476
+ | "Clock"
477
+ | "Collapse"
478
+ | "Cube"
479
+ | "Delete"
480
+ | "Expand"
481
+ | "Flag"
482
+ | "Folder"
483
+ | "FolderOpen"
484
+ | "Grid"
485
+ | "Hive"
486
+ | "ImageProjection"
487
+ | "Map"
488
+ | "Move"
489
+ | "MoveDown"
490
+ | "MoveUp"
491
+ | "NorthWest"
492
+ | "Note"
493
+ | "NoteFilled"
494
+ | "Points"
495
+ | "PrecisionManufacturing"
496
+ | "Radar"
497
+ | "Settings"
498
+ | "Shapes"
499
+ | "Share"
500
+ | "Star"
501
+ | "SouthEast"
502
+ | "Timeline"
503
+ | "Topic"
504
+ | "Walk"
505
+ | "World";
506
+
507
+ /**
508
+ * A settings tree field specifies the input type and the value of a field
509
+ * in the settings editor.
510
+ */
511
+ export type SettingsTreeFieldValue =
512
+ | {
513
+ input: "autocomplete";
514
+ value?: string;
515
+ items: string[];
516
+
517
+ /**
518
+ * Optional placeholder text displayed in the field input when value is undefined
519
+ */
520
+ placeholder?: string;
521
+ }
522
+ | { input: "boolean"; value?: boolean }
523
+ | {
524
+ input: "rgb";
525
+ value?: string;
526
+
527
+ /**
528
+ * Optional placeholder text displayed in the field input when value is undefined
529
+ */
530
+ placeholder?: string;
531
+
532
+ /**
533
+ * Optional field that's true if the clear button should be hidden.
534
+ */
535
+ hideClearButton?: boolean;
536
+ }
537
+ | {
538
+ input: "rgba";
539
+ value?: string;
540
+
541
+ /**
542
+ * Optional placeholder text displayed in the field input when value is undefined
543
+ */
544
+ placeholder?: string;
545
+
546
+ /**
547
+ * Optional field that's true if the clear button should be hidden.
548
+ */
549
+ hideClearButton?: boolean;
550
+ }
551
+ | { input: "gradient"; value?: [string, string] }
552
+ | {
553
+ input: "messagepath";
554
+ value?: string;
555
+ validTypes?: string[];
556
+ /** True if the input should allow math modifiers like @abs. */
557
+ supportsMathModifiers?: boolean;
558
+ }
559
+ | {
560
+ input: "number";
561
+ value?: number;
562
+ step?: number;
563
+ max?: number;
564
+ min?: number;
565
+ precision?: number;
566
+
567
+ /**
568
+ * Optional placeholder text displayed in the field input when value is undefined
569
+ */
570
+ placeholder?: string;
571
+ }
572
+ | {
573
+ input: "select";
574
+ value?: number | number[];
575
+ options: Array<{ label: string; value: undefined | number; disabled?: boolean }>;
576
+ }
577
+ | {
578
+ input: "select";
579
+ value?: string | string[];
580
+ options: Array<{ label: string; value: undefined | string; disabled?: boolean }>;
581
+ }
582
+ | {
583
+ input: "string";
584
+ value?: string;
585
+
586
+ /**
587
+ * Optional placeholder text displayed in the field input when value is undefined
588
+ */
589
+ placeholder?: string;
590
+ }
591
+ | {
592
+ input: "toggle";
593
+ value?: string;
594
+ options: string[] | Array<{ label: string; value: undefined | string }>;
595
+ }
596
+ | {
597
+ input: "toggle";
598
+ value?: number;
599
+ options: number[] | Array<{ label: string; value: undefined | number }>;
600
+ }
601
+ | {
602
+ input: "vec3";
603
+ value?: [undefined | number, undefined | number, undefined | number];
604
+ placeholder?: [undefined | string, undefined | string, undefined | string];
605
+ step?: number;
606
+ precision?: number;
607
+ labels?: [string, string, string];
608
+ max?: number;
609
+ min?: number;
610
+ }
611
+ | {
612
+ input: "vec2";
613
+ value?: [undefined | number, undefined | number];
614
+ placeholder?: [undefined | string, undefined | string];
615
+ step?: number;
616
+ precision?: number;
617
+ labels?: [string, string];
618
+ max?: number;
619
+ min?: number;
620
+ };
621
+
622
+ export type SettingsTreeField = SettingsTreeFieldValue & {
623
+ /**
624
+ * True if the field is disabled.
625
+ */
626
+ disabled?: boolean;
627
+
628
+ /**
629
+ * Optional help text to explain the purpose of the field.
630
+ */
631
+ help?: string;
632
+
633
+ /**
634
+ * The label displayed alongside the field.
635
+ */
636
+ label: string;
637
+
638
+ /**
639
+ * True if the field is readonly.
640
+ */
641
+ readonly?: boolean;
642
+
643
+ /**
644
+ * Optional message indicating any error state for the field.
645
+ */
646
+ error?: string;
647
+ };
648
+
649
+ export type SettingsTreeFields = Record<string, undefined | SettingsTreeField>;
650
+
651
+ export type SettingsTreeChildren = Record<string, undefined | SettingsTreeNode>;
652
+
653
+ export type SettingsTreeNodeActionItem = {
654
+ type: "action";
655
+
656
+ /**
657
+ * A unique idenfier for the action.
658
+ */
659
+ id: string;
660
+
661
+ /**
662
+ * A descriptive label for the action.
663
+ */
664
+ label: string;
665
+
666
+ /**
667
+ * Optional icon to display with the action.
668
+ */
669
+ icon?: SettingsIcon;
670
+
671
+ /**
672
+ * Specifies whether the item is rendered as an inline action or as an item in the
673
+ * context menu. Defaults to "menu" if not specified. Inline items will be rendered
674
+ * as an icon only if their icon is specified.
675
+ */
676
+ display?: "menu" | "inline";
677
+ };
678
+
679
+ export type SettingsTreeNodeActionDivider = { type: "divider" };
680
+
681
+ /**
682
+ * An action included in the action menu for a settings node.
683
+ */
684
+ export type SettingsTreeNodeAction = SettingsTreeNodeActionItem | SettingsTreeNodeActionDivider;
685
+
686
+ export type SettingsTreeNode = {
687
+ /**
688
+ * An array of actions that can be performed on this node.
689
+ */
690
+ actions?: SettingsTreeNodeAction[];
691
+
692
+ /**
693
+ * Other settings tree nodes nested under this node.
694
+ */
695
+ children?: SettingsTreeChildren;
696
+
697
+ /**
698
+ * Set to collapsed if the node should be initially collapsed.
699
+ */
700
+ defaultExpansionState?: "collapsed" | "expanded";
701
+
702
+ /**
703
+ * Optional message indicating any error state for the node.
704
+ */
705
+ error?: string;
706
+
707
+ /**
708
+ * Field inputs attached directly to this node.
709
+ */
710
+ fields?: SettingsTreeFields;
711
+
712
+ /**
713
+ * Optional icon to display next to the node label.
714
+ */
715
+ icon?: SettingsIcon;
716
+
717
+ /**
718
+ * An optional label shown at the top of this node.
719
+ */
720
+ label?: string;
721
+
722
+ /**
723
+ * True if the node label can be edited by the user.
724
+ */
725
+ renamable?: boolean;
726
+
727
+ /**
728
+ * Optional sort order to override natural object ordering. All nodes
729
+ * with a sort order will be rendered before nodes all with no sort order.
730
+ *
731
+ * Nodes without an explicit order will be ordered according to ECMA
732
+ * object ordering rules.
733
+ *
734
+ * https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
735
+ */
736
+ order?: number | string;
737
+
738
+ /**
739
+ * An optional visibility status. If this is not undefined, the node
740
+ * editor will display a visiblity toggle button and send update actions
741
+ * to the action handler.
742
+ **/
743
+ visible?: boolean;
744
+
745
+ /**
746
+ * Filter Children by visibility status
747
+ */
748
+ enableVisibilityFilter?: boolean;
749
+ };
750
+
751
+ /**
752
+ * Distributes Pick<T, K> across all members of a union, used for extracting structured
753
+ * subtypes.
754
+ */
755
+ type DistributivePick<T, K extends keyof T> = T extends unknown ? Pick<T, K> : never;
756
+
757
+ /**
758
+ * Represents actions that can be dispatched to source of the SettingsTree to implement
759
+ * edits and updates.
760
+ */
761
+ export type SettingsTreeAction =
762
+ | {
763
+ action: "update";
764
+ payload: { path: readonly string[] } & DistributivePick<
765
+ SettingsTreeFieldValue,
766
+ "input" | "value"
767
+ >;
768
+ }
769
+ | {
770
+ action: "perform-node-action";
771
+ payload: { id: string; path: readonly string[] };
772
+ };
773
+
774
+ export type SettingsTreeNodes = Record<string, undefined | SettingsTreeNode>;
775
+
776
+ /**
777
+ * A settings tree is a tree of panel settings that can be displayed and edited in
778
+ * the panel settings sidebar.
779
+ *
780
+ * Nodes and fields in the tree can be referred to by a string path, which collects
781
+ * the keys of each node on the path from the root to the child node or field.
782
+ *
783
+ * For example, for the following tree:
784
+ *
785
+ * root: {
786
+ * children: {
787
+ * a: {
788
+ * children: {
789
+ * b: {
790
+ * fields: {
791
+ * toggleMe: {
792
+ * label: "Toggle me",
793
+ * input: "boolean",
794
+ * value: false,
795
+ * },
796
+ * },
797
+ * },
798
+ * },
799
+ * },
800
+ * },
801
+ * },
802
+ *
803
+ * the path to the node at b would be ["a", "b"] and the path to the toggleMe
804
+ * field would be ["a", "b", "toggleMe"]. These paths are used in the
805
+ * actionHandler, which responds to updates to values in the tree, and also in
806
+ * the focusedPath, which is used to focus the editor UI at a particular node
807
+ * in the tree.
808
+ */
809
+ export type SettingsTree = {
810
+ /**
811
+ * Handler to process all actions on the settings tree initiated by the UI.
812
+ */
813
+ actionHandler: (action: SettingsTreeAction) => void;
814
+
815
+ /**
816
+ * True if the settings editor should show the filter control.
817
+ */
818
+ enableFilter?: boolean;
819
+
820
+ /**
821
+ * Setting this will have a one-time effect of scrolling the editor to the
822
+ * node at the path and highlighting it. This is a transient effect so it is
823
+ * not necessary to subsequently unset this.
824
+ */
825
+ focusedPath?: readonly string[];
826
+
827
+ /**
828
+ * The settings tree root nodes. Updates to these will automatically be
829
+ * reflected in the editor UI.
830
+ */
831
+ nodes: SettingsTreeNodes;
832
+ };