import { ChevronRight, RotateCcw } from "lucide-react"; import { type KeyboardEvent, type PointerEvent, useCallback, useEffect, useId, useRef, useState, } from "react"; import { Tooltip, TooltipProvider } from "@/components/ui/tooltip"; import { useScopedT } from "@/contexts/I18nContext"; import type { EditorSettingsPatch, EditorSettingsSnapshot, } from "@/lib/ai-edition/store/editorSettings"; import { describeWebcamShadow, WEBCAM_SHADOW_BOUNDS, WEBCAM_SHADOW_COLORS, WEBCAM_SHADOW_PRESETS, type WebcamShadowPresetId, type WebcamShadowValues, webcamShadowCss, webcamShadowPreset, webcamShadowPresetPatch, } from "@/lib/webcamShadowStyles"; import css from "./CameraShadowControls.module.css"; import { ColorField } from "./ColorField"; import styles from "./NewEditorShell.module.css"; import { ChoiceRow, SliderCell } from "./RightPanes"; /** Preview scales: what a 1080 px shadow becomes in the tiles and in the position pad. */ const TILE_SCALE = 0.11; const PAD_SCALE = 0.42; /** Within this many pixels of an axis, the position snaps onto it. */ const SNAP_PX = 3; const OPEN_KEY = "os-camera-shadow-customize-open"; type ShadowSettings = Pick< EditorSettingsSnapshot, | "webcamShadowPreset" | "webcamShadowOffsetX" | "webcamShadowOffsetY" | "webcamShadowBlur" | "webcamShadowSpread" | "webcamShadowOpacity" | "webcamShadowColor" >; function valuesOf(settings: ShadowSettings): WebcamShadowValues { return { offsetX: settings.webcamShadowOffsetX, offsetY: settings.webcamShadowOffsetY, blur: settings.webcamShadowBlur, spread: settings.webcamShadowSpread, opacity: settings.webcamShadowOpacity, color: settings.webcamShadowColor, }; } function readOpen(): boolean { try { return window.localStorage.getItem(OPEN_KEY) === "1"; } catch { return false; } } function writeOpen(open: boolean) { try { window.localStorage.setItem(OPEN_KEY, open ? "1" : "0"); } catch { // A per-viewer convenience: without storage the section just starts closed. } } /** * The picture-in-picture camera's shadow: six presets, then a "Customize" section that tunes * position, blur, spread, opacity and colour from the one picked. An edited preset stays * pressed and says so, with a way back to it; a shadow from before the presets that matches * none of them presses none. * * Every drag writes live and records one undo step on release (`setLive` + `commit`); a preset * or a colour preset is one `set`. */ export function CameraShadowControls({ settings, disabled, set, setLive, commit, }: { settings: ShadowSettings; disabled: boolean; set: (patch: EditorSettingsPatch) => unknown; setLive: (patch: EditorSettingsPatch) => void; commit: () => unknown; }) { const ts = useScopedT("settings"); const values = valuesOf(settings); const { preset, edited } = describeWebcamShadow(values, settings.webcamShadowPreset); const [open, setOpen] = useState(readOpen); const bodyId = useId(); const toggle = () => { setOpen((was) => { writeOpen(!was); return !was; }); }; /** The patch an edit writes. It keeps the preset it was tuned from, and an edit made while * there is no shadow at all starts from the default one: moving the blur of an invisible * shadow would show nothing. */ const editPatch = (change: Partial): EditorSettingsPatch => { const startsFromNothing = values.opacity <= 0 && change.opacity === undefined; const base: WebcamShadowValues = startsFromNothing ? webcamShadowPreset("soft") : values; const next = { ...base, ...change }; return { webcamShadowPreset: startsFromNothing ? "soft" : (preset ?? null), webcamShadowOffsetX: next.offsetX, webcamShadowOffsetY: next.offsetY, webcamShadowBlur: next.blur, webcamShadowSpread: next.spread, webcamShadowOpacity: next.opacity, webcamShadowColor: next.color, }; }; const live = (change: Partial) => setLive(editPatch(change)); const label = (id: WebcamShadowPresetId) => ts(`layout.cameraShadow.${id}`); const base = preset ? webcamShadowPreset(preset) : null; return (
{ts("effects.shadow")}
label={ts("effects.shadow")} display="both" tiles columns={3} options={WEBCAM_SHADOW_PRESETS.map((p) => ({ value: p.id, label: label(p.id), title: ts(`layout.cameraShadow.${p.id}Tip`), icon: , }))} value={preset ?? ""} disabled={disabled} onChange={(id) => { if (id !== "") void set(webcamShadowPresetPatch(id)); }} />
{edited && base ? ( ) : null}
live({ offsetX, offsetY })} onCommit={() => void commit()} onReset={(offsetX, offsetY) => void set(editPatch({ offsetX, offsetY }))} />
live({ blur })} onCommit={() => void commit()} /> live({ spread })} onCommit={() => void commit()} /> live({ opacity: pct / 100 })} onCommit={() => void commit()} />
{ts("layout.cameraShadow.color")} { if (/^#[0-9a-f]{6}$/i.test(hex)) live({ color: hex.toLowerCase() }); }} onCommit={() => void commit()} />
); } /** A preset's picture: a camera on a small stage, casting that preset's shadow. */ function ShadowTile({ values, edited }: { values: WebcamShadowValues; edited: boolean }) { return (