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
This commit is contained in:
Miguel Ángel
2026-11-16 06:27:15 +00:00
committed by GitHub
parent 6411df4e35
commit 1e040f6fef
2 changed files with 40 additions and 7 deletions
@@ -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 });
@@ -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]);