@sdeck/plugin-basics 1.2.0 → 1.3.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/src/index.tsx CHANGED
@@ -2,9 +2,18 @@
2
2
  * Basics: what most decks want. Labels, time (clock, date, timer, stopwatch), a counter, page
3
3
  * navigation and the deck's brightness.
4
4
  */
5
- import { defineAction, definePlugin, useKeyPress, useKeyPressed } from "@sdeck/plugin";
6
- import { iconLayer, type Layer, type Look, textLayer } from "@sdeck/schema";
5
+ import { setting, useKeyPress, useKeyPressed } from "@sdeck/plugin";
6
+ import {
7
+ iconLayer,
8
+ iconLook,
9
+ type Layer,
10
+ type Look,
11
+ surface,
12
+ surfaceStyle,
13
+ textLayer,
14
+ } from "@sdeck/schema";
7
15
  import { clock, date } from "./clock.tsx";
16
+ import { plugin } from "./plugin.ts";
8
17
  import { column, THEME } from "./theme.ts";
9
18
  import { stopwatch, timer, timerEnded } from "./timers.tsx";
10
19
 
@@ -12,33 +21,9 @@ export { analogFace, timeParts } from "./clock.tsx";
12
21
  export { formatDuration } from "./theme.ts";
13
22
  export { elapsed, type Run, toggled } from "./timers.tsx";
14
23
 
15
- /** An icon with a caption under it, or the icon alone. */
16
- function iconWithCaption(options: {
17
- icon: string;
18
- iconColor: string;
19
- caption: string;
20
- captionColor: string;
21
- background: string;
22
- }): Look {
23
- const { icon, iconColor, caption, captionColor, background } = options;
24
- return {
25
- background,
26
- layers: [
27
- iconLayer("icon", icon, {
28
- color: iconColor,
29
- size: caption ? 44 : 54,
30
- position: caption ? "top" : "middle",
31
- }),
32
- ...(caption
33
- ? [textLayer("label", caption, { color: captionColor, size: 13, position: "bottom" })]
34
- : []),
35
- ],
36
- };
37
- }
38
-
39
24
  // -- Label --------------------------------------------------------------------
40
25
 
