import * as Menu from '@radix-ui/react-dropdown-menu' import { Check, ChevronsUpDown, CircleAlert, Lock, RotateCcw } from 'lucide-react' import React, { useEffect, useId, useLayoutEffect, useRef, useState } from 'react' import { IconButton } from './controls' // Settings page row shell and shared form controls (see docs/design-guidelines.md). /** A labelled block of rows, like a grouped list in macOS System Settings. */ export const SetGroup: React.FC<{ title?: string; note?: React.ReactNode; children: React.ReactNode }> = ({ title, note, children }) => (
{title &&

{title}

}
{children}
{note &&

{note}

}
) interface RowProps { title: string desc?: React.ReactNode /** Control drawn on the right of the title. */ control?: React.ReactNode /** Control drawn under the title at full width (sliders, cards). */ block?: React.ReactNode /** The value differs from what the VM has now. */ dirty?: boolean onReset?: () => void resetLabel?: string /** The row can't act right now; `reason` says why. */ disabled?: boolean reason?: string error?: string /** Anchor so a validation error can scroll its row into view. */ field?: string } /** One setting: title, one line of description, a control, and its state marks. */ export const SetRow: React.FC = ({ title, desc, control, block, dirty, onReset, resetLabel, disabled, reason, error, field }) => { const titleId = useId() return (
{title} {dirty &&
{desc &&
{desc}
} {disabled && reason && (
)}
{(control || (dirty && onReset)) && (
{dirty && onReset && !disabled && ( } onClick={onReset} className="srow-reset" tipSide="top" /> )} {control &&
{control}
}
)}
{block &&
{block}
} {error &&
}
) } /** Read-only row: a label and its value. */ export const SetValue: React.FC<{ title: string; value: React.ReactNode; mono?: boolean }> = ({ title, value, mono }) => (
{title}
{value}
) export const Switch: React.FC<{ checked: boolean; onChange: (v: boolean) => void; label: string; disabled?: boolean }> = ({ checked, onChange, label, disabled }) => ( ) interface SegOption { value: V label: string disabled?: boolean } /** * Segmented control with a sliding highlight. A radio group under the hood: * arrow keys move the choice, Tab leaves the group. */ export function Segmented({ options, value, onChange, label, wide }: { options: SegOption[] value: V | undefined onChange: (v: V) => void label: string /** Stretch to the row's width with equal segments. */ wide?: boolean }) { const box = useRef(null) const [thumb, setThumb] = useState<{ x: number; w: number; value: V } | null>(null) const measure = () => { const el = box.current?.querySelector('[aria-checked="true"]') setThumb((prev) => { // Options disabled while something is saving keep the highlight in place. if (!el || value === undefined) return prev?.value === value ? prev : null if (prev?.value === value && prev.x === el.offsetLeft && prev.w === el.offsetWidth) return prev return { x: el.offsetLeft, w: el.offsetWidth, value } }) } const latestMeasure = useRef(measure) latestMeasure.current = measure const optionsSig = options.map((o) => `${String(o.value)}\x1f${o.label}\x1f${!!o.disabled}`).join('\x1e') const docDir = typeof document !== 'undefined' ? document.documentElement.dir : '' // eslint-disable-next-line react-hooks/exhaustive-deps useLayoutEffect(measure, [value, optionsSig, wide, docDir]) useEffect(() => { if (!box.current || typeof ResizeObserver === 'undefined') return let raf = 0 const ro = new ResizeObserver(() => { if (raf) cancelAnimationFrame(raf) raf = requestAnimationFrame(() => { raf = 0 latestMeasure.current() }) }) ro.observe(box.current) return () => { if (raf) cancelAnimationFrame(raf) ro.disconnect() } }, []) const onKey = (e: React.KeyboardEvent) => { if (e.key !== 'ArrowRight' && e.key !== 'ArrowLeft' && e.key !== 'ArrowDown' && e.key !== 'ArrowUp') return e.preventDefault() const enabled = options.filter((o) => !o.disabled) const i = enabled.findIndex((o) => o.value === value) const isRTL = box.current ? getComputedStyle(box.current).direction === 'rtl' : false const forward = e.key === 'ArrowDown' || (e.key === 'ArrowRight' && !isRTL) || (e.key === 'ArrowLeft' && isRTL) const delta = forward ? 1 : -1 const next = enabled[(i + delta + enabled.length) % enabled.length] if (next) { onChange(next.value) requestAnimationFrame(() => box.current?.querySelector('[aria-checked="true"]')?.focus()) } } const firstEnabled = options.findIndex((o) => !o.disabled) const hasActive = options.some((o) => !o.disabled && o.value === value) return (
{thumb &&
) } interface NumberProps { value: number min: number max: number step?: number unit?: string label: string onChange: (v: number) => void /** Width in characters; the default fits five digits. */ size?: number } /** * Number field with a unit suffix and no spinner. It keeps what is typed so * far and commits a whole, clamped number on Enter or blur; ↑/↓ step it. */ export const NumberField: React.FC = ({ value, min, max, step = 1, unit, label, onChange, size = 5 }) => { const [text, setText] = useState(String(value)) useEffect(() => { setText(String(value)) }, [value]) const clamp = (v: number) => Math.min(max, Math.max(min, Math.round(v / step) * step)) const commit = () => { const n = Number.parseFloat(text) if (!Number.isFinite(n)) { setText(String(value)); return } const c = clamp(n) setText(String(c)) if (c !== value) onChange(c) } return (
setText(e.target.value.replace(/[^\d.]/g, ''))} onBlur={commit} onKeyDown={(e) => { if (e.key === 'Enter') { e.preventDefault(); commit() } if (e.key === 'ArrowUp' || e.key === 'ArrowDown') { e.preventDefault() onChange(clamp(value + (e.key === 'ArrowUp' ? step : -step))) } }} /> {unit && {unit}}
) } /** Drop marks that would sit closer than `gap` percent to the one before. */ function thinMarks(marks: RangeMark[], min: number, max: number, gap = 9): RangeMark[] { const span = Math.max(max - min, 1) const out: RangeMark[] = [] let last = -Infinity for (const m of marks) { if (m.value < min || m.value > max) continue const p = ((m.value - min) / span) * 100 if (p - last < gap) continue out.push(m) last = p } return out } interface RangeMark { value: number label: string } interface RangeProps { value: number min: number max: number step?: number unit: string label: string onChange: (v: number) => void /** Clickable labels under the track. */ marks?: RangeMark[] /** Highlighted "recommended" span of the track. */ recommend?: [number, number] /** From here to the end the track turns amber. */ warnFrom?: number } /** * Slider and number field bound to one value. The slider and the marks write * straight away; the field commits on Enter or blur (NumberField). */ export const RangeField: React.FC = ({ value, min, max, step = 1, unit, label, onChange, marks, recommend, warnFrom }) => { const span = Math.max(max - min, 1) const pct = (v: number) => `${((Math.min(max, Math.max(min, v)) - min) / span) * 100}%` const shown = marks ? thinMarks(marks, min, max) : [] const over = warnFrom !== undefined && value >= warnFrom return (