Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

42.0 KB
Raw Blame
import "@radix-ui/colors/amber.css";
import "@radix-ui/colors/amber-dark.css";
import { createTimer } from "@solid-primitives/timer";
import { createMutation } from "@tanstack/solid-query";
import { LogicalPosition } from "@tauri-apps/api/dpi";
import {
	CheckMenuItem,
	Menu,
	MenuItem,
	PredefinedMenuItem,
} from "@tauri-apps/api/menu";
import { getCurrentWindow } from "@tauri-apps/api/window";
import * as dialog from "@tauri-apps/plugin-dialog";
import { type as ostype } from "@tauri-apps/plugin-os";
import { cx } from "cva";
import {
	type ComponentProps,
	createEffect,
	createMemo,
	createSignal,
	createUniqueId,
	For,
	type JSX,
	onCleanup,
	onMount,
	Show,
	splitProps,
} from "solid-js";
import { createStore, produce, reconcile } from "solid-js/store";
import { TransitionGroup } from "solid-transition-group";
import Tooltip from "~/components/Tooltip";
import { authStore } from "~/store";
import { getCameraWindow } from "~/utils/camera-window";
import { createTauriEventListener } from "~/utils/createEventListener";
import {
	createCleanCaptureQuery,
	createCurrentRecordingQuery,
	createOptionsQuery,
	revealRecordingWindow,
} from "~/utils/queries";
import {
	handleRecordingResult,
	runRecordingStopRequest,
} from "~/utils/recording";
import type {
	CameraInfo,
	CurrentRecording,
	DeviceOrModelID,
	RecordingInputKind,
} from "~/utils/tauri";
import { commands, events } from "~/utils/tauri";

type State =
	| { variant: "initializing" }
	| { variant: "countdown"; from: number; current: number }
	| { variant: "recording" }
	| { variant: "paused" }
	| { variant: "stopped" };

type RecordingInputState = Record<RecordingInputKind, boolean>;

declare global {
	interface Window {
		COUNTDOWN: number;
	}
}

const MAX_RECORDING_FOR_FREE = 5 * 60 * 1000;
const NO_MICROPHONE = "No Microphone";
const NO_WEBCAM = "No Webcam";
const FAKE_WINDOW_BOUNDS_NAME = "recording-controls-interactive-area";

export default function () {
	console.log("[in-progress-recording] Wrapper rendering");

	document.documentElement.setAttribute("data-transparent-window", "true");
	document.body.style.background = "transparent";

	return <InProgressRecordingInner />;
}

