From 1e040f6fef08c9589fec5f48c4bb67b5acdd5eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Mon, 16 Nov 2026 06:27:15 +0000 Subject: [PATCH] fix(studio): the preview picture takes its box's size in the same frame, scaled or not (#7907) * fix(studio): the preview picture takes a new box size in the same frame The preview stage measured its box in a ResizeObserver and stored the size in React state, which rendered a frame later. A box that grows at once (the window going full screen) painted one frame with the old, small picture, and macOS animated that frame for its whole full-screen transition. * fix(studio): the preview picture takes a new box size in the same frame * fix(studio): the preview picture is sized from its box's layout, not its on-screen rect * style(studio): format the preview stage size test --- .../src/components/nle/NLEPreview.test.ts | 38 +++++++++++++++++-- .../studio/src/components/nle/NLEPreview.tsx | 9 +++-- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/packages/studio/src/components/nle/NLEPreview.test.ts b/packages/studio/src/components/nle/NLEPreview.test.ts index faf7a3cb91..dd97454f04 100644 --- a/packages/studio/src/components/nle/NLEPreview.test.ts +++ b/packages/studio/src/components/nle/NLEPreview.test.ts @@ -56,10 +56,9 @@ class MockResizeObserver { constructor(cb: ResizeObserverCallback) { this.cb = cb; } + // A browser calls back at its next frame, never inside observe(); renderPreview fires them. observe() { - const fire = () => this.cb([], this as unknown as ResizeObserver); - resizeCallbacks.push(fire); - fire(); + resizeCallbacks.push(() => this.cb([], this as unknown as ResizeObserver)); } disconnect() {} } @@ -67,6 +66,8 @@ class MockResizeObserver { const originalResizeObserver = globalThis.ResizeObserver; function setRect(node: Element, rect: { width: number; height: number }) { + Object.defineProperty(node, "clientWidth", { configurable: true, value: rect.width }); + Object.defineProperty(node, "clientHeight", { configurable: true, value: rect.height }); Object.defineProperty(node, "getBoundingClientRect", { configurable: true, value: () => ({ @@ -493,6 +494,37 @@ describe("NLEPreview", () => { filled.cleanup(); }); + it("sizes the stage within the resize callback, so the frame painted after a resize is never at the old size", () => { + const view = renderPreview(undefined, { box: { width: 640, height: 360 }, fillBox: true }); + setRect(view.viewport, { width: 1920, height: 1080 }); + // Outside act: the browser paints right after this callback returns. + for (const fire of resizeCallbacks) fire(); + expect([view.stage.style.width, view.stage.style.height]).toEqual(["1920px", "1080px"]); + view.cleanup(); + }); + + it("sizes the stage from the box's own size while a host scales the box on screen", () => { + const view = renderPreview(undefined, { box: { width: 640, height: 360 }, fillBox: true }); + setRect(view.viewport, { width: 1920, height: 1080 }); + // A host growing the preview from a quarter of its size: on screen the box is 480 x 270 for now. + Object.defineProperty(view.viewport, "getBoundingClientRect", { + configurable: true, + value: () => ({ + x: 0, + y: 0, + left: 0, + top: 0, + right: 480, + bottom: 270, + width: 480, + height: 270, + }), + }); + for (const fire of resizeCallbacks) fire(); + expect([view.stage.style.width, view.stage.style.height]).toEqual(["1920px", "1080px"]); + view.cleanup(); + }); + it("takes the host's composition size as the stage's shape until the preview measures its own", () => { const box = { width: 800, height: 600 }; const plain = renderPreview(undefined, { box }); diff --git a/packages/studio/src/components/nle/NLEPreview.tsx b/packages/studio/src/components/nle/NLEPreview.tsx index 0bcb4f7afc..2ddd52ccd8 100644 --- a/packages/studio/src/components/nle/NLEPreview.tsx +++ b/packages/studio/src/components/nle/NLEPreview.tsx @@ -11,6 +11,7 @@ import { useState, type RefObject, } from "react"; +import { flushSync } from "react-dom"; import { Player } from "../../player"; import { postFrameDisplayScale } from "../../player/lib/runtimeProtocol"; import type { NLEContextValue } from "./NLEContext"; @@ -148,11 +149,10 @@ export const NLEPreview = memo(function NLEPreview({ if (!viewport) return; const updateStageSize = () => { - const rect = viewport.getBoundingClientRect(); setStageSize( resolvePreviewStageSize( - rect.width, - rect.height, + viewport.clientWidth, + viewport.clientHeight, compositionSize ?? (hintWidth && hintHeight ? { width: hintWidth, height: hintHeight } : null), portrait, @@ -163,7 +163,8 @@ export const NLEPreview = memo(function NLEPreview({ }; updateStageSize(); - const observer = new ResizeObserver(updateStageSize); + const resizeBeforePaint = () => flushSync(updateStageSize); + const observer = new ResizeObserver(resizeBeforePaint); observer.observe(viewport); return () => observer.disconnect(); }, [compositionSize, hintWidth, hintHeight, portrait, gutterPx, insetPx]);