41
- const label = defineAction({
26
+ const label = plugin.action({
42
27
  id: "label",
43
28
  name: "Label",
44
29
  description: "Text and an icon or picture. The text can show live values: CPU {{system.cpu}}%.",
@@ -46,11 +31,11 @@ const label = defineAction({
46
31
  icon: "type",
47
32
  slots: ["key", "sensor", "window"],
48
33
  settings: {
49
- text: { type: "text", label: "Text", default: "Hello" },
50
- image: { type: "image", label: "Icon or picture" },
51
- background: { type: "color", label: "Background", default: THEME.background },
52
- color: { type: "color", label: "Text colour", default: THEME.text },
53
- fontSize: { type: "number", label: "Text size", default: 18, min: 8, max: 60 },
34
+ text: setting.text("Text", { default: "Hello" }),
35
+ image: setting.image("Icon or picture"),
36
+ background: setting.color("Background", { default: THEME.background }),
37
+ color: setting.color("Text colour", { default: THEME.text }),
38
+ fontSize: setting.number("Text size", { default: 18, min: 8, max: 60 }),
54
39
  },
55
40
  // Pressing a label does nothing, but it can still start a button's own steps.
56
41
  display: true,
@@ -61,8 +46,8 @@ const label = defineAction({
61
46
  id: "icon",
62
47
  type: "image",
63
48
  src: s.image,
64
- size: s.text ? 50 : 68,
65
- position: s.text ? "top" : "middle",
49
+ size: s.text ? 48 : 68,
50
+ position: "middle",
66
51
  // Icons take the text's colour; pictures keep their own.
67
52
  color: s.color,
68
53
  });
@@ -72,28 +57,27 @@ const label = defineAction({
72
57
  textLayer("label", s.text, {
73
58
  color: s.color,
74
59
  size: s.image ? Math.min(s.fontSize, 14) : s.fontSize,
75
- position: s.image ? "bottom" : "middle",
76
60
  }),
77
61
  );
78
62
  }
79
- return { background: s.background, layers };
63
+ return { background: surface(s.background), layers };
80
64
  },
81
65
  });
82
66
 
83
67
  // -- Counter ------------------------------------------------------------------
84
68
 
85
- const counter = defineAction({
69
+ const counter = plugin.action({
86
70
  id: "counter",
87
71
  name: "Counter",
88
72
  description: "Counts presses. Hold to reset. The count is saved.",
89
73
  category: "Display",
90
74
  icon: "hash",
91
75
  settings: {
92
- title: { type: "text", label: "Title", default: "" },
93
- count: { type: "number", label: "Count", default: 0 },
94
- step: { type: "number", label: "Step", default: 1 },
95
- accent: { type: "color", label: "Colour", default: THEME.accent },
96
- background: { type: "color", label: "Background", default: THEME.background },
76
+ title: setting.text("Title", { default: "" }),
77
+ count: setting.number("Count", { default: 0 }),
78
+ step: setting.number("Step", { default: 1 }),
79
+ accent: setting.color("Colour", { default: THEME.accent }),
80
+ background: setting.color("Background", { default: THEME.background }),
97
81
  },
98
82
  component: ({ settings, setSettings }) => {
99
83
  const pressed = useKeyPressed();
@@ -106,7 +90,7 @@ const counter = defineAction({
106
90
  <div
107
91
  style={{
108
92
  ...column,
109
- background: pressed ? settings.accent : settings.background,
93
+ ...surfaceStyle(pressed ? settings.accent : settings.background),
110
94
  fontWeight: 700,
111
95
  gap: 2,
112
96
  }}
@@ -162,7 +146,7 @@ const targets = {
162
146
  favorite: "Favorites",
163
147
  } as const;
164
148
 
165
- const navigate = defineAction({
149
+ const navigate = plugin.action({
166
150
  id: "navigate",
167
151
  name: "Navigate",
168
152
  description: "Go to a page, back, forward, or to the next or previous page.",
@@ -170,30 +154,23 @@ const navigate = defineAction({
170
154
  icon: "move-right",
171
155
  slots: ["key", "sensor"],
172
156
  settings: {
173
- to: {
174
- type: "select",
175
- label: "Go to",
176
- default: "page",
177
- options: [
178
- { value: "page", label: "A page" },
179
- { value: "back", label: "Back (history)" },
180
- { value: "forward", label: "Forward (history)" },
181
- { value: "next", label: "Next page" },
182
- { value: "previous", label: "Previous page" },
183
- { value: "home", label: "First page" },
184
- { value: "favorite", label: "Next favorite page" },
185
- ],
186
- },
187
- page: { type: "page", label: "Page", visibleIf: { to: "page" } },
188
- wrap: {
189
- type: "boolean",
190
- label: "Wrap around",
157
+ to: setting.select("Go to", {
158
+ page: "A page",
159
+ back: "Back (history)",
160
+ forward: "Forward (history)",
161
+ next: "Next page",
162
+ previous: "Previous page",
163
+ home: "First page",
164
+ favorite: "Next favorite page",
165
+ }),
166
+ page: setting.page("Page", { required: true, visibleIf: { to: "page" } }),
167
+ wrap: setting.boolean("Wrap around", {
191
168
  description: "After the last page comes the first.",
192
169
  visibleIf: { to: ["next", "previous"] },
193
- },
194
- text: { type: "text", label: "Text", placeholder: "Defaults to the target", appearance: true },
195
- background: { type: "color", label: "Background", default: THEME.background, appearance: true },
196
- color: { type: "color", label: "Icon colour", default: THEME.accent, appearance: true },
170
+ }),
171
+ text: setting.text("Text", { placeholder: "Defaults to the target", appearance: true }),
172
+ background: setting.color("Background", { default: THEME.background, appearance: true }),
173
+ color: setting.color("Icon colour", { default: THEME.accent, appearance: true }),
197
174
  },
198
175
  run: ({ settings, deck }) => {
199
176
  if (settings.to === "page") {
@@ -203,19 +180,18 @@ const navigate = defineAction({
203
180
  return { label: settings.to === "page" ? `Go to ${settings.page}` : targets[settings.to] };
204
181
  },
205
182
  look: (s) =>
206
- iconWithCaption({
183
+ iconLook({
207
184
  icon: NAVIGATE_ICONS[s.to],
208
- iconColor: s.to === "favorite" ? THEME.warm : s.color,
185
+ color: s.to === "favorite" ? THEME.warm : s.color,
209
186
  // A page key names its page, so keys to different pages can be told apart.
210
187
  caption: s.text || (s.to === "page" ? s.page : ""),
211
- captionColor: THEME.text,
212
188
  background: s.background,
213
189
  }),
214
190
  });
215
191
 
216
192
  // -- Page name ----------------------------------------------------------------
217
193
 
218
- const pageName = defineAction({
194
+ const pageName = plugin.action({
219
195
  id: "page-name",
220
196
  name: "Page name",
221
197
  description: "Shows which page is open.",
@@ -223,14 +199,14 @@ const pageName = defineAction({
223
199
  icon: "file-text",
224
200
  slots: ["key", "sensor", "window"],
225
201
  settings: {
226
- color: { type: "color", label: "Text colour", default: THEME.text },
227
- accent: { type: "color", label: "Icon colour", default: THEME.accent },
228
- background: { type: "color", label: "Background", default: THEME.background },
229
- fontSize: { type: "number", label: "Text size", default: 20, min: 8, max: 60 },
202
+ color: setting.color("Text colour", { default: THEME.text }),
203
+ accent: setting.color("Icon colour", { default: THEME.accent }),
204
+ background: setting.color("Background", { default: THEME.background }),
205
+ fontSize: setting.number("Text size", { default: 20, min: 8, max: 60 }),
230
206
  },
231
207
  display: true,
232
208
  look: (s) => ({
233
- background: s.background,
209
+ background: surface(s.background),
234
210
  layers: [
235
211
  iconLayer("icon", "lucide/layers", { color: s.accent, size: 30, row: "name" }),
236
212
  textLayer("label", "{{page.name}}", { color: s.color, size: s.fontSize, row: "name" }),
@@ -257,7 +233,7 @@ export function nextLevel(levels: readonly number[], current: number): number |
257
233
  return levels[(i + 1) % levels.length];
258
234
  }
259
235
 
260
- const brightness = defineAction({
236
+ const brightness = plugin.action({
261
237
  id: "brightness",
262
238
  name: "Brightness",
263
239
  description: "Set the deck's brightness, or cycle through levels.",
@@ -265,30 +241,23 @@ const brightness = defineAction({
265
241
  icon: "sun",
266
242
  slots: ["key", "sensor"],
267
243
  settings: {
268
- mode: {
269
- type: "select",
270
- label: "Mode",
271
- default: "cycle",
272
- options: [
273
- { value: "cycle", label: "Cycle through levels" },
274
- { value: "set", label: "Set a level" },
275
- ],
276
- },
277
- level: {
278
- type: "number",
279
- label: "Level (%)",
244
+ mode: setting.select("Mode", { cycle: "Cycle through levels", set: "Set a level" }),
245
+ level: setting.number("Level (%)", {
280
246
  default: 100,
281
247
  min: 0,
282
248
  max: 100,
283
249
  visibleIf: { mode: "set" },
284
- },
285
- levels: {
286
- type: "text",
287
- label: "Levels to cycle",
250
+ }),
251
+ levels: setting.text("Levels to cycle", {
288
252
  default: "10, 40, 70, 100",
253
+ required: true,
254
+ validate: (list) =>
255
+ parseLevels(list).length > 0
256
+ ? undefined
257
+ : "should be numbers from 0 to 100, like 10, 50, 100",
289
258
  visibleIf: { mode: "cycle" },
290
- },
291
- background: { type: "color", label: "Background", default: THEME.background, appearance: true },
259
+ }),
260
+ background: setting.color("Background", { default: THEME.background, appearance: true }),
292
261
  },
293
262
  run: async ({ settings, deck }) => {
294
263
  const before = deck.brightness();
@@ -303,23 +272,17 @@ const brightness = defineAction({
303
272
  };
304
273
  },
305
274
  look: (s) =>
306
- iconWithCaption({
275
+ iconLook({
307
276
  icon:
308
277
  s.mode === "cycle" ? "lucide/sun-medium" : s.level < 50 ? "lucide/sun-dim" : "lucide/sun",
309
- iconColor: THEME.warm,
278
+ color: THEME.warm,
310
279
  caption: s.mode === "cycle" ? "" : `${s.level}%`,
311
- captionColor: THEME.text,
312
280
  background: s.background,
313
281
  }),
314
282
  });
315
283
 
316
284
  /** Can be used directly by hosts and apps: `import basics from "@sdeck/plugin-basics"`. */
317
- export default definePlugin({
318
- id: "@sdeck/plugin-basics",
319
- name: "Basics",
320
- version: "1.2.0",
321
- description: "Labels, a clock, the date, timers, a counter, page navigation and brightness.",
322
- author: "sdeck",
285
+ export default plugin.define({
323
286
  actions: [label, clock, date, timer, stopwatch, counter, navigate, pageName, brightness],
324
287
  triggers: [timerEnded],
325
288
  });
package/src/plugin.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { createPlugin } from "@sdeck/plugin";
2
+
3
+ export const plugin = createPlugin({
4
+ id: "@sdeck/plugin-basics",
5
+ name: "Basics",
6
+ version: "1.3.0",
7
+ description: "Labels, a clock, the date, timers, a counter, page navigation and brightness.",
8
+ author: "sdeck",
9
+ });
package/src/theme.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  /** The colours and shapes Basics draws with, so its keys look like one set. */
2
+ import { KEY } from "@sdeck/schema";
2
3
  import type { CSSProperties } from "react";
3
4
 
4
5
  export const THEME = {
5
- /** Key background: near black with a hint of blue. */
6
- background: "#16171d",
6
+ background: KEY,
7
7
  /** Main text and icons. */
8
8
  text: "#f4f4f5",
9
9
  /** Secondary text: dates, units, hints. */
package/src/timers.tsx CHANGED
@@ -2,16 +2,10 @@
2
2
  * A countdown timer and a stopwatch. Their state lives here, by page and slot, so they keep
3
3
  * running while another page is shown.
4
4
  */
5
- import {
6
- defineAction,
7
- defineTrigger,
8
- type SlotInfo,
9
- useKeyPress,
10
- useKeyPressed,
11
- useNow,
12
- usePage,
13
- } from "@sdeck/plugin";
5
+ import { type SlotInfo, setting, useKeyPress, useKeyPressed, useNow, usePage } from "@sdeck/plugin";
6
+ import { surfaceStyle } from "@sdeck/schema";
14
7
  import { useEffect, useReducer } from "react";
8
+ import { plugin } from "./plugin.ts";
15
9
  import { column, fill, formatDuration, ring, THEME } from "./theme.ts";
16
10
 
17
11
  /** A running or paused clock: `startedAt` while running, `banked` ms from earlier runs. */
@@ -38,7 +32,7 @@ const slotId = (page: string, slot: SlotInfo, kind: string) =>
38
32
  /** Listeners of the "timer ends" trigger. */
39
33
  const ended = new Set<(event: { label: string; seconds: number }) => void>();
40
34
 
41
- export const timerEnded = defineTrigger({
35
+ export const timerEnded = plugin.trigger({
42
36
  id: "timer-ended",
43
37
  name: "When a timer ends",
44
38
  description: "Fires when any Basics timer reaches zero.",
@@ -71,7 +65,15 @@ function Countdown(props: {
71
65
  const color = done ? "#ffffff" : running ? THEME.text : THEME.muted;
72
66
  if (wide) {
73
67
  return (
74
- <div style={{ ...column, background, padding: "0 16px", gap: 6, fontWeight: 700 }}>
68
+ <div
69
+ style={{
70
+ ...column,
71
+ ...surfaceStyle(background),
72
+ padding: "0 16px",
73
+ gap: 6,
74
+ fontWeight: 700,
75
+ }}
76
+ >
75
77
  <div
76
78
  style={{
77
79
  display: "flex",
@@ -108,7 +110,7 @@ function Countdown(props: {
108
110
  );
109
111
  }
110
112
  return (
111
- <div style={{ ...fill, background, position: "relative" }}>
113
+ <div style={{ ...fill, ...surfaceStyle(background), position: "relative" }}>
112
114
  <img
113
115
  src={ring(1 - remaining / total, done ? "#ffffff" : accent, THEME.faint)}
114
116
  width={86}
@@ -135,7 +137,7 @@ function Countdown(props: {
135
137
  );
136
138
  }
137
139
 
138
- export const timer = defineAction({
140
+ export const timer = plugin.action({
139
141
  id: "timer",
140
142
  name: "Timer",
141
143
  description: "Counts down. Press to start or pause, hold to reset. Blinks when it ends.",
@@ -143,11 +145,11 @@ export const timer = defineAction({
143
145
  icon: "timer",
144
146
  slots: ["key", "window"],
145
147
  settings: {
146
- minutes: { type: "number", label: "Minutes", default: 5, min: 0, max: 600 },
147
- seconds: { type: "number", label: "Seconds", default: 0, min: 0, max: 59 },
148
- label: { type: "text", label: "Label", placeholder: "Tea, Focus…" },
149
- accent: { type: "color", label: "Ring colour", default: THEME.warm },
150
- background: { type: "color", label: "Background", default: THEME.background },
148
+ minutes: setting.number("Minutes", { default: 5, min: 0, max: 600 }),
149
+ seconds: setting.number("Seconds", { default: 0, min: 0, max: 59 }),
150
+ label: setting.text("Label", { placeholder: "Tea, Focus…" }),
151
+ accent: setting.color("Ring colour", { default: THEME.warm }),
152
+ background: setting.color("Background", { default: THEME.background }),
151
153
  },
152
154
  component: ({ settings, slot, plugin }) => {
153
155
  const page = usePage();
@@ -203,7 +205,7 @@ export const timer = defineAction({
203
205
  },
204
206
  });
205
207
 
206
- export const stopwatch = defineAction({
208
+ export const stopwatch = plugin.action({
207
209
  id: "stopwatch",
208
210
  name: "Stopwatch",
209
211
  description: "Counts up. Press to start or stop, hold to reset.",
@@ -211,9 +213,9 @@ export const stopwatch = defineAction({
211
213
  icon: "watch",
212
214
  slots: ["key", "window"],
213
215
  settings: {
214
- label: { type: "text", label: "Label", placeholder: "Shown under the time" },
215
- accent: { type: "color", label: "Colour while running", default: THEME.success },
216
- background: { type: "color", label: "Background", default: THEME.background },
216
+ label: setting.text("Label", { placeholder: "Shown under the time" }),
217
+ accent: setting.color("Colour while running", { default: THEME.success }),
218
+ background: setting.color("Background", { default: THEME.background }),
217
219
  },
218
220
  component: ({ settings, slot }) => {
219
221
  const page = usePage();
@@ -246,7 +248,7 @@ export const stopwatch = defineAction({
246
248
  style={{
247
249
  ...column,
248
250
  ...(wide && { flexDirection: "row", gap: 14 }),
249
- background: pressed ? THEME.faint : settings.background,
251
+ ...surfaceStyle(pressed ? THEME.faint : settings.background),
250
252
  fontWeight: 700,
251
253
  }}
252
254
  >