function InProgressRecordingInner() {
	console.log("[in-progress-recording] Inner component rendering");

	const [state, setState] = createSignal<State>(
		window.COUNTDOWN === 0
			? { variant: "initializing" }
			: {
					variant: "countdown",
					from: window.COUNTDOWN,
					current: window.COUNTDOWN,
				},
	);
	const [start, setStart] = createSignal(Date.now());
	const [time, setTime] = createSignal(Date.now());
	// When we last entered the "stopped" state. The reconcile effect compares
	// this against the recording query's dataUpdatedAt so a refetch that is
	// still in flight when a stop completes can't resurrect the old recording.
	let stoppedAt = 0;
	let stopRequest: { cleanCaptureGeneration: number | undefined } | undefined;
	let stopErrorRequest: typeof stopRequest;
	const markStopped = () => {
		stoppedAt = Date.now();
		setState({ variant: "stopped" });
	};
	const currentRecording = createCurrentRecordingQuery();
	const cleanCapture = createCleanCaptureQuery();
	const optionsQuery = createOptionsQuery();
	const startedWithMicrophone = optionsQuery.rawOptions.micName != null;
	const startedWithCameraInput = optionsQuery.rawOptions.cameraID != null;

	const [authData, setAuthData] = createSignal<{
		plan?: { upgraded?: boolean };
	} | null>(null);
	onMount(() => {
		authStore
			.get()
			.then(setAuthData)
			.catch(() => setAuthData(null));
	});

	const audioLevel = createAudioInputLevel();
	const [disconnectedInputs, setDisconnectedInputs] =
		createStore<RecordingInputState>({ microphone: false, camera: false });
	const [recordingFailure, setRecordingFailure] = createSignal<string | null>(
		null,
	);
	const [pauseError, setPauseError] = createSignal<string | null>(null);
	const [pausePendingAction, setPausePendingAction] = createSignal<
		string | null
	>(null);
	let pauseRequest: object | undefined;
	const [degradedReason, setDegradedReason] = createSignal<string | null>(null);
	const [issuePanelVisible, setIssuePanelVisible] = createSignal(false);
	const [issueKey, setIssueKey] = createSignal("");
	const [cameraWindowOpen, setCameraWindowOpen] = createSignal(false);
	const [startingDismissed, setStartingDismissed] = createSignal(false);
	const [stopRequested, setStopRequested] = createSignal(false);
	const [stopError, setStopError] = createSignal<string | null>(null);
	const resetStopNotice = () => {
		stopRequest = undefined;
		setStopRequested(false);
		stopErrorRequest = undefined;
		setStopError(null);
	};
	createEffect(() => {
		const snapshot = cleanCapture.data;
		const status = currentRecording.data?.status;
		if (!snapshot) return;
		for (const request of [stopRequest, stopErrorRequest]) {
			if (!request) continue;
			if (
				((snapshot.phase === "awaitingShortcut" && status === "pending") ||
					((snapshot.phase === "recording" || snapshot.phase === "paused") &&
						status === "recording")) &&
				request.cleanCaptureGeneration !== undefined &&
				snapshot.generation > request.cleanCaptureGeneration
			) {
				if (stopRequest === request) {
					stopRequest = undefined;
					setStopRequested(false);
				}
				if (stopErrorRequest === request) {
					stopErrorRequest = undefined;
					setStopError(null);
				}
			} else if (request.cleanCaptureGeneration === undefined) {
				request.cleanCaptureGeneration = snapshot.generation;
			}
		}
	});
	const [teardownInFlight, setTeardownInFlight] = createSignal(false);
	// Mirrors the backend's recording-scoped mic mute. The backend flag lives
	// on the per-recording microphone lock, so every new recording starts
	// unmuted — this signal must be reset wherever a new session begins.
	const [micMuted, setMicMuted] = createSignal(false);
	const [interactiveAreaRef, setInteractiveAreaRef] =
		createSignal<HTMLDivElement | null>(null);
	let settingsButtonRef: HTMLButtonElement | undefined;
	let lastInteractiveBoundsKey = "";
	let pendingInteractiveBoundsKey = "";
	let interactiveBoundsDisposed = false;
	const recordingMode = createMemo(
		() => currentRecording.data?.mode ?? optionsQuery.rawOptions.mode,
	);
	const canPauseRecording = createMemo(() => {
		const mode = recordingMode();
		const os = ostype();
		return (
			mode === "studio" ||
			os === "macos" ||
			(os === "windows" && mode === "instant")
		);
	});

	const _hasDisconnectedInput = () =>
		disconnectedInputs.microphone || disconnectedInputs.camera;

	const issueMessages = createMemo(() => {
		const issues: string[] = [];
		if (disconnectedInputs.microphone)
			issues.push(
				"Microphone disconnected. Silence will be used until it reconnects.",
			);
		if (disconnectedInputs.camera)
			issues.push(
				"Camera disconnected. Recording continues without camera overlay.",
			);
		const failure = recordingFailure();
		if (failure) issues.push(failure);
		const controlError = pauseError();
		if (controlError) issues.push(controlError);
		const degraded = degradedReason();
		if (degraded) issues.push(degraded);
		const nativeError = cleanCapture.data?.error;
		if (nativeError) issues.push(nativeError);
		const localError = stopError();
		if (localError) issues.push(localError);
		return [...new Set(issues.map(recordingIssueMessage))];
	});

	const hasRecordingIssue = () => issueMessages().length > 0;

	const toggleIssuePanel = () => {
		if (!hasRecordingIssue()) return;
		setIssuePanelVisible((visible) => !visible);
	};

	const dismissIssuePanel = () => setIssuePanelVisible(false);
	const hasCameraInput = () => optionsQuery.rawOptions.cameraID != null;
	const microphoneTitle = createMemo(() => {
		if (disconnectedInputs.microphone) return "Microphone disconnected";
		if (optionsQuery.rawOptions.micName)
			return `Microphone: ${optionsQuery.rawOptions.micName}`;
		return "Microphone not configured";
	});

	const [pauseResumes, setPauseResumes] = createStore<
		| []
		| [
				...Array<{ pause: number; resume?: number }>,
				{ pause: number; resume?: number },
		  ]
	>([]);

	createEffect(() => {
		const messages = issueMessages();
		if (messages.length === 0) {
			setIssueKey("");
			setIssuePanelVisible(false);
			return;
		}
		const nextKey = messages.join("||");
		if (nextKey !== issueKey()) {
			setIssueKey(nextKey);
			setIssuePanelVisible(true);
		}
	});

	createTauriEventListener(events.recordingEvent, (payload) => {
		switch (payload.variant) {
			case "Countdown":
				pauseRequest = undefined;
				setPausePendingAction(null);
				setPauseError(null);
				setStartingDismissed(false);
				setDisconnectedInputs({ microphone: false, camera: false });
				resetStopNotice();
				setRecordingFailure(null);
				setDegradedReason(null);
				setPauseResumes([]);
				setStopRequested(false);
				setMicMuted(false);
				setState({
					variant: "countdown",
					from: payload.value,
					current: payload.value,
				});
				break;
			case "Started": {
				pauseRequest = undefined;
				setPausePendingAction(null);
				setPauseError(null);
				const wasStartingDismissed = startingDismissed();
				setStartingDismissed(false);
				setDisconnectedInputs({ microphone: false, camera: false });
				resetStopNotice();
				setRecordingFailure(null);
				setDegradedReason(null);
				setPauseResumes([]);
				setStopRequested(false);
				setMicMuted(false);
				aborted = false;
				// This window is reused across recordings, so `start`/`time` still
				// hold the previous session's values here. Effects (the free-plan
				// length limit) run synchronously on the state flip below, so the
				// timestamps must be reset first or the new recording gets measured
				// against the old session and stopped immediately.
				setStart(Date.now());
				setTime(Date.now());
				setState({ variant: "recording" });
				if (wasStartingDismissed) {
					void revealRecordingWindow();
				}
				break;
			}
			case "Paused":
				setPauseError(null);
				if (state().variant === "recording") {
					setPauseResumes((a) => [...a, { pause: Date.now() }]);
				}
				setState({ variant: "paused" });
				setTime(Date.now());
				break;
			case "Resumed":
				setPauseError(null);
				setPauseResumes(
					produce((a) => {
						if (a.length === 0) return a;
						a[a.length - 1].resume = Date.now();
					}),
				);
				setState({ variant: "recording" });
				setTime(Date.now());
				break;
			case "InputLost": {
				setDisconnectedInputs(payload.input, () => true);
				break;
			}
			case "InputRestored":
				setDisconnectedInputs(payload.input, () => false);
				break;
			case "Failed":
				setRecordingFailure(payload.error);
				break;
			case "Degraded": {
				const p = payload as { variant: "Degraded"; reason: string };
				setDegradedReason(p.reason);
				break;
			}
			case "Recovered":
				setDegradedReason(null);
				break;
		}
	});

	// A recording can end outside this window: the main window's stop button,
	// the tray, a global shortcut, or a mid-recording failure. The switch above
	// never resets state for those (RecordingEvent::Stopped exists but comes
	// from a racing wait-actor and can land mid-restart, so it is deliberately
	// not handled). RecordingStopped is only emitted after the recording state
	// clears and strictly before any next recording can start, making it the
	// safe reset signal — without it this reused window keeps ticking a phantom
	// session that poisons the next recording's elapsed-time checks.
	createTauriEventListener(events.recordingStopped, () => {
		// Restart/delete drive their own state while the discarded recording
		// tears down and must not consume the previous recording's stop event.
		if (teardownInFlight()) return;
		markStopped();
	});

	createEffect(() => {
		// While restart/delete teardown is running the query data is stale;
		// reconciling against it would resurrect the discarded recording's state.
		if (teardownInFlight()) return;

		const s = state();
		const recording = currentRecording.data as
			| CurrentRecording
			| null
			| undefined;

		if (s.variant === "stopped" && !currentRecording.isPending && recording) {
			// Only trust data fetched after we entered "stopped". The stop
			// command resolves before the invalidated query can refetch (the
			// backend holds the recording state lock until the command returns),
			// so `data` here can still describe the recording that just ended.
			// Resurrecting from it would leave this reused window in a phantom
			// "recording" state, with the timer running against a dead session.
			// (dataUpdatedAt marks fetch resolution, not the snapshot, so a
			// fetch dispatched pre-stop can slip through — the fresh start/time
			// set below keeps even that phantom harmless to the next session.)
			if (currentRecording.dataUpdatedAt <= stoppedAt) return;
			setStartingDismissed(false);
			setDisconnectedInputs({ microphone: false, camera: false });
			setRecordingFailure(null);
			setDegradedReason(null);
			setPauseResumes([]);
			setStopRequested(false);
			setMicMuted(false);
			aborted = false;
			if (recording.status === "recording") {
				setStart(Date.now());
				setTime(Date.now());
				setState({ variant: "recording" });
			} else {
				setState({ variant: "initializing" });
			}
			return;
		}

		if (s.variant !== "initializing" && s.variant !== "countdown") return;
		if (currentRecording.isPending) return;
		if (recording?.status === "recording") {
			setStartingDismissed(false);
			setDisconnectedInputs({ microphone: false, camera: false });
			setRecordingFailure(null);
			setDegradedReason(null);
			setPauseResumes([]);
			setMicMuted(false);
			aborted = false;
			setStart(Date.now());
			setTime(Date.now());
			setState({ variant: "recording" });
			return;
		}
		if (s.variant === "initializing" && !recording) {
			markStopped();
			void getCurrentWindow().hide();
		}
	});

	createTimer(
		() => {
			if (state().variant !== "recording") return;
			setTime(Date.now());
		},
		100,
		setInterval,
	);
	const refreshCameraWindowState = async () => {
		try {
			setCameraWindowOpen(await commands.isCameraWindowOpen());
		} catch {
			setCameraWindowOpen(false);
		}
	};

	createEffect(() => {
		void refreshCameraWindowState();
	});

	const syncInteractiveAreaBounds = () => {
		if (interactiveBoundsDisposed) return;

		const element = interactiveAreaRef();
		if (!element) {
			if (lastInteractiveBoundsKey !== "") {
				lastInteractiveBoundsKey = "";
				pendingInteractiveBoundsKey = "";
				void commands.removeFakeWindow(FAKE_WINDOW_BOUNDS_NAME);
			}
			return;
		}

		const rect = element.getBoundingClientRect();
		if (rect.width === 0 || rect.height === 0) return;

		const key = [rect.left, rect.top, rect.width, rect.height]
			.map((value) => value.toFixed(2))
			.join(":");
		if (key === lastInteractiveBoundsKey) return;
		if (pendingInteractiveBoundsKey !== "") return;

		pendingInteractiveBoundsKey = key;
		const bounds = {
			position: { x: rect.left, y: rect.top },
			size: { width: rect.width, height: rect.height },
		};
		void commands
			.setFakeWindowBounds(FAKE_WINDOW_BOUNDS_NAME, bounds)
			.then(() => {
				if (interactiveBoundsDisposed) {
					void commands.removeFakeWindow(FAKE_WINDOW_BOUNDS_NAME);
					return;
				}

				lastInteractiveBoundsKey = key;
			})
			.catch((error) => {
				console.error("Failed to sync recording controls hit area", error);
			})
			.finally(() => {
				if (pendingInteractiveBoundsKey === key) {
					pendingInteractiveBoundsKey = "";
					if (lastInteractiveBoundsKey === key) syncInteractiveAreaBounds();
				}
			});
	};

	createEffect(() => {
		const area = interactiveAreaRef();
		if (!area) return;
		const observer = new ResizeObserver(syncInteractiveAreaBounds);
		observer.observe(area);
		onCleanup(() => observer.disconnect());
		queueMicrotask(syncInteractiveAreaBounds);
	});

	createEffect(() => {
		state();
		issuePanelVisible();
		queueMicrotask(syncInteractiveAreaBounds);
	});

	onCleanup(() => {
		stopRequest = undefined;
		stopErrorRequest = undefined;
		interactiveBoundsDisposed = true;
		lastInteractiveBoundsKey = "";
		pendingInteractiveBoundsKey = "";
		void commands.removeFakeWindow(FAKE_WINDOW_BOUNDS_NAME);
	});

	onMount(() => {
		const onResize = () => syncInteractiveAreaBounds();
		window.addEventListener("resize", onResize);
		onCleanup(() => window.removeEventListener("resize", onResize));
		requestAnimationFrame(() => syncInteractiveAreaBounds());
		setTimeout(() => syncInteractiveAreaBounds(), 150);
	});

	createTimer(
		() => {
			void refreshCameraWindowState();
		},
		2000,
		setInterval,
	);

	createTimer(syncInteractiveAreaBounds, 250, setInterval);

	createEffect(() => {
		if (
			state().variant === "stopped" &&
			!currentRecording.isPending &&
			(currentRecording.data === undefined || currentRecording.data === null)
		)
			getCurrentWindow().hide();
	});

	const stopRecording = createMutation(() => ({
		mutationFn: async () => {
			if (stopRequested()) return;
			const request = {
				cleanCaptureGeneration: cleanCapture.data?.generation,
			};
			stopRequest = request;
			setStopRequested(true);
			await runRecordingStopRequest({
				stop: () => commands.stopRecording(),
				isCurrent: () => stopRequest === request,
				onError: (error) => {
					stopErrorRequest = request;
					setStopError(error instanceof Error ? error.message : String(error));
					setIssuePanelVisible(true);
				},
				onSettled: () => {
					stopRequest = undefined;
					setStopRequested(false);
				},
			});
		},
	}));

	const requestStopRecording = () => {
		if (isCountdown() || stopRequested()) return;
		stopRecording.mutate();
	};

	const togglePause = createMutation(() => ({
		mutationFn: async () => {
			if (
				pauseRequest ||
				(state().variant !== "recording" && state().variant !== "paused")
			)
				return;
			const request = { start: start(), resume: state().variant === "paused" };
			pauseRequest = request;
			setPauseError(null);
			setPausePendingAction(request.resume ? "Resuming…" : "Pausing…");
			try {
				if (request.resume) await commands.resumeRecording();
				else await commands.pauseRecording();
			} catch (error) {
				if (
					pauseRequest === request &&
					start() === request.start &&
					(state().variant === "recording" || state().variant === "paused")
				) {
					setPauseError(
						String(error).startsWith("Could not ")
							? String(error)
							: `Could not ${request.resume ? "resume" : "pause"} recording: ${String(error)}`,
					);
				}
				throw error;
			} finally {
				if (pauseRequest === request) {
					pauseRequest = undefined;
					setPausePendingAction(null);
				}
			}
		},
	}));

	// Muting zeroes the mic samples backend-side while the stream keeps its
	// normal cadence, so the recording timeline is unaffected. Only exposed for
	// instant mode: studio records the mic as an editable track, where muted
	// spans would silently bake zeros into it.
	const canToggleMicMute = createMemo(
		() =>
			recordingMode() === "instant" &&
			optionsQuery.rawOptions.micName != null &&
			!disconnectedInputs.microphone &&
			(state().variant === "recording" || state().variant === "paused"),
	);

	const toggleMicMute = createMutation(() => ({
		mutationFn: async () => {
			const next = !micMuted();
			setMicMuted(next);
			try {
				await commands.setMicRecordingMuted(next);
			} catch (error) {
				setMicMuted(!next);
				throw error;
			}
		},
	}));

	const restartRecording = createMutation(() => ({
		mutationFn: async () => {
			const shouldRestart = await dialog.confirm(
				"Are you sure you want to restart the recording? The current recording will be discarded.",
				{ title: "Confirm Restart", okLabel: "Restart", cancelLabel: "Cancel" },
			);

			if (!shouldRestart) return;

			setTeardownInFlight(true);
			setState({ variant: "initializing" });
			try {
				await handleRecordingResult(commands.restartRecording(), undefined);
			} finally {
				setTeardownInFlight(false);
			}
		},
	}));

	const deleteRecording = createMutation(() => ({
		mutationFn: async () => {
			const shouldDelete = await dialog.confirm(
				"Are you sure you want to delete the recording?",
				{ title: "Confirm Delete", okLabel: "Delete", cancelLabel: "Cancel" },
			);

			if (!shouldDelete) return;

			setTeardownInFlight(true);
			markStopped();
			void getCurrentWindow().hide();
			try {
				await commands.deleteRecording();
			} finally {
				setTeardownInFlight(false);
			}
		},
	}));

	const toggleCameraPreview = createMutation(() => ({
		mutationFn: async () => {
			if (cameraWindowOpen()) {
				const cameraWindow = await getCameraWindow();
				if (cameraWindow) await cameraWindow.close();
			} else {
				await commands.showWindow({ Camera: { centered: false } });
			}
			await refreshCameraWindowState();
		},
	}));

	const pauseRecordingForDeviceChange = async () => {
		if (state().variant !== "recording") return false;
		await commands.pauseRecording();
		return true;
	};

	const updateMicInput = createMutation(() => ({
		mutationFn: async (name: string | null) => {
			if (!startedWithMicrophone && name !== null) return;
			await pauseRecordingForDeviceChange();
			optionsQuery.setOptions("micName", name);
			try {
				await commands.setMicInput(name);
			} catch (error) {
				if (
					(optionsQuery.rawOptions.micName ?? null) !== name ||
					String(error).includes("selection was superseded by a newer request")
				)
					return;
				throw error;
			}
		},
	}));

	const updateCameraInput = createMutation(() => ({
		mutationFn: async (camera: CameraInfo | null) => {
			if (!startedWithCameraInput && camera != null) return;
			await pauseRecordingForDeviceChange();
			const next = cameraInfoToId(camera);
			optionsQuery.setOptions("cameraID", reconcile(next));
			try {
				await commands.setCameraInput(next, null);
				if (
					!next &&
					optionsQuery.rawOptions.cameraID == null &&
					cameraWindowOpen()
				) {
					const cameraWindow = await getCameraWindow();
					if (cameraWindow && optionsQuery.rawOptions.cameraID == null)
						await cameraWindow.close();
					await refreshCameraWindowState();
				}
			} catch (error) {
				if (
					JSON.stringify(optionsQuery.rawOptions.cameraID ?? null) !==
						JSON.stringify(next) ||
					String(error).includes("selection was superseded by a newer request")
				)
					return;
				throw error;
			}
		},
	}));

	const openRecordingSettingsMenu = async () => {
		try {
			let audioDevices: string[] = [];
			let videoDevices: CameraInfo[] = [];
			try {
				audioDevices = await commands.listAudioDevices();
			} catch {
				audioDevices = [];
			}
			try {
				videoDevices = await commands.listCameras();
			} catch {
				videoDevices = [];
			}
			const items: (
				| Awaited<ReturnType<typeof CheckMenuItem.new>>
				| Awaited<ReturnType<typeof MenuItem.new>>
				| Awaited<ReturnType<typeof PredefinedMenuItem.new>>
			)[] = [];
			items.push(
				await CheckMenuItem.new({
					text: "Show Camera Preview",
					checked: cameraWindowOpen(),
					enabled: startedWithCameraInput && hasCameraInput(),
					action: () => {
						if (!startedWithCameraInput || !hasCameraInput()) return;
						toggleCameraPreview.mutate();
					},
				}),
			);
			items.push(await PredefinedMenuItem.new({ item: "Separator" }));
			items.push(
				await MenuItem.new({
					text: startedWithMicrophone
						? "Microphone"
						: "Microphone (locked for this recording)",
					enabled: false,
				}),
			);
			items.push(
				await CheckMenuItem.new({
					text: NO_MICROPHONE,
					checked: optionsQuery.rawOptions.micName == null,
					enabled: startedWithMicrophone,
					action: () => updateMicInput.mutate(null),
				}),
			);
			for (const name of audioDevices) {
				items.push(
					await CheckMenuItem.new({
						text: name,
						checked: optionsQuery.rawOptions.micName === name,
						enabled: startedWithMicrophone,
						action: () => updateMicInput.mutate(name),
					}),
				);
			}
			items.push(await PredefinedMenuItem.new({ item: "Separator" }));
			items.push(
				await MenuItem.new({
					text: startedWithCameraInput
						? "Webcam"
						: "Webcam (locked for this recording)",
					enabled: false,
				}),
			);
			items.push(
				await CheckMenuItem.new({
					text: NO_WEBCAM,
					checked: !hasCameraInput(),
					enabled: startedWithCameraInput,
					action: () => updateCameraInput.mutate(null),
				}),
			);
			for (const camera of videoDevices) {
				items.push(
					await CheckMenuItem.new({
						text: camera.display_name,
						checked: cameraMatchesSelection(
							camera,
							optionsQuery.rawOptions.cameraID ?? null,
						),
						enabled: startedWithCameraInput,
						action: () => updateCameraInput.mutate(camera),
					}),
				);
			}
			const menu = await Menu.new({ items });
			const rect = settingsButtonRef?.getBoundingClientRect();
			if (rect)
				menu.popup(new LogicalPosition(rect.x, rect.y + rect.height + 4));
			else menu.popup();
		} catch (error) {
			console.error("Failed to open recording settings menu", error);
		}
	};

	const adjustedTime = () => {
		if (state().variant === "countdown" || state().variant === "initializing")
			return 0;
		let t = time() - start();
		for (const { pause, resume } of pauseResumes) {
			if (pause && resume) t -= resume - pause;
		}
		return Math.max(0, t);
	};

	const isMaxRecordingLimitEnabled = () => {
		// Only enforce the limit on instant mode.
		// We enforce it on studio mode when exporting.
		return (
			optionsQuery.rawOptions.mode === "instant" &&
			// If the data is loaded and the user is not upgraded
			authData()?.plan?.upgraded === false
		);
	};

	let aborted = false;
	createEffect(() => {
		// Only a live session may trip the limit; in the other variants
		// `time`/`start` are leftovers from a previous recording in this
		// reused window and must never trigger a stop.
		const variant = state().variant;
		if (variant !== "recording" && variant !== "paused") return;
		if (
			isMaxRecordingLimitEnabled() &&
			adjustedTime() > MAX_RECORDING_FOR_FREE &&
			!aborted
		) {
			aborted = true;
			stopRecording.mutate();
		}
	});

	const remainingRecordingTime = () => {
		if (MAX_RECORDING_FOR_FREE < adjustedTime()) return 0;
		return MAX_RECORDING_FOR_FREE - adjustedTime();
	};

	const isInitializing = () => state().variant === "initializing";
	const closeStartingBar = async () => {
		setStartingDismissed(true);
		markStopped();
		await getCurrentWindow().hide();
	};
	const isCountdown = () => state().variant === "countdown";
	const countdownCurrent = () => {
		const s = state();
		return s.variant === "countdown" ? s.current : 0;
	};
	const isStarting = () =>
		isInitializing() || (isCountdown() && countdownCurrent() === 0);

	return (
		<div class="flex h-full w-full flex-col justify-end p-3">
			<div
				ref={setInteractiveAreaRef}
				class="flex max-h-full w-full min-h-0 flex-col gap-2"
			>
				<Show when={hasRecordingIssue() && issuePanelVisible()}>
					<div class="flex min-h-0 w-full flex-row items-start gap-2 rounded-xl border border-red-8 bg-gray-1 p-2 text-xs leading-snug text-red-11 shadow-lg">
						<IconLucideAlertTriangle class="mt-0.5 size-5 shrink-0 text-red-9" />
						<div
							role="alert"
							tabIndex={0}
							class="max-h-14 min-w-0 flex-1 space-y-1 overflow-auto whitespace-pre-wrap [overflow-wrap:anywhere]"
						>
							{issueMessages().map((message) => (
								<p>{message}</p>
							))}
						</div>
						<button
							type="button"
							class="shrink-0 text-red-9 transition hover:text-red-11"
							onClick={() => dismissIssuePanel()}
							aria-label="Dismiss recording issue"
						>
							<IconLucideX class="size-4" />
						</button>
					</div>
				</Show>
				<div class="h-10 w-full flex-none rounded-2xl">
					<div
						class="flex h-full w-full flex-row items-stretch overflow-hidden rounded-2xl bg-gray-1 border shadow-[0_1px_3px_rgba(0,0,0,0.1)]"
						style={{
							"border-color":
								state().variant === "paused"
									? "var(--amber-6)"
									: "var(--gray-5)",
						}}
					>
						<div class="flex min-w-0 flex-1 flex-col p-1">
							<div class="flex min-w-0 flex-1 flex-row items-center justify-between gap-1">
								<Show
									when={!isStarting()}
									fallback={
										<div class="flex flex-row items-center gap-1.5 rounded-lg py-1 px-2 text-gray-12">
											<IconLucideLoader2 class="size-4 animate-spin" />
											<span class="truncate text-sm font-medium tabular-nums">
												Starting
											</span>
										</div>
									}
								>
									<RecordingControlButton
										flexible
										disabled={
											stopRequested() ||
											stopRecording.isPending ||
											isCountdown()
										}
										class="flex min-w-0 flex-1 flex-row items-center gap-1 rounded-lg py-1 px-1.5 text-red-300 transition-colors duration-100 hover:bg-red-500/8 active:bg-red-500/12 disabled:opacity-60 disabled:hover:bg-transparent"
										type="button"
										onPointerDown={(event) => {
											if (event.button !== 0) return;
											event.preventDefault();
											event.stopPropagation();
											requestStopRecording();
										}}
										onClick={requestStopRecording}
										title="Stop recording"
										aria-label="Stop recording"
									>
										<IconCapStopCircle class="size-5 shrink-0" />
										<span class="truncate text-sm font-medium tabular-nums">
											<Show
												when={!isCountdown()}
												fallback={
													<div class="relative inline-block h-[1.5em] w-[1ch] overflow-hidden align-middle">
														<TransitionGroup
															onEnter={(el, done) => {
																const a = el.animate(
																	[
																		{
																			opacity: 0,
																			transform: "translateY(-100%)",
																		},
																		{ opacity: 1, transform: "translateY(0)" },
																	],
																	{
																		duration: 300,
																		easing: "cubic-bezier(0.16, 1, 0.3, 1)",
																	},
																);
																a.finished.then(done);
															}}
															onExit={(el, done) => {
																const a = el.animate(
																	[
																		{ opacity: 1, transform: "translateY(0)" },
																		{
																			opacity: 0,
																			transform: "translateY(100%)",
																		},
																	],
																	{
																		duration: 300,
																		easing: "cubic-bezier(0.16, 1, 0.3, 1)",
																	},
																);
																a.finished.then(done);
															}}
														>
															<For each={[countdownCurrent()]}>
																{(num) => (
																	<span class="absolute inset-0 flex items-center justify-center">
																		{num}
																	</span>
																)}
															</For>
														</TransitionGroup>
													</div>
												}
											>
												<Show
													when={
														pausePendingAction() || state().variant === "paused"
													}
													fallback={
														<Show
															when={isMaxRecordingLimitEnabled()}
															fallback={formatTime(adjustedTime() / 1000)}
														>
															{formatTime(remainingRecordingTime() / 1000)}
														</Show>
													}
												>
													<span role="status" aria-live="polite">
														{pausePendingAction() ?? "Paused"}
													</span>
												</Show>
											</Show>
										</span>
									</RecordingControlButton>
								</Show>

								<div class="flex shrink-0 items-center">
									<Show
										when={canToggleMicMute()}
										fallback={
											<RecordingControlTooltip content={microphoneTitle()}>
												<div
													aria-label={microphoneTitle()}
													class="relative flex h-8 w-7 shrink-0 items-center justify-center"
												>
													{optionsQuery.rawOptions.micName != null ? (
														disconnectedInputs.microphone ? (
															<IconLucideMicOff class="size-5 text-[var(--amber-11)]" />
														) : (
															<>
																<IconCapMicrophone class="size-5 text-gray-12" />
																<div class="absolute bottom-1 left-1 right-1 h-0.5 overflow-hidden rounded-full bg-gray-10">
																	<div
																		class="absolute inset-0 bg-blue-9 transition-transform duration-100"
																		style={{
																			transform: `translateX(-${
																				(1 - audioLevel()) * 100
																			}%)`,
																		}}
																	/>
																</div>
															</>
														)
													) : (
														<IconLucideMicOff
															class="size-5 text-gray-7"
															data-tauri-drag-region
														/>
													)}
												</div>
											</RecordingControlTooltip>
										}
									>
										<RecordingControlButton
											type="button"
											class="relative flex h-8 w-7 shrink-0 items-center justify-center rounded-lg transition-colors duration-100 hover:bg-gray-12/6 active:bg-gray-12/10 disabled:opacity-50 disabled:hover:bg-transparent dark:hover:bg-white/8 dark:active:bg-white/12"
											disabled={toggleMicMute.isPending}
											onClick={() => toggleMicMute.mutate()}
											title={
												micMuted() ? "Unmute microphone" : "Mute microphone"
											}
											aria-pressed={micMuted() ? "true" : "false"}
											aria-label={
												micMuted() ? "Unmute microphone" : "Mute microphone"
											}
										>
											{micMuted() ? (
												<IconLucideMicOff class="size-5 text-red-9" />
											) : (
												<>
													<IconCapMicrophone class="size-5 text-gray-12" />
													<div class="absolute bottom-1 left-1 right-1 h-0.5 overflow-hidden rounded-full bg-gray-10">
														<div
															class="absolute inset-0 bg-blue-9 transition-transform duration-100"
															style={{
																transform: `translateX(-${
																	(1 - audioLevel()) * 100
																}%)`,
															}}
														/>
													</div>
												</>
											)}
										</RecordingControlButton>
									</Show>
									<Show
										when={!isInitializing()}
										fallback={
											<ActionButton
												onClick={() => {
													void closeStartingBar();
												}}
												title="Close recording controls"
												aria-label="Close recording controls"
											>
												<IconLucideX class="size-5" />
											</ActionButton>
										}
									>
										<Show when={hasRecordingIssue()}>
											<ActionButton
												class={cx(
													"text-red-10 hover:bg-red-3/40",
													issuePanelVisible() &&
														"bg-red-3/40 ring-1 ring-red-8",
												)}
												onClick={() => toggleIssuePanel()}
												title={issueMessages().join(", ")}
												aria-pressed={issuePanelVisible() ? "true" : "false"}
												aria-label="Recording issues"
											>
												<IconLucideAlertTriangle class="size-5" />
											</ActionButton>
										</Show>

										{canPauseRecording() && (
											<ActionButton
												disabled={
													togglePause.isPending ||
													isCountdown() ||
													stopRequested() ||
													teardownInFlight()
												}
												onClick={() => togglePause.mutate()}
												aria-pressed={state().variant === "paused"}
												aria-busy={togglePause.isPending}
												class={cx(
													"active:scale-90 motion-reduce:transform-none",
													state().variant === "paused" &&
														"bg-[var(--amber-3)] text-[var(--amber-11)] ring-1 ring-[var(--amber-6)]",
												)}
												title={
													state().variant === "paused"
														? "Resume recording"
														: "Pause recording"
												}
												aria-label={
													state().variant === "paused"
														? "Resume recording"
														: "Pause recording"
												}
											>
												<Show
													when={togglePause.isPending}
													fallback={
														state().variant === "paused" ? (
															<IconCapPlayCircle />
														) : (
															<IconCapPauseCircle />
														)
													}
												>
													<IconLucideLoader2 class="size-5 animate-spin motion-reduce:animate-none" />
												</Show>
											</ActionButton>
										)}

										<ActionButton
											disabled={restartRecording.isPending || isCountdown()}
											onClick={() => restartRecording.mutate()}
											title="Restart recording"
											aria-label="Restart recording"
										>
											<IconCapRestart />
										</ActionButton>
										<ActionButton
											disabled={deleteRecording.isPending || isCountdown()}
											onClick={() => deleteRecording.mutate()}
											title="Delete recording"
											aria-label="Delete recording"
										>
											<IconCapTrash />
										</ActionButton>
										<ActionButton
											ref={(el) => {
												settingsButtonRef = el ?? undefined;
											}}
											onClick={() => {
												void openRecordingSettingsMenu();
											}}
											title="Recording settings"
											aria-label="Recording settings"
										>
											<IconCapSettings class="size-5" />
										</ActionButton>
									</Show>
								</div>
							</div>
						</div>
						<div
							aria-label="Move recording controls"
							class="non-styled-move flex w-6 shrink-0 cursor-move items-center justify-center border-l border-gray-5 p-1 hover:cursor-move transition-colors duration-100 hover:bg-gray-12/4 dark:hover:bg-white/6"
							data-tauri-drag-region
						>
							<IconCapMoreVertical class="pointer-events-none size-4 text-gray-10" />
						</div>
					</div>
				</div>
			</div>
		</div>
	);
}

