import { convertFileSrc, isTauri } from "@tauri-apps/api/core"; import { listen } from "@tauri-apps/api/event"; import { Check, Gauge, LoaderCircle, Mic, MicOff, ScanLine, Square, X } from "lucide-react"; import { useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; import { useI18n } from "../i18n"; import { api } from "../lib/api"; import { messageFrom, normalizeSelection } from "../lib/utils"; import { FEEDBACK } from "../lib/feedback"; import type { CaptureSession, RecordingCapabilities } from "../types"; interface Point { x: number; y: number; } /** Choices the frame-rate chip cycles through, low to high. */ const FRAME_RATES = [15, 24, 30]; export function CaptureOverlay() { const { t } = useI18n(); const [session, setSession] = useState(null); const [start, setStart] = useState(null); const [end, setEnd] = useState(null); const [dragging, setDragging] = useState(false); const [busy, setBusy] = useState(false); const [status, setStatus] = useState(null); const [audio, setAudio] = useState(false); // Which recording switches this platform actually implements. Offering a control the // backend ignores would promise something the file cannot deliver, so the toolbar // only renders what the platform reports. const [capabilities, setCapabilities] = useState({ audio: true, fps: false, }); // Frame rate is only sent where the capture backend accepts one, so this is a real // request rather than a number that gets dropped on the floor. const [fps, setFps] = useState(30); const stageRef = useRef(null); const toolbarRef = useRef(null); const [toolbarWidth, setToolbarWidth] = useState(260); useEffect(() => { if (!isTauri()) { setSession({ mode: "screenshot", source: { path: "/demo-preview.svg", width: 1200, height: 800, monitorX: 0, monitorY: 0, scaleFactor: 1, }, }); return; } void api.getCaptureSession().then((next) => { if (next) setSession(next); }); void api.recordingCapabilities().then(setCapabilities).catch(() => undefined); }, []); useEffect(() => { if (!isTauri()) return; let unlisten: (() => void) | undefined; void listen("capture://ready", (event) => { setSession(event.payload); setStart(null); setEnd(null); setDragging(false); setBusy(false); setStatus(null); setAudio(false); }).then((stop) => { unlisten = stop; }); return () => unlisten?.(); }, []); const selection = useMemo(() => { if (!start || !end || !stageRef.current) return null; const bounds = stageRef.current.getBoundingClientRect(); return normalizeSelection(start.x, start.y, end.x, end.y, bounds.width, bounds.height); }, [start, end]); async function cancel() { if (busy) return; if (!isTauri()) { setStatus(t("cancel")); return; } await api.cancelCapture().catch(() => undefined); } async function confirm() { if (busy || !selection || selection.width < 8 || selection.height < 8) { setStatus(t("dragSelect")); window.setTimeout(() => setStatus(null), FEEDBACK.hint); return; } if (!isTauri()) { setBusy(true); setStatus(`${t("confirm")} ${Math.round(selection.width)} × ${Math.round(selection.height)}`); window.setTimeout(() => { setBusy(false); setStatus(null); }, 1200); return; } setBusy(true); setStatus(t("processing")); try { if (session?.mode === "recording") { // Only the switches this platform honours are sent, so nothing in the // toolbar can promise a recording option the backend would drop. await api.startRecording(selection.normalized, { audio: capabilities.audio ? audio : false, ...(capabilities.fps ? { fps } : {}), }); } else { await api.captureCrop(selection.normalized); } } catch (error) { setBusy(false); setStatus(t(messageFrom(error))); window.setTimeout(() => setStatus(null), FEEDBACK.error); } } useEffect(() => { const onKeyDown = (event: KeyboardEvent) => { if (event.key === "Escape") void cancel(); if (event.key === "Enter") void confirm(); }; window.addEventListener("keydown", onKeyDown); return () => window.removeEventListener("keydown", onKeyDown); }); useLayoutEffect(() => { const node = toolbarRef.current; if (!node) return; // Keep the measured width so the clamping below centres the toolbar wherever the // selection sits, without hard-coding how many chips the platform shows. setToolbarWidth((current) => (current === node.offsetWidth ? current : node.offsetWidth)); }); if (!session) { return
; } const imageUrl = isTauri() ? convertFileSrc(session.source.path) : session.source.path; const selectionStyle = selection ? { left: selection.left, top: selection.top, width: selection.width, height: selection.height, } : undefined; const toolbarTop = selection ? Math.min( Math.max(12, selection.top + selection.height + 14), Math.max(12, window.innerHeight - 68), ) : 0; const toolbarLeft = selection ? Math.min( Math.max(12, selection.left + selection.width / 2 - toolbarWidth / 2), Math.max(12, window.innerWidth - toolbarWidth - 12), ) : 0; return (
{ if (busy || event.button !== 0 || event.target !== event.currentTarget) return; const point = { x: event.clientX, y: event.clientY }; setStart(point); setEnd(point); setDragging(true); event.currentTarget.setPointerCapture(event.pointerId); }} onPointerMove={(event) => { if (!dragging) return; setEnd({ x: event.clientX, y: event.clientY }); }} onPointerUp={(event) => { if (!dragging) return; setEnd({ x: event.clientX, y: event.clientY }); setDragging(false); if (event.currentTarget.hasPointerCapture(event.pointerId)) { event.currentTarget.releasePointerCapture(event.pointerId); } }} onPointerCancel={() => setDragging(false)} > event.preventDefault()} />
{selection ? ( <>
{Math.max(0, Math.round(selection.width))} × {Math.max(0, Math.round(selection.height))}
event.stopPropagation()} onClick={(event) => event.stopPropagation()} > {session.mode === "recording" ? ( <> {capabilities.audio ? ( ) : null} {capabilities.fps ? ( ) : null} ) : ( )}
) : (
{session.mode === "recording" ? : }
{t("dragSelect")}
ESC
)} {busy ? (
{status || t("processing")}
) : status ? (
{status}
) : null}
); }