Round dashboards: one face at a time, a face per widget type, paged by swipe, dots and keys; slider reading; face row in the editor

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Melvin Strobl
2026-10-14 15:24:25 +02:00
co-authored by Claude Opus 5.5
parent 8b8cb5cda9
commit 84071ee106
20 changed files with 1238 additions and 604 deletions
+132 -113
View File
@@ -3,10 +3,8 @@ import { useEffect, useRef } from "react"
import type { WidgetDef } from "@/client"
import { cn } from "@/lib/utils"
import { canvasOf, type Dashboard, ringOf, widgetsOf } from "./DashboardView"
import { useBoundValue } from "./dataContext"
import { usePublish } from "./publish"
import { LockedProvider } from "./settings"
import {
colorFormatOf,
cssOf,
@@ -24,26 +22,27 @@ import {
text,
} from "./ui/core/config"
import {
CENTRE_LABEL,
CENTRE_SWATCH,
CENTRE_VALUE,
fitSize,
fractionAt,
GAP_RISE,
GAP_SIZE,
GAP_WIDTH,
RING_HANDLE,
RING_INSET,
RING_MARK,
RING_STROKE,
RING_TITLE_RISE,
RING_TITLE_SIZE,
ringArc,
ringPoint,
TITLE_RISE,
TITLE_SIZE,
TITLE_WIDTH,
useRingDraft,
} from "./ui/core/ring"
import { useAnimatedFraction } from "./ui/core/values"
/** How far apart the gap's line may run, centred: inside the face at any size. */
const GAP_WIDTH = 160
/** The centre reading, drawn when the ring is all the dashboard has. */
const CENTRE_VALUE = 72
const CENTRE_LABEL = 36
const CENTRE_SWATCH = 96
import type { Series } from "./widgets"
/** How much wheel a notch is: what one detent of a mouse wheel reports. */
const WHEEL_NOTCH = 100
@@ -173,41 +172,64 @@ function dialOf(widget: WidgetDef, live: unknown): Dial {
}
/**
* A round dashboard's edge: its ring, and what the ring says.
* A face's title: one slot, above the middle, whichever widget it is.
*
* Drawn over the whole face rather than in a cell, at canvas pixels — the
* geometry is in `ui/core/ring.ts`. Nothing for a dashboard without a ring.
* Arranging, a press on the ring picks it as a press on a tile does, and it is
* never locked, for the reason the grid is not.
* What is written there names what the middle shows — which is why a slider
* reading something beside its value hands the slot to that reading.
*/
export function RoundFace({
dashboard,
editing,
selected,
onPick,
export function FaceTitle({
size,
children,
}: {
dashboard: Dashboard
editing?: boolean
selected?: boolean
onPick?: (id: string) => void
size: number
children: React.ReactNode
}) {
const ring = ringOf(dashboard)
if (!ring) return null
const face = (
<Ring
widget={ring}
dashboard={dashboard.name}
size={canvasOf(dashboard).width}
alone={widgetsOf(dashboard).length === 1}
editing={editing}
selected={selected}
onPick={onPick && (() => onPick(ring.id))}
/>
return (
<p
className="pointer-events-none absolute truncate text-center text-muted-foreground"
style={{
left: size / 2 - TITLE_WIDTH / 2,
width: TITLE_WIDTH,
top: size / 2 - TITLE_RISE - TITLE_SIZE,
lineHeight: `${TITLE_SIZE * 2}px`,
fontSize: TITLE_SIZE,
}}
>
{children}
</p>
)
return editing ? (
face
) : (
<LockedProvider dashboard={dashboard}>{face}</LockedProvider>
}
/**
* A reading written large in the middle of a face, its unit small beside it.
*
* Sized from what is written (`fitSize`), so a word shrinks to fit where a
* number would not have to; `children` may tween towards it meanwhile.
*/
export function CentreReading({
written,
unit,
max = CENTRE_VALUE,
children,
}: {
written: string
unit?: string
max?: number
children?: React.ReactNode
}) {
return (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<span
data-testid="face-centre"
className="flex items-baseline gap-1 whitespace-nowrap leading-none tabular-nums"
style={{ fontSize: fitSize(written, max) }}
>
{children ?? written}
{unit ? (
<span className="text-[0.4em] text-muted-foreground">{unit}</span>
) : null}
</span>
</div>
)
}
@@ -218,30 +240,35 @@ export function RoundFace({
* 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
* turning stops (`useRingDraft`).
* turning stops (`useRingDraft`). Its reading is written large in the middle,
* unless a slider reads something beside it: then that is in the middle, and
* the slider's own title and value move to the gap's line.
*
* Arranging, a press on the ring picks it as a press on a tile does.
*/
function Ring({
export function Ring({
widget,
dashboard,
size,
alone,
editing,
selected,
onPick,
}: {
widget: WidgetDef
dashboard: string
size: number
/** Nothing else on the face: the reading is written large in the middle. */
alone: boolean
editing?: boolean
selected?: boolean
onPick?: () => void
}) {
const { value, send, locked } = usePublish(widget, dashboard)
const cfg = config(widget)
// A gauge reads its message rather than publishing to a target.
const message = text(config(widget).message)
const bound = useBoundValue(widget.type === "gauge" ? message : undefined)
const bound = useBoundValue(
widget.type === "gauge" ? text(cfg.message) : undefined,
)
// What a slider sets against — a room's temperature beside its setpoint.
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)
const { current, latest, move, commit } = useRingDraft(dial.value, (next) =>
send(dial.wire(next)),
@@ -295,9 +322,12 @@ function Ring({
const title = showTitle(widget) ? widget.title || "" : ""
const reading = dial.reading(current)
// Written to the slider's own decimals and in its unit: the two are read
// against each other.
const other = beside.message ? dial.reading(read?.value ?? null) : null
const handle = dial.handle && at !== null ? ringPoint(size, at) : null
const whole = ringArc(size, 0, 1)
const name = widget.title || text(config(widget).target, widget.id)
const name = widget.title || text(cfg.target, widget.id)
return (
<div
@@ -397,14 +427,6 @@ function Ring({
/>
) : null}
</g>
{selected ? (
<path
d={whole}
fill="none"
strokeWidth={RING_INSET}
className="stroke-primary opacity-15"
/>
) : null}
{turns || editing ? (
<path
d={whole}
@@ -435,62 +457,59 @@ function Ring({
) : null}
</svg>
{/* The gap at the bottom: the title, and the reading unless it is
already written large in the middle. */}
<div
className="absolute flex items-center justify-center gap-1.5 whitespace-nowrap"
style={{
left: size / 2 - GAP_WIDTH / 2,
width: GAP_WIDTH,
top: size - RING_TITLE_RISE - RING_TITLE_SIZE,
height: RING_TITLE_SIZE * 2,
fontSize: RING_TITLE_SIZE,
}}
>
{title ? (
<span className="min-w-0 truncate text-muted-foreground">
{title}
</span>
) : null}
{!alone && reading ? (
<span className="shrink-0 tabular-nums">
{reading}
{dial.unit ?? ""}
</span>
) : null}
</div>
{alone ? (
<div className="absolute inset-0 flex items-center justify-center">
{dial.hue ? (
<span
data-testid="ring-centre"
className="rounded-full border border-border"
style={{
width: CENTRE_SWATCH,
height: CENTRE_SWATCH,
background: cssOf(current as Triple),
}}
/>
) : (
<span
data-testid="ring-centre"
className="flex items-baseline gap-1 leading-none tabular-nums"
style={{
fontSize:
widget.type === "dropdown" ? CENTRE_LABEL : CENTRE_VALUE,
}}
>
{other === null ? (
title ? (
<FaceTitle size={size}>{title}</FaceTitle>
) : null
) : (
<>
{beside.label ? (
<FaceTitle size={size}>{beside.label}</FaceTitle>
) : null}
{/* The gap at the bottom: what the ring sets, by name. */}
<div
data-testid="ring-gap"
className="absolute flex items-center justify-center gap-1.5 whitespace-nowrap"
style={{
left: size / 2 - GAP_WIDTH / 2,
width: GAP_WIDTH,
top: size - GAP_RISE - GAP_SIZE,
height: GAP_SIZE * 2,
fontSize: GAP_SIZE,
}}
>
{title ? (
<span className="min-w-0 truncate text-muted-foreground">
{title}
</span>
) : null}
<span className="shrink-0 tabular-nums">
{reading}
{dial.unit ? (
<span className="text-[0.4em] text-muted-foreground">
{dial.unit}
</span>
) : null}
{dial.unit ?? ""}
</span>
)}
</div>
</>
)}
{dial.hue ? (
<div className="pointer-events-none absolute inset-0 flex items-center justify-center">
<span
data-testid="face-centre"
className="rounded-full border border-border"
style={{
width: CENTRE_SWATCH,
height: CENTRE_SWATCH,
background: cssOf(current as Triple),
}}
/>
</div>
) : null}
) : (
<CentreReading
written={other ?? reading}
unit={dial.unit}
max={widget.type === "dropdown" ? CENTRE_LABEL : CENTRE_VALUE}
/>
)}
</div>
)
}