function recordingIssueMessage(message: string): string {
	if (
		message.includes("Insufficient disk space") &&
		message.includes("Your recording is still paused")
	) {
		return "Not enough disk space to resume. Free up space and try again, or press Stop to save.";
	}
	return message;
}

function RecordingControlTooltip(props: {
	content: string | undefined;
	children: JSX.Element;
	flexible?: boolean;
}) {
	const [open, setOpen] = createSignal(false);
	const boundsPrefix = `recording-control-tooltip-${createUniqueId()}`;
	let contentElement: HTMLSpanElement | undefined;
	let opening = 0;

	createEffect(() => {
		if (!open()) return;

		const boundsName = `${boundsPrefix}-${++opening}`;
		let disposed = false;
		let observer: ResizeObserver | undefined;
		const clearBounds = () => {
			void commands.removeFakeWindow(boundsName).catch((error) => {
				console.error("Failed to clear recording tooltip hit area", error);
			});
		};
		const syncBounds = () => {
			if (disposed) return;
			const tooltip = contentElement?.closest<HTMLElement>('[role="tooltip"]');
			if (!tooltip) return;
			const rect = tooltip.getBoundingClientRect();
			if (rect.width === 0 || rect.height === 0) return;
			void commands
				.setFakeWindowBounds(boundsName, {
					position: { x: rect.left, y: rect.top },
					size: {
						width: rect.width,
						height: Math.min(window.innerHeight - rect.top, rect.height + 16),
					},
				})
				.then(() => {
					if (disposed) clearBounds();
				})
				.catch((error) => {
					console.error("Failed to sync recording tooltip hit area", error);
				});
		};
		const frame = requestAnimationFrame(() => {
			syncBounds();
			if (contentElement) {
				observer = new ResizeObserver(syncBounds);
				observer.observe(contentElement);
			}
		});
		window.addEventListener("resize", syncBounds);
		onCleanup(() => {
			disposed = true;
			cancelAnimationFrame(frame);
			observer?.disconnect();
			window.removeEventListener("resize", syncBounds);
			clearBounds();
		});
	});

	return (
		<Tooltip
			content={
				<span
					ref={contentElement}
					class="block max-h-[calc(100vh-80px)] max-w-[calc(100vw-48px)] overflow-y-auto whitespace-normal break-words text-left"
				>
					{props.content}
				</span>
			}
			childClass={cx(
				"flex h-full min-w-0 items-center",
				props.flexible ? "flex-1" : "shrink-0",
			)}
			placement="top"
			gutter={6}
			flip={false}
			slide
			overflowPadding={8}
			open={open()}
			onOpenChange={setOpen}
		>
			<span
				class="flex h-full min-w-0 w-full items-center"
				onFocusIn={() => setOpen(true)}
				onFocusOut={(event) => {
					if (
						!(event.relatedTarget instanceof Node) ||
						(!event.currentTarget.contains(event.relatedTarget) &&
							!contentElement?.contains(event.relatedTarget))
					) {
						setOpen(false);
					}
				}}
			>
				{props.children}
			</span>
		</Tooltip>
	);
}

