Skip to content
Merged
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
24 changes: 21 additions & 3 deletions apps/desktop/src/preview/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2415,10 +2415,18 @@ describe("PreviewManager", () => {
Effect.gen(function* () {
const host = yield* DesktopBrowserHost.DesktopBrowserHost;
const serverTab = { threadId: "thread-1", tabId: "server-tab-1" };
const windowThrottling = vi.fn<(enabled: boolean) => void>();
const mainWebContents = Object.assign(makeTestHostWebContents(), {
setBackgroundThrottling: windowThrottling,
});
const contents = Object.assign(
makeTestPreviewWebContents(async () => {
throw new Error("unused");
}, 42),
makeTestPreviewWebContents(
async () => {
throw new Error("unused");
},
42,
mainWebContents,
),
{
isDevToolsOpened: () => false,
getUserAgent: () => "Electron",
Expand All @@ -2431,6 +2439,11 @@ describe("PreviewManager", () => {
) as unknown as typeof contents.debugger.sendCommand;
fromId.mockReturnValue(contents);
const manager = yield* PreviewManager.PreviewManager;
yield* manager.setMainWindow({
isDestroyed: () => false,
once: vi.fn(),
webContents: mainWebContents,
} as never);
const capturing = yield* Queue.unbounded<boolean>();
yield* manager.subscribeStateChanges((_tabId, state) =>
Queue.offer(capturing, state.capturing).pipe(Effect.asVoid),
Expand Down Expand Up @@ -2463,9 +2476,14 @@ describe("PreviewManager", () => {
}),
);
expect(yield* Queue.take(capturing)).toBe(true);
// A locked or covered window paints a guest it had hidden only while unthrottled.
expect(windowThrottling.mock.calls).toEqual([[false]]);
expect(contents.setBackgroundThrottling.mock.calls).toEqual([[false]]);
yield* manager.setCaptureDrawn("tab_1", true);
screenshot.resolve({ data: "" });
expect(yield* Queue.take(capturing)).toBe(false);
expect(windowThrottling.mock.calls).toEqual([[false], [true]]);
expect(contents.setBackgroundThrottling.mock.calls).toEqual([[false], [true]]);
}).pipe(Effect.provide(managerLayer())),
);

Expand Down
21 changes: 21 additions & 0 deletions apps/desktop/src/preview/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3623,6 +3623,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
{ discard: true },
);
});
// A hidden or locked window draws no frames, so a guest it hid stays unpainted until unthrottled.
const serverCaptureLeases = new Map<string, { readonly tabId: string; readonly lease: symbol }>();
const updateServerTabCapturing = Effect.fn("PreviewManager.updateServerTabCapturing")(
function* (painting: {
readonly key: { readonly threadId: string; readonly tabId: string };
Expand All @@ -3633,6 +3635,25 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
candidate.serverTab?.threadId === painting.key.threadId &&
candidate.serverTab.tabId === painting.key.tabId,
);
const key = `${painting.key.threadId}\u0000${painting.key.tabId}`;
const held = serverCaptureLeases.get(key);
if (!painting.active && held) {
serverCaptureLeases.delete(key);
yield* stopFrameCapture(held.tabId, held.lease);
} else if (painting.active && !held && tab) {
const lease = Symbol("server-capture");
yield* startFrameCapture(tab.tabId, lease).pipe(
Effect.andThen(
Effect.sync(() => serverCaptureLeases.set(key, { tabId: tab.tabId, lease })),
),
Effect.catch((error) =>
Effect.logWarning("Failed to unthrottle a server tab for its capture.", {
tabId: tab.tabId,
error,
}),
),
);
}
if (tab && tab.capturing !== painting.active) {
yield* update(tab.tabId, { capturing: painting.active });
}
Expand Down
Loading