Files
xenia-pos-local/manager_dashboard/src/pages/signage/CalibrateDialog.jsx
T
bonaminandClaude Opus 5.5 cd8218689c feat(signage): widget settings regrouped (main / Text and colours / More), alignment that lines up multi-line widgets, multi-line texts, gallery title, logos larger than the slide; calibration past the recognised resolution and shown the first time
Owner review 2026-11-23 of every widget:
- A widget card is its main settings (the row and what needs room under it: pictures, a gallery's
  pictures or folder, weather details and place, a countdown's start and end, reviews' stars and
  author, a scrolling text's speed, a shape's colours and border, QR caption), then "Text and
  colours" (new LookFields.jsx: text colour, plate, font, style, size, weight, highlight, shadow,
  QR colours; a logo's / gallery's border), then More settings (opacity, rotation, animation, fit,
  typing, blink, QR logo; shadow / border only where there is no Text and colours).
- Date format / separator and clock 12/24 / separator are main settings.
- Alignment only where it lines something up; the TV now aligns the lines of a text, the hours
  (centre: days and times meet in the middle), contact rows, Wi-Fi (code left / right / above),
  the weather's column / card / list / hourly, reviews (stars too), a countdown, a QR caption.
- Text and message: a 2-line textarea in its own row; Enter makes new lines (the TV sizes by the
  longest line and the number of lines).
- Gallery: a title above or below the pictures (title_position).
- A widget's box may reach 400 % of the slide (a logo zoomed in past the slide's edges saved again).
- "Auto" stays "Auto" in Greek; scrolling text Left / Right; reviews' stars choice no longer clipped;
  countdown "When it ends" hint as an info tip (fields level).

Calibration: an edge may go past the recognised window (down to -2000 px); the dialog's rows are
centred, "Each click moves" sits above its choice, the window size is labelled "Recognised
resolution" with what it means; the TV asks for its screen answer on every calibrate event and
while calibrating (the outline shows the first time), the dialog sends "on" again after 1.5 s.
NumberInput takes a leading minus when min < 0. E2E: waits for the Add-a-slide dialog to go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 05:40:04 +02:00

136 lines
8.0 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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 => (
<Field label={t(`signage.calibrate.edges.${e}`)} className="w-36">
<NumberInput size="sm" value={area[e]} min={-OUT} max={4000} step={step} suffix="px" onChange={v => setEdge(e, v)} data-testid={`signage-calibrate-${e}`} />
</Field>
)
return (
<Dialog open size="lg" onClose={onClose} title={t('signage.calibrate.title', { name: screen.name })} description={t('signage.calibrate.intro')}
footer={<>
<Button variant="secondary" onClick={() => change({ left: 0, top: 0, right: 0, bottom: 0 })} data-testid="signage-calibrate-reset">{t('signage.calibrate.reset')}</Button>
<Button onClick={onClose} data-testid="signage-calibrate-done">{t('signage.calibrate.done')}</Button>
</>}>
<div className="grid gap-5 justify-items-center text-center" data-testid="signage-calibrate-dialog">
<div className="grid gap-1 justify-items-center">
<span className="text-caption font-medium text-ink-3 uppercase tracking-wide">{t('signage.calibrate.recognised')}</span>
<p className="text-body-lg font-semibold text-ink tabular-nums" data-testid="signage-calibrate-window">
{win ? t('signage.calibrate.window', { w: win.w, h: win.h, shape: shapeName(win.w, win.h) }) : t('signage.calibrate.waiting')}
</p>
{win && <p className="text-caption text-ink-3 max-w-md">{t('signage.calibrate.recognisedHint')}</p>}
</div>
<div className="grid gap-2 justify-items-center">
<span className="text-label font-medium text-ink">{t('signage.calibrate.presets')}</span>
<div className="flex flex-wrap justify-center gap-1.5">
{PRESETS.map(p => (
<Button key={p} size="sm" variant="secondary" disabled={!win} onClick={() => change(presetInsets(win, p))} data-preset={p}
icon={p === 'full' ? undefined : <ShapeMark shape={p} size={16} />}>
{p === 'full' ? t('signage.calibrate.full') : p}
</Button>
))}
</div>
</div>
<div className="grid gap-2 justify-items-center">
<span className="text-label font-medium text-ink">{t('signage.calibrate.step')}</span>
<Segmented value={step} onChange={setStep} options={[1, 10, 50].map(n => ({ value: n, label: `${n} px` }))} data-testid="signage-calibrate-step" />
</div>
<div className="grid justify-items-center gap-2">
{edge('top')}
<div className="flex items-center gap-3 flex-wrap justify-center">
{edge('left')}
<div className="relative rounded-control bg-surface-2" style={{ width: W, height: H }} aria-hidden="true" data-testid="signage-calibrate-diagram">
{pic && bounds && <>
<div className="absolute border-2 border-accent bg-accent-soft grid place-items-center text-caption text-accent-ink tabular-nums"
style={{ left: (pic.left - bounds.x) * k, top: (pic.top - bounds.y) * k, width: pic.width * k, height: pic.height * k }}>
{Math.round(pic.width)} × {Math.round(pic.height)}
</div>
<div className="absolute border border-dashed border-line-strong rounded-[2px] pointer-events-none"
style={{ left: -bounds.x * k, top: -bounds.y * k, width: win.w * k, height: win.h * k }} />
</>}
</div>
{edge('right')}
</div>
{edge('bottom')}
</div>
<p className="text-label text-ink-2 text-center" data-testid="signage-calibrate-picture">
{pic ? t('signage.calibrate.picture', { w: Math.round(pic.width), h: Math.round(pic.height), shape: shapeName(pic.width, pic.height) }) : ''}
</p>
<p className="text-caption text-ink-3 max-w-md">{t('signage.calibrate.edgesHint')}</p>
{EDGES.some(e => area[e] > 0) && <p className="text-caption text-ink-3">{t('signage.calibrate.outside')}</p>}
{EDGES.some(e => area[e] < 0) && <p className="text-caption text-warning-ink" data-testid="signage-calibrate-past">{t('signage.calibrate.past')}</p>}
</div>
</Dialog>
)
}