Counter widget: one to three linked counters are the digits of one number, published on one message in the finest counter's unit (hours and minutes send 07:30 as 450, 07:55 + 5 min carries to 08:00); wraps or stops at the coarsest's min/max. The arithmetic is one pure module, ui/core/counter.ts, with counter.check.ts. A tile steps each counter with a press up and down and publishes once 300 ms after the last; a round face turns the selected counter on the ring (wheel, keys, a drag counted in steps), its arc where that counter stands in its turn, and a press on a counter's digits selects it. Default config is the time picker. Editor rows, widgetIssue, server type and dtype, docs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016ezWcLmBCtTVKFPxfdK2MK
This commit is contained in:
Melvin Strobl
2026-10-16 08:58:42 +02:00
co-authored by Claude Opus 5.5
parent 0c7f8bde82
commit 3105505192
16 changed files with 745 additions and 33 deletions
@@ -7,6 +7,7 @@
* ends the duplication — nothing in `ui/` may import a widget.
*/
import type { WidgetDef } from "@/client"
import { type Counter, DEFAULT_COUNTERS, MAX_COUNTERS } from "./counter"
export const config = (widget: WidgetDef): Record<string, unknown> =>
(widget.config ?? {}) as Record<string, unknown>
@@ -149,3 +150,17 @@ export function rowsOf(widget: WidgetDef): BarRow[] {
: []
return [...outer, ...inner].slice(0, MAX_ROWS)
}
/** A counter widget's counters, coarsest first: a time of day when it stores
* none (`DEFAULT_COUNTERS`). */
export function countersOf(widget: WidgetDef): Counter[] {
const stored = config(widget).counters
return Array.isArray(stored) && stored.length
? (stored as Counter[]).slice(0, MAX_COUNTERS)
: DEFAULT_COUNTERS
}
/** Whether a counter wraps round at its ends rather than stopping; absent is
* yes, as a time of day does. */
export const wrapsOf = (widget: WidgetDef): boolean =>
config(widget).wrap !== false
@@ -185,6 +185,22 @@
height: auto;
}
/* A counter: a column per counter, its press up, its digits, its press down
and its label, with a colon between two columns' digits. */
.dui-counter {
display: grid;
justify-content: center;
justify-items: center;
align-items: center;
gap: 0.25rem 0.5rem;
}
.dui-counter-digits {
font-size: clamp(1.25rem, min(22cqh, 12cqw), 4rem);
line-height: 1;
font-variant-numeric: tabular-nums;
}
/* A chart is mostly plot; its legend is the hover readout and goes first. */
@container (max-height: 150px) {
.dui-chart .u-legend {
@@ -0,0 +1,83 @@
/**
* The counter's arithmetic, checked: the edges the firmware port must agree on.
*
* cd frontend && bun run src/components/Dashboard/ui/core/counter.check.ts
*/
import assert from "node:assert/strict"
import {
type Counter,
cycleAt,
DEFAULT_COUNTERS,
digitsOf,
rangeOf,
stepCounter,
weightsOf,
writtenOf,
} from "./counter"
const time = DEFAULT_COUNTERS
const HOUR = 0
const MIN = 1
const at = (h: number, m: number) => h * 60 + m
// Digits and how they are written.
assert.deepEqual(weightsOf(time), [60, 1])
assert.deepEqual(digitsOf(time, at(7, 30)), [7, 30])
assert.deepEqual(writtenOf(time, at(7, 5)), ["07", "05"])
// Carry and borrow.
assert.equal(stepCounter(time, true, at(7, 55), MIN, 1), at(8, 0))
assert.equal(stepCounter(time, true, at(8, 0), MIN, -1), at(7, 55))
assert.equal(stepCounter(time, true, at(7, 30), HOUR, 1), at(8, 30))
// The top is the last value the finest step reaches: 23:55.
assert.equal(rangeOf(time).top, at(23, 55))
// Wrap: round the day either way.
assert.equal(stepCounter(time, true, at(23, 55), MIN, 1), 0)
assert.equal(stepCounter(time, true, 0, HOUR, -1), at(23, 0))
assert.equal(stepCounter(time, true, 0, MIN, -1), at(23, 55))
// Stop: held at either end, as a slider is.
assert.equal(stepCounter(time, false, at(23, 55), MIN, 1), at(23, 55))
assert.equal(stepCounter(time, false, at(23, 30), HOUR, 1), at(23, 55))
assert.equal(stepCounter(time, false, at(0, 30), HOUR, -1), 0)
// One counter, step 5 to 100: the top is 100 itself, padded to max's width.
const single: Counter[] = [{ step: 5, max: 100 }]
assert.equal(rangeOf(single).top, 100)
assert.equal(stepCounter(single, false, 100, 0, 1), 100)
assert.equal(stepCounter(single, true, 100, 0, 1), 4)
assert.deepEqual(writtenOf(single, 5), ["005"])
// No max: no top, nothing to wrap at, still held at min.
const open: Counter[] = [{ step: 1 }, { step: 15, size: 60 }]
assert.equal(stepCounter(open, true, at(99, 45), 1, 1), at(100, 0))
assert.equal(stepCounter(open, true, 0, 1, -1), 0)
assert.deepEqual(writtenOf(open, at(100, 0)), ["100", "00"])
assert.equal(cycleAt(open, 0, 0), null)
// Three counters: hours, minutes, seconds.
const clock: Counter[] = [
{ step: 1, max: 23 },
{ step: 1, size: 60 },
{ step: 10, size: 60 },
]
assert.deepEqual(weightsOf(clock), [3600, 60, 1])
assert.equal(stepCounter(clock, true, 3599, 2, 1), 3609)
assert.equal(rangeOf(clock).top, 86_390)
// Below zero the digits still add up: floor, and a remainder never negative.
const signed: Counter[] = [
{ step: 1, min: -5, max: 5 },
{ step: 5, size: 60 },
]
assert.deepEqual(digitsOf(signed, -5), [-1, 55])
assert.deepEqual(writtenOf(signed, -5), ["-1", "55"])
assert.equal(stepCounter(signed, true, at(5, 55), 1, 1), -300)
// The ring's arc: the finest like a minute hand, the coarsest across its range.
assert.equal(cycleAt(time, at(7, 30), MIN), 0.5)
assert.equal(cycleAt(time, at(6, 0), HOUR), 0.25)
console.log("counter: ok")
@@ -0,0 +1,128 @@
/**
* A counter widget's arithmetic: one to three counters that are the digits of
* one integer, counted in the unit of the finest. Hours and minutes at 07:30
* are 450, and 07:55 stepped five minutes on is 08:00.
*
* Pure and on its own because the openpanel firmware ports it line for line:
* a knob and a browser that disagreed on an edge would set the same face to
* two different times. `app/docs/interface/dashboards.md` states the same
* formulas.
*/
/** One counter, as the document stores it. Counters are ordered coarsest
* first. */
export type Counter = {
label?: string
/** One press or notch, in this counter's own unit. */
step?: number
/** How many of it make one of the next coarser counter: 60 minutes. Every
* counter but the coarsest has one. */
size?: number
/** The range, on the coarsest only and in its own unit. No `max` is no
* upper bound. */
min?: number
max?: number
}
/** What a counter widget is until it says otherwise: a time of day. */
export const DEFAULT_COUNTERS: Counter[] = [
{ label: "h", step: 1, min: 0, max: 23 },
{ label: "min", step: 5, size: 60 },
]
/** How many counters one widget links. Mirrored server-side as `COUNTERS`
* (`app/flow/dashboards.py`). */
export const MAX_COUNTERS = 3
/** The remainder that is never negative, as a clock's is. */
const mod = (a: number, b: number) => ((a % b) + b) % b
/** What one unit of each counter is worth in the finest's: the finest 1, each
* coarser one the next finer one's weight times that one's size. */
export function weightsOf(counters: Counter[]): number[] {
const weights = counters.map(() => 1)
for (let i = counters.length - 2; i >= 0; i--) {
weights[i] = weights[i + 1] * (counters[i + 1].size ?? 1)
}
return weights
}
/** Each counter's digit of `value`: floor(value / weight) mod size, and the
* coarsest, which has no size, floor(value / weight). */
export function digitsOf(counters: Counter[], value: number): number[] {
const weights = weightsOf(counters)
return counters.map((counter, i) => {
const digit = Math.floor(value / weights[i])
return i === 0 ? digit : mod(digit, counter.size ?? 1)
})
}
/**
* The value's range: `lo`, the highest value the finest step reaches from it
* (`top`), and the `period` a wrap counts in. Without a `max` there is no top
* and nothing to wrap at.
*/
export function rangeOf(counters: Counter[]) {
const coarsest = counters[0]
const weight = weightsOf(counters)[0]
const min = coarsest.min ?? 0
const lo = min * weight
if (coarsest.max == null) return { lo, top: Infinity, period: null }
const period = (coarsest.max - min + 1) * weight
const fine = counters[counters.length - 1].step ?? 1
return { lo, top: lo + Math.floor((period - 1) / fine) * fine, period }
}
/**
* `value` with counter `index` stepped `by` steps, carrying into and borrowing
* from the others. At the ends it wraps round the period, or stops at `lo` and
* `top`; without a `max` it only stops at `lo`.
*/
export function stepCounter(
counters: Counter[],
wrap: boolean,
value: number,
index: number,
by: number,
): number {
const next =
value + by * (counters[index].step ?? 1) * weightsOf(counters)[index]
const { lo, top, period } = rangeOf(counters)
if (wrap && period !== null) return lo + mod(next - lo, period)
return Math.min(top, Math.max(lo, next))
}
/**
* Every counter's digit of `value` as written: zero-padded to the width of
* size − 1 (05), the coarsest to the width of its `max` when it has one (07).
*/
export function writtenOf(counters: Counter[], value: number): string[] {
return digitsOf(counters, value).map((digit, i) => {
const widest = i > 0 ? (counters[i].size ?? 1) - 1 : counters[0].max
const width = widest == null ? 0 : String(widest).length
const padded = String(Math.abs(digit)).padStart(width, "0")
return digit < 0 ? `-${padded}` : padded
})
}
/** How many of counter `index` make one turn of it: its size, or the
* coarsest's range. Null for a coarsest without `max`, which has none. */
export function cycleOf(counters: Counter[], index: number): number | null {
if (index > 0) return counters[index].size ?? 1
const { min = 0, max } = counters[0]
return max == null ? null : max - min + 1
}
/** Where counter `index` stands in its turn, 0..1: digit / size, as a minute
* hand stands, and the coarsest (digit − min) / (max − min + 1). */
export function cycleAt(
counters: Counter[],
value: number,
index: number,
): number | null {
const cycle = cycleOf(counters, index)
if (cycle === null) return null
const from = index > 0 ? 0 : (counters[0].min ?? 0)
const at = (digitsOf(counters, value)[index] - from) / cycle
return Math.min(1, Math.max(0, at))
}
@@ -31,6 +31,10 @@ export const RING_MARK = 6
export const RING_READING = 12
/** How long the ring waits after the last notch before it publishes. */
export const RING_SETTLE_MS = 300
/** How far a drag along the ring turns a counter that has no arc, a step at
* a time; one with an arc takes a step per step's share of the sweep, so the
* arc stays under the finger. */
export const RING_DRAG_STEP = 15
/** The gap's line — a slider's title and value, when what it reads is in
* the middle — centred this far above the bottom of the face, at this size,
* and at most this wide: clear of the ring's end caps at any size. */
@@ -56,6 +60,8 @@ export const CENTRE_VALUE = 72
export const CENTRE_LABEL = 36
export const CENTRE_SWATCH = 96
export const CENTRE_FLOOR = 24
/** A line under a centre reading: a counter's labels. */
export const CENTRE_CAPTION = 17
/** A switch's or a button's disc, as a diameter. */
export const PRESS = 128