Skip to content

Commit 92bce54

Browse files
Kiri110Kclaude
andcommitted
fix(desktop): preview snapshots no longer time out while the T3 window is hidden
capturePage waits for a window frame that contains the guest. After the first reveal the main window is throttled again, so a covered, minimized, or off-Space window draws no frame and every capture attempt times out. Keep the main window unthrottled for each capturePage call, sharing the switch that recording and picture-in-picture already use. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 86054b6 commit 92bce54

2 files changed

Lines changed: 132 additions & 20 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,92 @@ describe("PreviewManager", () => {
22232223
),
22242224
);
22252225

2226+
effectIt.effect("keeps the main window painting while a screenshot is captured", () =>
2227+
withManager((manager) =>
2228+
Effect.gen(function* () {
2229+
const setBackgroundThrottling = vi.fn<(enabled: boolean) => void>();
2230+
const image = {
2231+
toPNG: () => Buffer.from("png"),
2232+
toJPEG: () => Buffer.from("jpeg"),
2233+
getSize: () => ({ width: 1, height: 1 }),
2234+
};
2235+
const pending = Promise.withResolvers<typeof image>();
2236+
const capturePage = vi.fn(() => pending.promise);
2237+
fromId.mockReturnValue(makeTestPreviewWebContents(capturePage, 42));
2238+
yield* manager.createTab("tab_1");
2239+
yield* manager.registerWebview("tab_1", 42);
2240+
yield* manager.setMainWindow({
2241+
isDestroyed: () => false,
2242+
once: vi.fn(),
2243+
webContents: { setBackgroundThrottling },
2244+
} as never);
2245+
2246+
const capture = yield* manager
2247+
.captureScreenshot("tab_1")
2248+
.pipe(Effect.forkChild({ startImmediately: true }));
2249+
yield* TestClock.adjust(0);
2250+
expect(capturePage).toHaveBeenCalledOnce();
2251+
expect(setBackgroundThrottling.mock.calls).toEqual([[false]]);
2252+
2253+
pending.resolve(image);
2254+
yield* Fiber.join(capture);
2255+
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
2256+
}),
2257+
),
2258+
);
2259+
2260+
effectIt.effect("leaves the main window painting when a recording stops mid-capture", () =>
2261+
withManager((manager) =>
2262+
Effect.gen(function* () {
2263+
const setBackgroundThrottling = vi.fn<(enabled: boolean) => void>();
2264+
const image = {
2265+
toPNG: () => Buffer.from("png"),
2266+
toJPEG: () => Buffer.from("jpeg"),
2267+
getSize: () => ({ width: 1, height: 1 }),
2268+
};
2269+
const pending = Promise.withResolvers<typeof image>();
2270+
const host = makeTestHostWebContents();
2271+
const snapshotWebContents = makeTestPreviewWebContents(() => pending.promise, 41, host);
2272+
const recordedWebContents = makeTestPreviewWebContents(
2273+
async () => ({
2274+
toJPEG: () => Buffer.from("recording-frame"),
2275+
getSize: () => ({ width: 1280, height: 720 }),
2276+
}),
2277+
42,
2278+
host,
2279+
);
2280+
const webContentsById = new Map([
2281+
[41, snapshotWebContents],
2282+
[42, recordedWebContents],
2283+
]);
2284+
fromId.mockImplementation((id) =>
2285+
id === undefined ? null : (webContentsById.get(id) ?? null),
2286+
);
2287+
yield* manager.createTab("tab_snapshot");
2288+
yield* manager.createTab("tab_recording");
2289+
yield* manager.registerWebview("tab_snapshot", 41);
2290+
yield* manager.registerWebview("tab_recording", 42);
2291+
yield* manager.setMainWindow({
2292+
isDestroyed: () => false,
2293+
once: vi.fn(),
2294+
webContents: { setBackgroundThrottling },
2295+
} as never);
2296+
2297+
const capture = yield* manager
2298+
.captureScreenshot("tab_snapshot")
2299+
.pipe(Effect.forkChild({ startImmediately: true }));
2300+
yield* TestClock.adjust(0);
2301+
yield* manager.startRecording("tab_recording");
2302+
yield* manager.stopRecording("tab_recording");
2303+
expect(setBackgroundThrottling.mock.calls).not.toContainEqual([true]);
2304+
2305+
pending.resolve(image);
2306+
yield* Fiber.join(capture);
2307+
expect(setBackgroundThrottling.mock.calls.at(-1)).toEqual([true]);
2308+
}),
2309+
),
2310+
);
2311+
22262312
effectIt.effect("keeps every recorded guest unthrottled until its frame capture stops", () =>
22272313
withManager((manager) =>
22282314
Effect.gen(function* () {

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
674674
const frameCaptureSessionsRef = yield* SynchronizedRef.make<
675675
ReadonlyMap<string, FrameCaptureSession>
676676
>(new Map());
677+
// capturePage calls in flight. Like frame capture sessions, each keeps the main window unthrottled.
678+
const paintingCapturesRef = yield* Ref.make(0);
677679
const pictureInPictureSessionsRef = yield* SynchronizedRef.make<
678680
ReadonlyMap<string, PictureInPictureSession>
679681
>(new Map());
@@ -735,12 +737,19 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
735737
yield* requireCurrentGuest;
736738
return image;
737739
});
738-
return yield* capture.pipe(
739-
Effect.retry({
740-
times: CAPTURE_PAGE_RETRY_ATTEMPTS - 1,
741-
schedule: Schedule.spaced(CAPTURE_PAGE_RETRY_DELAY_MS),
742-
while: isPreviewOperationError,
743-
}),
740+
// capturePage waits for a window frame that contains the guest. A throttled main window
741+
// that is covered, minimized, or on another Space draws none, so the capture never settles.
742+
return yield* Effect.acquireUseRelease(
743+
holdMainWindowPainting,
744+
() =>
745+
capture.pipe(
746+
Effect.retry({
747+
times: CAPTURE_PAGE_RETRY_ATTEMPTS - 1,
748+
schedule: Schedule.spaced(CAPTURE_PAGE_RETRY_DELAY_MS),
749+
while: isPreviewOperationError,
750+
}),
751+
),
752+
() => releaseMainWindowPainting,
744753
);
745754
});
746755
const currentIso = DateTime.now.pipe(Effect.map(DateTime.formatIso));
@@ -795,6 +804,34 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
795804
if (Option.isNone(mainWindow)) return;
796805
yield* setWindowBackgroundThrottling(mainWindow.value, enabled);
797806
});
807+
/** Re-throttles the main window once no frame capture session or capturePage call needs frames. */
808+
const throttleMainWindowWhenIdle = Effect.fnUntraced(function* (remainingSessions: number) {
809+
if (remainingSessions > 0 || (yield* Ref.get(paintingCapturesRef)) > 0) return;
810+
yield* setFrameCaptureBackgroundThrottling(true).pipe(
811+
Effect.retry({ times: 2 }),
812+
Effect.catch((error) =>
813+
Effect.logWarning("Failed to restore preview frame capture throttling.", { error }),
814+
),
815+
);
816+
});
817+
// Both run under the frame capture lock so a session that starts or stops mid-capture sees the count.
818+
const holdMainWindowPainting = SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
819+
Effect.gen(function* () {
820+
const held = yield* Ref.getAndUpdate(paintingCapturesRef, (count) => count + 1);
821+
if (held === 0 && sessions.size === 0) {
822+
yield* setFrameCaptureBackgroundThrottling(false).pipe(Effect.ignore);
823+
}
824+
return [undefined, sessions] as const;
825+
}),
826+
);
827+
const releaseMainWindowPainting = SynchronizedRef.modifyEffect(
828+
frameCaptureSessionsRef,
829+
(sessions) =>
830+
Ref.update(paintingCapturesRef, (count) => count - 1).pipe(
831+
Effect.andThen(throttleMainWindowWhenIdle(sessions.size)),
832+
Effect.as([undefined, sessions] as const),
833+
),
834+
);
798835
const setFrameCaptureWebContentsBackgroundThrottling = Effect.fnUntraced(function* (
799836
wc: Electron.WebContents,
800837
enabled: boolean,
@@ -909,14 +946,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
909946
yield* restoreFrameCaptureWebContentsBackgroundThrottling(
910947
current.unthrottledWebContentsIds,
911948
);
912-
if (remainingSessions.size === 0) {
913-
yield* setFrameCaptureBackgroundThrottling(true).pipe(
914-
Effect.retry({ times: 2 }),
915-
Effect.catch((error) =>
916-
Effect.logWarning("Failed to restore preview frame capture throttling.", { error }),
917-
),
918-
);
919-
}
949+
yield* throttleMainWindowWhenIdle(remainingSessions.size);
920950
return [current.scope, remainingSessions] as const;
921951
}),
922952
).pipe(
@@ -2079,7 +2109,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20792109
}
20802110
yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
20812111
Effect.gen(function* () {
2082-
if (sessions.size > 0) {
2112+
if (sessions.size > 0 || (yield* Ref.get(paintingCapturesRef)) > 0) {
20832113
yield* setWindowBackgroundThrottling(window, false);
20842114
}
20852115
yield* Ref.set(mainWindowRef, Option.some(window));
@@ -3090,11 +3120,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
30903120
yield* setFrameCaptureBackgroundThrottling(false);
30913121
}
30923122
yield* setFrameCaptureWebContentsBackgroundThrottling(wc, false).pipe(
3093-
Effect.onError(() =>
3094-
sessions.size === 0
3095-
? setFrameCaptureBackgroundThrottling(true).pipe(Effect.ignore)
3096-
: Effect.void,
3097-
),
3123+
Effect.onError(() => throttleMainWindowWhenIdle(sessions.size)),
30983124
);
30993125
const scope =
31003126
consumer === "picture-in-picture" ? yield* Scope.fork(parentScope, "sequential") : null;

0 commit comments

Comments
 (0)