@sdeck/plugin 0.3.1 → 0.4.1

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/src/create.ts ADDED
@@ -0,0 +1,223 @@
1
+ /**
2
+ * `createPlugin`: the parts of a plugin made from one object that knows its settings, permissions
3
+ * and namespace, so that each part is checked against them. What it makes is what `definePlugin`,
4
+ * `defineAction` and `defineTrigger` make.
5
+ */
6
+ import { useHostState } from "./bridge.ts";
7
+ import type { Permission } from "./permissions.ts";
8
+ import {
9
+ type Action,
10
+ type ActionDef,
11
+ type Deactivate,
12
+ defineAction,
13
+ definePlugin,
14
+ defineTrigger,
15
+ type Onboarding,
16
+ type OptionSource,
17
+ type PluginCommand,
18
+ type PluginContext,
19
+ type PluginDef,
20
+ pluginNamespace,
21
+ type StateDef,
22
+ type Trigger,
23
+ type TriggerDef,
24
+ } from "./plugin.ts";
25
+ import type { InferSettings, SelectOption, SettingsSchema } from "./settings.ts";
26
+
27
+ /** A plugin's context with its own settings typed. */
28
+ export interface TypedContext<S extends SettingsSchema> extends PluginContext {
29
+ settings(): InferSettings<S>;
30
+ }
31
+
32
+ /** What every part of the plugin needs to know about it. */
33
+ export interface PluginBase<S extends SettingsSchema, P extends Permission>
34
+ extends Pick<
35
+ PluginDef,
36
+ "id" | "name" | "version" | "description" | "author" | "homepage" | "namespace"
37
+ > {
38
+ /** As on a plugin definition. Its parts can only need what is listed here. */
39
+ permissions?: readonly P[];
40
+ settings?: S;
41
+ }
42
+
43
+ /** The rest of it: the parts it offers. */
44
+ export interface PluginParts<S extends SettingsSchema> {
45
+ actions: readonly Action[];
46
+ /** What `plugin.state` made, and plain definitions. */
47
+ state?: readonly (StateHandle<unknown> | StateDef)[];
48
+ triggers?: readonly Trigger[];
49
+ /** What `plugin.source` made. */
50
+ sources?: readonly Source[];
51
+ commands?: readonly PluginCommand[];
52
+ onboarding?: Onboarding;
53
+ // biome-ignore lint/suspicious/noConfusingVoidType: lets `activate` simply not return anything.
54
+ activate?(context: TypedContext<S>): void | Deactivate | Promise<void | Deactivate>;
55
+ }
56
+
57
+ /** Choices for `setting.pick`, `setting.text` and `setting.groups`. */
58
+ export interface Source {
59
+ readonly id: string;
60
+ readonly load: OptionSource;
61
+ }
62
+
63
+ /** A live value of the plugin, published as `<namespace>.<id>`. */
64
+ export interface StateHandle<T> {
65
+ readonly def: StateDef;
66
+ /** Its name in expressions: `weather.temperature`. */
67
+ readonly key: string;
68
+ /** It as text in a look: `{{weather.temperature}}`. */
69
+ readonly template: string;
70
+ /** Publishes a value; `undefined` while there is none. Does nothing while the plugin is off. */
71
+ set(value: T | undefined): void;
72
+ get(): T | undefined;
73
+ /** In a component: the value, kept up to date. */
74
+ use(): T | undefined;
75
+ }
76
+
77
+ type StateOptions<S extends SettingsSchema, P, T> = Omit<
78
+ StateDef,
79
+ "id" | "name" | "type" | "permissions" | "poll" | "start"
80
+ > & {
81
+ permissions?: readonly P[];
82
+ poll?: {
83
+ everyMs: number;
84
+ read(context: TypedContext<S>): T | undefined | Promise<T | undefined>;
85
+ };
86
+ // biome-ignore lint/suspicious/noConfusingVoidType: lets `start` simply not return anything.
87
+ start?(set: (value: T | undefined) => void, context: TypedContext<S>): (() => void) | void;
88
+ };
89
+
90
+ type MakeState<S extends SettingsSchema, P, T> = (
91
+ id: string,
92
+ name: string,
93
+ options?: StateOptions<S, P, T>,
94
+ ) => StateHandle<T>;
95
+
96
+ export interface PluginBuilder<S extends SettingsSchema, P extends Permission> {
97
+ readonly id: string;
98
+ readonly namespace: string;
99
+ /** The plugin's context while it runs, `undefined` while it is off. */
100
+ readonly context: TypedContext<S> | undefined;
101
+ /** An action, as `defineAction` makes it. */
102
+ action<const A extends SettingsSchema = Record<never, never>>(
103
+ def: ActionDef<A, TypedContext<S>, P>,
104
+ ): Action;
105
+ /** A trigger, as `defineTrigger` makes it. */
106
+ trigger<const T extends SettingsSchema = Record<never, never>>(
107
+ def: TriggerDef<T, TypedContext<S>, P>,
108
+ ): Trigger;
109
+ /** Live values, by type. */
110
+ readonly state: {
111
+ number: MakeState<S, P, number>;
112
+ string: MakeState<S, P, string>;
113
+ boolean: MakeState<S, P, boolean>;
114
+ json<T>(id: string, name: string, options?: StateOptions<S, P, T>): StateHandle<T>;
115
+ };
116
+ /** Choices fetched when a form opens. Pass it to `setting.pick`. */
117
+ source(
118
+ id: string,
119
+ load: (
120
+ context: TypedContext<S>,
121
+ settings: Readonly<Record<string, unknown>>,
122
+ ) => SelectOption[] | Promise<SelectOption[]>,
123
+ ): Source;
124
+ /** A button in the plugin's settings. */
125
+ command(def: PluginCommand<TypedContext<S>, P>): PluginCommand;
126
+ /** The finished plugin, for the module's default export. Only the parts listed are in it. */
127
+ define(parts: PluginParts<S>): PluginDef;
128
+ }
129
+
130
+ /**
131
+ * Starts a plugin:
132
+ *
133
+ * ```ts
134
+ * const plugin = createPlugin({
135
+ * id: "sdeck-plugin-weather",
136
+ * name: "Weather",
137
+ * version: "1.0.0",
138
+ * permissions: [Permissions.network],
139
+ * settings: { city: setting.text("City", { required: true }) },
140
+ * });
141
+ *
142
+ * const temperature = plugin.state.number("temperature", "Temperature", {
143
+ * unit: "°C",
144
+ * poll: { everyMs: 60_000, read: (context) => fetchTemperature(context.settings().city) },
145
+ * });
146
+ *
147
+ * const now = plugin.action({
148
+ * id: "now",
149
+ * name: "Temperature",
150
+ * display: true,
151
+ * look: () => iconLook({ icon: "lucide/sun", background: "#1e293b", caption: temperature.template }),
152
+ * });
153
+ *
154
+ * export default plugin.define({ actions: [now], state: [temperature] });
155
+ * ```
156
+ */
157
+ export function createPlugin<
158
+ const S extends SettingsSchema = Record<never, never>,
159
+ const P extends Permission = never,
160
+ >(base: PluginBase<S, P>): PluginBuilder<S, P> {
161
+ type Context = TypedContext<S>;
162
+ const namespace = pluginNamespace(base);
163
+ let context: Context | undefined;
164
+
165
+ const makeState =
166
+ (type: StateDef["type"]) =>
167
+ <T>(id: string, name: string, options: StateOptions<S, P, T> = {}): StateHandle<T> => {
168
+ const key = `${namespace}.${id}`;
169
+ return {
170
+ def: { ...options, id, name, type } as StateDef,
171
+ key,
172
+ template: `{{${key}}}`,
173
+ set: (value) => context?.state.set(id, value),
174
+ get: () => context?.state.get(key) as T | undefined,
175
+ use: () => useHostState(key) as T | undefined,
176
+ };
177
+ };
178
+
179
+ return {
180
+ id: base.id,
181
+ namespace,
182
+ get context() {
183
+ return context;
184
+ },
185
+ // The context every part gets at run time is the plugin's own: only its type is narrowed.
186
+ action: (def) => defineAction(def as unknown as ActionDef),
187
+ trigger: (def) => defineTrigger(def as unknown as TriggerDef),
188
+ state: {
189
+ number: makeState("number"),
190
+ string: makeState("string"),
191
+ boolean: makeState("boolean"),
192
+ json: makeState("json"),
193
+ },
194
+ source: (id, load) => ({ id, load: load as unknown as OptionSource }),
195
+ command: (def) => def as unknown as PluginCommand,
196
+ define: ({ state, sources, activate, ...parts }) =>
197
+ definePlugin({
198
+ ...base,
199
+ ...parts,
200
+ ...(state && { state: state.map((s) => ("def" in s ? s.def : s)) }),
201
+ ...(sources && {
202
+ optionSources: Object.fromEntries(sources.map((s) => [s.id, s.load])),
203
+ }),
204
+ activate: async (given) => {
205
+ context = given as Context;
206
+ let stop: Deactivate | undefined;
207
+ try {
208
+ stop = (await activate?.(context)) ?? undefined;
209
+ } catch (error) {
210
+ context = undefined;
211
+ throw error;
212
+ }
213
+ return async () => {
214
+ try {
215
+ await stop?.();
216
+ } finally {
217
+ context = undefined;
218
+ }
219
+ };
220
+ },
221
+ }),
222
+ };
223
+ }
package/src/fields.ts ADDED
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Short, typed ways to write settings. They return the same plain fields as writing them by hand,
3
+ * so both can be mixed.
4
+ */
5
+ import type {
6
+ BooleanField,
7
+ ColorField,
8
+ GroupsField,
9
+ ImageField,
10
+ NumberField,
11
+ PageField,
12
+ SecretField,
13
+ SelectField,
14
+ SelectOption,
15
+ SlotsField,
16
+ TextField,
17
+ } from "./settings.ts";
18
+
19
+ type Options<F> = Omit<F, "type" | "label">;
20
+
21
+ /** Choices as `{ value: "What people read" }`, or a list where their order matters. */
22
+ export type Choices<V extends string> =
23
+ | Readonly<Record<V, string>>
24
+ | readonly { readonly value: V; readonly label: string }[];
25
+
26
+ /** A select whose value is one of `V`. */
27
+ export interface ChoiceField<V extends string> extends Omit<SelectField, "options" | "source"> {
28
+ options: readonly { readonly value: V; readonly label: string }[];
29
+ }
30
+
31
+ /** One of a plugin's option sources: its name, or what `plugin.source()` returned. */
32
+ export type SourceRef = string | { readonly id: string };
33
+
34
+ const sourceId = (source: SourceRef) => (typeof source === "string" ? source : source.id);
35
+
36
+ function optionList<V extends string>(choices: Choices<V>) {
37
+ if (Array.isArray(choices)) return choices as readonly { value: V; label: string }[];
38
+ return Object.entries(choices).map(([value, label]) => ({
39
+ value: value as V,
40
+ label: label as string,
41
+ }));
42
+ }
43
+
44
+ /**
45
+ * Builds settings:
46
+ *
47
+ * ```ts
48
+ * settings: {
49
+ * room: setting.text("Room", { default: "Office", required: true }),
50
+ * mode: setting.select("Mode", { set: "Set a level", mute: "Mute" }),
51
+ * level: setting.number("Level", { min: 0, max: 100, visibleIf: { mode: "set" } }),
52
+ * }
53
+ * ```
54
+ */
55
+ export const setting = {
56
+ text(label: string, options?: Options<TextField>): TextField {
57
+ return { ...options, type: "text", label };
58
+ },
59
+ number(label: string, options?: Options<NumberField>): NumberField {
60
+ return { ...options, type: "number", label };
61
+ },
62
+ boolean(label: string, options?: Options<BooleanField>): BooleanField {
63
+ return { ...options, type: "boolean", label };
64
+ },
65
+ /** A `#rrggbb` colour. */
66
+ color(label: string, options?: Options<ColorField>): ColorField {
67
+ return { ...options, type: "color", label };
68
+ },
69
+ /** One of a few fixed choices; its value has their exact type. */
70
+ select<const V extends string>(
71
+ label: string,
72
+ choices: Choices<V>,
73
+ options?: Omit<Options<ChoiceField<V>>, "options" | "default"> & { default?: NoInfer<V> },
74
+ ): ChoiceField<V> {
75
+ return { ...options, type: "select", label, options: optionList(choices) };
76
+ },
77
+ /** A choice from what one of the plugin's option sources returns when the form opens. */
78
+ pick(
79
+ label: string,
80
+ source: SourceRef,
81
+ options?: Omit<Options<SelectField>, "options" | "source"> & {
82
+ /** Choices shown before the fetched ones (`{ "": "The one in use" }`). */
83
+ first?: Readonly<Record<string, string>> | readonly SelectOption[];
84
+ },
85
+ ): SelectField & { source: string } {
86
+ const { first = [], ...rest } = options ?? {};
87
+ return { ...rest, type: "select", label, options: optionList(first), source: sourceId(source) };
88
+ },
89
+ /** One of the user's pages. */
90
+ page(label: string, options?: Options<PageField>): PageField {
91
+ return { ...options, type: "page", label };
92
+ },
93
+ /** Keys, touch sensors and the window. */
94
+ slots(label: string, options?: Options<SlotsField>): SlotsField {
95
+ return { ...options, type: "slots", label };
96
+ },
97
+ image(label: string, options?: Options<ImageField>): ImageField {
98
+ return { ...options, type: "image", label };
99
+ },
100
+ /** Named sets of options. Only for a plugin's own settings. */
101
+ groups(
102
+ label: string,
103
+ source: SourceRef,
104
+ options?: Omit<Options<GroupsField>, "source">,
105
+ ): GroupsField {
106
+ return { ...options, type: "groups", label, source: sourceId(source) };
107
+ },
108
+ /** A password or token, kept out of the config. Only for a plugin's own settings. */
109
+ secret(label: string, options?: Options<SecretField>): SecretField {
110
+ return { ...options, type: "secret", label };
111
+ },
112
+ } as const;
package/src/index.ts CHANGED
@@ -21,9 +21,20 @@ export {
21
21
  ManagedConnection,
22
22
  PermanentError,
23
23
  } from "./connection.ts";
