Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop): add device selection overlay and stable devices query

Richie McIlroy9d33c707c139
diff --git a/apps/desktop/src/routes/(window-chrome)/new-main/DeviceSelectOverlay.tsx b/apps/desktop/src/routes/(window-chrome)/new-main/DeviceSelectOverlay.tsx
new file mode 100644
index 000000000..cd842f71e
--- /dev/null
+++ b/apps/desktop/src/routes/(window-chrome)/new-main/DeviceSelectOverlay.tsx
@@ -0,0 +1,280 @@
+import { cx } from "cva";
+import {
+ type Accessor,
+ createEffect,
+ createMemo,
+ createSignal,
+ For,
+ type JSX,
+ onCleanup,
+ Show,
+} from "solid-js";
+import { Portal } from "solid-js/web";
+
+export type DeviceSelectOverlayProps<T> = {
+ open: boolean;
+ onOpenChange: (open: boolean) => void;
+ anchorRef: Accessor<HTMLElement | undefined>;
+ items: T[];
+ selectedItem: T | null;
+ onSelect: (item: T | null) => void;
+ renderItem: (item: T, isSelected: boolean) => JSX.Element;
+ renderStats?: (item: T) => JSX.Element;
+ keyExtractor: (item: T) => string;
+ emptyMessage?: string;
+ noneLabel?: string;
+ title?: string;
+ showNoneOption?: boolean;
+};
+
+export default function DeviceSelectOverlay<T>(
+ props: DeviceSelectOverlayProps<T>,
+) {
+ let overlayRef: HTMLDivElement | undefined;
+ let listRef: HTMLDivElement | undefined;
+ const [searchQuery, setSearchQuery] = createSignal("");
+ const [position, setPosition] = createSignal({ top: 0, left: 0 });
+
+ const filteredItems = createMemo(() => {
+ const query = searchQuery().toLowerCase().trim();
+ if (!query) return props.items;
+ return props.items.filter((item) => {
+ const key = props.keyExtractor(item);
+ return key.toLowerCase().includes(query);
+ });
+ });
+
+ createEffect(() => {
+ if (props.open) {
+ setSearchQuery("");
+ const anchor = props.anchorRef();
+ if (anchor) {
+ const rect = anchor.getBoundingClientRect();
+ setPosition({
+ top: rect.bottom + 8,
+ left: rect.left,
+ });
+ }
+
+ setTimeout(() => {
+ const selected = listRef?.querySelector<HTMLButtonElement>(
+ 'button[data-device-item][data-selected="true"]',
+ );
+ if (selected) {
+ selected.focus();
+ selected.scrollIntoView({ block: "nearest" });
+ } else {
+ const firstItem = listRef?.querySelector<HTMLButtonElement>(
+ "button[data-device-item]",
+ );
+ firstItem?.focus();
+ }
+ }, 50);
+ }
+ });
+
+ createEffect(() => {
+ if (!props.open) return;
+
+ const handleClickOutside = (e: MouseEvent) => {
+ const anchor = props.anchorRef();
+ if (
+ overlayRef &&
+ !overlayRef.contains(e.target as Node) &&
+ anchor &&
+ !anchor.contains(e.target as Node)
+ ) {
+ props.onOpenChange(false);
+ }
+ };
+
+ const handleEscape = (e: KeyboardEvent) => {
+ if (e.key === "Escape") {
+ props.onOpenChange(false);
+ }
+ };
+
+ document.addEventListener("mousedown", handleClickOutside);
+ document.addEventListener("keydown", handleEscape);
+
+ onCleanup(() => {
+ document.removeEventListener("mousedown", handleClickOutside);
+ document.removeEventListener("keydown", handleEscape);
+ });
+ });
+
+ const handleKeyDown = (e: KeyboardEvent) => {
+ const buttons = listRef?.querySelectorAll<HTMLButtonElement>(
+ "button[data-device-item]",
+ );
+ if (!buttons) return;
+
+ const focusedIndex = Array.from(buttons).indexOf(
+ document.activeElement as HTMLButtonElement,
+ );
+
+ switch (e.key) {
+ case "ArrowDown": {
+ e.preventDefault();
+ const nextIndex =
+ focusedIndex < buttons.length - 1 ? focusedIndex + 1 : 0;
+ buttons[nextIndex]?.focus();
+ break;
+ }
+ case "ArrowUp": {
+ e.preventDefault();
+ const prevIndex =
+ focusedIndex > 0 ? focusedIndex - 1 : buttons.length - 1;
+ buttons[prevIndex]?.focus();
+ break;
+ }
+ case "Home": {
+ e.preventDefault();
+ buttons[0]?.focus();
+ break;
+ }
+ case "End": {
+ e.preventDefault();
+ buttons[buttons.length - 1]?.focus();
+ break;
+ }
+ }
+ };
+
+ return (
+ <Show when={props.open}>
+ <Portal>
+ <div
+ ref={overlayRef}
+ class={cx(
+ "fixed z-50 flex flex-col overflow-hidden rounded-xl shadow-lg w-[280px]",
+ "animate-in fade-in zoom-in-95 origin-top-left duration-150",
+ )}
+ style={{
+ top: `${position().top}px`,
+ left: `${position().left}px`,
+ "background-color": "rgba(30, 30, 30, 0.95)",
+ "backdrop-filter": "blur(12px)",
+ border: "1px solid rgba(255, 255, 255, 0.1)",
+ }}
+ onKeyDown={handleKeyDown}
+ >
+ <Show when={props.title}>
+ <div
+ class="px-3 py-2.5"
+ style={{ "border-bottom": "1px solid rgba(255, 255, 255, 0.1)" }}
+ >
+ <h3 class="text-xs font-medium text-white/60">{props.title}</h3>
+ </div>
+ </Show>
+
+ <Show when={props.items.length > 5}>
+ <div class="px-2 pt-2">
+ <input
+ type="text"
+ placeholder="Search..."
+ value={searchQuery()}
+ onInput={(e) => setSearchQuery(e.currentTarget.value)}
+ class="w-full px-3 py-1.5 text-sm rounded-lg outline-none placeholder:text-white/40 text-white"
+ style={{
+ "background-color": "rgba(255, 255, 255, 0.1)",
+ border: "1px solid rgba(255, 255, 255, 0.1)",
+ }}
+ />
+ </div>
+ </Show>
+
+ <div
+ ref={listRef}
+ class="flex flex-col p-1.5 overflow-y-auto max-h-[320px]"
+ style={{
+ "scrollbar-width": "thin",
+ "scrollbar-color": "rgba(255,255,255,0.2) transparent",
+ }}
+ >
+ <Show when={props.showNoneOption !== false}>
+ <button
+ type="button"
+ data-device-item
+ data-selected={props.selectedItem === null}
+ onClick={() => {
+ props.onSelect(null);
+ props.onOpenChange(false);
+ }}
+ class={cx(
+ "flex items-center gap-2 px-2.5 py-2 rounded-lg text-sm transition-colors text-left",
+ props.selectedItem === null
+ ? "bg-blue-500 text-white"
+ : "hover:bg-white/10 text-white/70",
+ )}
+ >
+ <IconLucideCircleOff class="size-4 shrink-0" />
+ <span class="truncate">{props.noneLabel ?? "None"}</span>
+ <Show when={props.selectedItem === null}>
+ <IconLucideCheck class="size-4 ml-auto shrink-0" />
+ </Show>
+ </button>
+ </Show>
+
+ <Show
+ when={filteredItems().length > 0}
+ fallback={
+ <div class="px-3 py-6 text-center text-sm text-white/50">
+ {searchQuery()
+ ? "No matching devices"
+ : (props.emptyMessage ?? "No devices found")}
+ </div>
+ }
+ >
+ <For each={filteredItems()}>
+ {(item) => {
+ const isSelected = () =>
+ props.selectedItem !== null &&
+ props.keyExtractor(props.selectedItem) ===
+ props.keyExtractor(item);
+
+ return (
+ <button
+ type="button"
+ data-device-item
+ data-selected={isSelected()}
+ onClick={() => {
+ props.onSelect(item);
+ props.onOpenChange(false);
+ }}
+ class={cx(
+ "flex flex-col gap-0.5 px-2.5 py-2 rounded-lg text-sm transition-colors text-left",
+ isSelected()
+ ? "bg-blue-500 text-white"
+ : "hover:bg-white/10 text-white",
+ )}
+ >
+ <div class="flex items-center gap-2 w-full">
+ {props.renderItem(item, isSelected())}
+ <Show when={isSelected()}>
+ <IconLucideCheck class="size-4 ml-auto shrink-0" />
+ </Show>
+ </div>
+ <Show when={props.renderStats}>
+ {(renderStats) => (
+ <div
+ class={cx(
+ "text-[11px] pl-6",
+ isSelected() ? "text-white/70" : "text-white/50",
+ )}
+ >
+ {renderStats()(item)}
+ </div>
+ )}
+ </Show>
+ </button>
+ );
+ }}
+ </For>
+ </Show>
+ </div>
+ </div>
+ </Portal>
+ </Show>
+ );
+}
diff --git a/apps/desktop/src/utils/devices.ts b/apps/desktop/src/utils/devices.ts
index a7da6163e..b7d9485cf 100644
--- a/apps/desktop/src/utils/devices.ts
+++ b/apps/desktop/src/utils/devices.ts
@@ -1,5 +1,6 @@
import { queryOptions, useQuery } from "@tanstack/solid-query";
-import { createEffect, onCleanup } from "solid-js";
+import { createEffect, createMemo, onCleanup, untrack } from "solid-js";
+import { createStore, produce } from "solid-js/store";
import {
type CameraInfo,
commands,
@@ -13,6 +14,16 @@ export type DevicesSnapshot = {
permissions: OSPermissionsCheck;
};
+export type CameraWithDetails = CameraInfo & {
+ bestFormat?: { width: number; height: number; frameRate: number };
+};
+
+export type MicrophoneWithDetails = {
+ name: string;
+ sampleRate?: number;
+ channels?: number;
+};
+
export const devicesSnapshot = queryOptions({
queryKey: ["devicesSnapshot"] as const,
queryFn: () => commands.getDevicesSnapshot(),
@@ -24,7 +35,7 @@ export function createDevicesQuery() {
const query = useQuery(() => devicesSnapshot);
createEffect(() => {
- const unlisten = events.devicesUpdated.listen((event) => {
+ const unlisten = events.devicesUpdated.listen(() => {
query.refetch();
});
@@ -35,3 +46,161 @@ export function createDevicesQuery() {
return query;
}
+
+type CameraDetailsCache = Record<
+ string,
+ { width: number; height: number; frameRate: number }
+>;
+type MicDetailsCache = Record<string, { sampleRate: number; channels: number }>;
+
+function cameraListChanged(
+ oldList: CameraWithDetails[],
+ newList: CameraInfo[],
+): boolean {
+ if (oldList.length !== newList.length) return true;
+ const oldIds = new Set(oldList.map((c) => c.device_id));
+ return newList.some((c) => !oldIds.has(c.device_id));
+}
+
+function micListChanged(
+ oldList: MicrophoneWithDetails[],
+ newList: string[],
+): boolean {
+ if (oldList.length !== newList.length) return true;
+ const oldNames = new Set(oldList.map((m) => m.name));
+ return newList.some((name) => !oldNames.has(name));
+}
+
+export function createStableDevicesQuery() {
+ const query = createDevicesQuery();
+
+ const [cameras, setCameras] = createStore<CameraWithDetails[]>([]);
+ const [microphones, setMicrophones] = createStore<MicrophoneWithDetails[]>(
+ [],
+ );
+
+ const cameraDetailsCache: CameraDetailsCache = {};
+ const micDetailsCache: MicDetailsCache = {};
+ const pendingCameraFetches = new Set<string>();
+ const pendingMicFetches = new Set<string>();
+
+ createEffect(() => {
+ const rawCameras = query.data?.cameras ?? [];
+
+ const currentCameras = untrack(() => cameras);
+ const hasListChanged = cameraListChanged(currentCameras, rawCameras);
+
+ if (hasListChanged) {
+ const existingMap = new Map(
+ currentCameras.map((c) => [c.device_id, c.bestFormat]),
+ );
+
+ const newCameras: CameraWithDetails[] = rawCameras.map((c) => ({
+ ...c,
+ bestFormat:
+ cameraDetailsCache[c.device_id] ?? existingMap.get(c.device_id),
+ }));
+
+ setCameras(newCameras);
+ }
+
+ for (const camera of rawCameras) {
+ if (
+ !cameraDetailsCache[camera.device_id] &&
+ !pendingCameraFetches.has(camera.device_id)
+ ) {
+ pendingCameraFetches.add(camera.device_id);
+ commands.getCameraFormats(camera.device_id).then((formats) => {
+ pendingCameraFetches.delete(camera.device_id);
+ if (formats?.bestFormat) {
+ const details = {
+ width: formats.bestFormat.width,
+ height: formats.bestFormat.height,
+ frameRate: formats.bestFormat.frameRate,
+ };
+ cameraDetailsCache[camera.device_id] = details;
+ setCameras(
+ produce((cams) => {
+ const cam = cams.find((c) => c.device_id === camera.device_id);
+ if (cam) {
+ cam.bestFormat = details;
+ }
+ }),
+ );
+ }
+ });
+ }
+ }
+ });
+
+ createEffect(() => {
+ const rawMics = query.data?.microphones ?? [];
+
+ const currentMics = untrack(() => microphones);
+ const hasListChanged = micListChanged(currentMics, rawMics);
+
+ if (hasListChanged) {
+ const existingMap = new Map(
+ currentMics.map((m) => [
+ m.name,
+ { sampleRate: m.sampleRate, channels: m.channels },
+ ]),
+ );
+
+ const newMics: MicrophoneWithDetails[] = rawMics.map((name) => ({
+ name,
+ ...(micDetailsCache[name] ?? existingMap.get(name)),
+ }));
+
+ setMicrophones(newMics);
+ }
+
+ for (const name of rawMics) {
+ if (!micDetailsCache[name] && !pendingMicFetches.has(name)) {
+ pendingMicFetches.add(name);
+ commands.getMicrophoneInfo(name).then((info) => {
+ pendingMicFetches.delete(name);
+ if (info) {
+ const details = {
+ sampleRate: info.sampleRate,
+ channels: info.channels,
+ };
+ micDetailsCache[name] = details;
+ setMicrophones(
+ produce((mics) => {
+ const mic = mics.find((m) => m.name === name);
+ if (mic) {
+ mic.sampleRate = details.sampleRate;
+ mic.channels = details.channels;
+ }
+ }),
+ );
+ }
+ });
+ }
+ }
+ });
+
+ const permissions = createMemo(() => query.data?.permissions);
+
+ return {
+ get cameras() {
+ return cameras;
+ },
+ get microphones() {
+ return microphones;
+ },
+ get permissions() {
+ return permissions();
+ },
+ get isPending() {
+ return query.isPending;
+ },
+ get isLoading() {
+ return query.isLoading;
+ },
+ get isFetching() {
+ return query.isFetching;
+ },
+ };
+}
diff --git a/packages/ui-solid/src/auto-imports.d.ts b/packages/ui-solid/src/auto-imports.d.ts
index 772f755ed..011ba3a60 100644
--- a/packages/ui-solid/src/auto-imports.d.ts
+++ b/packages/ui-solid/src/auto-imports.d.ts
@@ -68,6 +68,7 @@ declare global {
const IconLucideBell: typeof import('~icons/lucide/bell.jsx')['default']
const IconLucideBoxSelect: typeof import('~icons/lucide/box-select.jsx')['default']
const IconLucideCheck: typeof import('~icons/lucide/check.jsx')['default']
+ const IconLucideCircleOff: typeof import('~icons/lucide/circle-off.jsx')['default']
const IconLucideClapperboard: typeof import('~icons/lucide/clapperboard.jsx')['default']
const IconLucideClock: typeof import('~icons/lucide/clock.jsx')['default']
const IconLucideEdit: typeof import('~icons/lucide/edit.jsx')['default']