Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit 32af2f0

Browse files
fix(preview): stabilize PiP viewport identity (pingdotgg#4661)
Co-authored-by: codex <[email protected]>
1 parent f0121f3 commit 32af2f0

29 files changed

Lines changed: 938 additions & 235 deletions

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,17 @@ import * as BrowserSession from "./BrowserSession.ts";
2222
import * as PreviewManager from "./Manager.ts";
2323

2424
describe("fitPictureInPictureContentSize", () => {
25-
it("fits landscape and portrait viewports without letterboxing the window", () => {
26-
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 16 / 9)).toEqual([480, 270]);
27-
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 9 / 16)).toEqual([240, 427]);
25+
it("preserves the PiP content area across aspect-ratio changes", () => {
26+
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 16 / 9)).toEqual([523, 294]);
27+
expect(PreviewManager.fitPictureInPictureContentSize([480, 320], 9 / 16)).toEqual([294, 523]);
28+
});
29+
30+
it("does not collapse toward the minimum size when orientation changes repeatedly", () => {
31+
const portrait = PreviewManager.fitPictureInPictureContentSize([523, 294], 9 / 16);
32+
const landscape = PreviewManager.fitPictureInPictureContentSize(portrait, 16 / 9);
33+
34+
expect(portrait).toEqual([294, 523]);
35+
expect(landscape).toEqual([523, 294]);
2836
});
2937
});
3038