24
+ export {
25
+ createPlugin,
26
+ type PluginBase,
27
+ type PluginBuilder,
28
+ type PluginParts,
29
+ type Source,
30
+ type StateHandle,
31
+ type TypedContext,
32
+ } from "./create.ts";
33
+ export { type ChoiceField, type Choices, type SourceRef, setting } from "./fields.ts";
24
34
  export {
25
35
  HOST_MODULES,
26
36
  type ManifestResult,
37
+ manifestIcon,
27
38
  PLUGIN_KEYWORD,
28
39
  type PluginManifest,
29
40
  parseManifest,
@@ -34,6 +45,7 @@ export {
34
45
  PERMISSIONS,
35
46
  type Permission,
36
47
  PermissionError,
48
+ Permissions,
37
49
  type PermissionText,
38
50
  } from "./permissions.ts";
39
51
  export {
@@ -51,6 +63,7 @@ export {
51
63
  type OptionSource,
52
64
  type Overlay,
53
65
  type OverlayProps,
66
+ onSettingsChange,
54
67
  PLUGIN_API_VERSION,
55
68
  type PluginCommand,
56
69
  type PluginContext,
@@ -95,7 +108,7 @@ export {
95
108
  slotList,
96
109
  type TextField,
97
110
  } from "./settings.ts";
98
- export { Shared } from "./shared.ts";
111
+ export { following, Shared, useShared } from "./shared.ts";
99
112
  export {
100
113
  fitText,
101
114
  layoutText,
@@ -103,3 +116,4 @@ export {
103
116
  type TextLayoutOptions,
104
117
  type TextRow,
105
118
  } from "./text.ts";
119
+ export { checkSettings, type StandardSchema, type Validator } from "./validate.ts";
package/src/manifest.ts CHANGED
@@ -9,7 +9,7 @@ import { PLUGIN_API_VERSION } from "./plugin.ts";
9
9
  * "version": "1.0.0",
10
10
  * "type": "module",
11
11
  * "keywords": ["sdeck-plugin"],
12
- * "sdeck": { "apiVersion": 2, "entry": "./dist/index.js" }
12
+ * "sdeck": { "apiVersion": 3, "entry": "./dist/index.js", "icon": "lucide/cloud-sun" }
13
13
  * }
14
14
  * ```
15
15
  *
@@ -25,6 +25,8 @@ export interface PluginManifest {
25
25
  /** Path of the module to import, relative to the package (default: `main`, else `index.js`). */
26
26
  entry: string;
27
27
  apiVersion: number;
28
+ /** Its icon in Studio, from any icon pack there: `bs/BsDiscord`, `si/SiSpotify#1ed760`. */
29
+ icon?: string;
28
30
  }
29
31
 
30
32
  /** The npm keyword that lists a package in the plugin registry. */
@@ -43,13 +45,24 @@ export const HOST_MODULES = [
43
45
  ] as const;
44
46
 
45
47
  /**
46
- * Plugin APIs this host loads. Version 1 plugins (actions that draw themselves) are a subset of
47
- * version 2 and run unchanged.
48
+ * Plugin APIs this host loads. Each one only adds to the one before, so older plugins run
49
+ * unchanged: 2 added actions without a component, 3 `createPlugin` and the helpers for looks.
48
50
  */
49
- export const SUPPORTED_API_VERSIONS: readonly number[] = [1, PLUGIN_API_VERSION];
51
+ export const SUPPORTED_API_VERSIONS: readonly number[] = [1, 2, PLUGIN_API_VERSION];
50
52
 
51
53
  export type ManifestResult = { ok: true; manifest: PluginManifest } | { ok: false; error: string };
52
54
 
55
+ const ICON = /^(?:icon:)?([a-z0-9]{1,12}\/[A-Za-z0-9-]{1,80}(?:#[0-9a-fA-F]{6})?)$/;
56
+
57
+ /**
58
+ * The `icon` of the `sdeck` field in a parsed `package.json`, as `<pack>/<name>` or
59
+ * `<pack>/<name>#rrggbb`. Anything else counts as no icon: a plugin works without one.
60
+ */
61
+ export function manifestIcon(pkg: unknown): string | undefined {
62
+ const icon = (pkg as { sdeck?: { icon?: unknown } | null } | null)?.sdeck?.icon;
63
+ return typeof icon === "string" ? ICON.exec(icon)?.[1] : undefined;
64
+ }
65
+
53
66
  /** Reads the plugin manifest out of a parsed `package.json`. */
54
67
  export function parseManifest(pkg: unknown): ManifestResult {
55
68
  if (!pkg || typeof pkg !== "object") return { ok: false, error: "package.json is not an object" };
@@ -63,7 +76,10 @@ export function parseManifest(pkg: unknown): ManifestResult {
63
76
  if (typeof apiVersion !== "number" || !SUPPORTED_API_VERSIONS.includes(apiVersion)) {
64
77
  return {
65
78
  ok: false,
66
- error: `${p.name} targets plugin API ${String(apiVersion)}, this host supports ${SUPPORTED_API_VERSIONS.join(" and ")}`,
79
+ error:
80
+ typeof apiVersion === "number" && apiVersion > PLUGIN_API_VERSION
81
+ ? `${p.name} needs a newer version of sdeck (plugin API ${apiVersion})`
82
+ : `${p.name} targets plugin API ${String(apiVersion)}, this host supports ${SUPPORTED_API_VERSIONS.join(", ")}`,
67
83
  };
68
84
  }
69
85
  const entry =
@@ -72,6 +88,7 @@ export function parseManifest(pkg: unknown): ManifestResult {
72
88
  : typeof p.main === "string"
73
89
  ? p.main
74
90
  : "index.js";
91
+ const icon = manifestIcon(pkg);
75
92
  return {
76
93
  ok: true,
77
94
  manifest: {
@@ -80,6 +97,7 @@ export function parseManifest(pkg: unknown): ManifestResult {
80
97
  ...(typeof p.description === "string" && { description: p.description }),
81
98
  entry,
82
99
  apiVersion,
100
+ ...(icon && { icon }),
83
101
  },
84
102
  };
85
103
  }
@@ -17,6 +17,23 @@ export type Permission =
17
17
  | "overlay"
18
18
  | "native";
19
19
 
20
+ /** Every permission by name, for `permissions: [Permissions.network]`. */
21
+ export const Permissions = {
22
+ network: "network",
23
+ filesystem: "filesystem",
24
+ process: "process",
25
+ shell: "shell",
26
+ input: "input",
27
+ clipboard: "clipboard",
28
+ audio: "audio",
29
+ environment: "environment",
30
+ variables: "variables",
31
+ state: "state",
32
+ actions: "actions",
33
+ overlay: "overlay",
34
+ native: "native",
35
+ } as const satisfies { [P in Permission]: P };
36
+
20
37
  /** A permission as the user reads it. */
21
38
  export interface PermissionText {
22
39
  name: string;
package/src/plugin.ts CHANGED
@@ -12,7 +12,7 @@ import {
12
12
  import type { TextRow } from "./text.ts";
13
13
 
14
14
  /** Bumped when the plugin contract changes incompatibly. Hosts refuse plugins built for another. */
15
- export const PLUGIN_API_VERSION = 2;
15
+ export const PLUGIN_API_VERSION = 3;
16
16
 
17
17
  /** Where an action can be placed. */
18
18
  export type SlotKind = "key" | "sensor" | "window";
@@ -102,7 +102,7 @@ export interface OverlayProps {
102
102
  export type StatusLevel = "ok" | "info" | "error";
103
103
 
104
104
  /** Something the user can run from the plugin's settings (sign in, reconnect). */
105
- export interface PluginCommand {
105
+ export interface PluginCommand<C = PluginContext, P extends Permission = Permission> {
106
106
  /** Unique within the plugin. */
107
107
  id: string;
108
108
  name: string;
@@ -111,9 +111,9 @@ export interface PluginCommand {
111
111
  * What it needs, out of the plugin's `permissions` (default: all of them; `[]` for nothing).
112
112
  * Without them it is refused.
113
113
  */
114
- permissions?: readonly Permission[];
114
+ permissions?: readonly P[];
115
115
  /** What it returns, if a string, is opened in the user's browser. Throw to fail. */
116
- run(context: PluginContext): unknown | Promise<unknown>;
116
+ run(context: C): unknown | Promise<unknown>;
117
117
  }
118
118
 
119
119
  /**
@@ -167,21 +167,21 @@ export interface Onboarding {
167
167
  }
168
168
 
169
169
  /** Props of an action's component. */
170
- export interface ActionProps<S extends SettingsSchema = SettingsSchema> {
170
+ export interface ActionProps<S extends SettingsSchema = SettingsSchema, C = PluginContext> {
171
171
  /** This placement's settings, resolved against the schema (always complete and typed). */
172
172
  settings: InferSettings<S>;
173
173
  /** Saves changed settings for this placement (persisted and shown in the UI). */
174
174
  setSettings(patch: Partial<InferSettings<S>>): void;
175
175
  /** Where the action is drawn. */
176
176
  slot: SlotInfo;
177
- plugin: PluginContext;
177
+ plugin: C;
178
178
  }
179
179
 
180
180
  /** Given to an action's `run`. */
181
- export interface RunContext<S extends SettingsSchema = SettingsSchema> {
181
+ export interface RunContext<S extends SettingsSchema = SettingsSchema, C = PluginContext> {
182
182
  /** Settings with `{{templates}}` already resolved, then checked against the schema. */
183
183
  settings: InferSettings<S>;
184
- plugin: PluginContext;
184
+ plugin: C;
185
185
  /** What started the run (a key, a trigger, a workflow…). */
186
186
  source: ExecSource;
187
187
  /** Aborted when the execution is cancelled. Long work should stop. */
@@ -215,7 +215,11 @@ export interface RunOutcome {
215
215
 
216
216
  export type RunResult = RunOutcome | undefined;
217
217
 
218
- export interface ActionDef<S extends SettingsSchema = SettingsSchema> {
218
+ export interface ActionDef<
219
+ S extends SettingsSchema = SettingsSchema,
220
+ C = PluginContext,
221
+ P extends Permission = Permission,
222
+ > {
219
223
  /** Unique within the plugin, e.g. `"clock"`. */
220
224
  id: string;
221
225
  name: string;
@@ -235,7 +239,7 @@ export interface ActionDef<S extends SettingsSchema = SettingsSchema> {
235
239
  * What it needs, out of the plugin's `permissions` (default: all of them; `[]` for nothing).
236
240
  * Without them it cannot run or draw itself.
237
241
  */
238
- permissions?: readonly Permission[];
242
+ permissions?: readonly P[];
239
243
  /**
240
244
  * It only shows something. It is offered for buttons, but not as a step of a workflow or
241
245
  * gesture, and needs no `run`.
@@ -246,15 +250,18 @@ export interface ActionDef<S extends SettingsSchema = SettingsSchema> {
246
250
  * Throw to fail. An action needs `run`, `component`, or both, unless it is `display`.
247
251
  */
248
252
  // biome-ignore lint/suspicious/noConfusingVoidType: lets `run` simply not return anything.
249
- run?(context: RunContext<S>): RunResult | void | Promise<RunResult | void>;
253
+ run?(context: RunContext<S, C>): RunResult | void | Promise<RunResult | void>;
250
254
  /**
251
255
  * Draws the action itself (it then handles its own input). It runs inside the deck's React
252
256
  * tree, so the `@sdeck/app` hooks and components work as in any app. Without it, the host
253
257
  * draws the button's look and runs `run` on press.
254
258
  */
255
- component?: ComponentType<ActionProps<S>>;
256
- /** The look a new button with this action starts with (default: its name on a dark key). */
257
- look?(settings: InferSettings<S>): Look;
259
+ component?: ComponentType<ActionProps<S, C>>;
260
+ /**
261
+ * The look a new button with this action starts with (default: its name on a dark key). `slot` is
262
+ * where it is drawn: the window is much wider than a key, and a touch sensor shows no text.
263
+ */
264
+ look?(settings: InferSettings<S>, slot: SlotKind): Look;
258
265
  }
259
266
 
260
267
  export type RunFn = (context: RunContext) => RunResult | undefined | Promise<RunResult | undefined>;
@@ -272,7 +279,7 @@ export interface Action {
272
279
  readonly display?: boolean;
273
280
  readonly run?: RunFn;
274
281
  readonly component?: ComponentType<ActionProps>;
275
- readonly look?: (settings: Record<string, unknown>) => Look;
282
+ readonly look?: (settings: Record<string, unknown>, slot: SlotKind) => Look;
276
283
  }
277
284
 
278
285
  /** A live value the plugin publishes as `<namespace>.<id>`. */
@@ -296,14 +303,18 @@ export interface StateDef {
296
303
  }
297
304
 
298
305
  /** Given to a trigger's `subscribe`. */
299
- export interface TriggerContext<S extends SettingsSchema = SettingsSchema> {
306
+ export interface TriggerContext<S extends SettingsSchema = SettingsSchema, C = PluginContext> {
300
307
  settings: InferSettings<S>;
301
- plugin: PluginContext;
308
+ plugin: C;
302
309
  /** Starts the trigger's workflow; `event` becomes `event` in its expressions. */
303
310
  fire(event?: unknown): void;
304
311
  }
305
312
 
306
- export interface TriggerDef<S extends SettingsSchema = SettingsSchema> {
313
+ export interface TriggerDef<
314
+ S extends SettingsSchema = SettingsSchema,
315
+ C = PluginContext,
316
+ P extends Permission = Permission,
317
+ > {
307
318
  /** Unique within the plugin; the trigger type is `<plugin id>:<id>`. */
308
319
  id: string;
309
320
  name: string;
@@ -313,10 +324,10 @@ export interface TriggerDef<S extends SettingsSchema = SettingsSchema> {
313
324
  * What it needs, out of the plugin's `permissions` (default: all of them; `[]` for nothing).
314
325
  * Without them it never fires.
315
326
  */
316
- permissions?: readonly Permission[];
327
+ permissions?: readonly P[];
317
328
  /** Starts watching; returns a function that stops. */
318
329
  // biome-ignore lint/suspicious/noConfusingVoidType: lets `subscribe` simply not return anything.
319
- subscribe(context: TriggerContext<S>): (() => void) | void;
330
+ subscribe(context: TriggerContext<S, C>): (() => void) | void;
320
331
  }
321
332
 
322
333
  export interface Trigger {
@@ -616,3 +627,17 @@ function checkOnboarding(def: Partial<PluginDef>): string[] {
616
627
  }
617
628
  return problems;
618
629
  }
630
+
631
+ /**
632
+ * Calls `listener` when the plugin's settings have other values than before, such as to connect
633
+ * again. `onSettings` also tells of saves that changed nothing. Returns a function that stops.
634
+ */
635
+ export function onSettingsChange(context: PluginContext, listener: () => void): () => void {
636
+ let before = JSON.stringify(context.settings());
637
+ return context.onSettings(() => {
638
+ const now = JSON.stringify(context.settings());
639
+ if (now === before) return;
640
+ before = now;
641
+ listener();
642
+ });
643
+ }