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:
co-authored by
Claude Opus 5.5
parent
0c7f8bde82
commit
3105505192
@@ -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<number | null>(null)
|
||||
const selected = Math.min(chosen ?? Infinity, counters.length - 1)
|
||||
const dial = dialOf(
|
||||
widget,
|
||||
widget.type === "gauge" ? bound?.value : value,
|
||||
selected,
|
||||
)
|
||||
const { current, held, latest, move, commit } = useRingDraft(
|
||||
dial.value,
|
||||
(next) => send(dial.wire(next)),
|
||||
@@ -305,9 +354,12 @@ export function Ring({
|
||||
const svg = useRef<SVGSVGElement>(null)
|
||||
const face = useRef<HTMLDivElement>(null)
|
||||
const spin = useRef(0)
|
||||
// Where a counting drag last was, and how far it has gone since its last
|
||||
// notch, in degrees.
|
||||
const dragged = useRef({ angle: 0, by: 0 })
|
||||
const turns = Boolean(dial.notch) && !locked
|
||||
const at = dial.at(current)
|
||||
const along = useEased(at ?? 0, held, Boolean(dial.hue))
|
||||
const along = useEased(at ?? 0, held, Boolean(dial.hue || dial.wraps))
|
||||
// At the minimum the value arc is a zero-length dash whose round caps would
|
||||
// still paint a dot, so it is not drawn there.
|
||||
const shown = useTransform(along, (fraction) => (fraction > 0 ? 1 : 0))
|
||||
@@ -338,19 +390,25 @@ export function Ring({
|
||||
return () => canvas?.removeEventListener("wheel", listener)
|
||||
}, [])
|
||||
|
||||
const aim = (event: React.PointerEvent) => {
|
||||
const aim = (event: React.PointerEvent, start = false) => {
|
||||
const box = svg.current?.getBoundingClientRect()
|
||||
if (!box || !dial.pick) return
|
||||
move(
|
||||
dial.pick(
|
||||
fractionAt(
|
||||
event.clientX - (box.left + box.width / 2),
|
||||
event.clientY - (box.top + box.height / 2),
|
||||
),
|
||||
latest(),
|
||||
),
|
||||
false,
|
||||
)
|
||||
if (!box) return
|
||||
const dx = event.clientX - (box.left + box.width / 2)
|
||||
const dy = event.clientY - (box.top + box.height / 2)
|
||||
if (dial.drag && dial.notch) {
|
||||
// Counted as a knob's ring counts its detents, the shorter way round
|
||||
// from the last move, so a drag carries on through the gap.
|
||||
const angle = (Math.atan2(dy, dx) * 180) / Math.PI
|
||||
const last = dragged.current
|
||||
last.by = start ? 0 : last.by + ((angle - last.angle + 540) % 360) - 180
|
||||
last.angle = angle
|
||||
const steps = Math.trunc(last.by / dial.drag)
|
||||
if (!steps) return
|
||||
last.by -= steps * dial.drag
|
||||
move(dial.notch(latest(), steps), false)
|
||||
return
|
||||
}
|
||||
if (dial.pick) move(dial.pick(fractionAt(dx, dy), latest()), false)
|
||||
}
|
||||
|
||||
const title = showTitle(widget) ? widget.title || "" : ""
|
||||
@@ -495,7 +553,7 @@ export function Ring({
|
||||
return
|
||||
}
|
||||
event.currentTarget.setPointerCapture(event.pointerId)
|
||||
aim(event)
|
||||
aim(event, true)
|
||||
}}
|
||||
onPointerMove={(event) => {
|
||||
if (event.currentTarget.hasPointerCapture(event.pointerId)) {
|
||||
@@ -554,7 +612,15 @@ export function Ring({
|
||||
</>
|
||||
)}
|
||||
|
||||
{dial.hue ? (
|
||||
{widget.type === "counter" ? (
|
||||
<CounterCentre
|
||||
counters={counters}
|
||||
value={current as number}
|
||||
selected={selected}
|
||||
// Nothing to choose between with one, or with a ring that is locked.
|
||||
onSelect={counters.length > 1 && turns ? choose : undefined}
|
||||
/>
|
||||
) : dial.hue ? (
|
||||
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
|
||||
<span
|
||||
data-testid="face-centre"
|
||||
@@ -577,6 +643,71 @@ export function Ring({
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* A counter's digits, written as large as a reading, each over its label.
|
||||
*
|
||||
* The digits are sized as one reading (`fitSize` of all of them, colons and
|
||||
* all), and the pair is centred on the face as the clock's time and date are.
|
||||
* A press on a counter's digits picks it for the ring, and the one it turns
|
||||
* is drawn in the primary colour.
|
||||
*/
|
||||
function CounterCentre({
|
||||
counters,
|
||||
value,
|
||||
selected,
|
||||
onSelect,
|
||||
}: {
|
||||
counters: Counter[]
|
||||
value: number
|
||||
selected: number
|
||||
onSelect?: (index: number) => void
|
||||
}) {
|
||||
const written = writtenOf(counters, value)
|
||||
const fontSize = fitSize(written.join(":"), CENTRE_VALUE)
|
||||
return (
|
||||
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
|
||||
<div
|
||||
data-testid="face-centre"
|
||||
className="flex items-start leading-none tabular-nums"
|
||||
>
|
||||
{counters.map((counter, index) => {
|
||||
const marked = onSelect && index === selected
|
||||
return (
|
||||
// Two counters may share a label; position is the identity.
|
||||
<Fragment key={`counter-${index}`}>
|
||||
{index > 0 ? (
|
||||
<span aria-hidden style={{ fontSize }}>
|
||||
:
|
||||
</span>
|
||||
) : null}
|
||||
<button
|
||||
type="button"
|
||||
data-testid={`counter-${index}`}
|
||||
aria-pressed={onSelect ? index === selected : undefined}
|
||||
disabled={!onSelect}
|
||||
className={cn(
|
||||
"flex flex-col items-center gap-1",
|
||||
onSelect && "pointer-events-auto",
|
||||
marked && "text-primary",
|
||||
)}
|
||||
onClick={() => onSelect?.(index)}
|
||||
>
|
||||
<span style={{ fontSize }}>{written[index]}</span>
|
||||
<span
|
||||
className={cn(!marked && "text-muted-foreground")}
|
||||
style={{ fontSize: CENTRE_CAPTION, lineHeight: 1.35 }}
|
||||
>
|
||||
{counter.label}
|
||||
</span>
|
||||
</button>
|
||||
</Fragment>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The ring's dots: its stops, and what a slider reads beside its value.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user