@kiosko/sdk 0.0.0-stage → 0.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/context.d.ts +19 -0
- package/dist/context.js +23 -0
- package/dist/filter.d.ts +26 -0
- package/dist/filter.js +53 -0
- package/dist/format.d.ts +59 -0
- package/dist/format.js +87 -0
- package/dist/i18n.d.ts +9 -0
- package/dist/i18n.js +8 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +10 -0
- package/dist/list.d.ts +133 -0
- package/dist/list.js +126 -0
- package/dist/location.d.ts +26 -0
- package/dist/location.js +33 -0
- package/dist/plugin.d.ts +314 -0
- package/dist/plugin.js +24 -0
- package/dist/schema.d.ts +155 -0
- package/dist/schema.js +169 -0
- package/dist/theme.d.ts +47 -0
- package/dist/theme.js +49 -0
- package/dist/workday.d.ts +48 -0
- package/dist/workday.js +40 -0
- package/package.json +47 -4
- package/src/context.ts +25 -0
- package/src/filter.ts +56 -0
- package/src/format.ts +123 -0
- package/src/i18n.ts +13 -0
- package/src/index.ts +10 -0
- package/src/list.ts +217 -0
- package/src/location.ts +41 -0
- package/src/plugin.ts +329 -0
- package/src/schema.ts +264 -0
- package/src/theme.ts +98 -0
- package/src/workday.ts +79 -0
- package/README.md +0 -3
package/src/list.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lists, the same in every widget: two shared options (`rows`, `layout`),
|
|
3
|
+
* one layout helper that decides how many rows fit and how tall they are,
|
|
4
|
+
* and one "+N more" line.
|
|
5
|
+
*
|
|
6
|
+
* options: t.object({
|
|
7
|
+
* ...listOptions({ layouts: ['one-line', 'detailed'] }), // rows, layout
|
|
8
|
+
* projects: t.filter({ label: 'Projects' }),
|
|
9
|
+
* }),
|
|
10
|
+
* render(g, ctx) {
|
|
11
|
+
* const items = …;
|
|
12
|
+
* if (!items.length) return drawEmpty(g, ctx, ctx.t({ en: 'Nothing to review', es: 'Nada que revisar' }));
|
|
13
|
+
* const plan = listLayout(ctx.box, items.length, ctx.options, {
|
|
14
|
+
* sizes: { 'one-line': { min: 22, max: 46 }, detailed: { min: 50, max: 80 } },
|
|
15
|
+
* auto: (box, n) => (box.h >= box.w * 0.7 && box.h / n >= 50 ? 'detailed' : 'one-line'),
|
|
16
|
+
* });
|
|
17
|
+
* plan.rows.forEach((row, i) => drawItem(items[i], row, plan.layout, plan.textSize));
|
|
18
|
+
* drawMore(g, ctx, plan);
|
|
19
|
+
* },
|
|
20
|
+
*
|
|
21
|
+
* `rows` = 0 fits as many rows as the box holds, each between the layout's
|
|
22
|
+
* `min` and `max` px. `rows` = N shows N items, in rows of the box's height
|
|
23
|
+
* divided by N but never taller than `max` (the rest of the box stays empty,
|
|
24
|
+
* below them) nor shorter than `min` (then fewer rows). Items left out are
|
|
25
|
+
* counted in "+N more", a compact line under the last row.
|
|
26
|
+
*/
|
|
27
|
+
import type { Localized } from './i18n.ts';
|
|
28
|
+
import type { Box, Graphics, RenderContext } from './plugin.ts';
|
|
29
|
+
import { t, type EnumField, type NumberField } from './schema.ts';
|
|
30
|
+
|
|
31
|
+
/** How a list row is drawn: one line per item, or a title with details under it. */
|
|
32
|
+
export type ListLayout = 'one-line' | 'detailed';
|
|
33
|
+
/** The `layout` option: a fixed layout, or `auto` (the widget's own rule). */
|
|
34
|
+
export type ListLayoutChoice = 'auto' | ListLayout;
|
|
35
|
+
|
|
36
|
+
const LAYOUT_LABELS: Record<ListLayoutChoice, Localized> = {
|
|
37
|
+
auto: { en: 'Automatic', es: 'Automático' },
|
|
38
|
+
'one-line': { en: 'One line', es: 'Una línea' },
|
|
39
|
+
detailed: { en: 'Detailed', es: 'Detallado' },
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const rowsField = (def: number): NumberField =>
|
|
43
|
+
t.integer({
|
|
44
|
+
default: def,
|
|
45
|
+
min: 0,
|
|
46
|
+
max: 30,
|
|
47
|
+
label: { en: 'Rows', es: 'Filas' },
|
|
48
|
+
help: {
|
|
49
|
+
en: '0 = as many as fit. A number shows that many, in rows of a readable size (fewer when the tile is too small for them); the rest are counted in "+N more".',
|
|
50
|
+
es: '0 = las que quepan. Con un número se ven esas, en filas de un tamaño legible (menos si la tesela no da para ellas); el resto se cuenta en «+N más».',
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The shared list options, to spread into the widget's `t.object({...})`:
|
|
56
|
+
* `rows` (0 = auto, up to 30) and, when the widget draws two layouts,
|
|
57
|
+
* `layout` (`auto` plus those). `rows` is the default of `rows`: 0 unless
|
|
58
|
+
* the widget had a fixed count before.
|
|
59
|
+
*/
|
|
60
|
+
export function listOptions(o?: { rows?: number }): { rows: NumberField };
|
|
61
|
+
export function listOptions<L extends ListLayout>(o: { rows?: number; layouts: readonly [L, L, ...L[]]; layout?: 'auto' | L }): { rows: NumberField; layout: EnumField<'auto' | L> };
|
|
62
|
+
export function listOptions(o: { rows?: number; layouts?: readonly ListLayout[]; layout?: ListLayoutChoice } = {}): { rows: NumberField; layout?: EnumField<ListLayoutChoice> } {
|
|
63
|
+
const rows = rowsField(o.rows ?? 0);
|
|
64
|
+
if (!o.layouts || o.layouts.length < 2) return { rows };
|
|
65
|
+
const values = ['auto', ...o.layouts] as ListLayoutChoice[];
|
|
66
|
+
return {
|
|
67
|
+
rows,
|
|
68
|
+
layout: t.enum(values, {
|
|
69
|
+
default: o.layout ?? 'auto',
|
|
70
|
+
label: { en: 'Row layout', es: 'Formato de fila' },
|
|
71
|
+
labels: Object.fromEntries(values.map((v) => [v, LAYOUT_LABELS[v]])),
|
|
72
|
+
help: { en: 'Automatic: the widget picks by the tile\'s shape.', es: 'Automático: el widget elige según la forma de la tesela.' },
|
|
73
|
+
}),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Row heights of one layout, in px of the widget's box. */
|
|
78
|
+
export interface RowSize {
|
|
79
|
+
/** Smallest row that can still be read. */
|
|
80
|
+
min: number;
|
|
81
|
+
/** Tallest row: rows never grow past it, however few they are (the rest of the box stays empty). */
|
|
82
|
+
max: number;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export interface ListSpec<L extends ListLayout = ListLayout> {
|
|
86
|
+
/** One entry per layout the widget draws: its row heights. */
|
|
87
|
+
sizes: { [K in L]: RowSize };
|
|
88
|
+
/**
|
|
89
|
+
* The layout `auto` picks: the widget's own rule (e.g. two lines on tall
|
|
90
|
+
* tiles). `count` is how many rows the box is sized for: `rows` when the
|
|
91
|
+
* user set it (and there are that many items), otherwise every item.
|
|
92
|
+
* Default: the first layout in `sizes`.
|
|
93
|
+
*/
|
|
94
|
+
auto?: (box: Box, count: number) => L;
|
|
95
|
+
/** In auto mode, size rows as if there were at least this many items, so one item doesn't get a giant row. Default 1. */
|
|
96
|
+
minRows?: number;
|
|
97
|
+
/** Height of the "+N more" line, in px. Default: `moreHeight(rowH)`. Never more than a row. */
|
|
98
|
+
moreH?: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface ListPlan<L extends ListLayout = ListLayout> {
|
|
102
|
+
/** The layout to draw (`auto` already resolved). */
|
|
103
|
+
layout: L;
|
|
104
|
+
/** Height of every row, in px: between the layout's `min` and `max` (less only in a box shorter than one row). */
|
|
105
|
+
rowH: number;
|
|
106
|
+
/** One box per row to draw, top to bottom from the top of the box: item i goes in `rows[i]`. */
|
|
107
|
+
rows: Box[];
|
|
108
|
+
/** Items drawn (`rows.length`). */
|
|
109
|
+
shown: number;
|
|
110
|
+
/** Items left out, counted in "+N more". */
|
|
111
|
+
hidden: number;
|
|
112
|
+
/** Where "+N more" goes, right under the last row; null when everything fits (or a tiny box leaves no room for it). */
|
|
113
|
+
more: Box | null;
|
|
114
|
+
/** Suggested size of a row's main text, px: 60 % of a one-line row, 42 % of a detailed one (its first line). */
|
|
115
|
+
textSize: number;
|
|
116
|
+
/** Suggested size of the secondary text: a detailed row's second line (30 % of the row), or the smaller bits of a one-line row (80 % of `textSize`). */
|
|
117
|
+
detailSize: number;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Rows may overflow the box by this fraction of a row (the descenders of the last one), as widgets always did. */
|
|
121
|
+
const TOLERANCE = 0.1;
|
|
122
|
+
/** A "+N more" line shorter than this is left out (its text would be under 8 px). */
|
|
123
|
+
const MIN_MORE_H = 12;
|
|
124
|
+
const clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, v));
|
|
125
|
+
|
|
126
|
+
/** Height of the "+N more" line under rows of `rowH` px: 60 % of a row, between 18 and 28 px, never more than a row. */
|
|
127
|
+
export function moreHeight(rowH: number): number {
|
|
128
|
+
return Math.min(clamp(Math.round(rowH * 0.6), 18, 28), rowH);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Plans a list of `count` items in `box` from the shared options (`rows`,
|
|
133
|
+
* `layout`) and the widget's row heights. Pure: no drawing, no canvas.
|
|
134
|
+
*
|
|
135
|
+
* - `rows` 0: rows of `box.h / count` (at least `minRows`), clamped to the
|
|
136
|
+
* layout's `min`–`max`; as many as fit.
|
|
137
|
+
* - `rows` N: N items in rows of `box.h / N` clamped the same way, so they
|
|
138
|
+
* never balloon; fewer when N rows don't fit at `min`.
|
|
139
|
+
* - Items left out: a compact "+N more" line (`more`) right under the last
|
|
140
|
+
* row, reserved only then. A box with room for one row but not for the row
|
|
141
|
+
* and that line still gets the row, with the line squeezed or left out.
|
|
142
|
+
*/
|
|
143
|
+
export function listLayout<L extends ListLayout>(box: Box, count: number, options: { rows?: number; layout?: string }, spec: ListSpec<L>): ListPlan<L> {
|
|
144
|
+
const declared = Object.keys(spec.sizes) as L[];
|
|
145
|
+
const n = Math.max(0, Math.floor(count));
|
|
146
|
+
const fixed = Math.max(0, Math.round(options.rows ?? 0));
|
|
147
|
+
const want = options.layout as L | 'auto' | undefined;
|
|
148
|
+
const layout: L = want && want !== 'auto' && declared.includes(want) ? want : (spec.auto?.(box, fixed > 0 ? Math.min(fixed, n) : n) ?? declared[0]!);
|
|
149
|
+
const size = spec.sizes[layout] ?? spec.sizes[declared[0]!];
|
|
150
|
+
const min = Math.max(1, size.min);
|
|
151
|
+
const max = Math.max(min, size.max);
|
|
152
|
+
const h = Math.max(0, box.h);
|
|
153
|
+
const text = (rowH: number) => {
|
|
154
|
+
const textSize = Math.round(rowH * (layout === 'detailed' ? 0.42 : 0.6));
|
|
155
|
+
return { textSize, detailSize: Math.round(layout === 'detailed' ? rowH * 0.3 : textSize * 0.8) };
|
|
156
|
+
};
|
|
157
|
+
if (h <= 0) return { layout, rowH: 0, rows: [], shown: 0, hidden: n, more: null, ...text(0) };
|
|
158
|
+
|
|
159
|
+
// Items to show and the rows the height is divided by.
|
|
160
|
+
const target = fixed > 0 ? Math.min(fixed, n) : n;
|
|
161
|
+
const divisor = fixed > 0 ? fixed : Math.max(n, spec.minRows ?? 1, 1);
|
|
162
|
+
const fits = (space: number, rowH: number) => Math.floor(space / rowH + TOLERANCE);
|
|
163
|
+
let rowH = clamp(h / divisor, min, max);
|
|
164
|
+
let shown = Math.min(target, fits(h, rowH));
|
|
165
|
+
let more: Box | null = null;
|
|
166
|
+
if (shown < n) {
|
|
167
|
+
// Items left out: the same rows over a compact "+N more" line.
|
|
168
|
+
let moreH = Math.min(spec.moreH ?? moreHeight(rowH), rowH);
|
|
169
|
+
rowH = clamp((h - moreH) / divisor, min, max);
|
|
170
|
+
shown = Math.min(target, fits(h - moreH, rowH));
|
|
171
|
+
if (shown < 1) {
|
|
172
|
+
// Room for a row but not for the row and the line: the row first (squeezed in a box shorter than one), the line in what's left.
|
|
173
|
+
shown = Math.min(1, n);
|
|
174
|
+
rowH = Math.min(h, min);
|
|
175
|
+
moreH = Math.min(moreH, h - rowH);
|
|
176
|
+
}
|
|
177
|
+
if (moreH >= MIN_MORE_H) more = { x: box.x, y: box.y + shown * rowH, w: box.w, h: moreH };
|
|
178
|
+
}
|
|
179
|
+
const rows = Array.from({ length: shown }, (_, i) => ({ x: box.x, y: box.y + i * rowH, w: box.w, h: rowH }));
|
|
180
|
+
return { layout, rowH, rows, shown, hidden: n - shown, more, ...text(rowH) };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** `"+3 more"` / `"+3 más"`. */
|
|
184
|
+
export function moreLabel(n: number, tr: (text: Localized) => string): string {
|
|
185
|
+
return tr({ en: `+${n} more`, es: `+${n} más` });
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
type DrawCtx = Pick<RenderContext<unknown, unknown>, 'kit' | 'theme' | 't'>;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Paints "+N more" in the plan's `more` box, dim, the same in every list.
|
|
192
|
+
* `indent` lines it up with the rows' text (after a status dot, say);
|
|
193
|
+
* `align: 'right'` puts it at the right edge instead.
|
|
194
|
+
*/
|
|
195
|
+
export function drawMore(g: Graphics, ctx: DrawCtx, plan: Pick<ListPlan, 'hidden' | 'more'>, o: { indent?: number; align?: 'left' | 'right' } = {}): void {
|
|
196
|
+
const m = plan.more;
|
|
197
|
+
if (!m || plan.hidden <= 0) return;
|
|
198
|
+
const size = Math.max(8, Math.round(m.h * 0.62));
|
|
199
|
+
const indent = o.indent ?? 0;
|
|
200
|
+
const font = { size, family: ctx.theme.fonts.sans, color: ctx.theme.colors.inkDim };
|
|
201
|
+
const text = ctx.kit.truncate(g, moreLabel(plan.hidden, ctx.t), Math.max(1, m.w - indent), font);
|
|
202
|
+
const y = m.y + (m.h + size * 0.7) / 2;
|
|
203
|
+
if (o.align === 'right') ctx.kit.drawTracked(g, text, m.x + m.w, y, { ...font, align: 'right' });
|
|
204
|
+
else ctx.kit.drawTracked(g, text, m.x + indent, y, font);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* The message of an empty list ("Nothing to review"), dim, at the top left
|
|
209
|
+
* of the box, the same in every list. Paint it unless the user asked to hide
|
|
210
|
+
* the widget when it's empty (see `WidgetDefinition.empty`).
|
|
211
|
+
*/
|
|
212
|
+
export function drawEmpty(g: Graphics, ctx: DrawCtx & { box: Box }, message: string): void {
|
|
213
|
+
const { box, kit, theme } = ctx;
|
|
214
|
+
const size = Math.max(10, Math.round(Math.min(32, box.h * 0.3)));
|
|
215
|
+
const font = { size, family: theme.fonts.sans, color: theme.colors.inkDim };
|
|
216
|
+
kit.drawTracked(g, kit.truncate(g, message, box.w, font), box.x, box.y + size * 1.1, font);
|
|
217
|
+
}
|
package/src/location.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The user's place is set once, in Settings, and reaches every plugin as
|
|
3
|
+
* `ctx.location`. A widget or source that can look somewhere else (the
|
|
4
|
+
* weather of another city) offers `locationOptions()`, advanced, and picks
|
|
5
|
+
* with `placeOf`: its own place when both coordinates are set, otherwise the
|
|
6
|
+
* user's.
|
|
7
|
+
*/
|
|
8
|
+
import type { Location } from './plugin.ts';
|
|
9
|
+
import { t, type NumberField, type StringField } from './schema.ts';
|
|
10
|
+
|
|
11
|
+
/** Advanced latitude, longitude and name fields; 0, 0 means "the place in Settings". */
|
|
12
|
+
export function locationOptions(): { latitude: NumberField; longitude: NumberField; placeName: StringField } {
|
|
13
|
+
return {
|
|
14
|
+
latitude: t.number({
|
|
15
|
+
default: 0,
|
|
16
|
+
min: -90,
|
|
17
|
+
max: 90,
|
|
18
|
+
step: 0.0001,
|
|
19
|
+
unit: '°',
|
|
20
|
+
advanced: true,
|
|
21
|
+
label: { en: 'Latitude', es: 'Latitud' },
|
|
22
|
+
help: { en: 'Leave latitude and longitude at 0 to use the place in Settings.', es: 'Con latitud y longitud a 0 se usa el lugar de los ajustes.' },
|
|
23
|
+
}),
|
|
24
|
+
longitude: t.number({ default: 0, min: -180, max: 180, step: 0.0001, unit: '°', advanced: true, label: { en: 'Longitude', es: 'Longitud' } }),
|
|
25
|
+
placeName: t.string({ advanced: true, placeholder: 'Madrid', label: { en: 'Place name', es: 'Nombre del lugar' }, help: { en: 'Empty: the one in Settings.', es: 'Vacío: el de los ajustes.' } }),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The place to use: `own` when it has both coordinates (not 0, 0), with its
|
|
31
|
+
* name or the user's; otherwise the user's (`ctx.location`), which may be
|
|
32
|
+
* unset.
|
|
33
|
+
*/
|
|
34
|
+
export function placeOf(own: { latitude?: number; longitude?: number; placeName?: string; name?: string } | undefined, user: Location | undefined): Location | undefined {
|
|
35
|
+
const lat = own?.latitude ?? 0;
|
|
36
|
+
const lon = own?.longitude ?? 0;
|
|
37
|
+
const name = (own?.placeName ?? own?.name ?? '').trim();
|
|
38
|
+
if (Number.isFinite(lat) && Number.isFinite(lon) && (lat !== 0 || lon !== 0)) return { latitude: lat, longitude: lon, ...(name ? { name } : {}) };
|
|
39
|
+
if (!user) return undefined;
|
|
40
|
+
return name ? { ...user, name } : user;
|
|
41
|
+
}
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plugin contract.
|
|
3
|
+
*
|
|
4
|
+
* A plugin is an ES module whose default export is `definePlugin({...})`. It
|
|
5
|
+
* can contribute three kinds of things:
|
|
6
|
+
*
|
|
7
|
+
* - **widgets**: paint inside a rectangle of the screen (a cell of the
|
|
8
|
+
* grid). Optionally fetch their own data on a timer.
|
|
9
|
+
* - **sources**: shared data (agent sessions, system vitals, weather…)
|
|
10
|
+
* read once and handed to every widget that asks for it, so ten widgets
|
|
11
|
+
* showing the CPU don't read /proc ten times.
|
|
12
|
+
* - **interludes**: full-screen animations the screen plays now and then
|
|
13
|
+
* (the 8-bit runner every quarter hour, a video clip…).
|
|
14
|
+
*
|
|
15
|
+
* Everything a widget needs arrives in its context: options already
|
|
16
|
+
* validated, data, theme, fonts, the drawing kit and the canvas factory.
|
|
17
|
+
* A plugin must not import its own copy of @napi-rs/canvas: images from
|
|
18
|
+
* another copy can't be drawn on the panel's canvas.
|
|
19
|
+
*/
|
|
20
|
+
import type { Canvas, Image, SKRSContext2D, createCanvas, loadImage } from '@napi-rs/canvas';
|
|
21
|
+
import type * as Kit from '@loksly/lcd-canvas-kit';
|
|
22
|
+
import type { Formatters } from './format.ts';
|
|
23
|
+
import type { Localized } from './i18n.ts';
|
|
24
|
+
import type { Field, Infer, ObjectField } from './schema.ts';
|
|
25
|
+
import type { Theme } from './theme.ts';
|
|
26
|
+
import type { Workday } from './workday.ts';
|
|
27
|
+
|
|
28
|
+
/** The 2D context widgets paint with. Same API as the browser's CanvasRenderingContext2D. */
|
|
29
|
+
export type Graphics = SKRSContext2D;
|
|
30
|
+
export type { Canvas, Image };
|
|
31
|
+
|
|
32
|
+
export interface CanvasApi {
|
|
33
|
+
createCanvas: typeof createCanvas;
|
|
34
|
+
loadImage: typeof loadImage;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A rectangle in panel pixels. */
|
|
38
|
+
export interface Box {
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
w: number;
|
|
42
|
+
h: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Size in panel pixels. */
|
|
46
|
+
export interface Size {
|
|
47
|
+
w: number;
|
|
48
|
+
h: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type Category = 'agents' | 'system' | 'time' | 'productivity' | 'dev' | 'info' | 'media' | 'scene' | 'fun';
|
|
52
|
+
|
|
53
|
+
export const CATEGORIES: { id: Category; label: Localized }[] = [
|
|
54
|
+
{ id: 'agents', label: { en: 'AI agents', es: 'Agentes IA' } },
|
|
55
|
+
{ id: 'system', label: { en: 'System', es: 'Sistema' } },
|
|
56
|
+
{ id: 'time', label: { en: 'Time & calendar', es: 'Tiempo y agenda' } },
|
|
57
|
+
{ id: 'productivity', label: { en: 'Productivity', es: 'Productividad' } },
|
|
58
|
+
{ id: 'dev', label: { en: 'Development', es: 'Desarrollo' } },
|
|
59
|
+
{ id: 'info', label: { en: 'News & world', es: 'Noticias y mundo' } },
|
|
60
|
+
{ id: 'media', label: { en: 'Media', es: 'Multimedia' } },
|
|
61
|
+
{ id: 'scene', label: { en: 'Scenes', es: 'Escenas' } },
|
|
62
|
+
{ id: 'fun', label: { en: 'Fun', es: 'Diversión' } },
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
export interface Logger {
|
|
66
|
+
(msg: string): void;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A place on Earth, in degrees. */
|
|
70
|
+
export interface Location {
|
|
71
|
+
latitude: number;
|
|
72
|
+
longitude: number;
|
|
73
|
+
/** Shown by some widgets, e.g. "Madrid". */
|
|
74
|
+
name?: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** What every context has. */
|
|
78
|
+
export interface BaseContext {
|
|
79
|
+
now: number;
|
|
80
|
+
/** BCP-47 language of the user (`es`, `en-GB`…). */
|
|
81
|
+
locale: string;
|
|
82
|
+
/** Picks the user's language from a `Localized` text. */
|
|
83
|
+
t: (text: Localized) => string;
|
|
84
|
+
/**
|
|
85
|
+
* Times and lengths of time in the user's language and clock (Settings →
|
|
86
|
+
* time format): `fmt.time(at)` "14:05" / "2:05 PM", `fmt.ago(at)`
|
|
87
|
+
* "5 min ago", `fmt.duration(ms)` "1 h 05". Use these rather than your own.
|
|
88
|
+
*/
|
|
89
|
+
fmt: Formatters;
|
|
90
|
+
log: Logger;
|
|
91
|
+
/** Where the plugin may keep files (caches, downloads). Created on demand. */
|
|
92
|
+
dataDir: string;
|
|
93
|
+
/**
|
|
94
|
+
* The user's place, if set in Settings (weather, sun, earthquakes…). Use it
|
|
95
|
+
* by default; offer a place of your own only where a second one makes sense
|
|
96
|
+
* (an advanced override), with an empty or 0,0 value meaning "this one".
|
|
97
|
+
*/
|
|
98
|
+
location?: Location;
|
|
99
|
+
/**
|
|
100
|
+
* The user's working week from Settings: days, start and end, hours due,
|
|
101
|
+
* days with other hours and holidays. Default to it instead of asking for
|
|
102
|
+
* your own schedule; `scheduleOn(ctx.workday, now)` gives one day's.
|
|
103
|
+
*/
|
|
104
|
+
workday: Workday;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface FetchContext<O> extends BaseContext {
|
|
108
|
+
options: O;
|
|
109
|
+
canvas: CanvasApi;
|
|
110
|
+
/** Aborted when the widget is removed, the app stops, or this fetch takes longer than `timeoutMs`. Pass it to `fetch` and child processes. */
|
|
111
|
+
signal: AbortSignal;
|
|
112
|
+
/** How long this fetch may take (the widget's `timeoutMs`), for a per-request budget of your own. */
|
|
113
|
+
timeoutMs: number;
|
|
114
|
+
/** Latest data of the sources the widget declared. */
|
|
115
|
+
sources: Record<string, unknown>;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export interface StateContext<O, D> {
|
|
119
|
+
options: O;
|
|
120
|
+
data: D | undefined;
|
|
121
|
+
sources: Record<string, unknown>;
|
|
122
|
+
now: number;
|
|
123
|
+
/** True for a work-hours slot, per the user's work schedule. */
|
|
124
|
+
working: boolean;
|
|
125
|
+
/** The user's working week (see `BaseContext.workday`); tests can pass `DEFAULT_WORKDAY`. */
|
|
126
|
+
workday: Workday;
|
|
127
|
+
/** The user's place, if set (see `BaseContext.location`). */
|
|
128
|
+
location?: Location;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export interface RenderContext<O, D> extends BaseContext, StateContext<O, D> {
|
|
132
|
+
/** Where to paint, already inside the card's padding and title. */
|
|
133
|
+
box: Box;
|
|
134
|
+
theme: Theme;
|
|
135
|
+
/** Families registered on the panel; same as `theme.fonts`. */
|
|
136
|
+
fonts: Theme['fonts'];
|
|
137
|
+
/** Helpers from @loksly/lcd-canvas-kit: tracked text, wrapping, sparklines, weather icons… */
|
|
138
|
+
kit: typeof Kit;
|
|
139
|
+
canvas: CanvasApi;
|
|
140
|
+
/** The widget is drawn in the editor (gallery thumbnail or live preview) and not on the panel. */
|
|
141
|
+
preview: boolean;
|
|
142
|
+
/** Frame index while an animation requested with `animate` is running. */
|
|
143
|
+
frame?: number;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* When the widget must be repainted even though its data didn't change: a
|
|
148
|
+
* clock every second, a countdown every minute. Fewer repaints are fewer
|
|
149
|
+
* frames over USB, so pick the slowest that still looks right.
|
|
150
|
+
*/
|
|
151
|
+
export type Redraw = 'never' | 'minute' | 'second' | { everyMs: number };
|
|
152
|
+
|
|
153
|
+
export interface WidgetDefinition<OF extends ObjectField<any> = ObjectField<any>, D = unknown> {
|
|
154
|
+
/** Unique inside its plugin: `clock`, `sessions`… The full type is `plugin/widget`. */
|
|
155
|
+
id: string;
|
|
156
|
+
name: Localized;
|
|
157
|
+
description?: Localized;
|
|
158
|
+
/** Name of an icon of the editor (see web/icons) or an inline SVG string. */
|
|
159
|
+
icon?: string;
|
|
160
|
+
category?: Category;
|
|
161
|
+
tags?: string[];
|
|
162
|
+
/**
|
|
163
|
+
* Sizes in panel pixels. `min` is a hard limit enforced by the editor;
|
|
164
|
+
* `preferred` is what it uses when the widget is dropped.
|
|
165
|
+
*/
|
|
166
|
+
size?: { min?: Size; preferred?: Size; max?: Size };
|
|
167
|
+
/** How the cell frames it: a card with optional title (default) or bare, edge to edge (scenes, photos). */
|
|
168
|
+
chrome?: 'card' | 'bare';
|
|
169
|
+
/** Options, as a schema: typed, validated and turned into a form by the editor. */
|
|
170
|
+
options?: OF;
|
|
171
|
+
/** Ids of shared sources (`plugin/source`) whose data the widget receives in `ctx.sources`. */
|
|
172
|
+
sources?: string[];
|
|
173
|
+
/** Own data, per instance. Runs in Node, never blocks the panel. */
|
|
174
|
+
fetch?: (ctx: FetchContext<Infer<OF>>) => Promise<D>;
|
|
175
|
+
/** How often `fetch` runs. Default: 10 minutes. The user can override it. */
|
|
176
|
+
intervalMs?: number;
|
|
177
|
+
/**
|
|
178
|
+
* How often `fetch` runs when that depends on the options or on the latest
|
|
179
|
+
* data (a module that declares its own interval): asked after every fetch;
|
|
180
|
+
* `undefined` keeps `intervalMs`. An interval the user set on the instance
|
|
181
|
+
* wins. Never under 5 s.
|
|
182
|
+
*/
|
|
183
|
+
intervalOf?: (ctx: { options: Infer<OF>; data: NoInfer<D> | undefined }) => number | undefined;
|
|
184
|
+
/** Longest one `fetch` may take before its `signal` aborts and it counts as failed. Default: 2 minutes. */
|
|
185
|
+
timeoutMs?: number;
|
|
186
|
+
/** Paint. Synchronous and fast: load images in `fetch` and paint them here. */
|
|
187
|
+
render: (g: Graphics, ctx: RenderContext<Infer<OF>, D>) => void;
|
|
188
|
+
/** Also a function, when the cadence depends on the options or the data (a carousel every `rotateMs`). */
|
|
189
|
+
redraw?: Redraw | ((ctx: StateContext<Infer<OF>, D>) => Redraw);
|
|
190
|
+
/** Has nothing to show right now (nothing playing, no rain): skipped in its cell's rotation. */
|
|
191
|
+
visible?: (ctx: StateContext<Infer<OF>, D>) => boolean;
|
|
192
|
+
/**
|
|
193
|
+
* Needs the user now (a meeting about to start): takes its cell over while
|
|
194
|
+
* true, unless the user turned the widget's "keep the tile while urgent"
|
|
195
|
+
* switch (`takeOver`) off. Don't add an option of your own for that.
|
|
196
|
+
*/
|
|
197
|
+
urgent?: (ctx: StateContext<Infer<OF>, D>) => boolean;
|
|
198
|
+
/** Default of the instance's `takeOver` switch (keep the tile while urgent). Default true. */
|
|
199
|
+
takeOver?: boolean;
|
|
200
|
+
/**
|
|
201
|
+
* Has nothing to list right now (no reviews, no tasks). Declaring it gives
|
|
202
|
+
* the widget a "hide when empty" switch (`hideWhenEmpty`): when on, an
|
|
203
|
+
* empty widget is skipped in its tile's rotation like `visible: false`;
|
|
204
|
+
* when off, it paints its empty message (`drawEmpty`). Unlike `visible`,
|
|
205
|
+
* which is the widget's own decision, this one is the user's.
|
|
206
|
+
*/
|
|
207
|
+
empty?: (ctx: StateContext<Infer<OF>, D>) => boolean;
|
|
208
|
+
/** Default of the instance's `hideWhenEmpty` switch. Default false. */
|
|
209
|
+
hideWhenEmpty?: boolean;
|
|
210
|
+
/**
|
|
211
|
+
* Nothing worth lighting the panel for. When every widget of the page is
|
|
212
|
+
* idle and the screen has `sleepWhenIdle`, the panel goes black.
|
|
213
|
+
*/
|
|
214
|
+
idle?: (ctx: StateContext<Infer<OF>, D>) => boolean;
|
|
215
|
+
/**
|
|
216
|
+
* Sample data for the gallery, the editor and the tests, so a widget can
|
|
217
|
+
* be seen before it's configured (no tokens, no network). `options` are
|
|
218
|
+
* sample options used only for gallery thumbnails (e.g. a few countdown
|
|
219
|
+
* dates), never for a widget the user placed.
|
|
220
|
+
*/
|
|
221
|
+
demo?: (ctx: { options: Infer<OF>; now: number; locale: string }) => { data?: D; sources?: Record<string, unknown>; options?: Partial<Infer<OF>> };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
export interface SourceContext<O> extends BaseContext {
|
|
225
|
+
options: O;
|
|
226
|
+
/**
|
|
227
|
+
* Aborted when the source stops (no widget uses it, options changed, the
|
|
228
|
+
* app stops) and, in `read`, when this read takes longer than `timeoutMs`.
|
|
229
|
+
* Pass it to `fetch` and child processes.
|
|
230
|
+
*/
|
|
231
|
+
signal: AbortSignal;
|
|
232
|
+
/** How often `read` runs (the user's `intervalMs`, clamped to `minIntervalMs`). */
|
|
233
|
+
intervalMs: number;
|
|
234
|
+
/** How long one `read` may take (the user's `timeoutMs`), for a per-request budget of your own. */
|
|
235
|
+
timeoutMs: number;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export interface SourceDefinition<OF extends ObjectField<any> = ObjectField<any>, D = unknown> {
|
|
239
|
+
/** Unique inside its plugin. Widgets ask for `plugin/source`. */
|
|
240
|
+
id: string;
|
|
241
|
+
name: Localized;
|
|
242
|
+
description?: Localized;
|
|
243
|
+
/** Global options, edited once in Settings → Sources. */
|
|
244
|
+
options?: OF;
|
|
245
|
+
/**
|
|
246
|
+
* Polled every `intervalMs` while some widget uses it. The user can change
|
|
247
|
+
* the interval and the timeout in Settings → Data sources
|
|
248
|
+
* (`sources["plugin/source"].intervalMs` / `.timeoutMs`): don't add options
|
|
249
|
+
* of your own for them.
|
|
250
|
+
*/
|
|
251
|
+
read?: (ctx: SourceContext<Infer<OF>>) => Promise<D>;
|
|
252
|
+
/** Default interval of `read`. Default: 30 s. */
|
|
253
|
+
intervalMs?: number;
|
|
254
|
+
/** Shortest interval the user may set: API rate limits, an expensive command. Default: 5 s (or `intervalMs`, if shorter). */
|
|
255
|
+
minIntervalMs?: number;
|
|
256
|
+
/** Default of the longest one `read` may take before its `signal` aborts and it counts as failed. Default: 2 minutes. */
|
|
257
|
+
timeoutMs?: number;
|
|
258
|
+
/**
|
|
259
|
+
* Push-based alternative (file watchers, sockets): call `emit` with new
|
|
260
|
+
* data, return a function that stops watching.
|
|
261
|
+
*/
|
|
262
|
+
watch?: (ctx: SourceContext<Infer<OF>>, emit: (data: D) => void) => () => void;
|
|
263
|
+
demo?: (ctx: { now: number; locale: string }) => D;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export interface InterludePlayer {
|
|
267
|
+
/** Number of frames. */
|
|
268
|
+
frames: number;
|
|
269
|
+
/** Paints frame `i` on a canvas of the full panel size. May be async (to decode a prerendered frame). */
|
|
270
|
+
draw: (g: Graphics, i: number) => void | Promise<void>;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export interface InterludeContext<O> extends BaseContext {
|
|
274
|
+
options: O;
|
|
275
|
+
width: number;
|
|
276
|
+
height: number;
|
|
277
|
+
/** Deterministic per slot: the same quarter hour plays the same story. */
|
|
278
|
+
seed: number;
|
|
279
|
+
theme: Theme;
|
|
280
|
+
canvas: CanvasApi;
|
|
281
|
+
sources: Record<string, unknown>;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export interface InterludeDefinition<OF extends ObjectField<any> = ObjectField<any>> {
|
|
285
|
+
id: string;
|
|
286
|
+
name: Localized;
|
|
287
|
+
description?: Localized;
|
|
288
|
+
icon?: string;
|
|
289
|
+
options?: OF;
|
|
290
|
+
sources?: string[];
|
|
291
|
+
/** Frames per second it was designed for. USB panels top out around 12-24 fps. */
|
|
292
|
+
fps: number;
|
|
293
|
+
/** Builds the animation. May be slow (it runs before playing, not during). */
|
|
294
|
+
prepare: (ctx: InterludeContext<Infer<OF>>) => Promise<InterludePlayer | null>;
|
|
295
|
+
/** Don't interrupt now (someone waits for a permission, a meeting is about to start). */
|
|
296
|
+
skip?: (ctx: { sources: Record<string, unknown>; now: number; options: Infer<OF> }) => boolean;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export interface PluginDefinition {
|
|
300
|
+
/** npm-like id: lowercase, digits and dashes. Stable: configs refer to it. */
|
|
301
|
+
id: string;
|
|
302
|
+
name: Localized;
|
|
303
|
+
version: string;
|
|
304
|
+
description?: Localized;
|
|
305
|
+
author?: string;
|
|
306
|
+
homepage?: string;
|
|
307
|
+
license?: string;
|
|
308
|
+
icon?: string;
|
|
309
|
+
widgets?: WidgetDefinition<any, any>[];
|
|
310
|
+
sources?: SourceDefinition<any, any>[];
|
|
311
|
+
interludes?: InterludeDefinition<any>[];
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/* Identity helpers: they only exist so TypeScript infers option and data types. */
|
|
315
|
+
|
|
316
|
+
export function defineWidget<OF extends ObjectField<any> = ObjectField<{}>, D = undefined>(w: WidgetDefinition<OF, D>): WidgetDefinition<OF, D> {
|
|
317
|
+
return w;
|
|
318
|
+
}
|
|
319
|
+
export function defineSource<OF extends ObjectField<any> = ObjectField<{}>, D = unknown>(s: SourceDefinition<OF, D>): SourceDefinition<OF, D> {
|
|
320
|
+
return s;
|
|
321
|
+
}
|
|
322
|
+
export function defineInterlude<OF extends ObjectField<any> = ObjectField<{}>>(i: InterludeDefinition<OF>): InterludeDefinition<OF> {
|
|
323
|
+
return i;
|
|
324
|
+
}
|
|
325
|
+
export function definePlugin(p: PluginDefinition): PluginDefinition {
|
|
326
|
+
return p;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export type { Field };
|