Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
fix(recording): persist auto-zoom preferences and reconcile media duration
超超超超超级喜欢你的达妮娅5118e71c3284
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.tsindex a9c00258b..3b9266dcb 100644--- a/electron/ipc/handlers.ts+++ b/electron/ipc/handlers.ts@@ -93,6 +93,7 @@ import {import { patchWebmDurationOnDisk } from "../recording/webm-duration";import { reindexRecordingOnDisk } from "../recording/webm-seek-index";import { registerNativeBridgeHandlers } from "./nativeBridge";+import { registerRecordingPrefsHandlers } from "./recordingPrefs";import { RecordingStreamRegistry, registerRecordingStreamHandlers } from "./recordingStream";const PROJECT_FILE_EXTENSION = "openscreen";@@ -587,8 +588,8 @@ let currentRecordingSession: RecordingSession | null = null;// useScreenRecorder (a separate renderer, own process, own React tree) picks// up those choices instead of silently reverting to its own defaults when// startNewRecording() switches windows. Mirrors the selectedSource pattern-// above (in-memory, broadcast on change) rather than persisting to disk —-// this is a live session preference, not project content.+// above (in-memory, broadcast on change). Auto-zoom is the one durable choice;+// the device selections remain session preferences, not project content.export interface RecordingPrefs {micEnabled: boolean;micDeviceId: string | null;@@ -611,7 +612,7 @@ export interface RecordingPrefs {/** After a take, suggest cursor-dwell zooms. Default on, matching 1.5. */autoZoomEnabled: boolean;}-let recordingPrefs: RecordingPrefs = {+const defaultRecordingPrefs: RecordingPrefs = {micEnabled: false,micDeviceId: null,micDeviceName: null,@@ -1954,18 +1955,7 @@ export function registerIpcHandlers(return selectedSource;});- ipcMain.handle("get-recording-prefs", () => {- return recordingPrefs;- });-- ipcMain.handle("set-recording-prefs", (_, prefs: Partial<RecordingPrefs>) => {- recordingPrefs = { ...recordingPrefs, ...prefs };- const mainWin = getMainWindow();- if (mainWin && !mainWin.isDestroyed()) {- mainWin.webContents.send("recording-prefs-changed", recordingPrefs);- }- return recordingPrefs;- });+ registerRecordingPrefsHandlers(defaultRecordingPrefs, getMainWindow);ipcMain.handle("request-camera-access", async () => {if (process.platform !== "darwin") {diff --git a/electron/ipc/recordingPrefs.test.ts b/electron/ipc/recordingPrefs.test.tsnew file mode 100644index 000000000..115a3c356--- /dev/null+++ b/electron/ipc/recordingPrefs.test.ts@@ -0,0 +1,85 @@+import { mkdtempSync, readFileSync, rmSync } from "node:fs";+import os from "node:os";+import path from "node:path";+import type { BrowserWindow } from "electron";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";+import type { RecordingPrefs } from "./handlers";+import { registerRecordingPrefsHandlers } from "./recordingPrefs";++const electron = vi.hoisted(() => ({ getPath: vi.fn(), handle: vi.fn() }));+vi.mock("electron", () => ({+ app: { getPath: electron.getPath },+ ipcMain: { handle: electron.handle },+}));++const defaults: RecordingPrefs = {+ micEnabled: false,+ micDeviceId: null,+ micDeviceName: null,+ camEnabled: false,+ camDeviceId: null,+ systemAudioEnabled: false,+ cursorCaptureMode: "editable-overlay",+ autoZoomEnabled: true,+};+let dir: string;+beforeEach(() => {+ dir = mkdtempSync(path.join(os.tmpdir(), "openscreen-recording-ipc-"));+ electron.getPath.mockReturnValue(dir);+ electron.handle.mockClear();+});+afterEach(() => rmSync(dir, { recursive: true, force: true }));++function start(getWindow: () => BrowserWindow | null = () => null) {+ electron.handle.mockClear();+ registerRecordingPrefsHandlers(defaults, getWindow);+ const get = electron.handle.mock.calls.find(+ ([name]) => name === "get-recording-prefs",+ )?.[1] as () => RecordingPrefs;+ const set = electron.handle.mock.calls.find(([name]) => name === "set-recording-prefs")?.[1] as (+ _event: unknown,+ prefs: Partial<RecordingPrefs>,+ ) => RecordingPrefs;+ return { get, set: (prefs: Partial<RecordingPrefs>) => set(undefined, prefs) };+}++describe("recording preferences IPC", () => {+ it("restores false on restart while device preferences reset", () => {+ const first = start();+ expect(first.get().autoZoomEnabled).toBe(true);+ expect(first.set({ autoZoomEnabled: false }).autoZoomEnabled).toBe(false);+ first.set({ micEnabled: true, micDeviceId: "temporary-device" });+ const disk = JSON.parse(readFileSync(path.join(dir, "recording-settings.json"), "utf8"));+ expect(disk).toEqual({ autoZoomEnabled: false });+ const restarted = start();+ expect(restarted.get()).toEqual({ ...defaults, autoZoomEnabled: false });+ restarted.set({ autoZoomEnabled: true });+ expect(start().get().autoZoomEnabled).toBe(true);+ });++ it("broadcasts the saved value and tolerates an absent or destroyed window", () => {+ const send = vi.fn();+ const isDestroyed = vi.fn(() => false);+ const window = { isDestroyed, webContents: { send } } as unknown as BrowserWindow;+ const session = start(() => window);+ const updated = session.set({ autoZoomEnabled: false });+ expect(send).toHaveBeenCalledWith("recording-prefs-changed", updated);+ isDestroyed.mockReturnValue(true);+ session.set({ micEnabled: true });+ expect(send).toHaveBeenCalledTimes(1);+ });++ it("does not publish an invalid or failed preference write", () => {+ const session = start();+ expect(() =>+ session.set({ autoZoomEnabled: null } as unknown as Partial<RecordingPrefs>),+ ).toThrow(TypeError);+ expect(session.get().autoZoomEnabled).toBe(true);+ session.set({ autoZoomEnabled: false });+ session.set({ autoZoomEnabled: undefined, camEnabled: true });+ expect(session.get().autoZoomEnabled).toBe(false);+ rmSync(dir, { recursive: true, force: true });+ expect(() => session.set({ autoZoomEnabled: true })).toThrow();+ expect(session.get().autoZoomEnabled).toBe(false);+ });+});diff --git a/electron/ipc/recordingPrefs.ts b/electron/ipc/recordingPrefs.tsnew file mode 100644index 000000000..edeb8b919--- /dev/null+++ b/electron/ipc/recordingPrefs.ts@@ -0,0 +1,30 @@+import { app, type BrowserWindow, ipcMain } from "electron";+import { loadAutoZoomEnabled, saveAutoZoomEnabled } from "../recording-settings";+import type { RecordingPrefs } from "./handlers";++/** Shared session preferences, with only the auto-zoom choice retained on disk. */+export function registerRecordingPrefsHandlers(+ defaults: RecordingPrefs,+ getMainWindow: () => BrowserWindow | null,+): void {+ const userData = app.getPath("userData");+ let recordingPrefs = { ...defaults, autoZoomEnabled: loadAutoZoomEnabled(userData) };++ ipcMain.handle("get-recording-prefs", () => recordingPrefs);+ ipcMain.handle("set-recording-prefs", (_, prefs: Partial<RecordingPrefs>) => {+ if (prefs.autoZoomEnabled !== undefined) {+ // Persist before publishing: a failed save must not report a durable change.+ saveAutoZoomEnabled(userData, prefs.autoZoomEnabled);+ }+ recordingPrefs = {+ ...recordingPrefs,+ ...prefs,+ autoZoomEnabled: prefs.autoZoomEnabled ?? recordingPrefs.autoZoomEnabled,+ };+ const mainWin = getMainWindow();+ if (mainWin && !mainWin.isDestroyed()) {+ mainWin.webContents.send("recording-prefs-changed", recordingPrefs);+ }+ return recordingPrefs;+ });+}diff --git a/electron/recording-settings.test.ts b/electron/recording-settings.test.tsnew file mode 100644index 000000000..49a1202f1--- /dev/null+++ b/electron/recording-settings.test.ts@@ -0,0 +1,70 @@+import { spawnSync } from "node:child_process";+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";+import os from "node:os";+import path from "node:path";+import { pathToFileURL } from "node:url";+import { afterEach, describe, expect, it } from "vitest";+import { loadAutoZoomEnabled, saveAutoZoomEnabled } from "./recording-settings";++const temps: string[] = [];+const tmp = () => {+ const dir = mkdtempSync(path.join(os.tmpdir(), "openscreen-recording-settings-"));+ temps.push(dir);+ return dir;+};+afterEach(() => {+ for (const dir of temps.splice(0)) rmSync(dir, { recursive: true, force: true });+});++describe("recording settings", () => {+ it("defaults to on for absent, malformed, and invalid settings", () => {+ const dir = tmp();+ expect(loadAutoZoomEnabled(dir)).toBe(true);+ for (const raw of ["{broken", "null", "[]", "42", "{}", '{"autoZoomEnabled":"false"}']) {+ writeFileSync(path.join(dir, "recording-settings.json"), raw);+ expect(loadAutoZoomEnabled(dir)).toBe(true);+ }+ });++ it("round-trips false and true without overwriting unrelated keys", () => {+ const dir = tmp();+ const file = path.join(dir, "recording-settings.json");+ writeFileSync(file, '{"futurePreference":"keep"}');+ for (const enabled of [false, true]) {+ saveAutoZoomEnabled(dir, enabled);+ expect(loadAutoZoomEnabled(dir)).toBe(enabled);+ expect(JSON.parse(readFileSync(file, "utf8"))).toEqual({+ futurePreference: "keep",+ autoZoomEnabled: enabled,+ });+ }+ });++ it("loads the disabled preference in a separate Node process", () => {+ const dir = tmp();+ saveAutoZoomEnabled(dir, false);+ const moduleUrl = pathToFileURL(path.resolve("electron/recording-settings.ts")).href;+ const result = spawnSync(+ process.execPath,+ [+ "--experimental-strip-types",+ "--input-type=module",+ "-e",+ `import { loadAutoZoomEnabled } from ${JSON.stringify(moduleUrl)}; process.stdout.write(JSON.stringify(loadAutoZoomEnabled(process.argv[1])));`,+ dir,+ ],+ { encoding: "utf8" },+ );+ expect(result.error).toBeUndefined();+ expect(result.status, result.stderr).toBe(0);+ expect(result.stdout).toBe("false");+ });++ it("rejects invalid writes and reports a failed disk write", () => {+ const dir = tmp();+ saveAutoZoomEnabled(dir, false);+ expect(() => saveAutoZoomEnabled(dir, "false" as unknown as boolean)).toThrow(TypeError);+ expect(loadAutoZoomEnabled(dir)).toBe(false);+ expect(() => saveAutoZoomEnabled(path.join(dir, "missing"), true)).toThrow();+ });+});diff --git a/electron/recording-settings.ts b/electron/recording-settings.tsnew file mode 100644index 000000000..25370cdab--- /dev/null+++ b/electron/recording-settings.ts@@ -0,0 +1,38 @@+import { readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";+import path from "node:path";++function readSettings(userData: string): Record<string, unknown> {+ try {+ const value: unknown = JSON.parse(+ readFileSync(path.join(userData, "recording-settings.json"), "utf8"),+ );+ return value !== null && typeof value === "object" && !Array.isArray(value)+ ? (value as Record<string, unknown>)+ : {};+ } catch {+ return {};+ }+}++/** Default on for new users; a saved false must survive an app restart. */+export function loadAutoZoomEnabled(userData: string): boolean {+ const value = readSettings(userData).autoZoomEnabled;+ return typeof value === "boolean" ? value : true;+}++/** Save only this durable preference; device selection remains session-only. */+export function saveAutoZoomEnabled(userData: string, enabled: boolean): void {+ if (typeof enabled !== "boolean") throw new TypeError("autoZoomEnabled must be a boolean");+ const destination = path.join(userData, "recording-settings.json");+ const temporary = `${destination}.${process.pid}.tmp`;+ try {+ writeFileSync(+ temporary,+ `${JSON.stringify({ ...readSettings(userData), autoZoomEnabled: enabled })}\n`,+ "utf8",+ );+ renameSync(temporary, destination);+ } finally {+ rmSync(temporary, { force: true });+ }+}diff --git a/src/lib/ai-edition/document/applyProbedDuration.test.ts b/src/lib/ai-edition/document/applyProbedDuration.test.tsindex 64081a666..a50944ac4 100644--- a/src/lib/ai-edition/document/applyProbedDuration.test.ts+++ b/src/lib/ai-edition/document/applyProbedDuration.test.ts@@ -11,7 +11,7 @@ import { describe, expect, it } from "vitest";import type { AxcutDocument } from "../schema";import { documentSchema } from "../schema";import { migrateProjectDataToAxcutDocument } from "./migrate";-import { applyProbedDuration, PLACEHOLDER_DURATION_SEC } from "./timeline";+import { applyProbedDuration, PLACEHOLDER_DURATION_SEC, replaceTimeline } from "./timeline";/** A v1.7 project file: one recording, one zoom region at 6–8s of that recording. */function legacyProjectWithZoom() {@@ -38,6 +38,23 @@ const zoomsOf = (doc: AxcutDocument) =>(doc.zoomRanges ?? []) as unknown as Array<Record<string, unknown>>;describe("applyProbedDuration — the v1.7 import gap", () => {+ it("updates a placeholder asset after the user trimmed its clip, preserving edits", () => {+ const doc = documentSchema.parse(migrateProjectDataToAxcutDocument(legacyProjectWithZoom()));+ const assetId = doc.assets[0].id;+ doc.assets[0].durationSec = PLACEHOLDER_DURATION_SEC;+ doc.timeline.clips[0].sourceEndSec = 9;+ doc.timeline.clips[0].timelineEndSec = 9;+ const next = applyProbedDuration(doc, assetId, 90);+ expect(next.assets[0].durationSec).toBe(90);+ expect(next.timeline).toBe(doc.timeline);+ expect(next.zoomRanges).toBe(doc.zoomRanges);+ expect(doc.assets[0].durationSec).toBe(PLACEHOLDER_DURATION_SEC);+ expect(+ replaceTimeline(next, [{ startSec: 0, endSec: 80 }], "extend").timeline.clips[0].sourceEndSec,+ ).toBe(80);+ expect(applyProbedDuration(next, assetId, 90)).toBe(next);+ expect(applyProbedDuration(doc, "missing_asset", 90)).toBe(doc);+ });it("opens a legacy project with an extent-less clip and unanchored regions", () => {// Documents the STARTING state this function exists to repair. If migration ever// learns the duration itself, this expectation is what will flag it.diff --git a/src/lib/ai-edition/document/timeline.ts b/src/lib/ai-edition/document/timeline.tsindex 5f9cb402b..b3e549fec 100644--- a/src/lib/ai-edition/document/timeline.ts+++ b/src/lib/ai-edition/document/timeline.ts@@ -516,7 +516,20 @@ export function applyProbedDuration(): AxcutDocument {if (!Number.isFinite(durationSec) || durationSec <= 0) return document;const clips = document.timeline.clips;- if (!clips.some((clip) => clipAwaitsProbedDuration(clip, assetId))) return document;+ if (!clips.some((clip) => clipAwaitsProbedDuration(clip, assetId))) {+ // A user may trim the placeholder before metadata arrives. Preserve that edit,+ // but replace the asset's fallback so later timeline operations use real bounds.+ const asset = document.assets.find((item) => item.id === assetId);+ if (asset?.durationSec !== PLACEHOLDER_DURATION_SEC || asset.durationSec === durationSec) {+ return document;+ }+ return {+ ...document,+ assets: document.assets.map((item) =>+ item.id === assetId ? { ...item, durationSec } : item,+ ),+ };+ }// Widening a clip pushes everything after it down the ruler by the same delta.let shiftSec = 0;