diff --git a/backend/fluksio/flow/dashboards.py b/backend/fluksio/flow/dashboards.py index a1c34ba..45888b8 100644 --- a/backend/fluksio/flow/dashboards.py +++ b/backend/fluksio/flow/dashboards.py @@ -48,6 +48,10 @@ RUN_LINES = 5 #: legible stacked in one tile. BAR_ROWS = 8 +#: How many counters one counter widget links into its number. Mirrored in the +#: client (``ui/core/counter.ts``, ``MAX_COUNTERS``). +COUNTERS = 3 + #: Resolved out here on purpose: the store has a ``list`` method, which #: shadows the builtin for any annotation written inside the class. Bindings = list[dict[str, Any]] @@ -75,6 +79,7 @@ WidgetType = Literal[ "input", "dropdown", "color", + "counter", ] #: Widgets whose only binding is the message they publish. A player is not one: @@ -88,6 +93,7 @@ INPUT_WIDGETS = { "input", "dropdown", "color", + "counter", } #: What a colour widget puts on the wire, by the format it was configured for. @@ -108,6 +114,8 @@ WIDGET_DTYPES: dict[str, set[str]] = { # answer and a `record` request instead, checked separately below. "chart": {"float", "int"}, "slider": {"float", "int"}, + # One number, the digits of which its counters are. + "counter": {"float", "int"}, "switch": {"bool"}, "agenda": {"list"}, "notification": {"record"}, @@ -229,6 +237,12 @@ class WidgetDef(BaseModel): A slider may also show what it is setting: ``reading`` binds ``{message, dtype, label}`` — a room's temperature beside its setpoint — held to the same numbers its target is. + + A counter publishes one number to its target, of which its ``counters`` + are the digits: up to three ``{label, step, size, min, max}``, coarsest + first, so hours and minutes publish 07:30 as 450. ``wrap`` says whether it + wraps round at its ends; ``docs/interface/dashboards.md`` has the + arithmetic. """ id: str @@ -397,6 +411,9 @@ class WidgetDef(BaseModel): if self.type == "bar" and len(self.bar_rows) > BAR_ROWS: raise ValueError(f"a bar draws at most {BAR_ROWS} readings") + if self.type == "counter" and len(self.config.get("counters") or []) > COUNTERS: + raise ValueError(f"a counter links at most {COUNTERS} counters") + if self.type == "color": # The row above allows both shapes a colour travels as; the format # is what decides which of them this widget actually sends. An diff --git a/docs/interface/dashboards.md b/docs/interface/dashboards.md index 9948097..b2039a8 100644 --- a/docs/interface/dashboards.md +++ b/docs/interface/dashboards.md @@ -59,6 +59,7 @@ still names the widget for accessibility and published values. | **Input** | text or number | free entry | | **Selector** | selected value | menu or row of choices | | **Colour** | list or string | HSV, RGB or hex | +| **Counter** | `int`, `float` | one number set by up to three linked counters, such as hours and minutes; see [A counter](#a-counter) | Colour defaults to `[h, s, v]`: hue 0–360°, saturation and brightness 0–100%. **Sends** can select RGB (`[r, g, b]`, 0–255) or hex (`"#rrggbb"`). @@ -125,6 +126,50 @@ dot that shows on the handle, so a reading at the setpoint is still visible. | Order | track, dots, fill, the dots again over the fill, handle, the reading again over the handle | | Locked | in a tile, the dots at 0.45 opacity, as the handle; on the ring, the whole ring at 0.45 | +### A counter + +A Counter sets one whole number with up to three linked counters. They are the +digits of that number, counted in the unit of the finest, and the widget +publishes the number on its one message: hours and minutes publish 07:30 as +450, and 07:55 plus five minutes is 08:00. + +| Field | Meaning | +|---|---| +| `counters` | 1 to 3 `{label, step, size, min, max}`, coarsest first. Absent or empty reads as the time below | +| `label` | written under the counter's digits | +| `step` | a press or a notch, in the counter's own unit: 1 hour moves the number by 60. Default 1, at least 1 | +| `size` | how many of it make one of the next coarser counter, 60 for minutes. Every counter but the coarsest needs one | +| `min`, `max` | the range, on the coarsest only and in its unit. `min` defaults to 0; no `max` is no upper bound | +| `wrap` | at either end, go round to the other (the default) or stop there | + +A new counter is a time of day: `[{label: "h", step: 1, min: 0, max: 23}, +{label: "min", step: 5, size: 60}]`, wrapping. + +For counters 0 to n − 1, coarsest first, and the number v (`ui/core/counter.ts`, +which the firmware ports as it stands): + +| Quantity | Formula | +|---|---| +| Weight wᵢ | the finest 1; each coarser one wᵢ = wᵢ₊₁ × sizeᵢ₊₁ | +| Digit dᵢ | d₀ = ⌊v / w₀⌋; dᵢ = ⌊v / wᵢ⌋ mod sizeᵢ, a remainder that is never negative | +| A step | counter i by k steps (k < 0 down): v + k × stepᵢ × wᵢ, so carry and borrow follow | +| Range | lo = min₀ × w₀. With a `max`, period = (max₀ − min₀ + 1) × w₀ and top = lo + ⌊(period − 1) / s⌋ × s, s the finest counter's step: 0–23 in 5-minute steps tops out at 1435 (23:55), one counter of step 5 up to 100 at 100 | +| Wrap | v = lo + ((v − lo) mod period): 23:55 + 5 min is 00:00, 00:00 − 1 h is 23:00 | +| Stop | v held to lo … top, as a slider is | +| No `max` | no top and nothing to wrap at: v is only held to lo or above | + +Written, the digits are joined by `:`, each counter's label under its own. +Every digit but the coarsest is zero-padded to the width of size − 1 (05); the +coarsest to the width of `max` when it has one (07), and a negative one keeps +its sign in front of the padding. The number shown is the message's, held as +sent until it comes back, as a slider's is, and lo until one arrives. + +In a tile each counter is a column: a press up, its digits, a press down and +its label, at the control sizes, so touch-friendly where the panel is. A press +moves the number at once and one publish follows 300 ms after the last press, +as a ring's does; holding a press does not repeat it. On a round face see +[Round dashboards](#round-dashboards). + ## Charts Choose the chart's data source: @@ -243,13 +288,22 @@ whole face, in the order of `widgets`; `layout` is not read. In the editor, the faces are a row above the dock: press one to show it and open its settings, drag it along the row (or Alt+←/→) to move it, and add more with **Add widget**. -A Slider, Selector, Colour or Gauge draws on the **ring** along the edge, which -a knob's own ring turns. In a browser, drag along the ring or use the mouse -wheel (or the arrow keys once it has focus): one notch is one step, one option -(no wrap) or one hue step (`hue_step`, default 5°, wrapping). The value moves -at once and is published once, 300 ms after the last notch or when the pointer -is released. A Gauge only shows. On any other face, turning a knob only wakes -the screen. +A Slider, Selector, Colour, Counter or Gauge draws on the **ring** along the +edge, which a knob's own ring turns. In a browser, drag along the ring or use +the mouse wheel (or the arrow keys once it has focus): one notch is one step, +one option (no wrap), one hue step (`hue_step`, default 5°, wrapping) or one +step of the counter selected. The value moves at once and is published once, +300 ms after the last notch or when the pointer is released. A Gauge only +shows. On any other face, turning a knob only wakes the screen. + +A Counter's ring turns one of its counters, with carry: the finest, until a +press on another's digits selects that one instead. The selection holds while +the face is shown and is the finest again once the face is left. Dragging +along its ring counts notches rather than picking a place: one per step's +share of the sweep, 300° × step / its turn (size, or max − min + 1 for the +coarsest), so the arc stays under the finger, and one per 15° for a coarsest +without `max`. The angle is counted from the last move the shorter way round, +so a drag carries on through the gap. The face has no up or down presses. A Slider's reading (**Beside it**) is written large in the middle, under its label; the slider's own title and value move to the line in the ring's gap. @@ -295,6 +349,7 @@ look's muted text colour. | Selector | a value arc from the start to the selected option (none at the first), a dot 6 across per option at i/(n−1) of the sweep (one option: the middle), the selected one under the handle; its label as the centre reading, at most 36 | | Colour | hue h at h/360 of the sweep in 150 strokes of 2°, red at both ends; the handle filled with the colour; a swatch 96 across in the middle, with a hairline border | | Gauge | track and value arc, no handle; the value as the centre reading | +| Counter | the digits, joined by `:`, sized as a centre reading of the whole line (07:30 is five characters: 72), each counter's label under its digits at 17, muted, 4 apart; the block centred at (c, c). With two or more counters, the one the ring turns is in `--primary`, digits and label, and pressing a counter's digits selects it. A value arc from the start to where the selected counter stands in its turn: dᵢ / sizeᵢ, as a minute hand stands, the coarsest (d₀ − min) / (max − min + 1); no arc for a coarsest without `max`. No handle and no dots | | Gap line | centred at y = S − 28, 15, at most 140 wide, clear of the ring's end caps | | Value | the reading as the centre reading, to its precision; a word shrinks like any other | | Bar | an arc per row, the first outermost, each over the ring's sweep (start 120°, 300°) with round caps: a track in the look's track paint and the row's value arc in its data colour, on its own scale (none at the minimum). n rows share a band 64 thick inward from c − 12, 3 apart: stroke s = min(16, (64 − 3(n − 1)) / n), centre-line radius rᵢ = c − 12 − s/2 − i(s + 3), so up to three rows are 16 thick and the first is on the ring (c − 20). The middle is the legend, a line per row in order: a dot 8 across in the row's colour, 8, the label (muted, cut with an ellipsis), 8, the value and unit to the row's precision (— for none). Lines of 22 at 17 for up to 5 rows, 18 at 15 for 6–7, 16 at 13 for 8; the block centred at (c, c), as wide as its widest line but at most 240 and at most the width of the disc inside the innermost arc (radius c − 12 − n·s − 3(n − 1)) at the block's top and bottom, less 16. The title is at most that disc's width at the title's line (y = c − 89), less 8 | @@ -342,6 +397,7 @@ dashboard's look's. | Ring value set by a notch, a key or the engine | the value arc's end, the handle (slider, selector, colour) | from where it is drawn to the new fraction of the sweep | spring | each notch retargets from where it is, at the speed it has | | Ring value set by a drag | the same | follows the pointer exactly | none | — | | Hue across red | the colour handle | a change of more than half the sweep jumps to the other end | none | — | +| Counter across its turn | the value arc | a change of more than half the sweep (a carry, a wrap, another counter selected) jumps | none | — | | Slider's reading on the ring, gauge on the ring | the reading's dot; the value arc | from where it is drawn to the new fraction; a dot's colour follows the fill and the handle as they pass | spring | retargets | | Numbers on the ring | centre reading, gap line | switch at once, with the notch | none | — | | Value face, Value and Gauge tiles | the number | counts to the new value, written to its precision | spring | retargets | diff --git a/docs/reference/payload-types.md b/docs/reference/payload-types.md index 0359822..ecca900 100644 --- a/docs/reference/payload-types.md +++ b/docs/reference/payload-types.md @@ -129,7 +129,7 @@ literal text in run parameters. | Widget | Accepts | |---|---| -| Gauge, live Chart, Slider, Bar | `float`, `int` | +| Gauge, live Chart, Slider, Bar, Counter | `float`, `int` | | Query Chart | `series` response, `record` request | | Switch | `bool` | | Agenda, Forecast | `list` | diff --git a/frontend/src/client/schemas.gen.ts b/frontend/src/client/schemas.gen.ts index 222746c..f8f54a1 100644 --- a/frontend/src/client/schemas.gen.ts +++ b/frontend/src/client/schemas.gen.ts @@ -4642,7 +4642,7 @@ export const WidgetDefSchema = { }, type: { type: 'string', - enum: ['stat', 'gauge', 'chart', 'markdown', 'agenda', 'notification', 'bar', 'icon', 'forecast', 'clock', 'media', 'player', 'embed', 'button', 'buttons', 'switch', 'slider', 'input', 'dropdown', 'color'], + enum: ['stat', 'gauge', 'chart', 'markdown', 'agenda', 'notification', 'bar', 'icon', 'forecast', 'clock', 'media', 'player', 'embed', 'button', 'buttons', 'switch', 'slider', 'input', 'dropdown', 'color', 'counter'], title: 'Type' }, title: { @@ -4694,7 +4694,13 @@ The first two are a \`\`list\`\` message, the third a \`\`str\`\`, which is what A slider may also show what it is setting: \`\`reading\`\` binds \`\`{message, dtype, label}\`\` — a room's temperature beside its setpoint — -held to the same numbers its target is.` +held to the same numbers its target is. + +A counter publishes one number to its target, of which its \`\`counters\`\` +are the digits: up to three \`\`{label, step, size, min, max}\`\`, coarsest +first, so hours and minutes publish 07:30 as 450. \`\`wrap\`\` says whether it +wraps round at its ends; \`\`docs/interface/dashboards.md\`\` has the +arithmetic.` } as const; export const WidgetFootprintSchema = { @@ -4721,7 +4727,7 @@ export const WidgetFootprintSchema = { }, type: { type: 'string', - enum: ['stat', 'gauge', 'chart', 'markdown', 'agenda', 'notification', 'bar', 'icon', 'forecast', 'clock', 'media', 'player', 'embed', 'button', 'buttons', 'switch', 'slider', 'input', 'dropdown', 'color'], + enum: ['stat', 'gauge', 'chart', 'markdown', 'agenda', 'notification', 'bar', 'icon', 'forecast', 'clock', 'media', 'player', 'embed', 'button', 'buttons', 'switch', 'slider', 'input', 'dropdown', 'color', 'counter'], title: 'Type' } }, diff --git a/frontend/src/client/types.gen.ts b/frontend/src/client/types.gen.ts index 4bde084..4beda3a 100644 --- a/frontend/src/client/types.gen.ts +++ b/frontend/src/client/types.gen.ts @@ -1522,10 +1522,16 @@ export type WebPushKey = { * A slider may also show what it is setting: ``reading`` binds * ``{message, dtype, label}`` — a room's temperature beside its setpoint — * held to the same numbers its target is. + * + * A counter publishes one number to its target, of which its ``counters`` + * are the digits: up to three ``{label, step, size, min, max}``, coarsest + * first, so hours and minutes publish 07:30 as 450. ``wrap`` says whether it + * wraps round at its ends; ``docs/interface/dashboards.md`` has the + * arithmetic. */ export type WidgetDef = { id: string; - type: 'stat' | 'gauge' | 'chart' | 'markdown' | 'agenda' | 'notification' | 'bar' | 'icon' | 'forecast' | 'clock' | 'media' | 'player' | 'embed' | 'button' | 'buttons' | 'switch' | 'slider' | 'input' | 'dropdown' | 'color'; + type: 'stat' | 'gauge' | 'chart' | 'markdown' | 'agenda' | 'notification' | 'bar' | 'icon' | 'forecast' | 'clock' | 'media' | 'player' | 'embed' | 'button' | 'buttons' | 'switch' | 'slider' | 'input' | 'dropdown' | 'color' | 'counter'; title?: string; layout?: { [key: string]: Placement; @@ -1535,7 +1541,7 @@ export type WidgetDef = { }; }; -export type type = 'stat' | 'gauge' | 'chart' | 'markdown' | 'agenda' | 'notification' | 'bar' | 'icon' | 'forecast' | 'clock' | 'media' | 'player' | 'embed' | 'button' | 'buttons' | 'switch' | 'slider' | 'input' | 'dropdown' | 'color'; +export type type = 'stat' | 'gauge' | 'chart' | 'markdown' | 'agenda' | 'notification' | 'bar' | 'icon' | 'forecast' | 'clock' | 'media' | 'player' | 'embed' | 'button' | 'buttons' | 'switch' | 'slider' | 'input' | 'dropdown' | 'color' | 'counter'; /** * A widget reduced to the shape it draws: what it is, and where it sits. @@ -1550,7 +1556,7 @@ export type WidgetFootprint = { y?: number; w?: number; h?: number; - type: 'stat' | 'gauge' | 'chart' | 'markdown' | 'agenda' | 'notification' | 'bar' | 'icon' | 'forecast' | 'clock' | 'media' | 'player' | 'embed' | 'button' | 'buttons' | 'switch' | 'slider' | 'input' | 'dropdown' | 'color'; + type: 'stat' | 'gauge' | 'chart' | 'markdown' | 'agenda' | 'notification' | 'bar' | 'icon' | 'forecast' | 'clock' | 'media' | 'player' | 'embed' | 'button' | 'buttons' | 'switch' | 'slider' | 'input' | 'dropdown' | 'color' | 'counter'; }; export type WorkerInfo = { diff --git a/frontend/src/components/Common/DashboardMosaic.tsx b/frontend/src/components/Common/DashboardMosaic.tsx index cd623eb..c3bd5bd 100644 --- a/frontend/src/components/Common/DashboardMosaic.tsx +++ b/frontend/src/components/Common/DashboardMosaic.tsx @@ -17,6 +17,7 @@ const INPUT = new Set([ "input", "dropdown", "color", + "counter", ]) const shade = (type: string) => diff --git a/frontend/src/components/Dashboard/CounterWidget.tsx b/frontend/src/components/Dashboard/CounterWidget.tsx new file mode 100644 index 0000000..1b9e677 --- /dev/null +++ b/frontend/src/components/Dashboard/CounterWidget.tsx @@ -0,0 +1,85 @@ +import { ChevronDown, ChevronUp } from "lucide-react" +import { Fragment } from "react" + +import { usePublish } from "./publish" +import { useUi } from "./ui" +import { countersOf, wrapsOf } from "./ui/core/config" +import { rangeOf, stepCounter, writtenOf } from "./ui/core/counter" +import { useRingDraft } from "./ui/core/ring" +import type { WidgetProps } from "./widgets" + +/** + * One number set by its digits: hours and minutes, say, published as minutes. + * + * A column per counter, a press up over its digits and one down under them, + * its label beneath. A press moves the value at once, carrying into the + * counter beside it, and one publish follows once the presses stop, as a + * round face's ring does (`useRingDraft`). + */ +export function CounterWidget({ widget, dashboard }: WidgetProps) { + const { Button } = useUi() + const { target, value, send, pulse, locked } = usePublish(widget, dashboard) + const counters = countersOf(widget) + const { current, latest, move } = useRingDraft( + typeof value === "number" ? value : rangeOf(counters).lo, + send, + ) + if (!target) return
Pick a message.
+ + const written = writtenOf(counters, current) + const press = (index: number, by: number) => + move(stepCounter(counters, wrapsOf(widget), latest(), index, by), true) + return ( + + ) +} diff --git a/frontend/src/components/Dashboard/RingWidget.tsx b/frontend/src/components/Dashboard/RingWidget.tsx index 524140c..5a2d1f7 100644 --- a/frontend/src/components/Dashboard/RingWidget.tsx +++ b/frontend/src/components/Dashboard/RingWidget.tsx @@ -1,6 +1,6 @@ import { Lock } from "lucide-react" import { type MotionValue, motion, useTransform } from "motion/react" -import { useEffect, useId, useRef } from "react" +import { Fragment, useEffect, useId, useRef, useState } from "react" import type { WidgetDef } from "@/client" import { cn } from "@/lib/utils" @@ -17,15 +17,26 @@ import { } from "./ui/core/color" import { config, + countersOf, format, fractionOf, num, showTitle, text, + wrapsOf, } from "./ui/core/config" import { TESTID } from "./ui/core/contract" import { tickIntervals } from "./ui/core/controls" import { + type Counter, + cycleAt, + cycleOf, + rangeOf, + stepCounter, + writtenOf, +} from "./ui/core/counter" +import { + CENTRE_CAPTION, CENTRE_LABEL, CENTRE_SWATCH, CENTRE_VALUE, @@ -34,11 +45,13 @@ import { GAP_RISE, GAP_SIZE, GAP_WIDTH, + RING_DRAG_STEP, RING_HANDLE, RING_INSET, RING_MARK, RING_READING, RING_STROKE, + RING_SWEEP, ringArc, ringPoint, TITLE_RISE, @@ -76,6 +89,9 @@ type Dial = { at: (value: unknown) => number | null notch?: (value: unknown, by: number) => unknown pick?: (fraction: number, value: unknown) => unknown + /** A drag that counts notches rather than picking a place: one per this + * many degrees along the ring, either way. */ + drag?: number /** What it publishes, in the shape the same widget sends from the grid. */ wire: (value: unknown) => unknown /** Written out; empty for a colour, whose handle is the reading. */ @@ -89,9 +105,13 @@ type Dial = { * slider's ticks. */ stops?: number[] hue?: boolean + /** Where it stands in a turn rather than along a range, so a move across + * the seam jumps, as a hue does. */ + wraps?: boolean } -function dialOf(widget: WidgetDef, live: unknown): Dial { +/** `selected` is the counter a counter's ring turns. */ +function dialOf(widget: WidgetDef, live: unknown, selected: number): Dial { const cfg = config(widget) const unit = cfg.unit ? text(cfg.unit) : undefined @@ -145,6 +165,26 @@ function dialOf(widget: WidgetDef, live: unknown): Dial { } } + if (widget.type === "counter") { + // The selected counter, stepped with carry; its arc is where it stands + // in its turn, as a minute hand stands in the hour. + const counters = countersOf(widget) + const wrap = wrapsOf(widget) + const cycle = cycleOf(counters, selected) + const step = counters[selected].step ?? 1 + return { + value: typeof live === "number" ? live : rangeOf(counters).lo, + at: (value) => cycleAt(counters, value as number, selected), + notch: (value, by) => + stepCounter(counters, wrap, value as number, selected, by), + drag: cycle === null ? RING_DRAG_STEP : (RING_SWEEP * step) / cycle, + wire: (value) => value, + reading: (value) => writtenOf(counters, value as number).join(":"), + fill: true, + wraps: true, + } + } + const min = num(cfg.min, 0) const max = num(cfg.max, 100) @@ -260,7 +300,7 @@ export function CentreReading({ /** * One widget, drawn along the edge of a round face. * - * A slider, a selector, a colour or a gauge — each is one value along a range, + * A slider, a selector, a colour, a gauge or a counter — each is one value, * which is the one thing a knob's ring can set and a circle's edge can show. * Turned by a drag along it or by the mouse wheel, a notch a step, an option * or a hue step; the value moves at once and one publish follows once the @@ -297,7 +337,16 @@ export function Ring({ const beside: Series = widget.type === "slider" ? ((cfg.reading ?? {}) as Series) : {} const read = useBoundValue(beside.message || undefined) - const dial = dialOf(widget, widget.type === "gauge" ? bound?.value : value) + // The counter the ring turns: the finest until another is pressed, and + // again once the face is left. + const counters = widget.type === "counter" ? countersOf(widget) : [] + const [chosen, choose] = useState