@sdeck/plugin-basics 1.3.1 → 1.4.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/clock.tsx CHANGED
@@ -1,18 +1,37 @@
1
- /** The clock and the date: drawn by themselves, since they change on their own. */
2
- import { setting, useNow } from "@sdeck/plugin";
3
- import { surfaceStyle } from "@sdeck/schema";
1
+ /**
2
+ * The clock and the date. The look draws them from the values given here, so every part can be moved,
3
+ * restyled or left out in the composer.
4
+ */
5
+ import {
6
+ type ActionValue,
7
+ type DateParts,
8
+ dateParts,
9
+ formatDate,
10
+ isTimeZone,
11
+ setting,
12
+ useNow,
13
+ } from "@sdeck/plugin";
14
+ import {
15
+ iconLayer,
16
+ type Layer,
17
+ type Look,
18
+ readable,
19
+ rectLayer,
20
+ surface,
21
+ textLayer,
22
+ } from "@sdeck/schema";
4
23
  import { plugin } from "./plugin.ts";
5
- import { column, fill, svg, THEME } from "./theme.ts";
24
+ import { svg, THEME } from "./theme.ts";
6
25
 
7
- /** An analog face at `date`: twelve ticks, three hands. */
26
+ /** An analog face at `at`: twelve ticks, three hands. */
8
27
  export function analogFace(
9
- date: Date,
28
+ at: Pick<DateParts, "hour" | "minute" | "second">,
10
29
  colors: { face: string; hands: string; accent: string; ticks: string },
11
30
  seconds: boolean,
12
31
  ): string {
13
- const h = date.getHours() % 12;
14
- const m = date.getMinutes();
15
- const s = date.getSeconds();
32
+ const h = at.hour % 12;
33
+ const m = at.minute;
34
+ const s = at.second;
16
35
  const hand = (angle: number, length: number, width: number, color: string, tail = 0) =>
17
36
  `<line x1="50" y1="${50 + tail}" x2="50" y2="${50 - length}" stroke="${color}" stroke-width="${width}" stroke-linecap="round" transform="rotate(${angle} 50 50)"/>`;
18
37
  const ticks = Array.from({ length: 12 }, (_, i) => {
@@ -28,17 +47,90 @@ export function analogFace(
28
47
  );
29
48
  }
30
49
 
31
- /** The parts of a digital time: `16:41` or `4:41` with `PM`, and the seconds. */
32
- export function timeParts(date: Date, hour12: boolean) {
33
- const hours = date.getHours();
34
- const shown = hour12 ? hours % 12 || 12 : hours;
50
+ /** Offered as "Time zone". Nothing chosen is the computer's own. */
51
+ export const zones = plugin.source("zones", () =>
52
+ Intl.supportedValuesOf("timeZone").map((value) => {
53
+ const [area = "", ...city] = value.split("/");
54
+ return {
55
+ value,
56
+ label: city.length > 0 ? `${city.join(", ")} (${area})`.replaceAll("_", " ") : value,
57
+ };
58
+ }),
59
+ );
60
+
61
+ const zoneSetting = setting.pick("Time zone", zones, { first: { "": "This computer's" } });
62
+
63
+ const text = (name: string, description?: string): ActionValue => ({
64
+ name,
65
+ type: "string",
66
+ ...(description && { description }),
67
+ });
68
+ const number = (name: string, description?: string): ActionValue => ({
69
+ ...text(name, description),
70
+ type: "number",
71
+ });
72
+
73
+ const DATE_VALUES = {
74
+ weekday: text("Weekday", "Thursday"),
75
+ weekdayShort: text("Weekday, short", "Thu"),
76
+ day: number("Day of the month"),
77
+ month: text("Month", "December"),
78
+ monthShort: text("Month, short", "Dec"),
79
+ monthNumber: number("Month, as a number"),
80
+ year: number("Year"),
81
+ now: number("Moment shown", 'For formats of your own: format(self.now, "D/M")'),
82
+ zone: text("Time zone", 'Empty for the computer\'s own: date("D MMM", self.now, self.zone)'),
83
+ };
84
+
85
+ /** The day at `now` in `zone`, in parts. */
86
+ export function dateValues(now: Date, zone: string) {
87
+ const at = dateParts(now, zone);
88
+ const part = (pattern: string) => formatDate(now, pattern, { zone });
89
+ return {
90
+ weekday: part("dddd"),
91
+ weekdayShort: part("ddd"),
92
+ day: at?.day,
93
+ month: part("MMMM"),
94
+ monthShort: part("MMM"),
95
+ monthNumber: at?.month,
96
+ year: at?.year,
97
+ now: now.getTime(),
98
+ zone,
99
+ };
100
+ }
101
+
102
+ /** The time at `now` in `zone`, in parts. `second` is empty while seconds are off. */
103
+ export function timeValues(
104
+ now: Date,
105
+ options: { zone: string; hour12: boolean; seconds: boolean },
106
+ ) {
107
+ const { zone, hour12, seconds } = options;
108
+ const part = (pattern: string) => formatDate(now, pattern, { zone });
109
+ const hour = part(hour12 ? "h" : "HH");
110
+ const minute = part("mm");
111
+ const second = seconds ? part("ss") : "";
112
+ const period = hour12 ? part("A") : "";
35
113
  return {
36
- main: `${hour12 ? shown : String(shown).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}`,
37
- seconds: String(date.getSeconds()).padStart(2, "0"),
38
- period: hour12 ? (hours < 12 ? "AM" : "PM") : "",
114
+ hour,
115
+ minute,
116
+ second,
117
+ period,
118
+ hour24: dateParts(now, zone)?.hour,
119
+ time: `${hour}:${minute}${second && `:${second}`}${period && ` ${period}`}`,
39
120
  };
40
121
  }
41
122
 
123
+ /** The date as this computer writes it: `Thu, Dec 3`. */
124
+ const usualDate = (now: Date, zone: string) =>
125
+ now.toLocaleDateString(undefined, {
126
+ weekday: "short",
127
+ day: "numeric",
128
+ month: "short",
129
+ ...(zone && { timeZone: zone }),
130
+ });
131
+
132
+ const known = (zone: string) => (isTimeZone(zone) ? zone : "");
133
+
42
134
  export const clock = plugin.action({
43
135
  id: "clock",
44
136
  name: "Clock",
@@ -52,78 +144,74 @@ export const clock = plugin.action({
52
144
  hour12: setting.boolean("12-hour clock", { visibleIf: { style: "digital" } }),
53
145
  seconds: setting.boolean("Show seconds", { default: true }),
54
146
  date: setting.boolean("Show the date"),
147
+ dateFormat: setting.text("Date format", {
148
+ placeholder: "ddd D MMM",
149
+ description:
150
+ "YYYY year, MMMM or MMM month, M its number, D day, Do 3rd, dddd or ddd weekday. Empty: as your computer writes it.",
151
+ visibleIf: { date: true },
152
+ }),
153
+ zone: zoneSetting,
55
154
  color: setting.color("Colour", { default: THEME.text }),
56
155
  accent: setting.color("Accent", { default: THEME.accent }),
57
156
  background: setting.color("Background", { default: THEME.background }),
58
157
  },
59
- component: ({ settings, slot }) => {
158
+ display: true,
159
+ values: {
160
+ hour: text("Hour", "As the settings say: 16, or 4"),
161
+ minute: text("Minute", "05"),
162
+ second: text("Second", "Empty while seconds are off"),
163
+ period: text("AM or PM", "Empty on a 24-hour clock"),
164
+ hour24: number("Hour, 0 to 23"),
165
+ time: text("Time", "All of it: 4:41:07 PM"),
166
+ date: text("Date", "In the date format"),
167
+ ...DATE_VALUES,
168
+ face: text("Clock face", "A picture, for an image layer"),
169
+ },
170
+ useValues: ({ settings }) => {
60
171
  const now = useNow(settings.seconds ? 1000 : 10_000);
61
- const wide = slot.kind === "window";
62
- const day = now.toLocaleDateString(undefined, {
63
- weekday: "short",
64
- day: "numeric",
65
- month: "short",
66
- });
67
- if (settings.style === "analog") {
68
- const face = analogFace(
69
- now,
172
+ const zone = known(settings.zone);
173
+ return {
174
+ ...dateValues(now, zone),
175
+ ...timeValues(now, { zone, hour12: settings.hour12, seconds: settings.seconds }),
176
+ date: settings.dateFormat
177
+ ? formatDate(now, settings.dateFormat, { zone })
178
+ : usualDate(now, zone),
179
+ face: analogFace(
180
+ dateParts(now, zone) ?? { hour: 0, minute: 0, second: 0 },
70
181
  { face: THEME.faint, hands: settings.color, accent: settings.accent, ticks: THEME.muted },
71
182
  settings.seconds,
183
+ ),
184
+ };
185
+ },
186
+ look: (s, slot): Look => {
187
+ const wide = slot === "window";
188
+ const layers: Layer[] = [];
189
+ const row = wide ? "time" : "small";
190
+ if (s.style === "analog") {
191
+ const size = wide ? 86 : s.date ? 69 : 85;
192
+ layers.push(iconLayer("face", "{{self.face}}", { size, ...(wide && { row }) }));
193
+ } else {
194
+ layers.push(
195
+ textLayer("time", "{{self.hour}}:{{self.minute}}", {
196
+ color: s.color,
197
+ size: wide ? 34 : 28,
198
+ ...(wide && { row }),
199
+ }),
72
200
  );
73
- const size = wide ? 50 : settings.date ? 66 : 82;
74
- return (
75
- <div
76
- style={{
77
- ...fill,
78
- flexDirection: wide ? "row" : "column",
79
- gap: wide ? 12 : 4,
80
- ...surfaceStyle(settings.background),
81
- }}
82
- >
83
- <img src={face} width={size} height={size} alt="" />
84
- {settings.date || wide ? (
85
- <div
86
- style={{
87
- display: "flex",
88
- color: THEME.muted,
89
- fontSize: wide ? 18 : 12,
90
- fontWeight: 700,
91
- }}
92
- >
93
- {day}
94
- </div>
95
- ) : null}
96
- </div>
201
+ const small = { color: s.accent, size: wide ? 16 : 13, row };
202
+ if (s.seconds) layers.push(textLayer("seconds", ":{{self.second}}", small));
203
+ if (s.hour12) layers.push(textLayer("period", "{{self.period}}", small));
204
+ }
205
+ if (s.date || (wide && s.style === "analog")) {
206
+ layers.push(
207
+ textLayer("date", "{{self.date}}", {
208
+ color: THEME.muted,
209
+ size: wide ? 16 : 12,
210
+ ...(wide && { row }),
211
+ }),
97
212
  );
98
213
  }
99
- const { main, seconds, period } = timeParts(now, settings.hour12);
100
- const small = [settings.seconds ? `:${seconds}` : "", period].filter(Boolean).join(" ");
101
- const big = wide ? 34 : main.length > 4 ? 27 : 31;
102
- return (
103
- <div
104
- style={{
105
- ...column,
106
- ...surfaceStyle(settings.background),
107
- color: settings.color,
108
- fontWeight: 700,
109
- gap: wide ? 0 : 2,
110
- ...(wide && { flexDirection: "row", gap: 14 }),
111
- }}
112
- >
113
- <div style={{ display: "flex", alignItems: "baseline", gap: 3 }}>
114
- <div style={{ display: "flex", fontSize: big, letterSpacing: -0.5 }}>{main}</div>
115
- {wide && small ? (
116
- <div style={{ display: "flex", fontSize: 16, color: settings.accent }}>{small}</div>
117
- ) : null}
118
- </div>
119
- {!wide && small ? (
120
- <div style={{ display: "flex", fontSize: 13, color: settings.accent }}>{small}</div>
121
- ) : null}
122
- {settings.date ? (
123
- <div style={{ display: "flex", fontSize: wide ? 16 : 12, color: THEME.muted }}>{day}</div>
124
- ) : null}
125
- </div>
126
- );
214
+ return { background: surface(s.background), layers };
127
215
  },
128
216
  });
129
217
 
@@ -135,57 +223,60 @@ export const date = plugin.action({
135
223
  icon: "calendar",
136
224
  slots: ["key", "window"],
137
225
  settings: {
226
+ zone: zoneSetting,
138
227
  accent: setting.color("Accent", { default: THEME.danger }),
139
228
  background: setting.color("Background", { default: THEME.background }),
140
229
  },
141
- component: ({ settings, slot }) => {
142
- const now = useNow(60_000);
143
- const weekday = now.toLocaleDateString(undefined, { weekday: "short" });
144
- const month = now.toLocaleDateString(undefined, { month: "short" });
145
- if (slot.kind === "window") {
146
- return (
147
- <div style={{ ...fill, gap: 14, ...surfaceStyle(settings.background), fontWeight: 700 }}>
148
- <div style={{ display: "flex", fontSize: 36, color: THEME.text }}>{now.getDate()}</div>
149
- <div style={{ display: "flex", flexDirection: "column" }}>
150
- <div style={{ display: "flex", fontSize: 15, color: settings.accent }}>
151
- {now.toLocaleDateString(undefined, { weekday: "long" })}
152
- </div>
153
- <div style={{ display: "flex", fontSize: 14, color: THEME.muted }}>
154
- {now.toLocaleDateString(undefined, { month: "long", year: "numeric" })}
155
- </div>
156
- </div>
157
- </div>
158
- );
230
+ display: true,
231
+ values: DATE_VALUES,
232
+ useValues: ({ settings }) => dateValues(useNow(60_000), known(settings.zone)),
233
+ look: (s, slot): Look => {
234
+ if (slot === "window") {
235
+ return {
236
+ background: surface(s.background),
237
+ layers: [
238
+ textLayer("day", "{{self.day}}", {
239
+ size: 36,
240
+ color: THEME.text,
241
+ align: "right",
242
+ frame: { x: 4, y: 0, width: 32, height: 100 },
243
+ }),
244
+ textLayer("weekday", "{{self.weekday}}", {
245
+ size: 15,
246
+ color: s.accent,
247
+ align: "left",
248
+ frame: { x: 41, y: 14, width: 57, height: 36 },
249
+ }),
250
+ textLayer("month", "{{self.month}} {{self.year}}", {
251
+ size: 14,
252
+ color: THEME.muted,
253
+ align: "left",
254
+ frame: { x: 41, y: 50, width: 57, height: 36 },
255
+ }),
256
+ ],
257
+ };
159
258
  }
160
- return (
161
- <div
162
- style={{
163
- ...column,
164
- justifyContent: "flex-start",
165
- ...surfaceStyle(settings.background),
166
- fontWeight: 700,
167
- }}
168
- >
169
- <div
170
- style={{
171
- display: "flex",
172
- width: "100%",
173
- height: 26,
174
- alignItems: "center",
175
- justifyContent: "center",
176
- background: settings.accent,
177
- color: "#ffffff",
178
- fontSize: 13,
179
- letterSpacing: 1,
180
- }}
181
- >
182
- {weekday.toUpperCase()}
183
- </div>
184
- <div style={{ display: "flex", fontSize: 40, color: THEME.text, marginTop: 4 }}>
185
- {now.getDate()}
186
- </div>
187
- <div style={{ display: "flex", fontSize: 12, color: THEME.muted }}>{month}</div>
188
- </div>
189
- );
259
+ const banner = { x: 0, y: 0, width: 100, height: 27 };
260
+ return {
261
+ background: surface(s.background),
262
+ layers: [
263
+ rectLayer("banner", { fill: s.accent, frame: banner }),
264
+ textLayer("weekday", "{{upper(self.weekdayShort)}}", {
265
+ size: 13,
266
+ color: readable("#ffffff", s.accent),
267
+ frame: banner,
268
+ }),
269
+ textLayer("day", "{{self.day}}", {
270
+ size: 40,
271
+ color: THEME.text,
272
+ frame: { x: 0, y: 29, width: 100, height: 46 },
273
+ }),
274
+ textLayer("month", "{{self.monthShort}}", {
275
+ size: 12,
276
+ color: THEME.muted,
277
+ frame: { x: 0, y: 75, width: 100, height: 18 },
278
+ }),
279
+ ],
280
+ };
190
281
  },
191
282
  });
package/src/index.tsx CHANGED
@@ -2,24 +2,25 @@
2
2
  * Basics: what most decks want. Labels, time (clock, date, timer, stopwatch), a counter, page
3
3
  * navigation and the deck's brightness.
4
4
  */
5
- import { setting, useKeyPress, useKeyPressed } from "@sdeck/plugin";
5
+ import { setting, useKeyPress } from "@sdeck/plugin";
6
6
  import {
7
7
  iconLayer,
8
8
  iconLook,
9
9
  type Layer,
10
10
  type Look,
11
+ rectLayer,
11
12
  surface,
12
- surfaceStyle,
13
13
  textLayer,
14
14
  } from "@sdeck/schema";
15
- import { clock, date } from "./clock.tsx";
15
+ import { clock, date, zones } from "./clock.tsx";
16
16
  import { plugin } from "./plugin.ts";
17
- import { column, THEME } from "./theme.ts";
17
+ import { followButtons, stopwatches, timers } from "./runs.ts";
18
+ import { THEME } from "./theme.ts";
18
19
  import { stopwatch, timer, timerEnded } from "./timers.tsx";
19
20
 
20
- export { analogFace, timeParts } from "./clock.tsx";
21
+ export { analogFace, dateValues, timeValues } from "./clock.tsx";
22
+ export { elapsed, namedButtons, type Run, timerValues, toggled, variableName } from "./runs.ts";
21
23
  export { formatDuration } from "./theme.ts";
22
- export { elapsed, type Run, toggled } from "./timers.tsx";
23
24
 
24
25
  // -- Label --------------------------------------------------------------------
25
26
 
@@ -79,49 +80,43 @@ const counter = plugin.action({
79
80
  accent: setting.color("Colour", { default: THEME.accent }),
80
81
  background: setting.color("Background", { default: THEME.background }),
81
82
  },
82
- component: ({ settings, setSettings }) => {
83
- const pressed = useKeyPressed();
83
+ values: {
84
+ count: { name: "Count", type: "number" },
85
+ title: { name: "Title", type: "string" },
86
+ },
87
+ useValues: ({ settings, setSettings }) => {
84
88
  useKeyPress(() => setSettings({ count: settings.count + settings.step }), {
85
89
  onLongPress: () => setSettings({ count: 0 }),
86
90
  });
87
- const text = String(settings.count);
88
- const size = text.length <= 2 ? 44 : text.length <= 4 ? 32 : 22;
89
- return (
90
- <div
91
- style={{
92
- ...column,
93
- ...surfaceStyle(pressed ? settings.accent : settings.background),
94
- fontWeight: 700,
95
- gap: 2,
96
- }}
97
- >
98
- {settings.title ? (
99
- <div
100
- style={{
101
- display: "flex",
102
- fontSize: 12,
103
- color: pressed ? "#ffffff" : THEME.muted,
104
- maxWidth: 86,
105
- }}
106
- >
107
- {settings.title}
108
- </div>
109
- ) : null}
110
- <div style={{ display: "flex", fontSize: size, color: pressed ? "#ffffff" : THEME.text }}>
111
- {text}
112
- </div>
113
- <div
114
- style={{
115
- display: "flex",
116
- width: 26,
117
- height: 3,
118
- borderRadius: 2,
119
- background: pressed ? "#ffffff" : settings.accent,
120
- marginTop: 2,
121
- }}
122
- />
123
- </div>
124
- );
91
+ return { count: settings.count, title: settings.title };
92
+ },
93
+ look: (s): Look => {
94
+ const { length } = String(s.count);
95
+ return {
96
+ background: surface(s.background),
97
+ layers: [
98
+ rectLayer("pressed", { fill: surface(s.accent), visible: "self.held" }),
99
+ ...(s.title
100
+ ? [
101
+ textLayer("title", "{{self.title}}", {
102
+ size: 12,
103
+ color: "#b6b8c4",
104
+ position: "top",
105
+ }),
106
+ ]
107
+ : []),
108
+ textLayer("count", "{{self.count}}", {
109
+ size: length <= 2 ? 44 : length <= 4 ? 32 : 22,
110
+ color: THEME.text,
111
+ }),
112
+ rectLayer("line", {
113
+ fill: s.accent,
114
+ frame: { x: 36.5, y: 82, width: 27, height: 3 },
115
+ radius: 2,
116
+ visible: "!self.held",
117
+ }),
118
+ ],
119
+ };
125
120
  },
126
121
  });
127
122
 
@@ -285,4 +280,7 @@ const brightness = plugin.action({
285
280
  export default plugin.define({
286
281
  actions: [label, clock, date, timer, stopwatch, counter, navigate, pageName, brightness],
287
282
  triggers: [timerEnded],
283
+ sources: [zones],
284
+ state: [timers, stopwatches],
285
+ activate: followButtons,
288
286
  });
package/src/plugin.ts CHANGED
@@ -3,7 +3,7 @@ import { createPlugin } from "@sdeck/plugin";
3
3
  export const plugin = createPlugin({
4
4
  id: "@sdeck/plugin-basics",
5
5
  name: "Basics",
6
- version: "1.3.1",
6
+ version: "1.4.0",
7
7
  description: "Labels, a clock, the date, timers, a counter, page navigation and brightness.",
8
8
  author: "sdeck",
9
9
  });