@sdeck/plugin-basics 0.0.0-stage → 1.0.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/timers.tsx ADDED
@@ -0,0 +1,269 @@
1
+ /**
2
+ * A countdown timer and a stopwatch. Their state lives here, by page and slot, so they keep
3
+ * running while another page is shown.
4
+ */
5
+ import {
6
+ defineAction,
7
+ defineTrigger,
8
+ type SlotInfo,
9
+ useKeyPress,
10
+ useKeyPressed,
11
+ useNow,
12
+ usePage,
13
+ } from "@sdeck/plugin";
14
+ import { useEffect, useReducer } from "react";
15
+ import { column, fill, formatDuration, ring, THEME } from "./theme.ts";
16
+
17
+ /** A running or paused clock: `startedAt` while running, `banked` ms from earlier runs. */
18
+ export interface Run {
19
+ startedAt?: number;
20
+ banked: number;
21
+ }
22
+
23
+ /** How long it has run, up to `now`. */
24
+ export const elapsed = (run: Run, now: number) =>
25
+ run.banked + (run.startedAt === undefined ? 0 : now - run.startedAt);
26
+
27
+ /** Starts a paused run, or pauses a running one. */
28
+ export function toggled(run: Run, now: number): Run {
29
+ return run.startedAt === undefined ? { ...run, startedAt: now } : { banked: elapsed(run, now) };
30
+ }
31
+
32
+ const runs = new Map<string, Run>();
33
+ const runOf = (id: string) => runs.get(id) ?? { banked: 0 };
34
+
35
+ const slotId = (page: string, slot: SlotInfo, kind: string) =>
36
+ `${kind}|${page}|${slot.kind}|${"index" in slot ? slot.index : 0}`;
37
+
38
+ /** Listeners of the "timer ends" trigger. */
39
+ const ended = new Set<(event: { label: string; seconds: number }) => void>();
40
+
41
+ export const timerEnded = defineTrigger({
42
+ id: "timer-ended",
43
+ name: "When a timer ends",
44
+ description: "Fires when any Basics timer reaches zero.",
45
+ subscribe: ({ fire }) => {
46
+ const listener = (event: { label: string; seconds: number }) => fire(event);
47
+ ended.add(listener);
48
+ return () => ended.delete(listener);
49
+ },
50
+ });
51
+
52
+ /** One countdown's ring, time and caption. */
53
+ function Countdown(props: {
54
+ remaining: number;
55
+ total: number;
56
+ running: boolean;
57
+ label: string;
58
+ accent: string;
59
+ background: string;
60
+ wide: boolean;
61
+ pressed: boolean;
62
+ }) {
63
+ const { remaining, total, running, label, accent, wide } = props;
64
+ const done = remaining <= 0;
65
+ // A finished timer blinks until it is reset.
66
+ const now = useNow(done ? 500 : 60_000);
67
+ const flash = done && now.getTime() % 1000 < 500;
68
+ const background = flash ? THEME.danger : props.pressed ? THEME.faint : props.background;
69
+ const caption = done ? "Done" : running ? label : remaining < total ? "Paused" : label;
70
+ const time = formatDuration(remaining);
71
+ const color = done ? "#ffffff" : running ? THEME.text : THEME.muted;
72
+ if (wide) {
73
+ return (
74
+ <div style={{ ...column, background, padding: "0 16px", gap: 6, fontWeight: 700 }}>
75
+ <div
76
+ style={{
77
+ display: "flex",
78
+ width: "100%",
79
+ justifyContent: "space-between",
80
+ alignItems: "baseline",
81
+ }}
82
+ >
83
+ <div style={{ display: "flex", fontSize: 14, color: done ? "#ffffff" : THEME.muted }}>
84
+ {caption || "Timer"}
85
+ </div>
86
+ <div style={{ display: "flex", fontSize: 26, color }}>{time}</div>
87
+ </div>
88
+ <div
89
+ style={{
90
+ display: "flex",
91
+ width: "100%",
92
+ height: 4,
93
+ background: THEME.faint,
94
+ borderRadius: 2,
95
+ }}
96
+ >
97
+ <div
98
+ style={{
99
+ display: "flex",
100
+ width: `${Math.round((1 - remaining / total) * 100)}%`,
101
+ height: 4,
102
+ background: accent,
103
+ borderRadius: 2,
104
+ }}
105
+ />
106
+ </div>
107
+ </div>
108
+ );
109
+ }
110
+ return (
111
+ <div style={{ ...fill, background, position: "relative" }}>
112
+ <img
113
+ src={ring(1 - remaining / total, done ? "#ffffff" : accent, THEME.faint)}
114
+ width={86}
115
+ height={86}
116
+ alt=""
117
+ style={{ position: "absolute", left: 5, top: 5 }}
118
+ />
119
+ <div style={{ ...column, fontWeight: 700 }}>
120
+ <div style={{ display: "flex", fontSize: time.length > 5 ? 17 : 22, color }}>{time}</div>
121
+ {caption ? (
122
+ <div
123
+ style={{
124
+ display: "flex",
125
+ fontSize: 10,
126
+ color: done ? "#ffffff" : THEME.muted,
127
+ maxWidth: 60,
128
+ }}
129
+ >
130
+ {caption}
131
+ </div>
132
+ ) : null}
133
+ </div>
134
+ </div>
135
+ );
136
+ }
137
+
138
+ export const timer = defineAction({
139
+ id: "timer",
140
+ name: "Timer",
141
+ description: "Counts down. Press to start or pause, hold to reset. Blinks when it ends.",
142
+ category: "Time",
143
+ icon: "timer",
144
+ slots: ["key", "window"],
145
+ 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 },
151
+ },
152
+ component: ({ settings, slot, plugin }) => {
153
+ const page = usePage();
154
+ const id = slotId(page, slot, "timer");
155
+ const total = Math.max(1, (settings.minutes * 60 + settings.seconds) * 1000);
156
+ const [, redraw] = useReducer((n: number) => n + 1, 0);
157
+ const pressed = useKeyPressed();
158
+ const run = runOf(id);
159
+ const running = run.startedAt !== undefined;
160
+ const now = useNow(running ? 250 : 60_000).getTime();
161
+ const remaining = Math.max(0, total - elapsed(run, now));
162
+ const finished = running && remaining <= 0;
163
+ useEffect(() => {
164
+ if (!finished) return;
165
+ // Ended: stop counting, and tell whoever waits for it.
166
+ runs.set(id, { banked: total });
167
+ const event = { label: settings.label, seconds: total / 1000 };
168
+ for (const listener of [...ended]) listener(event);
169
+ plugin.emit("timer-ended", event);
170
+ redraw();
171
+ }, [finished, id, total, settings.label, plugin]);
172
+ useKeyPress(
173
+ () => {
174
+ const current = runOf(id);
175
+ // A finished timer starts over.
176
+ runs.set(
177
+ id,
178
+ elapsed(current, Date.now()) >= total
179
+ ? { banked: 0, startedAt: Date.now() }
180
+ : toggled(current, Date.now()),
181
+ );
182
+ redraw();
183
+ },
184
+ {
185
+ onLongPress: () => {
186
+ runs.delete(id);
187
+ redraw();
188
+ },
189
+ },
190
+ );
191
+ return (
192
+ <Countdown
193
+ remaining={remaining}
194
+ total={total}
195
+ running={running}
196
+ label={settings.label}
197
+ accent={settings.accent}
198
+ background={settings.background}
199
+ wide={slot.kind === "window"}
200
+ pressed={pressed}
201
+ />
202
+ );
203
+ },
204
+ });
205
+
206
+ export const stopwatch = defineAction({
207
+ id: "stopwatch",
208
+ name: "Stopwatch",
209
+ description: "Counts up. Press to start or stop, hold to reset.",
210
+ category: "Time",
211
+ icon: "watch",
212
+ slots: ["key", "window"],
213
+ 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 },
217
+ },
218
+ component: ({ settings, slot }) => {
219
+ const page = usePage();
220
+ const id = slotId(page, slot, "stopwatch");
221
+ const [, redraw] = useReducer((n: number) => n + 1, 0);
222
+ const pressed = useKeyPressed();
223
+ const run = runOf(id);
224
+ const running = run.startedAt !== undefined;
225
+ const now = useNow(running ? 100 : 60_000).getTime();
226
+ const ms = elapsed(run, now);
227
+ useKeyPress(
228
+ () => {
229
+ runs.set(id, toggled(runOf(id), Date.now()));
230
+ redraw();
231
+ },
232
+ {
233
+ onLongPress: () => {
234
+ runs.delete(id);
235
+ redraw();
236
+ },
237
+ },
238
+ );
239
+ const time = formatDuration(ms, false);
240
+ const tenths = Math.floor((ms % 1000) / 100);
241
+ const color = running ? settings.accent : ms > 0 ? THEME.text : THEME.muted;
242
+ const caption = settings.label || (running ? "" : ms > 0 ? "Stopped" : "Stopwatch");
243
+ const wide = slot.kind === "window";
244
+ return (
245
+ <div
246
+ style={{
247
+ ...column,
248
+ ...(wide && { flexDirection: "row", gap: 14 }),
249
+ background: pressed ? THEME.faint : settings.background,
250
+ fontWeight: 700,
251
+ }}
252
+ >
253
+ <div style={{ display: "flex", alignItems: "baseline" }}>
254
+ <div style={{ display: "flex", fontSize: wide ? 32 : time.length > 5 ? 20 : 26, color }}>
255
+ {time}
256
+ </div>
257
+ <div style={{ display: "flex", fontSize: wide ? 20 : 15, color: THEME.muted }}>
258
+ .{tenths}
259
+ </div>
260
+ </div>
261
+ {caption ? (
262
+ <div style={{ display: "flex", fontSize: wide ? 14 : 11, color: THEME.muted }}>
263
+ {caption}
264
+ </div>
265
+ ) : null}
266
+ </div>
267
+ );
268
+ },
269
+ });
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.