Round faces: a bar as concentric arcs with a legend in the middle; larger title, gap line and face text; Read-only in the ring's gap; a round dashboard unpadded on a phone; reduced-motion ring assertion

This commit is contained in:
Melvin Strobl
2026-10-14 16:52:46 +02:00
parent bae4b15505
commit c294789d85
9 changed files with 308 additions and 33 deletions
+179 -6
View File
@@ -1,40 +1,66 @@
import { Power } from "lucide-react"
import { Lock, Power } from "lucide-react"
import {
AnimatePresence,
motion,
useIsPresent,
useTransform,
type Variants,
} from "motion/react"
import { useEffect, useRef, useState } from "react"
import { ErrorBoundary } from "react-error-boundary"
import type { WidgetDef } from "@/client"
import { slotsFor } from "@/components/Common/UplotChart"
import { fadeIn } from "@/lib/motion"
import { useShortcuts } from "@/lib/shortcuts"
import { useClock } from "./ClockWidget"
import type { Dashboard } from "./DashboardView"
import { useBoundValue } from "./dataContext"
import { useBoundValue, useBoundValues } from "./dataContext"
import { resolveIcon } from "./icons"
import { usePublish } from "./publish"
import { CentreReading, FaceTitle, Ring } from "./RingWidget"
import { CentreReading, FaceTitle, hasGapLine, Ring } from "./RingWidget"
import { useLocked, usePalette } from "./settings"
import { useUi } from "./ui"
import { config, format, num, scrolls, showTitle, text } from "./ui/core/config"
import { arcPath } from "./ui/core/arc"
import {
config,
format,
num,
rowsOf,
scrolls,
showTitle,
text,
} from "./ui/core/config"
import { TESTID } from "./ui/core/contract"
import { useLook } from "./ui/core/look"
import { LOOK } from "./ui/core/motion"
import {
ANNOUNCE_MS,
BODY_HEIGHT,
BODY_WIDTH,
barArcs,
barLegend,
CENTRE_VALUE,
chordWidth,
DOT,
DOT_PITCH,
DOTS_RISE,
DOTS_WIDTH,
fitSize,
GAP_RISE,
GAP_SIZE,
PRESS,
RING_START,
RING_SWEEP,
SWIPE,
TITLE_RISE,
TITLE_SIZE,
TITLE_WIDTH,
} from "./ui/core/ring"
import { useAnimatedNumber } from "./ui/core/values"
import {
barReadings,
useAnimatedFraction,
useAnimatedNumber,
} from "./ui/core/values"
import {
WIDGET_LABELS,
WidgetBody,
@@ -58,6 +84,7 @@ export const RING_TYPES = new Set<WidgetKind>([
*/
export const ROUND_TYPES = new Set<WidgetKind>([
...RING_TYPES,
"bar",
"stat",
"switch",
"button",
@@ -198,6 +225,7 @@ export function RoundView({
const swiped = useRef(false)
// A dashboard on its way out keeps its keys to itself.
const present = useIsPresent()
const locked = useLocked()
const [announce, setAnnounce] = useState(
() =>
Boolean(onDashboard) &&
@@ -312,6 +340,23 @@ export function RoundView({
onPick={onPick && (() => onPick(widget.id))}
/>
</ErrorBoundary>
{/* In the ring's gap, where a knob's bezel does not reach; a slider's
gap line holds it already, led by the lock. */}
{locked && !editing && !hasGapLine(widget) ? (
<div
aria-live="polite"
data-testid={TESTID.locked}
className="pointer-events-none absolute inset-x-0 flex items-center justify-center gap-1.5 text-muted-foreground"
style={{
top: size - GAP_RISE - GAP_SIZE,
height: GAP_SIZE * 2,
fontSize: GAP_SIZE,
}}
>
<Lock style={{ width: GAP_SIZE, height: GAP_SIZE }} aria-hidden />
Read-only
</div>
) : null}
</motion.div>
</AnimatePresence>
@@ -386,6 +431,9 @@ function Face({
onPick?: () => void
}) {
const issue = widgetIssue(widget)
if (widget.type === "bar" && !issue) {
return <BarFace widget={widget} size={size} />
}
if (RING_TYPES.has(widget.type) && !issue) {
return (
<Ring
@@ -499,6 +547,131 @@ function StatFace({ widget }: { widget: WidgetDef }) {
)
}
/**
* A bar as rings: an arc per row, the first outermost, each on its own scale
* and in the dashboard's data colour for its row, as the tile's bars are. The
* middle is their legend — a dot of the row's colour, its label and its value
* — which is what says which arc is which.
*/
function BarFace({ widget, size }: { widget: WidgetDef; size: number }) {
const rows = rowsOf(widget).filter((row) => row.message)
const live = useBoundValues(rows.map((row) => row.message as string))
const readings = barReadings(widget, live, slotsFor(usePalette()))
const { stroke, radii, inner } = barArcs(size, readings.length)
const legend = barLegend(readings.length)
const height = readings.length * legend.line
// As wide as its widest line, and no wider than the inner disc is at the
// legend's top and bottom lines.
const width = Math.min(BODY_WIDTH, chordWidth(inner, height / 2, 16))
const title = showTitle(widget) ? widget.title : ""
return (
<>
<svg
width={size}
height={size}
className="pointer-events-none absolute inset-0"
aria-hidden="true"
>
{readings.map((row, index) => (
<BarArc
// Two rows may read the same message; position is the identity.
key={`row-${index}`}
fraction={row.fraction}
color={row.color}
radius={radii[index]}
stroke={stroke}
size={size}
/>
))}
</svg>
{title ? (
<FaceTitle
size={size}
// Clear of the inner arcs, which come in this far with many rows.
width={Math.min(
TITLE_WIDTH,
chordWidth(inner, TITLE_RISE + TITLE_SIZE / 2, 8),
)}
>
{title}
</FaceTitle>
) : null}
<div
className="pointer-events-none absolute grid items-center gap-x-2 tabular-nums"
style={{
left: size / 2,
translate: "-50%",
width: "max-content",
maxWidth: width,
top: (size - height) / 2,
gridTemplateColumns: "auto minmax(0, 1fr) auto",
gridAutoRows: legend.line,
fontSize: legend.size,
}}
>
{readings.map((row, index) => (
<div
key={`row-${index}`}
role="img"
aria-label={`${row.label} ${format(row.value, row.precision)}${row.unit ?? ""}`}
data-testid="bar-face-row"
className="contents"
>
<span
className="size-2 rounded-full"
style={{ background: row.color }}
/>
<span className="truncate text-muted-foreground">{row.label}</span>
<span className="whitespace-nowrap text-right">
{format(row.value, row.precision)}
{row.unit ?? ""}
</span>
</div>
))}
</div>
</>
)
}
/** One row's arc: its track, and its reading eased along it. */
function BarArc({
fraction,
color,
radius,
stroke,
size,
}: {
fraction: number
color: string
radius: number
stroke: number
size: number
}) {
const along = useAnimatedFraction(fraction)
// None at the minimum, where the round caps would still paint a dot.
const shown = useTransform(along, (at) => (at > 0 ? 1 : 0))
const d = arcPath(RING_START, RING_START + RING_SWEEP, radius, size / 2)
return (
<>
<path
d={d}
fill="none"
strokeWidth={stroke}
strokeLinecap="round"
className="dui-ring-track"
/>
<motion.path
d={d}
fill="none"
stroke={color}
strokeWidth={stroke}
strokeLinecap="round"
style={{ pathLength: along, opacity: shown }}
/>
</>
)
}
/** The time as large as a reading, shrunk to fit as one is, over the date. */
function ClockFace() {
const { time, date } = useClock()