Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/desktop/src/preview/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2112,6 +2112,12 @@ describe("PreviewManager", () => {
yield* manager.createTab("tab_1", { serverTab });
yield* manager.registerWebview("tab_1", 42);
yield* Deferred.await(attached);
const setBackgroundThrottling = vi.fn();
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn(),
webContents: { setBackgroundThrottling },
} as never);
// Navigating rebuilds the tab's state, which must keep the server tab it answers for.
yield* manager.navigate("tab_1", "https://example.com/next");
yield* Queue.clear(capturing);
Expand All @@ -2130,6 +2136,8 @@ describe("PreviewManager", () => {
expect(yield* Queue.take(capturing)).toBe(true);
screenshot.resolve({ data: "" });
expect(yield* Queue.take(capturing)).toBe(false);
// A hidden, minimized or covered main window commits no frames while throttled.
expect(setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
}).pipe(Effect.provide(managerLayer())),
);

Expand Down
29 changes: 25 additions & 4 deletions apps/desktop/src/preview/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
let pendingRecording: PendingRecording | null = null;
const displayMediaHandlerSessions = new WeakSet<Session>();
let frameCaptureWindowOpen = true;
// Server tabs the client paints for a capture; guarded by frameCaptureSessionsRef like sessions.
const paintingServerTabs = new Set<string>();
let currentMainWindow: BrowserWindow | undefined;
let mainWindowCleanupFiber: Fiber.Fiber<void, never> | undefined;
const tabLifecycleLocks = new Map<
Expand Down Expand Up @@ -822,7 +824,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
yield* restoreFrameCaptureWebContentsBackgroundThrottling(
current.unthrottledWebContentsIds,
);
if (remainingSessions.size === 0) {
if (remainingSessions.size === 0 && paintingServerTabs.size === 0) {
yield* setFrameCaptureBackgroundThrottling(true).pipe(
Effect.retry({ times: 2 }),
Effect.catch((error) =>
Expand Down Expand Up @@ -1680,7 +1682,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
Effect.gen(function* () {
// Cleanup or lock acquisition can outlive the replacement window.
if (window.isDestroyed()) return [undefined, sessions] as const;
if (sessions.size > 0) {
if (sessions.size > 0 || paintingServerTabs.size > 0) {
yield* setWindowBackgroundThrottling(window, false);
}
yield* Ref.set(mainWindowRef, Option.some(window));
Expand Down Expand Up @@ -2829,12 +2831,13 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
}),
] as const;
}
if (sessions.size === 0) {
const mainWindowThrottled = sessions.size === 0 && paintingServerTabs.size === 0;
if (mainWindowThrottled) {
yield* setFrameCaptureBackgroundThrottling(false);
}
yield* setFrameCaptureWebContentsBackgroundThrottling(wc, false).pipe(
Effect.onError(() =>
sessions.size === 0
mainWindowThrottled
? setFrameCaptureBackgroundThrottling(true).pipe(Effect.ignore)
: Effect.void,
),
Expand Down Expand Up @@ -3437,6 +3440,24 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
readonly key: { readonly threadId: string; readonly tabId: string };
readonly active: boolean;
}) {
// A hidden, minimized or covered main window commits no frames while throttled.
yield* SynchronizedRef.modifyEffect(frameCaptureSessionsRef, (sessions) =>
Effect.gen(function* () {
const key = `${painting.key.threadId}\u0000${painting.key.tabId}`;
if (paintingServerTabs.has(key) === painting.active)
return [undefined, sessions] as const;
if (painting.active) paintingServerTabs.add(key);
else paintingServerTabs.delete(key);
if (sessions.size === 0 && paintingServerTabs.size === (painting.active ? 1 : 0)) {
yield* setFrameCaptureBackgroundThrottling(!painting.active).pipe(
Effect.catch((error) =>
Effect.logWarning("Failed to update preview capture throttling.", { error }),
),
);
}
return [undefined, sessions] as const;
}),
).pipe(Effect.uninterruptible);
const tab = [...(yield* SynchronizedRef.get(tabsRef)).values()].find(
(candidate) =>
candidate.serverTab?.threadId === painting.key.threadId &&
Expand Down
Loading