// The mixer's controls: a rotary knob and a fader. Mouse-driven — drag (Shift // for fine), scroll, double-click to reset — and NOT focusable, so the deck's // keys (←/→, Space) never land on them by accident. import { useRef, type PointerEvent, type WheelEvent } from 'react' const clamp = (v: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, v)) /** Pixels of drag for the whole travel (Shift: five times finer). */ const DRAG_PX = 150 interface KnobProps { value: number min: number max: number /** Reset value (double-click); also where the arc starts on a bipolar knob. */ defaultValue: number /** The arc grows from the centre (EQ, colour) rather than from the left. */ bipolar?: boolean onChange: (v: number) => void label: string /** The tooltip, for the value it would show. */ describe: (v: number) => string color?: string size?: number /** Its MIDI action, for on-screen learning (lib/midi). */ midi?: string } /** A point on the knob's circle at angle `deg` (0 = straight up). */ function polar(r: number, deg: number, c: number): [number, number] { const rad = ((deg - 90) * Math.PI) / 180 return [c + r * Math.cos(rad), c + r * Math.sin(rad)] } function arc(r: number, from: number, to: number, c: number): string { if (Math.abs(to - from) < 0.5) return '' const [a, b] = from < to ? [from, to] : [to, from] const [x1, y1] = polar(r, a, c) const [x2, y2] = polar(r, b, c) return `M ${x1} ${y1} A ${r} ${r} 0 ${b - a > 180 ? 1 : 0} 1 ${x2} ${y2}` } export function Knob({ midi, value, min, max, defaultValue, bipolar = false, onChange, label, describe, color = 'var(--color-accent)', size = 26, }: KnobProps) { const drag = useRef<{ y: number; v: number } | null>(null) const range = max - min const angle = -135 + ((clamp(value, min, max) - min) / range) * 270 const start = bipolar ? -135 + ((defaultValue - min) / range) * 270 : -135 const c = size / 2 const stroke = size >= 36 ? 3.5 : 2.5 const r = c - stroke const [px, py] = polar(r - stroke - 2, angle, c) const onPointerDown = (e: PointerEvent) => { if (e.button !== 0) return e.preventDefault() e.currentTarget.setPointerCapture(e.pointerId) drag.current = { y: e.clientY, v: value } } const onPointerMove = (e: PointerEvent) => { const d = drag.current if (!d) return const px = e.shiftKey ? DRAG_PX * 5 : DRAG_PX onChange(clamp(d.v + ((d.y - e.clientY) / px) * range, min, max)) } const onWheel = (e: WheelEvent) => { onChange(clamp(value - (e.deltaY / (e.shiftKey ? 2000 : 400)) * range, min, max)) } return (
(drag.current = null)} onPointerCancel={() => (drag.current = null)} onDoubleClick={() => onChange(defaultValue)} onWheel={onWheel} className="cursor-ns-resize touch-none select-none" style={{ width: size, height: size }} > = 36 ? 2.5 : 2} strokeLinecap="round" />
= 36 ? 'text-[9.5px]' : 'text-[8.5px]'}`} > {label}
) } interface FaderProps { value: number min: number max: number defaultValue: number onChange: (v: number) => void orientation: 'vertical' | 'horizontal' label: string describe: (v: number) => string className?: string /** Its MIDI action, for on-screen learning (lib/midi). */ midi?: string } /** A fader: grab anywhere on it and the cap follows the pointer. */ export function Fader({ value, min, max, defaultValue, onChange, orientation, label, describe, className = '', midi }: FaderProps) { const vertical = orientation === 'vertical' const t = (clamp(value, min, max) - min) / (max - min) const set = (e: PointerEvent) => { const box = e.currentTarget.getBoundingClientRect() const f = vertical ? 1 - (e.clientY - box.top) / box.height : (e.clientX - box.left) / box.width onChange(min + clamp(f, 0, 1) * (max - min)) } return (
{ if (e.button !== 0) return e.preventDefault() e.currentTarget.setPointerCapture(e.pointerId) set(e) }} onPointerMove={(e) => e.currentTarget.hasPointerCapture(e.pointerId) && set(e)} onDoubleClick={() => onChange(defaultValue)} className={`relative touch-none select-none ${vertical ? 'w-6 cursor-ns-resize' : 'h-6 cursor-ew-resize'} ${className}`} >
) }