Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ describe("a press while the preview reloads", () => {
pointer(editor.box, "pointerdown", 150, 150);
pointer(editor.overlay, "pointermove", 190, 170);
expect(marked(editor.live.contentDocument!), "the page being replaced").toHaveLength(0);
expect(editor.box.hasAttribute(PRESS_WAITING_ATTR), "drawn at the pointer").toBe(true);
expect(editor.box.getAttribute(PRESS_WAITING_ATTR), "drawn at the pointer").toBe("save");
pointer(editor.overlay, "pointerup", 190, 170);
act(() => usePlayerStore.setState({ currentTime: 3 }));
let pressedAt: number | undefined;
Expand Down
5 changes: 3 additions & 2 deletions packages/studio/src/components/editor/pressOutline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type OutlineRefs = Pick<
"boxRef" | "rafPausedRef" | "iframeRef" | "waitingPressRef"
>;
type ReplayDrawWait = { frame: HTMLIFrameElement };
type PressWait = "frame" | "save";

function createPressOutline({ boxRef, rafPausedRef, iframeRef, waitingPressRef }: OutlineRefs) {
let drawWait: ReplayDrawWait | null = null;
Expand All @@ -30,9 +31,9 @@ function createPressOutline({ boxRef, rafPausedRef, iframeRef, waitingPressRef }
});
return {
idle,
waitOnSave() {
waitOnSave(reason: PressWait) {
drawWait = null;
boxRef.current?.setAttribute(PRESS_WAITING_ATTR, "true");
boxRef.current?.setAttribute(PRESS_WAITING_ATTR, reason);
rafPausedRef.current = true;
},
/** A replay ended: `drewIn` is the sealed frame its draws went to, null if none. */
Expand Down
5 changes: 3 additions & 2 deletions packages/studio/src/components/editor/previewGestureStarts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
startGroupDrag as _startGroupDrag,
} from "./domEditOverlayStartGesture";
import { giveUpOnPreviewChange, isPreviewChanging } from "../../player/previewReloading";
import { setStudioWaitingPressCancel } from "../../utils/studioPendingEdits";
import { isStudioEditSaving, setStudioWaitingPressCancel } from "../../utils/studioPendingEdits";
import { playheadMoment } from "../../hooks/editMoment";
import type { EditMoment } from "./manualEditsTypes";
import { previewFrameOpaque } from "../../player/lib/previewChannel";
Expand Down Expand Up @@ -175,7 +175,8 @@ export function createPreviewGestureStarts(
ended: false,
};
opts.waitingPressRef.current = press;
outline.waitOnSave();
const onSave = !!press.after || isPreviewChanging() || isStudioEditSaving();
outline.waitOnSave(onSave ? "save" : "frame");
let shownFrames = 0;
let settledFrames = 0;
const since = performance.now();
Expand Down
42 changes: 36 additions & 6 deletions packages/studio/src/components/editor/previewOverlayRects.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,23 @@ describe("a press held across a preview change on an opaque frame", () => {
) {
press.changing = true;
press.start.mockClear();
const { opts, overlay, down, starts } = pressOn(selection, { iframe, overlayRect, box });
starts.startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
await new Promise((resolve) => setTimeout(resolve, 50));
expect(press.start).not.toHaveBeenCalled();
whileWaiting(opts);
press.changing = false;
return { opts, overlay, down, ...starts };
}

function pressOn(
selection: DomEditSelection,
{
iframe,
overlayRect,
box,
}: { iframe: HTMLIFrameElement; overlayRect: OverlayRect | null; box: HTMLDivElement | null },
) {
const overlay = sized(document.createElement("div"), [20, 10, 1200, 800]) as HTMLDivElement;
const opts = {
waitingPressRef: { current: null },
Expand All @@ -424,14 +441,27 @@ describe("a press held across a preview change on an opaque frame", () => {
preventDefault: vi.fn(),
stopPropagation: vi.fn(),
};
starts.startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
await new Promise((resolve) => setTimeout(resolve, 50));
expect(press.start).not.toHaveBeenCalled();
whileWaiting(opts);
press.changing = false;
return { opts, overlay, down, ...starts };
return { opts, overlay, down, starts };
}

// fails without the fix: the box said only that a press waited, so the edit bench could not tell a press waiting on
// the frame's answer, its outline resting on the layer, from one waiting on a save, its layer still where it was.
it.each([
[false, "frame"],
[true, "save"],
])(
"says why a press on an opaque frame waits, the preview changing: %s",
async (changing, reason) => {
const turned = document.getElementById("turned")!;
const stale = { ...makeSelection("turned", turned), handle: `${"f".repeat(16)}:1` };
const box = document.createElement("div");
press.changing = changing;
const { starts, down } = pressOn(stale, { iframe: opaqueFrame(), overlayRect: null, box });
starts.startGesture("drag", down as unknown as React.PointerEvent<HTMLElement>);
expect(box.getAttribute(PRESS_WAITING_ATTR)).toBe(reason);
},
);

// fails without the fix: the outline went at once while the frame still showed the layer where the last save put
// it, so for one frame the layer showed at the drag's start (312 px on the edit bench's seqheldtwice case).
it("keeps the waiting outline until the frame has drawn the replayed drag", async () => {
Expand Down
19 changes: 14 additions & 5 deletions packages/studio/tests/e2e/edit-accuracy/case.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -718,10 +718,19 @@ async function waitingQuad(page, map) {
return quad;
}

const WAITING_BOX = "[data-dom-edit-press-waiting]";
const WAITING_ON_SAVE = `${WAITING_BOX}:not([data-dom-edit-press-waiting="frame"])`;

/** The box as the preview shows it: the waiting marker's quad while a press waits for its replay, else the element. */
export async function measureShown(ctx, gesture, read = measure, quadOf = contentQuad) {
export async function measureShown(
ctx,
gesture,
read = measure,
quadOf = contentQuad,
pointerMoved = true,
) {
for (;;) {
const waiting = await ctx.page.$("[data-dom-edit-press-waiting]");
const waiting = await ctx.page.$(pointerMoved ? WAITING_BOX : WAITING_ON_SAVE);
try {
const m = await read(ctx);
if (gesture === "crop") m.outline = await cropOutline(ctx, m.map);
Expand All @@ -740,8 +749,8 @@ export async function measureShown(ctx, gesture, read = measure, quadOf = conten
}
}

export async function sample(ctx, gesture, point, pointerScreen) {
const { m, actual } = await measureShown(ctx, gesture);
export async function sample(ctx, gesture, point, pointerScreen, pointerMoved = true) {
const { m, actual } = await measureShown(ctx, gesture, measure, contentQuad, pointerMoved);
return { m, actual, p: point(m), c: m.map.toComp(pointerScreen) };
}

Expand Down Expand Up @@ -929,7 +938,7 @@ export async function pointerGesture(ctx, gesture, pre, route) {
traceBench("press", { gesture });
await ctx.page.mouse.down();
await nextFrame(ctx.page);
const s0 = await sample(ctx, gesture, g.point, press);
const s0 = await sample(ctx, gesture, g.point, press, false);
traceBench("pressed", { gesture, jump: pre && quadDistance(s0.m.visible, pre.visible) });
await recording(ctx.page, true);
const errors = [];
Expand Down
25 changes: 25 additions & 0 deletions packages/studio/tests/e2e/edit-accuracy/settle.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,31 @@ describe("the box a press is measured against", () => {
expect(shown.actual[0][0]).toBe(0);
});

// fails without the fix: the press itself read the waiting outline, so a sealed press jumped 0.2 px.
it("reads the element at the press, before the pointer moves", async () => {
const shown = await measureShown(
{ page: { $: async (selector) => (selector.includes('="frame"') ? null : marker([true])) } },
"move",
read,
async () => quad(260),
false,
);
expect(shown.m.visible[0][0]).toBe(0);
});

// fails without the fix: a press made while Studio saved read the element, still where the last save
// left it, 312 px from the box Studio draws at the pointer.
it("reads the waiting box at the press while it waits on a save", async () => {
const shown = await measureShown(
{ page: { $: async () => marker([true]) } },
"move",
read,
async () => quad(260),
false,
);
expect(shown.m.visible[0][0]).toBe(260);
});

it("reads the element when nothing waits", async () => {
const shown = await measureShown({ page: page([]) }, "move", read, async () => quad(260));
expect(shown.m.visible[0][0]).toBe(0);
Expand Down
23 changes: 19 additions & 4 deletions packages/studio/tests/e2e/edit-accuracy/teleport.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ function frameSampler() {
const waiting = document.querySelector("[data-dom-edit-press-waiting]");
return {
...(outline && { outline: quadOf(outline) }),
...(waiting && { waitingQuad: quadOf(waiting) }),
...(waiting && {
waitingQuad: quadOf(waiting),
waitingOnSave: waiting.getAttribute("data-dom-edit-press-waiting") !== "frame",
}),
};
};
// Each push becomes a sample paired with the pointer at the push's own stamp, so a late push is never scored
Expand Down Expand Up @@ -444,12 +447,24 @@ function trackers(gesture, first, p0) {

const round = (v) => Math.round(v * 100) / 100;

/** Until the pointer moves, a box waiting only on the frame's answer is Studio's resting outline: read the element. */
function untilPointerMoves(frames) {
const [x, y] = frames[0]?.pointer ?? [];
const moved = frames.findIndex((f) => f.pointer[0] !== x || f.pointer[1] !== y);
const atRest = (i) => moved < 0 || i < moved;
return frames.map((f, i) =>
f.waitingQuad && !f.waitingOnSave && atRest(i) ? { ...f, waitingQuad: undefined } : f,
);
}

/** Fails a frame whose tracked point outran the pointer or left its path; after release, the release point. */
// fallow-ignore-next-line complexity
export function scoreTeleport(gesture, samples) {
const frames = samples
.filter((s) => s.quad && s.root && s.pointer)
.map((s) => ({ ...s, map: compositionMapper(s.root, COMPOSITION) }));
const frames = untilPointerMoves(
samples
.filter((s) => s.quad && s.root && s.pointer)
.map((s) => ({ ...s, map: compositionMapper(s.root, COMPOSITION) })),
);
if (frames.length < 2) {
const count = (f) => samples.filter(f).length;
const failed = samples.find((s) => s.error)?.error;
Expand Down
13 changes: 13 additions & 0 deletions packages/studio/tests/e2e/edit-accuracy/teleport.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ describe("scoreTeleport", () => {
pass: true,
});
});
// fails without the fix: a sealed press anchored on Studio's outline, which sits off the element in both modes.
it("scores a press that waits with the pointer still like one that never waited", () => {
const sealed = drag.map((f, i) => (i < 2 ? { ...f, waitingQuad: box(0.2) } : f));
expect(scoreTeleport("move", sealed).max).toBe(scoreTeleport("move", drag).max);
});
// fails without the fix: a press that waited on Studio's save was scored from the stale element, not the box shown.
it("scores a press that waits on Studio from the box Studio shows", () => {
const waiting = drag.map((f, i) =>
i < 2 ? { ...f, quad: box(312), waitingQuad: f.quad, waitingOnSave: true } : f,
);
expect(scoreTeleport("move", waiting).max).toBe(scoreTeleport("move", drag).max);
});
it("rejects a jumping outline while the element waits", () => {
const jumped = waitingDrag.map((f, i) => (i === 4 ? { ...f, waitingQuad: box(f.t + 37) } : f));
expect(scoreTeleport("move", [...jumped, frame(50, false)]).pass).toBe(false);
Expand Down Expand Up @@ -580,6 +592,7 @@ describe("frameSamplerScript in a sealed preview", () => {
parentElement: null,
getRootNode: () => ({}),
getBoundingClientRect: () => ({ left: 0, top: 0, width: 1920, height: 1080 }),
getAttribute: () => "save",
};
try {
move(150);
Expand Down
Loading