Skip to content

Commit 4cc0368

Browse files
committed
fix(desktop): still capture when the preview window isn't painting
1 parent c3b5a92 commit 4cc0368

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,21 @@ describe("DesktopBrowserHost", () => {
154154
}).pipe(Effect.scoped),
155155
);
156156

157-
it.effect("does not issue a screenshot when its render surface is unavailable", () =>
157+
it.effect.each<[string, () => Promise<unknown>]>([
158+
["never becomes ready", async () => false],
159+
["can't be reached", () => Promise.reject(new Error("Script failed"))],
160+
])("still issues a screenshot when its render surface %s", ([, prepareSurface]) =>
158161
Effect.gen(function* () {
159162
const host = yield* DesktopBrowserHost.make;
160-
const debuggee = makeDebuggee(async () => false);
163+
const debuggee = makeDebuggee(prepareSurface);
161164
host.attach(key, debuggee.tab);
162165
const replies = yield* watchReplies(host);
163166

164-
yield* command(host, 1, "Page.captureScreenshot");
167+
yield* sendScreenshot(host, debuggee, 1);
168+
debuggee.releaseFirst();
165169
expect(yield* Queue.take(replies)).toBe(1);
166-
expect(debuggee.sent).toEqual([]);
170+
expect(debuggee.sent).toEqual([1]);
171+
expect(debuggee.surfaceRequests).toHaveLength(2);
167172
}).pipe(Effect.scoped),
168173
);
169174

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ export const make = Effect.gen(function* () {
132132
) => {
133133
const wc = tab.debuggee.webContents;
134134
const host = wc.hostWebContents;
135-
if (!host || host.isDestroyed() || wc.isDestroyed()) {
136-
throw new Error("The preview render surface is unavailable.");
137-
}
135+
if (!host || host.isDestroyed() || wc.isDestroyed()) return capture();
138136
const captureId = NodeCrypto.randomUUID();
139137
const setSurface = (active: boolean) =>
140138
host.executeJavaScript(
@@ -143,9 +141,9 @@ export const make = Effect.gen(function* () {
143141
try {
144142
// The guest stays offscreen until React commits the activity lease and Electron
145143
// receives its new bounds. CDP can otherwise wait forever for a composited frame.
146-
if ((await setSurface(true)) !== true) {
147-
throw new Error("The preview render surface did not become ready for capture.");
148-
}
144+
// A window that isn't painting, like a minimized one, never reports ready, yet its
145+
// on-screen tab can still be captured, so the screenshot is sent either way.
146+
await setSurface(true).catch(() => false);
149147
if (tab.relay !== relay || tab.screenshotTurn !== turn || wc.isDestroyed()) {
150148
throw new Error("The preview connection that asked for this screenshot closed.");
151149
}

0 commit comments

Comments
 (0)