@sdeck/plugin-basics 1.3.1 → 1.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/dist/index.js CHANGED
@@ -1,23 +1,35 @@
1
1
  // src/index.tsx
2
- import { setting as setting3, useKeyPress as useKeyPress2, useKeyPressed as useKeyPressed2 } from "@sdeck/plugin";
2
+ import { setting as setting3, useKeyPress as useKeyPress2 } from "@sdeck/plugin";
3
3
  import {
4
- iconLayer,
4
+ iconLayer as iconLayer3,
5
5
  iconLook,
6
- surface,
7
- surfaceStyle as surfaceStyle3,
8
- textLayer
6
+ rectLayer as rectLayer3,
7
+ surface as surface3,
8
+ textLayer as textLayer3
9
9
  } from "@sdeck/schema";
10
10
 
11
11
  // src/clock.tsx
12
- import { setting, useNow } from "@sdeck/plugin";
13
- import { surfaceStyle } from "@sdeck/schema";
12
+ import {
13
+ dateParts,
14
+ formatDate,
15
+ isTimeZone,
16
+ setting,
17
+ useNow
18
+ } from "@sdeck/plugin";
19
+ import {
20
+ iconLayer,
21
+ readable,
22
+ rectLayer,
23
+ surface,
24
+ textLayer
25
+ } from "@sdeck/schema";
14
26
 
15
27
  // src/plugin.ts
16
28
  import { createPlugin } from "@sdeck/plugin";
17
29
  var plugin = createPlugin({
18
30
  id: "@sdeck/plugin-basics",
19
31
  name: "Basics",
20
- version: "1.3.1",
32
+ version: "1.4.1",
21
33
  description: "Labels, a clock, the date, timers, a counter, page navigation and brightness.",
22
34
  author: "sdeck"
23
35
  });
@@ -34,14 +46,6 @@ var THEME = {
34
46
  danger: "#ff5d5d",
35
47
  success: "#3ddc84"
36
48
  };
