// Calibrating a screen's picture (6R.13, owner 2026-11-22). While this is open the TV outlines its // picture area with four arrows pointing out and shows its size; each edge moves in or out here // pixel by pixel (or in steps of 10 / 50 px), or a preset sets the largest box of a common shape in // the middle of the TV's window (it reports its size). Every change reaches the TV at once; closing // ends the calibration (the TV also stops by itself after 15 min). // Owner 2026-11-23: every row centred; "Each click moves" above its choice; the window size is // marked as the one the TV reported (recognised), and an edge may go past it (below 0 px) when the // TV's real picture is larger; "on" is sent again a moment after opening (the TV shows the outline // the first time). import { useCallback, useEffect, useRef, useState } from 'react' import toast from 'react-hot-toast' import { errorMessage } from '@xenia/api' import { useTranslation } from '@xenia/i18n' import { Button, Dialog, Field, NumberInput, Segmented } from '@xenia/ui' import { pictureArea, presetInsets, shapeName } from '@xenia/signage' import * as api from './signageApi' import { ShapeMark } from './ShapeField' const EDGES = ['top', 'right', 'bottom', 'left'] const PRESETS = ['full', '16:9', '16:10', '21:9', '32:9', '4:3', '9:16'] const SAVE_MS = 200 const OUT = 2000 // px an edge may reach past the recognised window export default function CalibrateDialog({ screen, onClose, onSaved }) { const { t } = useTranslation('manager') const [area, setArea] = useState(screen.area || { left: 0, top: 0, right: 0, bottom: 0 }) const [viewport, setViewport] = useState(screen.viewport || null) const [step, setStep] = useState(1) const timer = useRef(null) const latest = useRef(area) // on while open; off when closed (a moment later, so a remount, as React's strict mode does in // development, never sends "off" after "on") const alive = useRef(0) useEffect(() => { const mine = ++alive.current api.calibrateScreen(screen.id, true).catch(err => toast.error(errorMessage(err, t))) const again = setTimeout(() => { if (alive.current === mine) api.calibrateScreen(screen.id, true).catch(() => {}) }, 1500) return () => { clearTimeout(again); setTimeout(() => { if (alive.current === mine) api.calibrateScreen(screen.id, false).catch(() => {}) }, 300) } }, [screen.id]) // eslint-disable-line react-hooks/exhaustive-deps // the TV's window size (it reports it when it starts and when it changes) useEffect(() => { const id = setInterval(() => { api.listScreens().then(list => setViewport(list.find(s => s.id === screen.id)?.viewport || null)).catch(() => {}) }, 2000) return () => clearInterval(id) }, [screen.id]) const send = useCallback(() => { const a = latest.current api.updateScreen(screen.id, { area_left: a.left, area_top: a.top, area_right: a.right, area_bottom: a.bottom }) .then(onSaved).catch(err => toast.error(errorMessage(err, t))) }, [screen.id, onSaved, t]) const change = next => { latest.current = next setArea(next) clearTimeout(timer.current) timer.current = setTimeout(send, SAVE_MS) } useEffect(() => () => clearTimeout(timer.current), []) const win = viewport ? { w: viewport.width, h: viewport.height } : null const pic = win ? pictureArea(win, area) : null const setEdge = (edge, v) => change({ ...area, [edge]: Math.max(-OUT, Math.min(4000, v ?? 0)) }) // the diagram: the recognised window (dashed) and the picture, to scale; a picture reaching past // the window makes the drawing as large as both const bounds = win && pic ? { x: Math.min(0, pic.left), y: Math.min(0, pic.top), r: Math.max(win.w, pic.left + pic.width), b: Math.max(win.h, pic.top + pic.height), } : null const W = 320 const H = bounds ? Math.round(W * (bounds.b - bounds.y) / (bounds.r - bounds.x)) : 180 const k = bounds ? W / (bounds.r - bounds.x) : 0 const edge = e => ( setEdge(e, v)} data-testid={`signage-calibrate-${e}`} /> ) return ( }>
{t('signage.calibrate.recognised')}

{win ? t('signage.calibrate.window', { w: win.w, h: win.h, shape: shapeName(win.w, win.h) }) : t('signage.calibrate.waiting')}

{win &&

{t('signage.calibrate.recognisedHint')}

}
{t('signage.calibrate.presets')}
{PRESETS.map(p => ( ))}
{t('signage.calibrate.step')} ({ value: n, label: `${n} px` }))} data-testid="signage-calibrate-step" />
{edge('top')}
{edge('left')} {edge('bottom')}

{pic ? t('signage.calibrate.picture', { w: Math.round(pic.width), h: Math.round(pic.height), shape: shapeName(pic.width, pic.height) }) : ''}

{t('signage.calibrate.edgesHint')}

{EDGES.some(e => area[e] > 0) &&

{t('signage.calibrate.outside')}

} {EDGES.some(e => area[e] < 0) &&

{t('signage.calibrate.past')}

}
) }