From b3da2cf58fb36dd2a7b346424da2ad4cb6b4e323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 24 Nov 2026 19:41:04 +0000 Subject: [PATCH] fix(studio): a refused sealed press always puts the box back on the layer (#8735) * test(studio): a sealed press the frame never answers is refused, box drawn or not * fix(studio): a sealed press the frame never answers is refused with a reason * test(studio): a refused sealed press puts Studio's box back on the layer * fix(studio): a refused sealed press puts Studio's box back on the layer * test(studio): a refused sealed press puts the box back after another press or a cleared selection * fix(studio): a refused sealed press puts the box where the press before it left the layer --- .../editor/domEditOverlayGestures.ts | 2 + .../components/editor/previewGestureStarts.ts | 15 +++-- .../editor/previewOverlayRects.test.ts | 55 +++++++++++++++++++ 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/packages/studio/src/components/editor/domEditOverlayGestures.ts b/packages/studio/src/components/editor/domEditOverlayGestures.ts index 869cbbdbdf..516fd5dc4b 100644 --- a/packages/studio/src/components/editor/domEditOverlayGestures.ts +++ b/packages/studio/src/components/editor/domEditOverlayGestures.ts @@ -139,6 +139,8 @@ export interface WaitingPressState { released: React.PointerEvent | null; after: WaitingPressState | null; ended: boolean; + refused: boolean; + drawBack: () => void; } export type FocusableDomEditOverlay = { diff --git a/packages/studio/src/components/editor/previewGestureStarts.ts b/packages/studio/src/components/editor/previewGestureStarts.ts index dc0e14215d..ffa29f490b 100644 --- a/packages/studio/src/components/editor/previewGestureStarts.ts +++ b/packages/studio/src/components/editor/previewGestureStarts.ts @@ -173,6 +173,7 @@ export function createPreviewGestureStarts( // The press already holds the pointer, which may be up by the time the gesture starts. const held = { setPointerCapture() {} }; const down = { ...e, currentTarget: held, preventDefault() {}, stopPropagation() {} }; + const before = opts.waitingPressRef.current; const press: WaitingPressState = { pointerId: e.pointerId, startX: e.clientX, @@ -181,8 +182,13 @@ export function createPreviewGestureStarts( frame: 0, moved: null, released: null, - after: opts.waitingPressRef.current, + after: before, ended: false, + refused: false, + drawBack: () => { + if (before?.refused) before.drawBack(); + else draw?.(0, 0); + }, }; opts.waitingPressRef.current = press; const onSave = !!press.after || isPreviewChanging() || isStudioEditSaving(); @@ -267,10 +273,11 @@ export function createPreviewGestureStarts( return true; }; const sayWhyNotStarted = (step: "lost" | "run" | "unanswered") => { + if (step !== "unanswered" || !channel) return; + press.refused = true; + press.drawBack(); const pressed = opts.selectionRef.current; - if (step !== "unanswered" || !channel || !pressed) return; - press.draw?.(0, 0); - opts.onBlockedMoveRef.current(pressed, unansweredMoveReason(channel)); + if (pressed) opts.onBlockedMoveRef.current(pressed, unansweredMoveReason(channel)); }; pollFirst(channel, press, poll); return true; diff --git a/packages/studio/src/components/editor/previewOverlayRects.test.ts b/packages/studio/src/components/editor/previewOverlayRects.test.ts index b77dd3e91a..4218e6c19c 100644 --- a/packages/studio/src/components/editor/previewOverlayRects.test.ts +++ b/packages/studio/src/components/editor/previewOverlayRects.test.ts @@ -676,6 +676,61 @@ describe("a press held across a preview change on an opaque frame", () => { expect(opts.overlayRectRef.current).toEqual(drawn); }); + // fails without the fix: the second press measured its box from the first drag; refused, it went back 30 px off. + it("puts the box back on the layer when two presses in a row go unanswered", async () => { + const turned = makeSelection("turned", document.getElementById("turned")!); + const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect; + const { opts, startGesture, down } = await pressDuringChange(turned, { + iframe: silentFrame(), + overlayRect: drawn, + whileWaiting: (o) => o.waitingPressRef.current!.draw!(30, 0), + }); + startGesture("drag", down as unknown as React.PointerEvent); + opts.waitingPressRef.current!.draw!(30, 0); + await vi.waitFor(() => expect(opts.onBlockedMoveRef.current).toHaveBeenCalledTimes(2), { + timeout: 8000, + }); + expect(opts.overlayRectRef.current).toEqual(drawn); + }, 10_000); + + // The first press started, so the layer goes where it dropped: the refused second press's own box, not the first's. + it("puts the box where a started press left it when the press after it goes unanswered", async () => { + const turned = makeSelection("turned", document.getElementById("turned")!); + const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect; + const { opts, startGesture, down } = await pressDuringChange(turned, { + overlayRect: drawn, + whileWaiting: (o) => o.waitingPressRef.current!.draw!(30, 0), + }); + press.start.mockImplementationOnce(() => { + opts.iframeRef.current = silentFrame(); + return true; + }); + startGesture("drag", down as unknown as React.PointerEvent); + opts.waitingPressRef.current!.draw!(30, 0); + await vi.waitFor(() => expect(opts.onBlockedMoveRef.current).toHaveBeenCalledOnce(), { + timeout: 3000, + }); + expect(press.start).toHaveBeenCalledOnce(); + expect(opts.overlayRectRef.current).toEqual({ ...drawn, left: 31 }); + }); + + // fails without the fix: with the selection cleared mid-wait, the refusal left the box where it was dragged. + it("puts the box back on the layer when the selection clears while an unanswered press waits", async () => { + const turned = makeSelection("turned", document.getElementById("turned")!); + const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect; + const { opts } = await pressDuringChange(turned, { + iframe: silentFrame(), + overlayRect: drawn, + whileWaiting: (o) => { + o.waitingPressRef.current!.draw!(30, 0); + o.selectionRef.current = null; + }, + }); + await vi.waitFor(() => expect(opts.waitingPressRef.current).toBeNull(), { timeout: 3000 }); + expect(opts.overlayRectRef.current).toEqual(drawn); + expect(opts.onBlockedMoveRef.current).not.toHaveBeenCalled(); + }); + it("asks the frame shown again when a swap comes after its first ask", async () => { const turned = document.getElementById("turned")!; const drawn = { left: 1, top: 2, width: 3, height: 4 } as OverlayRect;