37
- var fill = {
38
- display: "flex",
39
- width: "100%",
40
- height: "100%",
41
- alignItems: "center",
42
- justifyContent: "center"
43
- };
44
- var column = { ...fill, flexDirection: "column" };
45
49
  function formatDuration(ms, roundUp = true) {
46
50
  const total = Math.max(0, roundUp ? Math.ceil(ms / 1000) : Math.floor(ms / 1000));
47
51
  const hours = Math.floor(total / 3600);
@@ -59,11 +63,10 @@ function ring(part, color, track, width = 7) {
59
63
  }
60
64
 
61
65
  // src/clock.tsx
62
- import { jsx, jsxs } from "react/jsx-runtime";
63
- function analogFace(date, colors, seconds) {
64
- const h = date.getHours() % 12;
65
- const m = date.getMinutes();
66
- const s = date.getSeconds();
66
+ function analogFace(at, colors, seconds) {
67
+ const h = at.hour % 12;
68
+ const m = at.minute;
69
+ const s = at.second;
67
70
  const hand = (angle, length, width, color, tail = 0) => `<line x1="50" y1="${50 + tail}" x2="50" y2="${50 - length}" stroke="${color}" stroke-width="${width}" stroke-linecap="round" transform="rotate(${angle} 50 50)"/>`;
68
71
  const ticks = Array.from({ length: 12 }, (_, i) => {
69
72
  const major = i % 3 === 0;
@@ -71,15 +74,72 @@ function analogFace(date, colors, seconds) {
71
74
  }).join("");
72
75
  return svg(`<circle cx="50" cy="50" r="47" fill="${colors.face}"/>${ticks}` + hand((h + m / 60) * 30, 24, 6, colors.hands) + hand((m + s / 60) * 6, 35, 4, colors.hands) + (seconds ? hand(s * 6, 38, 1.8, colors.accent, 8) : "") + `<circle cx="50" cy="50" r="3.5" fill="${seconds ? colors.accent : colors.hands}"/>`);
73
76
  }
74
- function timeParts(date, hour12) {
75
- const hours = date.getHours();
76
- const shown = hour12 ? hours % 12 || 12 : hours;
77
+ var zones = plugin.source("zones", () => Intl.supportedValuesOf("timeZone").map((value) => {
78
+ const [area = "", ...city] = value.split("/");
79
+ return {
80
+ value,
81
+ label: city.length > 0 ? `${city.join(", ")} (${area})`.replaceAll("_", " ") : value
82
+ };
83
+ }));
84
+ var zoneSetting = setting.pick("Time zone", zones, { first: { "": "This computer's" } });
85
+ var text = (name, description) => ({
86
+ name,
87
+ type: "string",
88
+ ...description && { description }
89
+ });
90
+ var number = (name, description) => ({
91
+ ...text(name, description),
92
+ type: "number"
93
+ });
94
+ var DATE_VALUES = {
95
+ weekday: text("Weekday", "Thursday"),
96
+ weekdayShort: text("Weekday, short", "Thu"),
97
+ day: number("Day of the month"),
98
+ month: text("Month", "December"),
99
+ monthShort: text("Month, short", "Dec"),
100
+ monthNumber: number("Month, as a number"),
101
+ year: number("Year"),
102
+ now: number("Moment shown", 'For formats of your own: format(self.now, "D/M")'),
103
+ zone: text("Time zone", `Empty for the computer's own: date("D MMM", self.now, self.zone)`)
104
+ };
105
+ function dateValues(now, zone) {
106
+ const at = dateParts(now, zone);
107
+ const part = (pattern) => formatDate(now, pattern, { zone });
108
+ return {
109
+ weekday: part("dddd"),
110
+ weekdayShort: part("ddd"),
111
+ day: at?.day,
112
+ month: part("MMMM"),
113
+ monthShort: part("MMM"),
114
+ monthNumber: at?.month,
115
+ year: at?.year,
116
+ now: now.getTime(),
117
+ zone
118
+ };
119
+ }
120
+ function timeValues(now, options) {
121
+ const { zone, hour12, seconds } = options;
122
+ const part = (pattern) => formatDate(now, pattern, { zone });
123
+ const hour = part(hour12 ? "h" : "HH");
124
+ const minute = part("mm");
125
+ const second = seconds ? part("ss") : "";
126
+ const period = hour12 ? part("A") : "";
77
127
  return {
78
- main: `${hour12 ? shown : String(shown).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`,
79
- seconds: String(date.getSeconds()).padStart(2, "0"),
80
- period: hour12 ? hours < 12 ? "AM" : "PM" : ""
128
+ hour,
129
+ minute,
130
+ second,
131
+ period,
132
+ hour24: dateParts(now, zone)?.hour,
133
+ time: `${hour}:${minute}${second && `:${second}`}${period && ` ${period}`}`
81
134
  };
82
135
  }
136
+ var usualDate = (now, zone) => now.toLocaleDateString(undefined, {
137
+ weekday: "short",
138
+ day: "numeric",
139
+ month: "short",
140
+ ...zone && { timeZone: zone }
141
+ });
142
+ var known = (zone) => isTimeZone(zone) ? zone : "";
83
143
  var clock = plugin.action({
84
144
  id: "clock",
85
145
  name: "Clock",
@@ -92,83 +152,65 @@ var clock = plugin.action({
92
152
  hour12: setting.boolean("12-hour clock", { visibleIf: { style: "digital" } }),
93
153
  seconds: setting.boolean("Show seconds", { default: true }),
94
154
  date: setting.boolean("Show the date"),
155
+ dateFormat: setting.text("Date format", {
156
+ placeholder: "ddd D MMM",
157
+ description: "YYYY year, MMMM or MMM month, M its number, D day, Do 3rd, dddd or ddd weekday. Empty: as your computer writes it.",
158
+ visibleIf: { date: true }
159
+ }),
160
+ zone: zoneSetting,
95
161
  color: setting.color("Colour", { default: THEME.text }),
96
162
  accent: setting.color("Accent", { default: THEME.accent }),
97
163
  background: setting.color("Background", { default: THEME.background })
98
164
  },
99
- component: ({ settings, slot }) => {
165
+ display: true,
166
+ values: {
167
+ hour: text("Hour", "As the settings say: 16, or 4"),
168
+ minute: text("Minute", "05"),
169
+ second: text("Second", "Empty while seconds are off"),
170
+ period: text("AM or PM", "Empty on a 24-hour clock"),
171
+ hour24: number("Hour, 0 to 23"),
172
+ time: text("Time", "All of it: 4:41:07 PM"),
173
+ date: text("Date", "In the date format"),
174
+ ...DATE_VALUES,
175
+ face: text("Clock face", "A picture, for an image layer")
176
+ },
177
+ useValues: ({ settings }) => {
100
178
  const now = useNow(settings.seconds ? 1000 : 1e4);
101
- const wide = slot.kind === "window";
102
- const day = now.toLocaleDateString(undefined, {
103
- weekday: "short",
104
- day: "numeric",
105
- month: "short"
106
- });
107
- if (settings.style === "analog") {
108
- const face = analogFace(now, { face: THEME.faint, hands: settings.color, accent: settings.accent, ticks: THEME.muted }, settings.seconds);
109
- const size = wide ? 50 : settings.date ? 66 : 82;
110
- return /* @__PURE__ */ jsxs("div", {
111
- style: {
112
- ...fill,
113
- flexDirection: wide ? "row" : "column",
114
- gap: wide ? 12 : 4,
115
- ...surfaceStyle(settings.background)
116
- },
117
- children: [
118
- /* @__PURE__ */ jsx("img", {
119
- src: face,
120
- width: size,
121
- height: size,
122
- alt: ""
123
- }),
124
- settings.date || wide ? /* @__PURE__ */ jsx("div", {
125
- style: {
126
- display: "flex",
127
- color: THEME.muted,
128
- fontSize: wide ? 18 : 12,
129
- fontWeight: 700
130
- },
131
- children: day
132
- }) : null
133
- ]
134
- });
179
+ const zone = known(settings.zone);
180
+ return {
181
+ ...dateValues(now, zone),
182
+ ...timeValues(now, { zone, hour12: settings.hour12, seconds: settings.seconds }),
183
+ date: settings.dateFormat ? formatDate(now, settings.dateFormat, { zone }) : usualDate(now, zone),
184
+ face: analogFace(dateParts(now, zone) ?? { hour: 0, minute: 0, second: 0 }, { face: THEME.faint, hands: settings.color, accent: settings.accent, ticks: THEME.muted }, settings.seconds)
185
+ };
186
+ },
187
+ look: (s, slot) => {
188
+ const wide = slot === "window";
189
+ const layers = [];
190
+ const row = wide ? "time" : "small";
191
+ if (s.style === "analog") {
192
+ const size = wide ? 86 : s.date ? 69 : 85;
193
+ layers.push(iconLayer("face", "{{self.face}}", { size, ...wide && { row } }));
194
+ } else {
195
+ layers.push(textLayer("time", "{{self.hour}}:{{self.minute}}", {
196
+ color: s.color,
197
+ size: wide ? 34 : 28,
198
+ ...wide && { row }
199
+ }));
200
+ const small = { color: s.accent, size: wide ? 16 : 13, row };
201
+ if (s.seconds)
202
+ layers.push(textLayer("seconds", ":{{self.second}}", small));
203
+ if (s.hour12)
204
+ layers.push(textLayer("period", "{{self.period}}", small));
135
205
  }
136
- const { main, seconds, period } = timeParts(now, settings.hour12);
137
- const small = [settings.seconds ? `:${seconds}` : "", period].filter(Boolean).join(" ");
138
- const big = wide ? 34 : main.length > 4 ? 27 : 31;
139
- return /* @__PURE__ */ jsxs("div", {
140
- style: {
141
- ...column,
142
- ...surfaceStyle(settings.background),
143
- color: settings.color,
144
- fontWeight: 700,
145
- gap: wide ? 0 : 2,
146
- ...wide && { flexDirection: "row", gap: 14 }
147
- },
148
- children: [
149
- /* @__PURE__ */ jsxs("div", {
150
- style: { display: "flex", alignItems: "baseline", gap: 3 },
151
- children: [
152
- /* @__PURE__ */ jsx("div", {
153
- style: { display: "flex", fontSize: big, letterSpacing: -0.5 },
154
- children: main
155
- }),
156
- wide && small ? /* @__PURE__ */ jsx("div", {
157
- style: { display: "flex", fontSize: 16, color: settings.accent },
158
- children: small
159
- }) : null
160
- ]
161
- }),
162
- !wide && small ? /* @__PURE__ */ jsx("div", {
163
- style: { display: "flex", fontSize: 13, color: settings.accent },
164
- children: small
165
- }) : null,
166
- settings.date ? /* @__PURE__ */ jsx("div", {
167
- style: { display: "flex", fontSize: wide ? 16 : 12, color: THEME.muted },
168
- children: day
169
- }) : null
170
- ]
171
- });
206
+ if (s.date || wide && s.style === "analog") {
207
+ layers.push(textLayer("date", "{{self.date}}", {
208
+ color: THEME.muted,
209
+ size: wide ? 16 : 12,
210
+ ...wide && { row }
211
+ }));
212
+ }
213
+ return { background: surface(s.background), layers };
172
214
  }
173
215
  });
174
216
  var date = plugin.action({
@@ -179,184 +221,229 @@ var date = plugin.action({
179
221
  icon: "calendar",
180
222
  slots: ["key", "window"],
181
223
  settings: {
224
+ zone: zoneSetting,
182
225
  accent: setting.color("Accent", { default: THEME.danger }),
183
226
  background: setting.color("Background", { default: THEME.background })
184
227
  },
185
- component: ({ settings, slot }) => {
186
- const now = useNow(60000);
187
- const weekday = now.toLocaleDateString(undefined, { weekday: "short" });
188
- const month = now.toLocaleDateString(undefined, { month: "short" });
189
- if (slot.kind === "window") {
190
- return /* @__PURE__ */ jsxs("div", {
191
- style: { ...fill, gap: 14, ...surfaceStyle(settings.background), fontWeight: 700 },
192
- children: [
193
- /* @__PURE__ */ jsx("div", {
194
- style: { display: "flex", fontSize: 36, color: THEME.text },
195
- children: now.getDate()
228
+ display: true,
229
+ values: DATE_VALUES,
230
+ useValues: ({ settings }) => dateValues(useNow(60000), known(settings.zone)),
231
+ look: (s, slot) => {
232
+ if (slot === "window") {
233
+ return {
234
+ background: surface(s.background),
235
+ layers: [
236
+ textLayer("day", "{{self.day}}", {
237
+ size: 36,
238
+ color: THEME.text,
239
+ align: "right",
240
+ frame: { x: 4, y: 0, width: 32, height: 100 }
241
+ }),
242
+ textLayer("weekday", "{{self.weekday}}", {
243
+ size: 15,
244
+ color: s.accent,
245
+ align: "left",
246
+ frame: { x: 41, y: 14, width: 57, height: 36 }
196
247
  }),
197
- /* @__PURE__ */ jsxs("div", {
198
- style: { display: "flex", flexDirection: "column" },
199
- children: [
200
- /* @__PURE__ */ jsx("div", {
201
- style: { display: "flex", fontSize: 15, color: settings.accent },
202
- children: now.toLocaleDateString(undefined, { weekday: "long" })
203
- }),
204
- /* @__PURE__ */ jsx("div", {
205
- style: { display: "flex", fontSize: 14, color: THEME.muted },
206
- children: now.toLocaleDateString(undefined, { month: "long", year: "numeric" })
207
- })
208
- ]
248
+ textLayer("month", "{{self.month}} {{self.year}}", {
249
+ size: 14,
250
+ color: THEME.muted,
251
+ align: "left",
252
+ frame: { x: 41, y: 50, width: 57, height: 36 }
209
253
  })
210
254
  ]
211
- });
255
+ };
212
256
  }
213
- return /* @__PURE__ */ jsxs("div", {
214
- style: {
215
- ...column,
216
- justifyContent: "flex-start",
217
- ...surfaceStyle(settings.background),
218
- fontWeight: 700
219
- },
220
- children: [
221
- /* @__PURE__ */ jsx("div", {
222
- style: {
223
- display: "flex",
224
- width: "100%",
225
- height: 26,
226
- alignItems: "center",
227
- justifyContent: "center",
228
- background: settings.accent,
229
- color: "#ffffff",
230
- fontSize: 13,
231
- letterSpacing: 1
232
- },
233
- children: weekday.toUpperCase()
257
+ const banner = { x: 0, y: 0, width: 100, height: 27 };
258
+ return {
259
+ background: surface(s.background),
260
+ layers: [
261
+ rectLayer("banner", { fill: s.accent, frame: banner }),
262
+ textLayer("weekday", "{{upper(self.weekdayShort)}}", {
263
+ size: 13,
264
+ color: readable("#ffffff", s.accent),
265
+ frame: banner
234
266
  }),
235
- /* @__PURE__ */ jsx("div", {
236
- style: { display: "flex", fontSize: 40, color: THEME.text, marginTop: 4 },
237
- children: now.getDate()
267
+ textLayer("day", "{{self.day}}", {
268
+ size: 40,
269
+ color: THEME.text,
270
+ frame: { x: 0, y: 29, width: 100, height: 46 }
238
271
  }),
239
- /* @__PURE__ */ jsx("div", {
240
- style: { display: "flex", fontSize: 12, color: THEME.muted },
241
- children: month
272
+ textLayer("month", "{{self.monthShort}}", {
273
+ size: 12,
274
+ color: THEME.muted,
275
+ frame: { x: 0, y: 75, width: 100, height: 18 }
242
276
  })
243
277
  ]
244
- });
278
+ };
245
279
  }
246
280
  });
247
281
 
248
- // src/timers.tsx
249
- import { setting as setting2, useKeyPress, useKeyPressed, useNow as useNow2, usePage } from "@sdeck/plugin";
250
- import { surfaceStyle as surfaceStyle2 } from "@sdeck/schema";
251
- import { useEffect, useReducer } from "react";
252
- import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
282
+ // src/runs.ts
283
+ import { useSyncExternalStore } from "react";
253
284
  var elapsed = (run, now) => run.banked + (run.startedAt === undefined ? 0 : now - run.startedAt);
254
285
  function toggled(run, now) {
255
286
  return run.startedAt === undefined ? { ...run, startedAt: now } : { banked: elapsed(run, now) };
256
287
  }
288
+ var IDLE = { banked: 0 };
257
289
  var runs = new Map;
258
- var runOf = (id) => runs.get(id) ?? { banked: 0 };
259
- var slotId = (page, slot, kind) => `${kind}|${page}|${slot.kind}|${"index" in slot ? slot.index : 0}`;
260
- var ended = new Set;
290
+ var watchers = new Set;
291
+ var subscribe = (watcher) => {
292
+ watchers.add(watcher);
293
+ return () => void watchers.delete(watcher);
294
+ };
295
+ var runOf = (button) => runs.get(button) ?? IDLE;
296
+ var useRun = (button) => useSyncExternalStore(subscribe, () => runOf(button));
297
+ function setRun(button, run) {
298
+ if (run === IDLE)
299
+ runs.delete(button);
300
+ else
301
+ runs.set(button, run);
302
+ changed();
303
+ }
304
+ function changed() {
305
+ arm();
306
+ publish();
307
+ for (const watcher of [...watchers])
308
+ watcher();
309
+ }
310
+ var lengthOf = (s) => Math.max(1, (s.minutes * 60 + s.seconds) * 1000);
311
+ function timerValues(run, total, now) {
312
+ const passed = elapsed(run, Math.max(now, run.startedAt ?? 0));
313
+ const remaining = Math.min(total, Math.max(0, total - passed));
314
+ const done = remaining <= 0;
315
+ const counting = run.startedAt !== undefined && !done;
316
+ return {
317
+ time: formatDuration(remaining),
318
+ remaining: Math.ceil(remaining / 1000),
319
+ total: total / 1000,
320
+ progress: Math.round((1 - remaining / total) * 1000) / 10,
321
+ counting,
322
+ paused: !counting && !done && remaining < total,
323
+ done,
324
+ ...counting && { endsAt: (run.startedAt ?? 0) + total - run.banked }
325
+ };
326
+ }
327
+ function stopwatchValues(run, now) {
328
+ const ms = elapsed(run, Math.max(now, run.startedAt ?? 0));
329
+ const counting = run.startedAt !== undefined;
330
+ return {
331
+ time: formatDuration(ms, false),
332
+ elapsed: Math.floor(ms / 1000),
333
+ counting,
334
+ paused: !counting && ms > 0
335
+ };
336
+ }
337
+ function variableName(text) {
338
+ const plain = text.normalize("NFKD").replace(/\p{M}/gu, "").toLowerCase();
339
+ const name = plain.replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "");
340
+ return /^\d/.test(name) ? `_${name}` : name;
341
+ }
342
+ function namedButtons(buttons) {
343
+ const named = new Map;
344
+ const place = ({ page, slot }) => variableName(`${page} ${slot.kind}${slot.kind === "window" ? "" : ` ${slot.index + 1}`}`);
345
+ const chosen = (b) => variableName(String(b.settings.variable ?? ""));
346
+ for (const button of [...buttons.filter(chosen), ...buttons.filter((b) => !chosen(b))]) {
347
+ const base = chosen(button) || variableName(String(button.settings.label ?? "")) || place(button);
348
+ let name = base;
349
+ for (let n = 2;named.has(name); n++)
350
+ name = `${base}_${n}`;
351
+ named.set(name, button);
352
+ }
353
+ return named;
354
+ }
355
+ var timerButtons = new Map;
356
+ var stopwatchButtons = new Map;
357
+ var readTimers = () => {
358
+ const now = Date.now();
359
+ return Object.fromEntries([...timerButtons].map(([name, { id, settings }]) => {
360
+ const s = settings;
361
+ return [name, { label: s.label, ...timerValues(runOf(id), lengthOf(s), now) }];
362
+ }));
363
+ };
364
+ var readStopwatches = () => {
365
+ const now = Date.now();
366
+ return Object.fromEntries([...stopwatchButtons].map(([name, { id, settings }]) => [
367
+ name,
368
+ { label: settings.label, ...stopwatchValues(runOf(id), now) }
369
+ ]));
370
+ };
371
+ var EVERY_MS = 200;
372
+ var timers = plugin.state.json("timers", "Timers", {
373
+ description: "Every timer by its variable name: basics.timers.tea.time",
374
+ poll: { everyMs: EVERY_MS, read: readTimers }
375
+ });
376
+ var stopwatches = plugin.state.json("stopwatches", "Stopwatches", {
377
+ description: "Every stopwatch by its variable name: basics.stopwatches.run.time",
378
+ poll: { everyMs: EVERY_MS, read: readStopwatches }
379
+ });
380
+ function publish() {
381
+ timers.set(readTimers());
382
+ stopwatches.set(readStopwatches());
383
+ }
384
+ var endListeners = new Set;
385
+ var alarms = new Map;
386
+ function arm() {
387
+ for (const alarm of alarms.values())
388
+ clearTimeout(alarm);
389
+ alarms.clear();
390
+ for (const [name, { id, settings }] of timerButtons) {
391
+ const s = settings;
392
+ const run = runOf(id);
393
+ if (run.startedAt === undefined)
394
+ continue;
395
+ const left = lengthOf(s) - elapsed(run, Date.now());
396
+ alarms.set(id, setTimeout(() => end(id, name, s), Math.max(0, left)));
397
+ }
398
+ }
399
+ function end(id, name, settings) {
400
+ setRun(id, { banked: lengthOf(settings) });
401
+ const event = { name, label: settings.label, seconds: lengthOf(settings) / 1000 };
402
+ for (const listener of [...endListeners])
403
+ listener(event);
404
+ plugin.context?.emit("timer-ended", event);
405
+ }
406
+ function followButtons(context) {
407
+ const refresh = () => {
408
+ const placed = context.buttons.list();
409
+ const of = (action) => placed.filter((b) => b.action === action);
410
+ timerButtons = namedButtons(of("timer"));
411
+ stopwatchButtons = namedButtons(of("stopwatch"));
412
+ const kept = new Set([...of("timer"), ...of("stopwatch")].map((b) => b.id));
413
+ for (const id of [...runs.keys()])
414
+ if (!kept.has(id))
415
+ runs.delete(id);
416
+ changed();
417
+ };
418
+ const stop = context.buttons.watch(refresh);
419
+ refresh();
420
+ return () => {
421
+ stop();
422
+ for (const alarm of alarms.values())
423
+ clearTimeout(alarm);
424
+ alarms.clear();
425
+ };
426
+ }
427
+
428
+ // src/timers.tsx
429
+ import { setting as setting2, useKeyPress, useNow as useNow2 } from "@sdeck/plugin";
430
+ import { iconLayer as iconLayer2, rectLayer as rectLayer2, surface as surface2, textLayer as textLayer2 } from "@sdeck/schema";
431
+ var variable = (kind, example) => setting2.text("Variable name", {
432
+ placeholder: "From the label",
433
+ description: `Reads it anywhere: {{basics.${kind}.${example}.time}}`,
434
+ validate: (name) => !name || /^[a-z_][a-z0-9_]*$/.test(name) ? undefined : "should be small letters, digits and _, like focus_time"
435
+ });
436
+ var paused = { name: "Paused", type: "boolean" };
261
437
  var timerEnded = plugin.trigger({
262
438
  id: "timer-ended",
263
439
  name: "When a timer ends",
264
- description: "Fires when any Basics timer reaches zero.",
440
+ description: "Fires when any Basics timer reaches zero, also on a page that is not shown. event.name says which.",
265
441
  subscribe: ({ fire }) => {
266
442
  const listener = (event) => fire(event);
267
- ended.add(listener);
268
- return () => ended.delete(listener);
443
+ endListeners.add(listener);
444
+ return () => endListeners.delete(listener);
269
445
  }
270
446
  });
271
- function Countdown(props) {
272
- const { remaining, total, running, label, accent, wide } = props;
273
- const done = remaining <= 0;
274
- const now = useNow2(done ? 500 : 60000);
275
- const flash = done && now.getTime() % 1000 < 500;
276
- const background = flash ? THEME.danger : props.pressed ? THEME.faint : props.background;
277
- const caption = done ? "Done" : running ? label : remaining < total ? "Paused" : label;
278
- const time = formatDuration(remaining);
279
- const color = done ? "#ffffff" : running ? THEME.text : THEME.muted;
280
- if (wide) {
281
- return /* @__PURE__ */ jsxs2("div", {
282
- style: {
283
- ...column,
284
- ...surfaceStyle2(background),
285
- padding: "0 16px",
286
- gap: 6,
287
- fontWeight: 700
288
- },
289
- children: [
290
- /* @__PURE__ */ jsxs2("div", {
291
- style: {
292
- display: "flex",
293
- width: "100%",
294
- justifyContent: "space-between",
295
- alignItems: "baseline"
296
- },
297
- children: [
298
- /* @__PURE__ */ jsx2("div", {
299
- style: { display: "flex", fontSize: 14, color: done ? "#ffffff" : THEME.muted },
300
- children: caption || "Timer"
301
- }),
302
- /* @__PURE__ */ jsx2("div", {
303
- style: { display: "flex", fontSize: 26, color },
304
- children: time
305
- })
306
- ]
307
- }),
308
- /* @__PURE__ */ jsx2("div", {
309
- style: {
310
- display: "flex",
311
- width: "100%",
312
- height: 4,
313
- background: THEME.faint,
314
- borderRadius: 2
315
- },
316
- children: /* @__PURE__ */ jsx2("div", {
317
- style: {
318
- display: "flex",
319
- width: `${Math.round((1 - remaining / total) * 100)}%`,
320
- height: 4,
321
- background: accent,
322
- borderRadius: 2
323
- }
324
- })
325
- })
326
- ]
327
- });
328
- }
329
- return /* @__PURE__ */ jsxs2("div", {
330
- style: { ...fill, ...surfaceStyle2(background), position: "relative" },
331
- children: [
332
- /* @__PURE__ */ jsx2("img", {
333
- src: ring(1 - remaining / total, done ? "#ffffff" : accent, THEME.faint),
334
- width: 86,
335
- height: 86,
336
- alt: "",
337
- style: { position: "absolute", left: 5, top: 5 }
338
- }),
339
- /* @__PURE__ */ jsxs2("div", {
340
- style: { ...column, fontWeight: 700 },
341
- children: [
342
- /* @__PURE__ */ jsx2("div", {
343
- style: { display: "flex", fontSize: time.length > 5 ? 17 : 22, color },
344
- children: time
345
- }),
346
- caption ? /* @__PURE__ */ jsx2("div", {
347
- style: {
348
- display: "flex",
349
- fontSize: 10,
350
- color: done ? "#ffffff" : THEME.muted,
351
- maxWidth: 60
352
- },
353
- children: caption
354
- }) : null
355
- ]
356
- })
357
- ]
358
- });
359
- }
360
447
  var timer = plugin.action({
361
448
  id: "timer",
362
449
  name: "Timer",
@@ -368,50 +455,104 @@ var timer = plugin.action({
368
455
  minutes: setting2.number("Minutes", { default: 5, min: 0, max: 600 }),
369
456
  seconds: setting2.number("Seconds", { default: 0, min: 0, max: 59 }),
370
457
  label: setting2.text("Label", { placeholder: "Tea, Focus…" }),
458
+ variable: variable("timers", "tea"),
371
459
  accent: setting2.color("Ring colour", { default: THEME.warm }),
372
460
  background: setting2.color("Background", { default: THEME.background })
373
461
  },
374
- component: ({ settings, slot, plugin: plugin2 }) => {
375
- const page = usePage();
376
- const id = slotId(page, slot, "timer");
377
- const total = Math.max(1, (settings.minutes * 60 + settings.seconds) * 1000);
378
- const [, redraw] = useReducer((n) => n + 1, 0);
379
- const pressed = useKeyPressed();
380
- const run = runOf(id);
381
- const running = run.startedAt !== undefined;
382
- const now = useNow2(running ? 250 : 60000).getTime();
383
- const remaining = Math.max(0, total - elapsed(run, now));
384
- const finished = running && remaining <= 0;
385
- useEffect(() => {
386
- if (!finished)
387
- return;
388
- runs.set(id, { banked: total });
389
- const event = { label: settings.label, seconds: total / 1000 };
390
- for (const listener of [...ended])
391
- listener(event);
392
- plugin2.emit("timer-ended", event);
393
- redraw();
394
- }, [finished, id, total, settings.label, plugin2]);
462
+ values: {
463
+ time: { name: "Time left", type: "string", description: "4:59" },
464
+ remaining: {
465
+ name: "Seconds left",
466
+ type: "number",
467
+ description: 'For formats of your own: duration(self.remaining, "mm:ss")'
468
+ },
469
+ total: { name: "Seconds in all", type: "number" },
470
+ progress: { name: "Done so far (%)", type: "number", description: "0 to 100, for a bar" },
471
+ counting: { name: "Counting", type: "boolean" },
472
+ paused,
473
+ done: { name: "Ended", type: "boolean" },
474
+ endsAt: {
475
+ name: "Ends at",
476
+ type: "number",
477
+ description: 'While it counts: time("HH:mm", self.endsAt)'
478
+ },
479
+ blink: { name: "Blinking", type: "boolean", description: "On and off once it ended" },
480
+ caption: { name: "Caption", type: "string", description: "The label, Paused or Done" },
481
+ ring: { name: "Ring", type: "string", description: "A picture, for an image layer" }
482
+ },
483
+ useValues: ({ settings, button }) => {
484
+ const run = useRun(button);
485
+ const total = lengthOf(settings);
486
+ const over = elapsed(run, Date.now()) >= total;
487
+ const now = useNow2(over ? 500 : run.startedAt === undefined ? 60000 : 250).getTime();
395
488
  useKeyPress(() => {
396
- const current = runOf(id);
397
- runs.set(id, elapsed(current, Date.now()) >= total ? { banked: 0, startedAt: Date.now() } : toggled(current, Date.now()));
398
- redraw();
399
- }, {
400
- onLongPress: () => {
401
- runs.delete(id);
402
- redraw();
403
- }
404
- });
405
- return /* @__PURE__ */ jsx2(Countdown, {
406
- remaining,
407
- total,
408
- running,
409
- label: settings.label,
410
- accent: settings.accent,
411
- background: settings.background,
412
- wide: slot.kind === "window",
413
- pressed
414
- });
489
+ const at = Date.now();
490
+ const current = runOf(button);
491
+ const next = elapsed(current, at) >= total ? IDLE : current;
492
+ setRun(button, toggled(next, at));
493
+ }, { onLongPress: () => setRun(button, IDLE) });
494
+ const values = timerValues(run, total, now);
495
+ return {
496
+ ...values,
497
+ blink: values.done && now % 1000 < 500,
498
+ caption: values.done ? "Done" : values.paused ? "Paused" : settings.label,
499
+ ring: ring(values.progress / 100, values.done ? "#ffffff" : settings.accent, THEME.faint)
500
+ };
501
+ },
502
+ look: (s, slot) => {
503
+ const alert = rectLayer2("alert", { fill: THEME.danger, visible: "self.blink" });
504
+ const states = (id, words, options) => [
505
+ textLayer2(id, words, {
506
+ ...options,
507
+ color: THEME.text,
508
+ visible: "self.counting || self.done"
509
+ }),
510
+ textLayer2(`${id}-waiting`, words, {
511
+ ...options,
512
+ color: THEME.muted,
513
+ visible: "!self.counting && !self.done"
514
+ })
515
+ ];
516
+ if (slot === "window") {
517
+ return {
518
+ background: surface2(s.background),
519
+ layers: [
520
+ alert,
521
+ textLayer2("caption", '{{self.caption || "Timer"}}', {
522
+ color: THEME.muted,
523
+ size: 14,
524
+ align: "left",
525
+ frame: { x: 6.5, y: 12, width: 43, height: 56 }
526
+ }),
527
+ ...states("time", "{{self.time}}", {
528
+ size: 26,
529
+ align: "right",
530
+ frame: { x: 50, y: 12, width: 43.5, height: 56 }
531
+ }),
532
+ {
533
+ id: "bar",
534
+ type: "bar",
535
+ value: "self.progress",
536
+ color: s.accent,
537
+ track: THEME.faint,
538
+ position: "bottom",
539
+ thickness: 4,
540
+ radius: 2,
541
+ frame: { x: 6.5, y: 75, width: 87, height: 7 }
542
+ }
543
+ ]
544
+ };
545
+ }
546
+ const long = s.minutes >= 60;
547
+ return {
548
+ background: surface2(s.background),
549
+ layers: [
550
+ alert,
551
+ iconLayer2("ring", "{{self.ring}}", { frame: { x: 5, y: 5, width: 90, height: 90 } }),
552
+ ...states("time", "{{self.time}}", { size: long ? 17 : 22 }),
553
+ textLayer2("caption", "{{self.caption}}", { color: "#b6b8c4", size: 10 })
554
+ ]
555
+ };
415
556
  }
416
557
  });
417
558
  var stopwatch = plugin.action({
@@ -423,67 +564,62 @@ var stopwatch = plugin.action({
423
564
  slots: ["key", "window"],
424
565
  settings: {
425
566
  label: setting2.text("Label", { placeholder: "Shown under the time" }),
567
+ variable: variable("stopwatches", "run"),
426
568
  accent: setting2.color("Colour while running", { default: THEME.success }),
427
569
  background: setting2.color("Background", { default: THEME.background })
428
570
  },
429
- component: ({ settings, slot }) => {
430
- const page = usePage();
431
- const id = slotId(page, slot, "stopwatch");
432
- const [, redraw] = useReducer((n) => n + 1, 0);
433
- const pressed = useKeyPressed();
434
- const run = runOf(id);
435
- const running = run.startedAt !== undefined;
436
- const now = useNow2(running ? 100 : 60000).getTime();
437
- const ms = elapsed(run, now);
438
- useKeyPress(() => {
439
- runs.set(id, toggled(runOf(id), Date.now()));
440
- redraw();
441
- }, {
442
- onLongPress: () => {
443
- runs.delete(id);
444
- redraw();
445
- }
571
+ values: {
572
+ time: { name: "Time", type: "string", description: "1:23" },
573
+ tenths: { name: "Tenths", type: "number", description: "0 to 9" },
574
+ elapsed: {
575
+ name: "Seconds so far",
576
+ type: "number",
577
+ description: 'For formats of your own: duration(self.elapsed, "mm:ss")'
578
+ },
579
+ counting: { name: "Counting", type: "boolean" },
580
+ paused,
581
+ caption: { name: "Caption", type: "string", description: "The label, or Stopped" }
582
+ },
583
+ useValues: ({ settings, button }) => {
584
+ const run = useRun(button);
585
+ const now = useNow2(run.startedAt === undefined ? 60000 : 100).getTime();
586
+ useKeyPress(() => setRun(button, toggled(runOf(button), Date.now())), {
587
+ onLongPress: () => setRun(button, IDLE)
446
588
  });
447
- const time = formatDuration(ms, false);
448
- const tenths = Math.floor(ms % 1000 / 100);
449
- const color = running ? settings.accent : ms > 0 ? THEME.text : THEME.muted;
450
- const caption = settings.label || (running ? "" : ms > 0 ? "Stopped" : "Stopwatch");
451
- const wide = slot.kind === "window";
452
- return /* @__PURE__ */ jsxs2("div", {
453
- style: {
454
- ...column,
455
- ...wide && { flexDirection: "row", gap: 14 },
456
- ...surfaceStyle2(pressed ? THEME.faint : settings.background),
457
- fontWeight: 700
458
- },
459
- children: [
460
- /* @__PURE__ */ jsxs2("div", {
461
- style: { display: "flex", alignItems: "baseline" },
462
- children: [
463
- /* @__PURE__ */ jsx2("div", {
464
- style: { display: "flex", fontSize: wide ? 32 : time.length > 5 ? 20 : 26, color },
465
- children: time
466
- }),
467
- /* @__PURE__ */ jsxs2("div", {
468
- style: { display: "flex", fontSize: wide ? 20 : 15, color: THEME.muted },
469
- children: [
470
- ".",
471
- tenths
472
- ]
473
- })
474
- ]
589
+ const values = stopwatchValues(run, now);
590
+ const ms = elapsed(run, Math.max(now, run.startedAt ?? 0));
591
+ return {
592
+ ...values,
593
+ tenths: Math.floor(ms % 1000 / 100),
594
+ caption: settings.label || (values.counting ? "" : values.paused ? "Stopped" : "Stopwatch")
595
+ };
596
+ },
597
+ look: (s, slot) => {
598
+ const wide = slot === "window";
599
+ const words = '{{self.time}}{{self.elapsed < 3600 ? "." + self.tenths : ""}}';
600
+ const row = wide ? { row: "time" } : {};
601
+ const size = wide ? 32 : 22;
602
+ return {
603
+ background: surface2(s.background),
604
+ layers: [
605
+ textLayer2("time", words, { ...row, size, color: s.accent, visible: "self.counting" }),
606
+ textLayer2("time-stopped", words, {
607
+ ...row,
608
+ size,
609
+ color: THEME.text,
610
+ visible: "!self.counting"
475
611
  }),
476
- caption ? /* @__PURE__ */ jsx2("div", {
477
- style: { display: "flex", fontSize: wide ? 14 : 11, color: THEME.muted },
478
- children: caption
479
- }) : null
612
+ textLayer2("caption", "{{self.caption}}", {
613
+ ...row,
614
+ size: wide ? 14 : 11,
615
+ color: THEME.muted
616
+ })
480
617
  ]
481
- });
618
+ };
482
619
  }
483
620
  });
484
621
 
485
622
  // src/index.tsx
486
- import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
487
623
  var label = plugin.action({
488
624
  id: "label",
489
625
  name: "Label",
@@ -512,12 +648,12 @@ var label = plugin.action({
512
648
  });
513
649
  }
514
650
  if (s.text) {
515
- layers.push(textLayer("label", s.text, {
651
+ layers.push(textLayer3("label", s.text, {
516
652
  color: s.color,
517
653
  size: s.image ? Math.min(s.fontSize, 14) : s.fontSize
518
654
  }));
519
655
  }
520
- return { background: surface(s.background), layers };
656
+ return { background: surface3(s.background), layers };
521
657
  }
522
658
  });
523
659
  var counter = plugin.action({
@@ -533,46 +669,41 @@ var counter = plugin.action({
533
669
  accent: setting3.color("Colour", { default: THEME.accent }),
534
670
  background: setting3.color("Background", { default: THEME.background })
535
671
  },
536
- component: ({ settings, setSettings }) => {
537
- const pressed = useKeyPressed2();
672
+ values: {
673
+ count: { name: "Count", type: "number" },
674
+ title: { name: "Title", type: "string" }
675
+ },
676
+ useValues: ({ settings, setSettings }) => {
538
677
  useKeyPress2(() => setSettings({ count: settings.count + settings.step }), {
539
678
  onLongPress: () => setSettings({ count: 0 })
540
679
  });
541
- const text = String(settings.count);
542
- const size = text.length <= 2 ? 44 : text.length <= 4 ? 32 : 22;
543
- return /* @__PURE__ */ jsxs3("div", {
544
- style: {
545
- ...column,
546
- ...surfaceStyle3(pressed ? settings.accent : settings.background),
547
- fontWeight: 700,
548
- gap: 2
549
- },
550
- children: [
551
- settings.title ? /* @__PURE__ */ jsx3("div", {
552
- style: {
553
- display: "flex",
554
- fontSize: 12,
555
- color: pressed ? "#ffffff" : THEME.muted,
556
- maxWidth: 86
557
- },
558
- children: settings.title
559
- }) : null,
560
- /* @__PURE__ */ jsx3("div", {
561
- style: { display: "flex", fontSize: size, color: pressed ? "#ffffff" : THEME.text },
562
- children: text
680
+ return { count: settings.count, title: settings.title };
681
+ },
682
+ look: (s) => {
683
+ const { length } = String(s.count);
684
+ return {
685
+ background: surface3(s.background),
686
+ layers: [
687
+ rectLayer3("pressed", { fill: surface3(s.accent), visible: "self.held" }),
688
+ ...s.title ? [
689
+ textLayer3("title", "{{self.title}}", {
690
+ size: 12,
691
+ color: "#b6b8c4",
692
+ position: "top"
693
+ })
694
+ ] : [],
695
+ textLayer3("count", "{{self.count}}", {
696
+ size: length <= 2 ? 44 : length <= 4 ? 32 : 22,
697
+ color: THEME.text
563
698
  }),
564
- /* @__PURE__ */ jsx3("div", {
565
- style: {
566
- display: "flex",
567
- width: 26,
568
- height: 3,
569
- borderRadius: 2,
570
- background: pressed ? "#ffffff" : settings.accent,
571
- marginTop: 2
572
- }
699
+ rectLayer3("line", {
700
+ fill: s.accent,
701
+ frame: { x: 36.5, y: 82, width: 27, height: 3 },
702
+ radius: 2,
703
+ visible: "!self.held"
573
704
  })
574
705
  ]
575
- });
706
+ };
576
707
  }
577
708
  });
578
709
  var NAVIGATE_ICONS = {
@@ -649,10 +780,10 @@ var pageName = plugin.action({
649
780
  },
650
781
  display: true,
651
782
  look: (s) => ({
652
- background: surface(s.background),
783
+ background: surface3(s.background),
653
784
  layers: [
654
- iconLayer("icon", "lucide/layers", { color: s.accent, size: 30, row: "name" }),
655
- textLayer("label", "{{page.name}}", { color: s.color, size: s.fontSize, row: "name" })
785
+ iconLayer3("icon", "lucide/layers", { color: s.accent, size: 30, row: "name" }),
786
+ textLayer3("label", "{{page.name}}", { color: s.color, size: s.fontSize, row: "name" })
656
787
  ]
657
788
  })
658
789
  });
@@ -709,17 +840,24 @@ var brightness = plugin.action({
709
840
  });
710
841
  var src_default = plugin.define({
711
842
  actions: [label, clock, date, timer, stopwatch, counter, navigate, pageName, brightness],
712
- triggers: [timerEnded]
843
+ triggers: [timerEnded],
844
+ sources: [zones],
845
+ state: [timers, stopwatches],
846
+ activate: followButtons
713
847
  });
714
848
  export {
715
849
  analogFace,
850
+ dateValues,
716
851
  src_default as default,
717
852
  elapsed,
718
853
  formatDuration,
854
+ namedButtons,
719
855
  nextLevel,
720
856
  parseLevels,
721
- timeParts,
722
- toggled
857
+ timeValues,
858
+ timerValues,
859
+ toggled,
860
+ variableName
723
861
  };
724
862
 
725
- //# debugId=60FA3404E28B884364756E2164756E21
863
+ //# debugId=9646FF200B7C742C64756E2164756E21