@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/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 +1559 -0
- package/dist/index.js.map +22 -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/clock.tsx
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/** The clock and the date: drawn by themselves, since they change on their own. */
|
|
2
|
+
import { defineAction, useNow } from "@sdeck/plugin";
|
|
3
|
+
import { column, fill, svg, THEME } from "./theme.ts";
|
|
4
|
+
|
|
5
|
+
/** An analog face at `date`: twelve ticks, three hands. */
|
|
6
|
+
export function analogFace(
|
|
7
|
+
date: Date,
|
|
8
|
+
colors: { face: string; hands: string; accent: string; ticks: string },
|
|
9
|
+
seconds: boolean,
|
|
10
|
+
): string {
|
|
11
|
+
const h = date.getHours() % 12;
|
|
12
|
+
const m = date.getMinutes();
|
|
13
|
+
const s = date.getSeconds();
|
|
14
|
+
const hand = (angle: number, length: number, width: number, color: string, tail = 0) =>
|
|
15
|
+
`<line x1="50" y1="${50 + tail}" x2="50" y2="${50 - length}" stroke="${color}" stroke-width="${width}" stroke-linecap="round" transform="rotate(${angle} 50 50)"/>`;
|
|
16
|
+
const ticks = Array.from({ length: 12 }, (_, i) => {
|
|
17
|
+
const major = i % 3 === 0;
|
|
18
|
+
return `<line x1="50" y1="${major ? 6 : 7}" x2="50" y2="${major ? 13 : 11}" stroke="${colors.ticks}" stroke-width="${major ? 3 : 2}" stroke-linecap="round" transform="rotate(${i * 30} 50 50)"/>`;
|
|
19
|
+
}).join("");
|
|
20
|
+
return svg(
|
|
21
|
+
`<circle cx="50" cy="50" r="47" fill="${colors.face}"/>${ticks}` +
|
|
22
|
+
hand((h + m / 60) * 30, 24, 6, colors.hands) +
|
|
23
|
+
hand((m + s / 60) * 6, 35, 4, colors.hands) +
|
|
24
|
+
(seconds ? hand(s * 6, 38, 1.8, colors.accent, 8) : "") +
|
|
25
|
+
`<circle cx="50" cy="50" r="3.5" fill="${seconds ? colors.accent : colors.hands}"/>`,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The parts of a digital time: `16:41` or `4:41` with `PM`, and the seconds. */
|
|
30
|
+
export function timeParts(date: Date, hour12: boolean) {
|
|
31
|
+
const hours = date.getHours();
|
|
32
|
+
const shown = hour12 ? hours % 12 || 12 : hours;
|
|
33
|
+
return {
|
|
34
|
+
main: `${hour12 ? shown : String(shown).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`,
|
|
35
|
+
seconds: String(date.getSeconds()).padStart(2, "0"),
|
|
36
|
+
period: hour12 ? (hours < 12 ? "AM" : "PM") : "",
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const clock = defineAction({
|
|
41
|
+
id: "clock",
|
|
42
|
+
name: "Clock",
|
|
43
|
+
description: "The time, as digits or hands, and optionally the date.",
|
|
44
|
+
category: "Time",
|
|
45
|
+
icon: "clock",
|
|
46
|
+
// The time is text or a face, which sensor strips are too small for.
|
|
47
|
+
slots: ["key", "window"],
|
|
48
|
+
settings: {
|
|
49
|
+
style: {
|
|
50
|
+
type: "select",
|
|
51
|
+
label: "Style",
|
|
52
|
+
default: "digital",
|
|
53
|
+
options: [
|
|
54
|
+
{ value: "digital", label: "Digits" },
|
|
55
|
+
{ value: "analog", label: "Hands" },
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
hour12: { type: "boolean", label: "12-hour clock", visibleIf: { style: "digital" } },
|
|
59
|
+
seconds: { type: "boolean", label: "Show seconds", default: true },
|
|
60
|
+
date: { type: "boolean", label: "Show the date" },
|
|
61
|
+
color: { type: "color", label: "Colour", default: THEME.text },
|
|
62
|
+
accent: { type: "color", label: "Accent", default: THEME.accent },
|
|
63
|
+
background: { type: "color", label: "Background", default: THEME.background },
|
|
64
|
+
},
|
|
65
|
+
component: ({ settings, slot }) => {
|
|
66
|
+
const now = useNow(settings.seconds ? 1000 : 10_000);
|
|
67
|
+
const wide = slot.kind === "window";
|
|
68
|
+
const day = now.toLocaleDateString(undefined, {
|
|
69
|
+
weekday: "short",
|
|
70
|
+
day: "numeric",
|
|
71
|
+
month: "short",
|
|
72
|
+
});
|
|
73
|
+
if (settings.style === "analog") {
|
|
74
|
+
const face = analogFace(
|
|
75
|
+
now,
|
|
76
|
+
{ face: THEME.faint, hands: settings.color, accent: settings.accent, ticks: THEME.muted },
|
|
77
|
+
settings.seconds,
|
|
78
|
+
);
|
|
79
|
+
const size = wide ? 50 : settings.date ? 66 : 82;
|
|
80
|
+
return (
|
|
81
|
+
<div
|
|
82
|
+
style={{
|
|
83
|
+
...fill,
|
|
84
|
+
flexDirection: wide ? "row" : "column",
|
|
85
|
+
gap: wide ? 12 : 4,
|
|
86
|
+
background: settings.background,
|
|
87
|
+
}}
|
|
88
|
+
>
|
|
89
|
+
<img src={face} width={size} height={size} alt="" />
|
|
90
|
+
{settings.date || wide ? (
|
|
91
|
+
<div
|
|
92
|
+
style={{
|
|
93
|
+
display: "flex",
|
|
94
|
+
color: THEME.muted,
|
|
95
|
+
fontSize: wide ? 18 : 12,
|
|
96
|
+
fontWeight: 700,
|
|
97
|
+
}}
|
|
98
|
+
>
|
|
99
|
+
{day}
|
|
100
|
+
</div>
|
|
101
|
+
) : null}
|
|
102
|
+
</div>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const { main, seconds, period } = timeParts(now, settings.hour12);
|
|
106
|
+
const small = [settings.seconds ? `:${seconds}` : "", period].filter(Boolean).join(" ");
|
|
107
|
+
const big = wide ? 34 : main.length > 4 ? 27 : 31;
|
|
108
|
+
return (
|
|
109
|
+
<div
|
|
110
|
+
style={{
|
|
111
|
+
...column,
|
|
112
|
+
background: settings.background,
|
|
113
|
+
color: settings.color,
|
|
114
|
+
fontWeight: 700,
|
|
115
|
+
gap: wide ? 0 : 2,
|
|
116
|
+
...(wide && { flexDirection: "row", gap: 14 }),
|
|
117
|
+
}}
|
|
118
|
+
>
|
|
119
|
+
<div style={{ display: "flex", alignItems: "baseline", gap: 3 }}>
|
|
120
|
+
<div style={{ display: "flex", fontSize: big, letterSpacing: -0.5 }}>{main}</div>
|
|
121
|
+
{wide && small ? (
|
|
122
|
+
<div style={{ display: "flex", fontSize: 16, color: settings.accent }}>{small}</div>
|
|
123
|
+
) : null}
|
|
124
|
+
</div>
|
|
125
|
+
{!wide && small ? (
|
|
126
|
+
<div style={{ display: "flex", fontSize: 13, color: settings.accent }}>{small}</div>
|
|
127
|
+
) : null}
|
|
128
|
+
{settings.date ? (
|
|
129
|
+
<div style={{ display: "flex", fontSize: wide ? 16 : 12, color: THEME.muted }}>{day}</div>
|
|
130
|
+
) : null}
|
|
131
|
+
</div>
|
|
132
|
+
);
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
export const date = defineAction({
|
|
137
|
+
id: "date",
|
|
138
|
+
name: "Date",
|
|
139
|
+
description: "Today's date, like a page torn off a calendar.",
|
|
140
|
+
category: "Time",
|
|
141
|
+
icon: "calendar",
|
|
142
|
+
slots: ["key", "window"],
|
|
143
|
+
settings: {
|
|
144
|
+
accent: { type: "color", label: "Accent", default: THEME.danger },
|
|
145
|
+
background: { type: "color", label: "Background", default: THEME.background },
|
|
146
|
+
},
|
|
147
|
+
component: ({ settings, slot }) => {
|
|
148
|
+
const now = useNow(60_000);
|
|
149
|
+
const weekday = now.toLocaleDateString(undefined, { weekday: "short" });
|
|
150
|
+
const month = now.toLocaleDateString(undefined, { month: "short" });
|
|
151
|
+
if (slot.kind === "window") {
|
|
152
|
+
return (
|
|
153
|
+
<div style={{ ...fill, gap: 14, background: settings.background, fontWeight: 700 }}>
|
|
154
|
+
<div style={{ display: "flex", fontSize: 36, color: THEME.text }}>{now.getDate()}</div>
|
|
155
|
+
<div style={{ display: "flex", flexDirection: "column" }}>
|
|
156
|
+
<div style={{ display: "flex", fontSize: 15, color: settings.accent }}>
|
|
157
|
+
{now.toLocaleDateString(undefined, { weekday: "long" })}
|
|
158
|
+
</div>
|
|
159
|
+
<div style={{ display: "flex", fontSize: 14, color: THEME.muted }}>
|
|
160
|
+
{now.toLocaleDateString(undefined, { month: "long", year: "numeric" })}
|
|
161
|
+
</div>
|
|
162
|
+
</div>
|
|
163
|
+
</div>
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
return (
|
|
167
|
+
<div
|
|
168
|
+
style={{
|
|
169
|
+
...column,
|
|
170
|
+
justifyContent: "flex-start",
|
|
171
|
+
background: settings.background,
|
|
172
|
+
fontWeight: 700,
|
|
173
|
+
}}
|
|
174
|
+
>
|
|
175
|
+
<div
|
|
176
|
+
style={{
|
|
177
|
+
display: "flex",
|
|
178
|
+
width: "100%",
|
|
179
|
+
height: 26,
|
|
180
|
+
alignItems: "center",
|
|
181
|
+
justifyContent: "center",
|
|
182
|
+
background: settings.accent,
|
|
183
|
+
color: "#ffffff",
|
|
184
|
+
fontSize: 13,
|
|
185
|
+
letterSpacing: 1,
|
|
186
|
+
}}
|
|
187
|
+
>
|
|
188
|
+
{weekday.toUpperCase()}
|
|
189
|
+
</div>
|
|
190
|
+
<div style={{ display: "flex", fontSize: 40, color: THEME.text, marginTop: 4 }}>
|
|
191
|
+
{now.getDate()}
|
|
192
|
+
</div>
|
|
193
|
+
<div style={{ display: "flex", fontSize: 12, color: THEME.muted }}>{month}</div>
|
|
194
|
+
</div>
|
|
195
|
+
);
|
|
196
|
+
},
|
|
197
|
+
});
|
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
|
+
run: () => undefined,
|
|
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" },
|
|
195
|
+
background: { type: "color", label: "Background", default: THEME.background },
|
|
196
|
+
color: { type: "color", label: "Icon colour", default: THEME.accent },
|
|
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
|
+
run: () => undefined,
|
|
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 },
|
|
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.0.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
|
+
}
|