diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.ts index f345bf6f150e6..3bc4eba2f240c 100644 --- a/apps/server/src/preview/ServerBrowser.test.ts +++ b/apps/server/src/preview/ServerBrowser.test.ts @@ -3047,6 +3047,11 @@ it.effect("a desktop tab reports the size the desktop draws after a resize", () expect( yield* resize({ mode: "freeform", width: 900, height: 600 }, 900, 600), ).toBeUndefined(); + // A hidden guest takes the new size only while the desktop paints it, which a capture asks for. + expect(cdp.send).toHaveBeenCalledWith( + "Page.captureScreenshot", + expect.objectContaining({ clip: expect.anything() }), + ); desktopSizeAnswer = null; expect(yield* resize({ mode: "freeform", width: 1280, height: 760 }, 1280, 760)).toEqual({ width: 1280, diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts index 0960faa3a30bf..0a0dad06fb50d 100644 --- a/apps/server/src/preview/ServerBrowser.ts +++ b/apps/server/src/preview/ServerBrowser.ts @@ -126,6 +126,12 @@ const AGENT_CURSOR_CLICK_LEAD_MS = 40; const DESKTOP_VIEWPORT_READ_TIMEOUT = "1 second"; /** A desktop page that keeps its size draws nothing on a resize, so the wait gives up. */ const DESKTOP_RESIZE_WAIT_MS = 1000; +/** Answered only once the guest paints a frame; one pixel keeps it cheap. */ +const DESKTOP_PAINT_CAPTURE = { + format: "jpeg", + quality: 1, + clip: { x: 0, y: 0, width: 1, height: 1, scale: 1 }, +} as const; // A revealed tab's client opens the panel and attaches its stream a few round trips later, remote ones included. const REVEAL_VIEWER_WAIT_MS = 1000; // A desktop panel's shown report waits on the renderer's layout, which a busy machine can push past a second. @@ -2827,7 +2833,20 @@ const make = Effect.gen(function* () { // The desktop draws the new size after the session update; its page resize says it has. // Half the time left keeps room for the read, which runs even if nothing was drawn, // because a page can keep another size than the one asked for. - if (changed) await within(drawn, Math.min(DESKTOP_RESIZE_WAIT_MS, remainingMs() / 2)); + if (changed) { + // Electron resizes no hidden guest it does not paint; a pending capture makes the desktop paint it. + let waiting = true; + void (async () => { + while (waiting && !tab.closing) { + await tab.cdp.send("Page.captureScreenshot", DESKTOP_PAINT_CAPTURE); + } + })().catch(constVoid); + try { + await within(drawn, Math.min(DESKTOP_RESIZE_WAIT_MS, remainingMs() / 2)); + } finally { + waiting = false; + } + } const changes = tab.desktopViewportChanges; const measured = await within(measureDesktopViewport(tab), remainingMs()); // Only a read after the draw, with no page resize or load during it, measures this resize.