Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

Chain recording after source selection

EtienneLescot48dfe748cbcd
diff --git a/electron/electron-env.d.ts b/electron/electron-env.d.ts
index 18d44f1ae..a4972be61 100644
--- a/electron/electron-env.d.ts
+++ b/electron/electron-env.d.ts
@@ -43,6 +43,8 @@ interface Window {
}>;
selectSource: (source: ProcessedDesktopSource) => Promise<ProcessedDesktopSource | null>;
getSelectedSource: () => Promise<ProcessedDesktopSource | null>;
+ onSelectedSourceChanged: (callback: (source: ProcessedDesktopSource) => void) => () => void;
+ onSourceSelectorClosed: (callback: () => void) => () => void;
requestCameraAccess: () => Promise<{
success: boolean;
granted: boolean;
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 1eb569efe..d02c6cdec 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -1336,6 +1336,10 @@ export function registerIpcHandlers(
selectedDesktopSource = null;
}
}
+ const mainWin = getMainWindow();
+ if (mainWin && !mainWin.isDestroyed()) {
+ mainWin.webContents.send("selected-source-changed", selectedSource);
+ }
const sourceSelectorWin = getSourceSelectorWindow();
if (sourceSelectorWin) {
sourceSelectorWin.close();
diff --git a/electron/main.ts b/electron/main.ts
index bfb949ed7..11d545fdf 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -411,6 +411,9 @@ function createSourceSelectorWindowWrapper() {
sourceSelectorWindow = createSourceSelectorWindow();
sourceSelectorWindow.on("closed", () => {
sourceSelectorWindow = null;
+ if (mainWindow && !mainWindow.isDestroyed()) {
+ mainWindow.webContents.send("source-selector-closed");
+ }
});
return sourceSelectorWindow;
}
diff --git a/electron/preload.ts b/electron/preload.ts
index 96cc831fd..fefcac044 100644
--- a/electron/preload.ts
+++ b/electron/preload.ts
@@ -53,6 +53,16 @@ contextBridge.exposeInMainWorld("electronAPI", {
getSelectedSource: () => {
return ipcRenderer.invoke("get-selected-source");
},
+ onSelectedSourceChanged: (callback: (source: ProcessedDesktopSource) => void) => {
+ const listener = (_event: unknown, source: ProcessedDesktopSource) => callback(source);
+ ipcRenderer.on("selected-source-changed", listener);
+ return () => ipcRenderer.removeListener("selected-source-changed", listener);
+ },
+ onSourceSelectorClosed: (callback: () => void) => {
+ const listener = () => callback();
+ ipcRenderer.on("source-selector-closed", listener);
+ return () => ipcRenderer.removeListener("source-selector-closed", listener);
+ },
requestCameraAccess: () => {
return ipcRenderer.invoke("request-camera-access");
},
diff --git a/src/components/launch/LaunchWindow.test.tsx b/src/components/launch/LaunchWindow.test.tsx
new file mode 100644
index 000000000..2bf9c2dbe
--- /dev/null
+++ b/src/components/launch/LaunchWindow.test.tsx
@@ -0,0 +1,286 @@
+import "@testing-library/jest-dom";
+import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import { TooltipProvider } from "../ui/tooltip";
+import { LaunchWindow } from "./LaunchWindow";
+
+type SelectedSourceChangedListener = Parameters<
+ Window["electronAPI"]["onSelectedSourceChanged"]
+>[0];
+
+const recorderState = vi.hoisted(() => ({
+ value: {
+ recording: false,
+ paused: false,
+ elapsedSeconds: 0,
+ toggleRecording: vi.fn(),
+ togglePaused: vi.fn(),
+ canPauseRecording: false,
+ restartRecording: vi.fn(),
+ cancelRecording: vi.fn(),
+ microphoneEnabled: false,
+ setMicrophoneEnabled: vi.fn(),
+ microphoneDeviceId: undefined,
+ setMicrophoneDeviceId: vi.fn(),
+ setMicrophoneDeviceName: vi.fn(),
+ webcamEnabled: false,
+ setWebcamEnabled: vi.fn(async () => true),
+ webcamDeviceId: undefined,
+ setWebcamDeviceId: vi.fn(),
+ setWebcamDeviceName: vi.fn(),
+ systemAudioEnabled: false,
+ setSystemAudioEnabled: vi.fn(),
+ cursorCaptureMode: "editable-overlay",
+ setCursorCaptureMode: vi.fn(),
+ },
+}));
+
+let selectedSourceChangedListeners: SelectedSourceChangedListener[] = [];
+let sourceSelectorClosedListeners: Array<() => void> = [];
+
+vi.mock("../../hooks/useScreenRecorder", () => ({
+ useScreenRecorder: () => recorderState.value,
+}));
+
+vi.mock("../../hooks/useMicrophoneDevices", () => ({
+ useMicrophoneDevices: () => ({
+ devices: [],
+ selectedDeviceId: "default",
+ setSelectedDeviceId: vi.fn(),
+ }),
+}));
+
+vi.mock("../../hooks/useCameraDevices", () => ({
+ useCameraDevices: () => ({
+ devices: [],
+ selectedDeviceId: "",
+ setSelectedDeviceId: vi.fn(),
+ isLoading: false,
+ error: null,
+ }),
+}));
+
+vi.mock("../../hooks/useAudioLevelMeter", () => ({
+ useAudioLevelMeter: () => ({ level: 0 }),
+}));
+
+vi.mock("../../lib/requestCameraAccess", () => ({
+ requestCameraAccess: vi.fn(async () => ({ success: true, granted: true, status: "granted" })),
+}));
+
+vi.mock("@/native", () => ({
+ nativeBridgeClient: {
+ system: {
+ getPlatform: vi.fn(async () => "darwin"),
+ },
+ },
+}));
+
+vi.mock("@/i18n/loader", () => ({
+ getAvailableLocales: () => ["en"],
+ getLocaleName: () => "English",
+}));
+
+vi.mock("@/contexts/I18nContext", () => ({
+ useI18n: () => ({
+ locale: "en",
+ setLocale: vi.fn(),
+ systemLocaleSuggestion: null,
+ acceptSystemLocaleSuggestion: vi.fn(),
+ dismissSystemLocaleSuggestion: vi.fn(),
+ resolveSystemLocaleSuggestion: vi.fn(),
+ }),
+ useScopedT: () => (key: string) => {
+ const translations: Record<string, string> = {
+ "sourceSelector.defaultSourceName": "Screen",
+ "recording.selectSource": "Please select a source to record",
+ "tooltips.useVerticalTray": "Use vertical tray",
+ "tooltips.useHorizontalTray": "Use horizontal tray",
+ "audio.enableSystemAudio": "Enable system audio",
+ "audio.disableSystemAudio": "Disable system audio",
+ "audio.enableMicrophone": "Enable microphone",
+ "audio.disableMicrophone": "Disable microphone",
+ "audio.defaultMicrophone": "Default Microphone",
+ "webcam.enableWebcam": "Enable webcam",
+ "webcam.disableWebcam": "Disable webcam",
+ "webcam.defaultCamera": "Default Camera",
+ "webcam.searching": "Searching...",
+ "webcam.noneFound": "No camera found",
+ "webcam.unavailable": "Camera unavailable",
+ "cursor.useEditableCursor": "Use editable cursor",
+ "cursor.useSystemCursor": "Use system cursor",
+ "tooltips.openStudio": "Open Studio",
+ "tooltips.hideHUD": "Hide HUD",
+ "tooltips.closeApp": "Close App",
+ language: "Language",
+ };
+ return translations[key] ?? key;
+ },
+}));
+
+function renderLaunchWindow() {
+ return render(
+ <TooltipProvider>
+ <LaunchWindow />
+ </TooltipProvider>,
+ );
+}
+
+function stubElectronAPI(getSelectedSource: Window["electronAPI"]["getSelectedSource"]) {
+ window.electronAPI = {
+ ...window.electronAPI,
+ getSelectedSource,
+ openSourceSelector: vi.fn(async () => ({ opened: true })),
+ requestScreenAccess: vi.fn(async () => ({
+ success: true,
+ granted: true,
+ status: "granted",
+ })),
+ getPlatform: vi.fn(async () => "darwin"),
+ setHudOverlaySize: vi.fn(),
+ setHudOverlayIgnoreMouseEvents: vi.fn(),
+ moveHudOverlayBy: vi.fn(),
+ hudOverlayHide: vi.fn(),
+ hudOverlayClose: vi.fn(),
+ switchToEditor: vi.fn(async () => undefined),
+ onSelectedSourceChanged: vi.fn((callback) => {
+ selectedSourceChangedListeners.push(callback);
+ return () => {
+ selectedSourceChangedListeners = selectedSourceChangedListeners.filter(
+ (listener) => listener !== callback,
+ );
+ };
+ }),
+ onSourceSelectorClosed: vi.fn((callback) => {
+ sourceSelectorClosedListeners.push(callback);
+ return () => {
+ sourceSelectorClosedListeners = sourceSelectorClosedListeners.filter(
+ (listener) => listener !== callback,
+ );
+ };
+ }),
+ } as typeof window.electronAPI;
+}
+
+const displayOneSource = {
+ id: "screen:1:0",
+ name: "Display 1",
+ display_id: "1",
+ thumbnail: null,
+ appIcon: null,
+} satisfies ProcessedDesktopSource;
+
+async function waitForSourceSelectionSubscription() {
+ await waitFor(() => {
+ expect(selectedSourceChangedListeners.length).toBeGreaterThan(0);
+ });
+}
+
+function emitSelectedSourceChanged(source: ProcessedDesktopSource) {
+ act(() => {
+ selectedSourceChangedListeners.forEach((listener) => listener(source));
+ });
+}
+
+function emitSourceSelectorClosed() {
+ act(() => {
+ sourceSelectorClosedListeners.forEach((listener) => listener());
+ });
+}
+
+describe("LaunchWindow record button", () => {
+ beforeEach(() => {
+ class ResizeObserver {
+ observe() {
+ return undefined;
+ }
+ unobserve() {
+ return undefined;
+ }
+ disconnect() {
+ return undefined;
+ }
+ }
+ vi.stubGlobal("ResizeObserver", ResizeObserver);
+ recorderState.value.toggleRecording.mockClear();
+ selectedSourceChangedListeners = [];
+ sourceSelectorClosedListeners = [];
+ stubElectronAPI(vi.fn(async () => null));
+ });
+
+ afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+ });
+
+ it("opens the source selector instead of disabling the primary action when no source is selected", async () => {
+ renderLaunchWindow();
+
+ const recordButton = await screen.findByTestId("launch-record-button");
+
+ expect(recordButton).toBeEnabled();
+ expect(recordButton).toHaveAttribute("title", "Please select a source to record");
+
+ fireEvent.click(recordButton);
+
+ await waitFor(() => {
+ expect(window.electronAPI.openSourceSelector).toHaveBeenCalledTimes(1);
+ });
+ expect(recorderState.value.toggleRecording).not.toHaveBeenCalled();
+ });
+
+ it("records immediately after source selection when the record button opened the picker", async () => {
+ renderLaunchWindow();
+ await waitForSourceSelectionSubscription();
+
+ fireEvent.click(await screen.findByTestId("launch-record-button"));
+ emitSelectedSourceChanged(displayOneSource);
+
+ await waitFor(() => {
+ expect(recorderState.value.toggleRecording).toHaveBeenCalledTimes(1);
+ });
+ expect(screen.getByTestId("launch-record-button")).toHaveAttribute("title", "Display 1");
+ });
+
+ it("does not record after manual source selection", async () => {
+ renderLaunchWindow();
+ await waitForSourceSelectionSubscription();
+
+ emitSelectedSourceChanged(displayOneSource);
+
+ await waitFor(() => {
+ expect(screen.getByTestId("launch-record-button")).toHaveAttribute("title", "Display 1");
+ });
+ expect(recorderState.value.toggleRecording).not.toHaveBeenCalled();
+ });
+
+ it("clears record-after-selection intent when the source picker closes without a selection", async () => {
+ renderLaunchWindow();
+ await waitForSourceSelectionSubscription();
+
+ fireEvent.click(await screen.findByTestId("launch-record-button"));
+ emitSourceSelectorClosed();
+ emitSelectedSourceChanged(displayOneSource);
+
+ await waitFor(() => {
+ expect(screen.getByTestId("launch-record-button")).toHaveAttribute("title", "Display 1");
+ });
+ expect(recorderState.value.toggleRecording).not.toHaveBeenCalled();
+ });
+
+ it("starts recording when a source is already selected", async () => {
+ stubElectronAPI(vi.fn(async () => displayOneSource));
+
+ renderLaunchWindow();
+
+ const recordButton = await screen.findByTestId("launch-record-button");
+ await waitFor(() => {
+ expect(recordButton).toHaveAttribute("title", "Display 1");
+ });
+
+ fireEvent.click(recordButton);
+
+ expect(recorderState.value.toggleRecording).toHaveBeenCalledTimes(1);
+ expect(window.electronAPI.openSourceSelector).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/components/launch/LaunchWindow.tsx b/src/components/launch/LaunchWindow.tsx
index a93514c5a..373aeac55 100644
--- a/src/components/launch/LaunchWindow.tsx
+++ b/src/components/launch/LaunchWindow.tsx
@@ -420,21 +420,31 @@ export function LaunchWindow() {
setHudMouseEventsEnabled(isLanguageMenuOpen);
}, [isLanguageMenuOpen, setHudMouseEventsEnabled]);
- const [selectedSource, setSelectedSource] = useState("Screen");
+ const defaultSourceName = t("sourceSelector.defaultSourceName");
+ const [selectedSource, setSelectedSource] = useState(defaultSourceName);
const [hasSelectedSource, setHasSelectedSource] = useState(false);
const [, setRecordPointerDownCount] = useState(0);
+ const recordAfterSourceSelectionRef = useRef(false);
+
+ const applySelectedSource = useCallback(
+ (source: ProcessedDesktopSource | null) => {
+ if (source) {
+ setSelectedSource(source.name);
+ setHasSelectedSource(true);
+ return;
+ }
+
+ setSelectedSource(defaultSourceName);
+ setHasSelectedSource(false);
+ },
+ [defaultSourceName],
+ );
useEffect(() => {
const checkSelectedSource = async () => {
if (window.electronAPI) {
const source = await window.electronAPI.getSelectedSource();
- if (source) {
- setSelectedSource(source.name);
- setHasSelectedSource(true);
- } else {
- setSelectedSource("Screen");
- setHasSelectedSource(false);
- }
+ applySelectedSource(source);
}
};
@@ -442,15 +452,51 @@ export function LaunchWindow() {
const interval = setInterval(checkSelectedSource, 500);
return () => clearInterval(interval);
- }, []);
+ }, [applySelectedSource]);
+
+ useEffect(() => {
+ const cleanupSourceChanged = window.electronAPI?.onSelectedSourceChanged?.((source) => {
+ applySelectedSource(source);
+ if (!recordAfterSourceSelectionRef.current || recording) {
+ return;
+ }
+
+ recordAfterSourceSelectionRef.current = false;
+ toggleRecording();
+ });
+ const cleanupSelectorClosed = window.electronAPI?.onSourceSelectorClosed?.(() => {
+ recordAfterSourceSelectionRef.current = false;
+ });
+
+ return () => {
+ cleanupSourceChanged?.();
+ cleanupSelectorClosed?.();
+ };
+ }, [applySelectedSource, recording, toggleRecording]);
const openSourceSelector = async () => {
if (window.electronAPI) {
- await openSourceSelectorWithPermissionRetry({
+ return await openSourceSelectorWithPermissionRetry({
openSourceSelector: () => window.electronAPI.openSourceSelector(),
requestScreenAccess: () => window.electronAPI.requestScreenAccess(),
});
}
+
+ return { opened: false, reason: "electron-api-unavailable" };
+ };
+
+ const handleRecordButtonClick = () => {
+ if (!hasSelectedSource && !recording) {
+ recordAfterSourceSelectionRef.current = true;
+ void openSourceSelector().then((result) => {
+ if (!result.opened) {
+ recordAfterSourceSelectionRef.current = false;
+ }
+ });
+ return;
+ }
+
+ toggleRecording();
};
const sendHudOverlayHide = () => {
@@ -844,32 +890,41 @@ export function LaunchWindow() {
</div>
{/* Record/Stop group */}
- <button
- data-testid="launch-record-button"
- className={`flex items-center justify-center rounded-full p-2 transition-[min-width,background-color] duration-150 ${recording ? "min-w-[78px]" : "min-w-[36px]"} ${trayLayout === "vertical" ? "min-h-9" : ""} ${styles.electronNoDrag} ${
- recording
- ? paused
- ? "bg-amber-500/10 hover:bg-amber-500/15"
- : "bg-red-500/12 hover:bg-red-500/16"
- : "bg-white/[0.06] hover:bg-white/[0.10]"
- }`}
- onClick={toggleRecording}
- disabled={!hasSelectedSource && !recording}
- style={{ flex: "0 0 auto" }}
+ <Tooltip
+ content={hasSelectedSource || recording ? selectedSource : t("recording.selectSource")}
>
- <div className={`flex items-center justify-center ${recording ? "gap-1.5" : ""}`}>
- {recording
- ? getIcon("stop", paused ? "text-amber-400" : "text-red-400")
- : getIcon("record", hasSelectedSource ? "text-white/80" : "text-white/30")}
- {recording && (
- <span
- className={`${paused ? "text-amber-400" : "text-red-400"} inline-block w-[34px] text-left text-xs font-semibold tabular-nums`}
- >
- {formatTimePadded(elapsedSeconds)}
- </span>
- )}
- </div>
- </button>
+ <button
+ data-testid="launch-record-button"
+ className={`flex items-center justify-center rounded-full p-2 transition-[min-width,background-color] duration-150 ${recording ? "min-w-[78px]" : "min-w-[36px]"} ${trayLayout === "vertical" ? "min-h-9" : ""} ${styles.electronNoDrag} ${
+ recording
+ ? paused
+ ? "bg-amber-500/10 hover:bg-amber-500/15"
+ : "bg-red-500/12 hover:bg-red-500/16"
+ : hasSelectedSource
+ ? "bg-white/[0.06] hover:bg-white/[0.10]"
+ : "bg-white/[0.035] hover:bg-white/[0.08]"
+ }`}
+ onClick={handleRecordButtonClick}
+ title={hasSelectedSource || recording ? selectedSource : t("recording.selectSource")}
+ aria-label={
+ hasSelectedSource || recording ? selectedSource : t("recording.selectSource")
+ }
+ style={{ flex: "0 0 auto" }}
+ >
+ <div className={`flex items-center justify-center ${recording ? "gap-1.5" : ""}`}>
+ {recording
+ ? getIcon("stop", paused ? "text-amber-400" : "text-red-400")
+ : getIcon("record", hasSelectedSource ? "text-white/80" : "text-white/45")}
+ {recording && (
+ <span
+ className={`${paused ? "text-amber-400" : "text-red-400"} inline-block w-[34px] text-left text-xs font-semibold tabular-nums`}
+ >
+ {formatTimePadded(elapsedSeconds)}
+ </span>
+ )}
+ </div>
+ </button>
+ </Tooltip>
{recording && (
<div
diff --git a/tests/e2e/windows-native-checklist.spec.ts b/tests/e2e/windows-native-checklist.spec.ts
index d19a1fd64..d1fdf58be 100644
--- a/tests/e2e/windows-native-checklist.spec.ts
+++ b/tests/e2e/windows-native-checklist.spec.ts
@@ -112,7 +112,7 @@ test.describe("Windows native checklist smoke tests", () => {
await hudWindow.waitForLoadState("domcontentloaded");
await dismissLanguagePrompt(hudWindow);
- await expect(hudWindow.getByTestId("launch-record-button")).toBeDisabled();
+ await expect(hudWindow.getByTestId("launch-record-button")).toBeEnabled();
await expect(hudWindow.getByTestId("launch-source-selector-button")).toBeVisible();
await expect(hudWindow.getByTestId("launch-system-audio-button")).toBeEnabled();
await expect(hudWindow.getByTestId("launch-microphone-button")).toBeEnabled();