function RecordingControlButton(
	props: ComponentProps<"button"> & { flexible?: boolean },
) {
	const [local, buttonProps] = splitProps(props, ["title", "flexible"]);
	return (
		<RecordingControlTooltip content={local.title} flexible={local.flexible}>
			<button
				{...buttonProps}
				class={cx(
					props.class,
					"outline-none focus-visible:ring-2 focus-visible:ring-blue-9 disabled:pointer-events-none",
				)}
			/>
		</RecordingControlTooltip>
	);
}

function ActionButton(props: ComponentProps<"button">) {
	return (
		<RecordingControlButton
			{...props}
			class={cx(
				"p-1 rounded-lg transition-colors duration-100",
				"text-gray-11 hover:text-gray-12",
				"hover:bg-gray-12/6 dark:hover:bg-white/8",
				"active:bg-gray-12/10 dark:active:bg-white/12",
				"h-8 w-7 shrink-0 flex items-center justify-center",
				"disabled:opacity-50 disabled:cursor-not-allowed disabled:hover:bg-transparent",
				props.class,
			)}
			type="button"
		/>
	);
}

function formatTime(secs: number) {
	const minutes = Math.floor(secs / 60);
	const seconds = Math.floor(secs % 60);

	return `${minutes}:${seconds.toString().padStart(2, "0")}`;
}

function createAudioInputLevel() {
	const [level, setLevel] = createSignal(0);

	createTauriEventListener(events.audioInputLevelChange, (dbs) => {
		const DB_MIN = -60;
		const DB_MAX = 0;

		const dbValue = dbs ?? DB_MIN;
		const normalizedLevel = Math.max(
			0,
			Math.min(1, (dbValue - DB_MIN) / (DB_MAX - DB_MIN)),
		);
		setLevel(normalizedLevel);
	});

	return level;
}

function cameraMatchesSelection(
	camera: CameraInfo,
	selected?: DeviceOrModelID | null,
) {
	if (!selected) return false;
	if ("DeviceID" in selected) return selected.DeviceID === camera.device_id;
	return camera.model_id != null && selected.ModelID === camera.model_id;
}

function cameraInfoToId(camera: CameraInfo | null): DeviceOrModelID | null {
	if (!camera) return null;
	if (camera.model_id) return { ModelID: camera.model_id };
	return { DeviceID: camera.device_id };
}