@sdeck/plugin-basics 0.0.0-stage → 1.1.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/dist/clock.d.ts +16 -0
- package/dist/clock.d.ts.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +754 -0
- package/dist/index.js.map +13 -0
- package/dist/theme.d.ts +25 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/timers.d.ts +13 -0
- package/dist/timers.d.ts.map +1 -0
- package/package.json +60 -4
- package/src/clock.tsx +197 -0
- package/src/index.tsx +325 -0
- package/src/theme.ts +56 -0
- package/src/timers.tsx +269 -0
- package/README.md +0 -3
package/src/index.tsx
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basics: what most decks want. Labels, time (clock, date, timer, stopwatch), a counter, page
|
|
3
|
+
* navigation and the deck's brightness.
|
|
4
|
+
*/
|
|
5
|
+
import { defineAction, definePlugin, useKeyPress, useKeyPressed } from "@sdeck/plugin";
|
|
6
|
+
import { iconLayer, type Layer, type Look, textLayer } from "@sdeck/schema";
|
|
7
|
+
import { clock, date } from "./clock.tsx";
|
|
8
|
+
import { column, THEME } from "./theme.ts";
|
|
9
|
+
import { stopwatch, timer, timerEnded } from "./timers.tsx";
|
|
10
|
+
|
|
11
|
+
export { analogFace, timeParts } from "./clock.tsx";
|
|
12
|
+
export { formatDuration } from "./theme.ts";
|
|
13
|
+
export { elapsed, type Run, toggled } from "./timers.tsx";
|
|
14
|
+
|
|
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
|
+
// -- Label --------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
const label = defineAction({
|
|
42
|
+
id: "label",
|
|
43
|
+
name: "Label",
|
|
44
|
+
description: "Text and an icon or picture. The text can show live values: CPU {{system.cpu}}%.",
|
|
45
|
+
category: "Display",
|
|
46
|
+
icon: "type",
|
|
47
|
+
slots: ["key", "sensor", "window"],
|
|
48
|
+
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 },
|
|
54
|
+
},
|
|
55
|
+
// Pressing a label does nothing, but it can still start a button's own steps.
|
|
56
|
+
display: true,
|
|
57
|
+
look: (s): Look => {
|
|
58
|
+
const layers: Layer[] = [];
|
|
59
|
+
if (s.image) {
|
|
60
|
+
layers.push({
|
|
61
|
+
id: "icon",
|
|
62
|
+
type: "image",
|
|
63
|
+
src: s.image,
|
|
64
|
+
size: s.text ? 50 : 68,
|
|
65
|
+
position: s.text ? "top" : "middle",
|
|
66
|
+
// Icons take the text's colour; pictures keep their own.
|
|
67
|
+
color: s.color,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
if (s.text) {
|
|
71
|
+
layers.push(
|
|
72
|
+
textLayer("label", s.text, {
|
|
73
|
+
color: s.color,
|
|
74
|
+
size: s.image ? Math.min(s.fontSize, 14) : s.fontSize,
|
|
75
|
+
position: s.image ? "bottom" : "middle",
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return { background: s.background, layers };
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
// -- Counter ------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
const counter = defineAction({
|
|
86
|
+
id: "counter",
|
|
87
|
+
name: "Counter",
|
|
88
|
+
description: "Counts presses. Hold to reset. The count is saved.",
|
|
89
|
+
category: "Display",
|
|
90
|
+
icon: "hash",
|
|
91
|
+
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 },
|
|
97
|
+
},
|
|
98
|
+
component: ({ settings, setSettings }) => {
|
|
99
|
+
const pressed = useKeyPressed();
|
|
100
|
+
useKeyPress(() => setSettings({ count: settings.count + settings.step }), {
|
|
101
|
+
onLongPress: () => setSettings({ count: 0 }),
|
|
102
|
+
});
|
|
103
|
+
const text = String(settings.count);
|
|
104
|
+
const size = text.length <= 2 ? 44 : text.length <= 4 ? 32 : 22;
|
|
105
|
+
return (
|
|
106
|
+
<div
|
|
107
|
+
style={{
|
|
108
|
+
...column,
|
|
109
|
+
background: pressed ? settings.accent : settings.background,
|
|
110
|
+
fontWeight: 700,
|
|
111
|
+
gap: 2,
|
|
112
|
+
}}
|
|
113
|
+
>
|
|
114
|
+
{settings.title ? (
|
|
115
|
+
<div
|
|
116
|
+
style={{
|
|
117
|
+
display: "flex",
|
|
118
|
+
fontSize: 12,
|
|
119
|
+
color: pressed ? "#ffffff" : THEME.muted,
|
|
120
|
+
maxWidth: 86,
|
|
121
|
+
}}
|
|
122
|
+
>
|
|
123
|
+
{settings.title}
|
|
124
|
+
</div>
|
|
125
|
+
) : null}
|
|
126
|
+
<div style={{ display: "flex", fontSize: size, color: pressed ? "#ffffff" : THEME.text }}>
|
|
127
|
+
{text}
|
|
128
|
+
</div>
|
|
129
|
+
<div
|
|
130
|
+
style={{
|
|
131
|
+
display: "flex",
|
|
132
|
+
width: 26,
|
|
133
|
+
height: 3,
|
|
134
|
+
borderRadius: 2,
|
|
135
|
+
background: pressed ? "#ffffff" : settings.accent,
|
|
136
|
+
marginTop: 2,
|
|
137
|
+
}}
|
|
138
|
+
/>
|
|
139
|
+
</div>
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// -- Navigation ---------------------------------------------------------------
|
|
145
|
+
|
|
146
|
+
const NAVIGATE_ICONS = {
|
|
147
|
+
page: "lucide/folder-open",
|
|
148
|
+
back: "lucide/arrow-left",
|
|
149
|
+
forward: "lucide/arrow-right",
|
|
150
|
+
next: "lucide/chevron-right",
|
|
151
|
+
previous: "lucide/chevron-left",
|
|
152
|
+
home: "lucide/house",
|
|
153
|
+
favorite: "lucide/star",
|
|
154
|
+
} as const;
|
|
155
|
+
|
|
156
|
+
const targets = {
|
|
157
|
+
back: "Back",
|
|
158
|
+
forward: "Forward",
|
|
159
|
+
next: "Next",
|
|
160
|
+
previous: "Prev",
|
|
161
|
+
home: "Home",
|
|
162
|
+
favorite: "Favorites",
|
|
163
|
+
} as const;
|
|
164
|
+
|
|
165
|
+
const navigate = defineAction({
|
|
166
|
+
id: "navigate",
|
|
167
|
+
name: "Navigate",
|
|
168
|
+
description: "Go to a page, back, forward, or to the next or previous page.",
|
|
169
|
+
category: "Navigation",
|
|
170
|
+
icon: "move-right",
|
|
171
|
+
slots: ["key", "sensor"],
|
|
172
|
+
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",
|
|
191
|
+
description: "After the last page comes the first.",
|
|
192
|
+
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 },
|
|
197
|
+
},
|
|
198
|
+
run: ({ settings, deck }) => {
|
|
199
|
+
if (settings.to === "page") {
|
|
200
|
+
if (!settings.page) throw new Error("no page chosen");
|
|
201
|
+
deck.navigate("page", settings.page);
|
|
202
|
+
} else deck.navigate(settings.to, undefined, { wrap: settings.wrap });
|
|
203
|
+
return { label: settings.to === "page" ? `Go to ${settings.page}` : targets[settings.to] };
|
|
204
|
+
},
|
|
205
|
+
look: (s) =>
|
|
206
|
+
iconWithCaption({
|
|
207
|
+
icon: NAVIGATE_ICONS[s.to],
|
|
208
|
+
iconColor: s.to === "favorite" ? THEME.warm : s.color,
|
|
209
|
+
// A page key names its page, so keys to different pages can be told apart.
|
|
210
|
+
caption: s.text || (s.to === "page" ? s.page : ""),
|
|
211
|
+
captionColor: THEME.text,
|
|
212
|
+
background: s.background,
|
|
213
|
+
}),
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// -- Page name ----------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
const pageName = defineAction({
|
|
219
|
+
id: "page-name",
|
|
220
|
+
name: "Page name",
|
|
221
|
+
description: "Shows which page is open.",
|
|
222
|
+
category: "Navigation",
|
|
223
|
+
icon: "file-text",
|
|
224
|
+
slots: ["key", "sensor", "window"],
|
|
225
|
+
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 },
|
|
230
|
+
},
|
|
231
|
+
display: true,
|
|
232
|
+
look: (s) => ({
|
|
233
|
+
background: s.background,
|
|
234
|
+
layers: [
|
|
235
|
+
iconLayer("icon", "lucide/layers", { color: s.accent, size: 30, row: "name" }),
|
|
236
|
+
textLayer("label", "{{page.name}}", { color: s.color, size: s.fontSize, row: "name" }),
|
|
237
|
+
],
|
|
238
|
+
}),
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// -- Brightness ---------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
/** The levels of a "10, 40, 70, 100" list, in order. */
|
|
244
|
+
export function parseLevels(list: string): number[] {
|
|
245
|
+
return list
|
|
246
|
+
.split(",")
|
|
247
|
+
.map((s) => s.trim())
|
|
248
|
+
.filter((s) => s !== "")
|
|
249
|
+
.map(Number)
|
|
250
|
+
.filter((n) => Number.isFinite(n) && n >= 0 && n <= 100);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** The level after `current` in `levels` (the first one when `current` is not in it). */
|
|
254
|
+
export function nextLevel(levels: readonly number[], current: number): number | undefined {
|
|
255
|
+
if (levels.length === 0) return undefined;
|
|
256
|
+
const i = levels.indexOf(current);
|
|
257
|
+
return levels[(i + 1) % levels.length];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const brightness = defineAction({
|
|
261
|
+
id: "brightness",
|
|
262
|
+
name: "Brightness",
|
|
263
|
+
description: "Set the deck's brightness, or cycle through levels.",
|
|
264
|
+
category: "Deck",
|
|
265
|
+
icon: "sun",
|
|
266
|
+
slots: ["key", "sensor"],
|
|
267
|
+
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 (%)",
|
|
280
|
+
default: 100,
|
|
281
|
+
min: 0,
|
|
282
|
+
max: 100,
|
|
283
|
+
visibleIf: { mode: "set" },
|
|
284
|
+
},
|
|
285
|
+
levels: {
|
|
286
|
+
type: "text",
|
|
287
|
+
label: "Levels to cycle",
|
|
288
|
+
default: "10, 40, 70, 100",
|
|
289
|
+
visibleIf: { mode: "cycle" },
|
|
290
|
+
},
|
|
291
|
+
background: { type: "color", label: "Background", default: THEME.background, appearance: true },
|
|
292
|
+
},
|
|
293
|
+
run: async ({ settings, deck }) => {
|
|
294
|
+
const before = deck.brightness();
|
|
295
|
+
const next =
|
|
296
|
+
settings.mode === "cycle" ? nextLevel(parseLevels(settings.levels), before) : settings.level;
|
|
297
|
+
if (next === undefined) throw new Error("no valid levels to cycle through");
|
|
298
|
+
await deck.setBrightness(next);
|
|
299
|
+
return {
|
|
300
|
+
result: next,
|
|
301
|
+
label: `Brightness ${next}%`,
|
|
302
|
+
undo: () => deck.setBrightness(before),
|
|
303
|
+
};
|
|
304
|
+
},
|
|
305
|
+
look: (s) =>
|
|
306
|
+
iconWithCaption({
|
|
307
|
+
icon:
|
|
308
|
+
s.mode === "cycle" ? "lucide/sun-medium" : s.level < 50 ? "lucide/sun-dim" : "lucide/sun",
|
|
309
|
+
iconColor: THEME.warm,
|
|
310
|
+
caption: s.mode === "cycle" ? "" : `${s.level}%`,
|
|
311
|
+
captionColor: THEME.text,
|
|
312
|
+
background: s.background,
|
|
313
|
+
}),
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
/** 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.1.0",
|
|
321
|
+
description: "Labels, a clock, the date, timers, a counter, page navigation and brightness.",
|
|
322
|
+
author: "sdeck",
|
|
323
|
+
actions: [label, clock, date, timer, stopwatch, counter, navigate, pageName, brightness],
|
|
324
|
+
triggers: [timerEnded],
|
|
325
|
+
});
|
package/src/theme.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/** The colours and shapes Basics draws with, so its keys look like one set. */
|
|
2
|
+
import type { CSSProperties } from "react";
|
|
3
|
+
|
|
4
|
+
export const THEME = {
|
|
5
|
+
/** Key background: near black with a hint of blue. */
|
|
6
|
+
background: "#16171d",
|
|
7
|
+
/** Main text and icons. */
|
|
8
|
+
text: "#f4f4f5",
|
|
9
|
+
/** Secondary text: dates, units, hints. */
|
|
10
|
+
muted: "#8b8d98",
|
|
11
|
+
/** Tracks, rings and dividers. */
|
|
12
|
+
faint: "#2b2d36",
|
|
13
|
+
accent: "#5b8cff",
|
|
14
|
+
warm: "#ffb547",
|
|
15
|
+
danger: "#ff5d5d",
|
|
16
|
+
success: "#3ddc84",
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
export const fill: CSSProperties = {
|
|
20
|
+
display: "flex",
|
|
21
|
+
width: "100%",
|
|
22
|
+
height: "100%",
|
|
23
|
+
alignItems: "center",
|
|
24
|
+
justifyContent: "center",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const column: CSSProperties = { ...fill, flexDirection: "column" };
|
|
28
|
+
|
|
29
|
+
/** `m:ss`, or `h:mm:ss` from an hour up. Rounds up, so a timer shows 0:00 only when done. */
|
|
30
|
+
export function formatDuration(ms: number, roundUp = true): string {
|
|
31
|
+
const total = Math.max(0, roundUp ? Math.ceil(ms / 1000) : Math.floor(ms / 1000));
|
|
32
|
+
const hours = Math.floor(total / 3600);
|
|
33
|
+
const minutes = Math.floor((total % 3600) / 60);
|
|
34
|
+
const seconds = total % 60;
|
|
35
|
+
const ss = String(seconds).padStart(2, "0");
|
|
36
|
+
return hours > 0 ? `${hours}:${String(minutes).padStart(2, "0")}:${ss}` : `${minutes}:${ss}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** An SVG as an image source. */
|
|
40
|
+
export const svg = (body: string, size = 100) =>
|
|
41
|
+
`data:image/svg+xml,${encodeURIComponent(
|
|
42
|
+
`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size} ${size}" width="${size}" height="${size}">${body}</svg>`,
|
|
43
|
+
)}`;
|
|
44
|
+
|
|
45
|
+
/** A ring that fills clockwise from the top, `part` from 0 to 1. */
|
|
46
|
+
export function ring(part: number, color: string, track: string, width = 7): string {
|
|
47
|
+
const r = 50 - width / 2 - 1;
|
|
48
|
+
const length = 2 * Math.PI * r;
|
|
49
|
+
const shown = Math.min(1, Math.max(0, part)) * length;
|
|
50
|
+
return svg(
|
|
51
|
+
`<circle cx="50" cy="50" r="${r}" fill="none" stroke="${track}" stroke-width="${width}"/>` +
|
|
52
|
+
(shown > 0
|
|
53
|
+
? `<circle cx="50" cy="50" r="${r}" fill="none" stroke="${color}" stroke-width="${width}" stroke-linecap="round" stroke-dasharray="${shown} ${length}" transform="rotate(-90 50 50)"/>`
|
|
54
|
+
: ""),
|
|
55
|
+
);
|
|
56
|
+
}
|
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