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 (
{thumb && }
{options.map((o, i) => {
const on = !o.disabled && o.value === value
return (
)
})}
)
}
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 (
)
}
/** 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 (
)
}
interface CardOption {
value: V
title: string
sub?: string
badges?: string[]
/** Small picture drawn above the title (e.g. a screen shape). */
art?: React.ReactNode
disabled?: boolean
}
/** A row of option cards, one picked: a radio group with larger targets. */
export function OptionCards({ options, value, onChange, label, columns }: {
options: CardOption[]
value: V | undefined
onChange: (v: V) => void
label: string
columns?: number
}) {
const name = useId()
return (
{options.map((o) => (
))}
)
}
interface PopupOption {
value: V
label: string
/** Secondary text drawn after the label in grey, e.g. a build number. */
detail?: string
disabled?: boolean
}
/**
* Pop-up button (HIG pop-up-buttons.md): shows the current choice with ⌃⌄
* and opens a menu where the current item carries a checkmark. Replaces the
* native