@@ -1065,7 +1073,7 @@ describe("PreviewManager", () => {
10651073
skipTransformProcessType: true,
10661074
});
10671075
expect(pictureInPictureWindow.setAspectRatio.mock.calls).toEqual([[0], [1280 / 720]]);
1068-
expect(pictureInPictureWindow.setContentSize).toHaveBeenCalledWith(480, 270, false);
1076+
expect(pictureInPictureWindow.setContentSize).toHaveBeenCalledWith(523, 294, false);
10691077
expect(pictureInPictureWindow.setAspectRatio.mock.invocationCallOrder[0]).toBeLessThan(
10701078
pictureInPictureWindow.setContentSize.mock.invocationCallOrder[0] ?? 0,
10711079
);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ const PICTURE_IN_PICTURE_INITIAL_WIDTH = 480;
104104
const PICTURE_IN_PICTURE_INITIAL_HEIGHT = 320;
105105
const PICTURE_IN_PICTURE_MIN_WIDTH = 240;
106106
const PICTURE_IN_PICTURE_MIN_HEIGHT = 160;
107+
const PICTURE_IN_PICTURE_ASPECT_RATIO_EPSILON = 0.002;
107108
const DIAGNOSTIC_BUFFER_LIMIT = 200;
108109
const MAX_ARTIFACT_SITE_SLUG_LENGTH = 80;
109110
const AGENT_CURSOR_MOVE_MS = 160;
@@ -164,8 +165,8 @@ export const fitPictureInPictureContentSize = (
164165
): readonly [width: number, height: number] => {
165166
const currentWidth = Math.max(1, current[0] ?? PICTURE_IN_PICTURE_INITIAL_WIDTH);
166167
const currentHeight = Math.max(1, current[1] ?? PICTURE_IN_PICTURE_INITIAL_HEIGHT);
167-
let width =
168-
currentWidth / currentHeight > aspectRatio ? currentHeight * aspectRatio : currentWidth;
168+
const currentArea = currentWidth * currentHeight;
169+
let width = Math.sqrt(currentArea * aspectRatio);
169170
let height = width / aspectRatio;
170171
const minimumScale = Math.max(
171172
1,
@@ -2074,7 +2075,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20742075
tabId,
20752076
);
20762077
const aspectRatio = frame.width / frame.height;
2077-
if (previousAspectRatio !== aspectRatio) {
2078+
if (
2079+
previousAspectRatio === undefined ||
2080+
Math.abs(previousAspectRatio - aspectRatio) > PICTURE_IN_PICTURE_ASPECT_RATIO_EPSILON
2081+
) {
20782082
yield* attempt(
20792083
{
20802084
operation: "pictureInPicture.setAspectRatio",

‎apps/web/src/browser/ElectronBrowserHost.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { useActivePreviewSessions } from "~/previewStateStore";
1111
import { readPreviewAnnotationTheme } from "./annotationTheme";
1212
import { useBrowserPointerStore } from "./browserPointerStore";
1313
import { HostedBrowserWebview } from "./HostedBrowserWebview";
14+
import { previewRuntimeTabId } from "./previewRuntimeTabId";
1415

1516
export function ElectronBrowserHost() {
1617
const { resolvedTheme } = useTheme();
@@ -23,6 +24,11 @@ export function ElectronBrowserHost() {
2324
? Object.values(previewState.sessions).map((snapshot) => ({
2425
threadRef,
2526
snapshot,
27+
runtimeTabId: previewRuntimeTabId(
28+
threadRef,
29+
previewState.serverEpoch,
30+
snapshot.tabId,
31+
),
2632
zoomFactor: previewState.desktopByTabId[snapshot.tabId]?.zoomFactor ?? 1,
2733
}))
2834
: [];
@@ -74,13 +80,14 @@ export function ElectronBrowserHost() {
7480
if (!isElectron) return null;
7581
return (
7682
<div className="contents" data-electron-browser-host>
77-
{sessions.map(({ threadRef, snapshot, zoomFactor }) => {
83+
{sessions.map(({ threadRef, snapshot, runtimeTabId, zoomFactor }) => {
7884
const url = snapshot.navStatus._tag === "Idle" ? null : snapshot.navStatus.url;
7985
return (
8086
<HostedBrowserWebview
81-
key={snapshot.tabId}
87+
key={runtimeTabId}
8288
threadRef={threadRef}
8389
tabId={snapshot.tabId}
90+
runtimeTabId={runtimeTabId}
8491
initialUrl={url}
8592
viewport={snapshot.viewport ?? FILL_PREVIEW_VIEWPORT}
8693
zoomFactor={zoomFactor}

‎apps/web/src/browser/HostedBrowserWebview.tsx‎

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import { usePreviewBridge } from "~/components/preview/usePreviewBridge";
99
import { cn } from "~/lib/utils";
1010

1111
import { resolveBrowserSurfacePanelRect, useBrowserSurfaceStore } from "./browserSurfaceStore";
12-
import { browserViewportSettingKey, resolveBrowserViewportLayout } from "./browserViewportLayout";
12+
import {
13+
browserViewportSettingKey,
14+
resolveBrowserViewportLayout,
15+
resolveFittedBrowserViewport,
16+
} from "./browserViewportLayout";
1317
import { BrowserDeviceToolbar } from "./BrowserDeviceToolbar";
1418
import { BrowserViewportResizeHandles } from "./BrowserViewportResizeHandles";
1519
import { acquireDesktopTab, type AcquiredDesktopTab } from "./desktopTabLifetime";
@@ -40,11 +44,12 @@ declare global {
4044
export function HostedBrowserWebview(props: {
4145
readonly threadRef: ScopedThreadRef;
4246
readonly tabId: string;
47+
readonly runtimeTabId: string;
4348
readonly initialUrl: string | null;
4449
readonly viewport: PreviewViewportSetting;
4550
readonly zoomFactor: number;
4651
}) {
47-
const { threadRef, tabId, initialUrl, viewport, zoomFactor } = props;
52+
const { threadRef, tabId, runtimeTabId, initialUrl, viewport, zoomFactor } = props;
4853
const config = usePreviewWebviewConfig(threadRef.environmentId);
4954
const [initialSrc] = useState(() => initialUrl ?? "about:blank");
5055
const tabLeaseRef = useRef<AcquiredDesktopTab | null>(null);
@@ -54,27 +59,28 @@ export function HostedBrowserWebview(props: {
5459
const [aspectRatioLocked, setAspectRatioLocked] = useState(false);
5560
const presentation = useBrowserSurfaceStore(
5661
useShallow((state) => {
57-
const current = state.byTabId[tabId];
62+
const current = state.byTabId[runtimeTabId];
5863
return {
64+
content: current?.content ?? null,
5965
cornerRadius: current?.cornerRadius ?? 0,
6066
fitSourceContent: current?.fitSourceContent ?? false,
6167
fittedSourceContent: current?.fittedSourceContent ?? null,
62-
rect: resolveBrowserSurfacePanelRect(state.byTabId, tabId),
68+
rect: resolveBrowserSurfacePanelRect(state.byTabId, runtimeTabId),
6369
visible: current?.visible ?? false,
6470
};
6571
}),
6672
);
67-
usePreviewBridge({ threadRef, tabId });
73+
usePreviewBridge({ threadRef, tabId, runtimeTabId });
6874

6975
useEffect(() => {
7076
crashRecoveryRef.current = INITIAL_WEBVIEW_CRASH_RECOVERY_STATE;
71-
const lease = acquireDesktopTab(tabId);
77+
const lease = acquireDesktopTab(runtimeTabId);
7278
tabLeaseRef.current = lease;
7379
return () => {
7480
if (tabLeaseRef.current === lease) tabLeaseRef.current = null;
7581
lease.release();
7682
};
77-
}, [tabId]);
83+
}, [runtimeTabId]);
7884

7985
const [webviewGeneration, setWebviewGeneration] = useState(0);
8086
const [recoverySrc, setRecoverySrc] = useState(initialSrc);
@@ -107,7 +113,7 @@ export function HostedBrowserWebview(props: {
107113
if (disposed || webviewRef.current !== webview) return;
108114
const webContentsId = webview.getWebContentsId();
109115
if (Number.isInteger(webContentsId) && webContentsId > 0) {
110-
await bridge.registerWebview(tabId, webContentsId);
116+
await bridge.registerWebview(runtimeTabId, webContentsId);
111117
}
112118
} catch {
113119
// did-attach/dom-ready will retry if the guest was not ready yet.
@@ -138,7 +144,7 @@ export function HostedBrowserWebview(props: {
138144
webview.removeEventListener("dom-ready", register);
139145
webview.removeEventListener("render-process-gone", recoverGuest);
140146
};
141-
}, [config, initialSrc, tabId, webviewGeneration]);
147+
}, [config, initialSrc, runtimeTabId, webviewGeneration]);
142148

143149
const active = presentation.visible && presentation.rect !== null;
144150
const lastRect = presentation.rect;
@@ -152,13 +158,22 @@ export function HostedBrowserWebview(props: {
152158
const handleAspectRatioChange = useCallback((aspectRatio: number | null) => {
153159
setAspectRatioLocked(aspectRatio !== null);
154160
}, []);
161+
const hiddenContentSize = presentation.content
162+
? {
163+
width: presentation.content.width / presentation.content.scale,
164+
height: presentation.content.height / presentation.content.scale,
165+
}
166+
: null;
155167
const hiddenSize =
156168
viewport._tag !== "fill"
157169
? {
158170
width: viewport.width * normalizedZoomFactor,
159171
height: viewport.height * normalizedZoomFactor,
160172
}
161-
: { width: lastRect?.width ?? 1280, height: lastRect?.height ?? 800 };
173+
: {
174+
width: hiddenContentSize?.width ?? lastRect?.width ?? 1280,
175+
height: hiddenContentSize?.height ?? lastRect?.height ?? 800,
176+
};
162177
const containerSize = active && lastRect ? lastRect : hiddenSize;
163178
const deviceToolbarVisible = active && viewport._tag !== "fill" && !presentation.fitSourceContent;
164179
const {
@@ -169,7 +184,7 @@ export function HostedBrowserWebview(props: {
169184
handleResizePointerDown,
170185
layout: viewportLayout,
171186
} = useBrowserViewportResize({
172-
tabId,
187+
tabId: runtimeTabId,
173188
viewport,
174189
zoomFactor,
175190
containerSize,
@@ -178,31 +193,11 @@ export function HostedBrowserWebview(props: {
178193
});
179194
const fittedSourceViewport =
180195
presentation.fitSourceContent && lastRect
181-
? presentation.fittedSourceContent
182-
? {
183-
_tag: "freeform" as const,
184-
width: Math.max(
185-
1,
186-
Math.round(
187-
presentation.fittedSourceContent.width /
188-
presentation.fittedSourceContent.scale /
189-
normalizedZoomFactor,
190-
),
191-
),
192-
height: Math.max(
193-
1,
194-
Math.round(
195-
presentation.fittedSourceContent.height /
196-
presentation.fittedSourceContent.scale /
197-
normalizedZoomFactor,
198-
),
199-
),
200-
}
201-
: {
202-
_tag: "freeform" as const,
203-
width: viewport._tag === "fill" ? 1280 : viewport.width,
204-
height: viewport._tag === "fill" ? 800 : viewport.height,
205-
}
196+
? resolveFittedBrowserViewport(
197+
viewport,
198+
presentation.fittedSourceContent,
199+
normalizedZoomFactor,
200+
)
206201
: null;
207202
const layout =
208203
fittedSourceViewport && lastRect
@@ -212,7 +207,7 @@ export function HostedBrowserWebview(props: {
212207
const syncContentPresentation = useCallback(() => {
213208
const wrapper = wrapperRef.current;
214209
if (!wrapper) return;
215-
useBrowserSurfaceStore.getState().presentContent(tabId, {
210+
useBrowserSurfaceStore.getState().presentContent(runtimeTabId, {
216211
x: layout.viewportX,
217212
y: layout.viewportY,
218213
width: layout.viewportWidth,
@@ -221,7 +216,7 @@ export function HostedBrowserWebview(props: {
221216
scrollLeft: wrapper.scrollLeft,
222217
scrollTop: wrapper.scrollTop,
223218
});
224-
}, [layout, tabId]);
219+
}, [layout, runtimeTabId]);
225220

226221
useEffect(() => {
227222
const frameId = window.requestAnimationFrame(syncContentPresentation);
@@ -232,7 +227,7 @@ export function HostedBrowserWebview(props: {
232227
const wrapper = wrapperRef.current;
233228
if (!wrapper) return;
234229
wrapper.scrollTo({ left: 0, top: 0 });
235-
}, [tabId, viewport._tag, viewportHeight, viewportWidth]);
230+
}, [runtimeTabId, viewport._tag, viewportHeight, viewportWidth]);
236231

237232
if (!config) return null;
238233

@@ -249,7 +244,7 @@ export function HostedBrowserWebview(props: {
249244
className="fixed overflow-hidden bg-muted/35"
250245
style={{ ...wrapperStyle, overscrollBehavior: "contain" }}
251246
onScroll={syncContentPresentation}
252-
data-preview-viewport={tabId}
247+
data-preview-viewport={runtimeTabId}
253248
>
254249
<div className="relative" style={{ width: layout.canvasWidth, height: layout.canvasHeight }}>
255250
{deviceToolbarVisible && effectiveViewport._tag !== "fill" ? (
@@ -268,7 +263,8 @@ export function HostedBrowserWebview(props: {
268263
partition={config.partition}
269264
webpreferences={config.webPreferences}
270265
{...(config.preloadUrl ? { preload: config.preloadUrl } : {})}
271-
data-preview-tab={tabId}
266+
data-preview-tab={runtimeTabId}
267+
data-preview-server-tab={tabId}
272268
data-preview-viewport-mode={effectiveViewport._tag}
273269
data-preview-viewport-key={browserViewportSettingKey(effectiveViewport)}
274270
data-preview-css-width={

‎apps/web/src/browser/browserRecording.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ import {
8989
BROWSER_RECORDING_FIRST_FRAME_SIZE_TIMEOUT_MS,
9090
BROWSER_RECORDING_STARTUP_SETTLE_TIMEOUT_MS,
9191
BrowserRecordingConflictError,
92+
findActiveBrowserRecordingRuntimeTabId,
9293
readActiveBrowserRecordingTabIds,
94+
readActiveBrowserRecordingTargets,
9395
startBrowserRecording,
9496
stopBrowserRecording,
9597
} from "./browserRecording";
98+
import { previewRuntimeTabId } from "./previewRuntimeTabId";
9699

97100
class FakeMediaRecorder {
98101
static isTypeSupported(): boolean {
@@ -363,6 +366,46 @@ describe("browser recording", () => {
363366
expect(save).toHaveBeenCalledTimes(2);
364367
});
365368

369+
it("keeps a recording reachable through its runtime id after a server epoch changes", async () => {
370+
const threadRef = {
371+
environmentId: EnvironmentId.make("environment-recording"),
372+
threadId: ThreadId.make("thread-recording-scoped"),
373+
};
374+
const runtimeTabId = previewRuntimeTabId(threadRef, "epoch-a", "tab_1");
375+
surfaceState.byTabId = {
376+
[runtimeTabId]: {
377+
visible: false,
378+
rect: { x: 0, y: 0, width: 1280, height: 800 },
379+
content: {
380+
x: 0,
381+
y: 0,
382+
width: 1280,
383+
height: 800,
384+
scale: 1,
385+
scrollLeft: 0,
386+
scrollTop: 0,
387+
},
388+
},
389+
};
390+
391+
await startBrowserRecording(runtimeTabId, threadRef, "tab_1");
392+
393+
expect(startScreencast).toHaveBeenCalledWith(runtimeTabId);
394+
expect(readActiveBrowserRecordingTabIds(threadRef)).toEqual(new Set([runtimeTabId]));
395+
expect(readActiveBrowserRecordingTargets(threadRef)).toEqual([
396+
{ runtimeTabId, serverTabId: "tab_1" },
397+
]);
398+
expect(findActiveBrowserRecordingRuntimeTabId(threadRef, "tab_1")).toBe(runtimeTabId);
399+
400+
const replacementRuntimeTabId = previewRuntimeTabId(threadRef, "epoch-b", "tab_1");
401+
await expect(
402+
startBrowserRecording(replacementRuntimeTabId, threadRef, "tab_1"),
403+
).rejects.toBeInstanceOf(BrowserRecordingConflictError);
404+
expect(startScreencast).toHaveBeenCalledTimes(1);
405+
406+
await stopBrowserRecording(runtimeTabId);
407+
});
408+
366409
it("does not report success for a second start while the first is still starting", async () => {
367410
let finishStartingScreencast: (() => void) | undefined;
368411
startScreencast.mockImplementationOnce(async (tabId: string) => {

0 commit comments

Comments
 (0)