Skip to content

Commit 6902069

Browse files
committed
fix(studio): seek ruler clicks to the pressed position and round retime percentages
A ruler press with no pointer movement settled the playhead at t=0 instead of the clicked time. handlePointerUp replays pendingClientXRef, which only the pointermove path wrote, so a plain click fell back to the ref's initial 0 and overwrote the correct pointerdown seek. Seed the ref on pointerdown. The keyframe retime move branch also returned the raw quotient while the resize branch rounded to 3dp, so values like 74.81203007518799% landed in the user's source and churned the diff on every drag. Round at the point of computation so the no-op test and the written value agree.
1 parent c691869 commit 6902069

4 files changed

Lines changed: 178 additions & 1 deletion

File tree

‎packages/studio/src/components/editor/keyframeRetime.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,45 @@ describe("resolveKeyframeRetime — guards", () => {
180180
expect(r.pctRemap).toEqual([]);
181181
});
182182
});
183+
184+
describe("resolveKeyframeRetime — move percentages are rounded like the resize path", () => {
185+
// The move branch used to return the raw quotient, so `74.81203007518799%`
186+
// landed in the user's source and churned the diff on every drag.
187+
const decimals = (n: number): number => String(n).split(".")[1]?.length ?? 0;
188+
189+
it("rounds a repeating quotient to 3dp", () => {
190+
const r = resolveKeyframeRetime({
191+
tweenStart: 2,
192+
tweenDuration: 3,
193+
keyframes: KEYFRAMES,
194+
draggedTweenPct: 0,
195+
dropAbsTime: 3, // (3-2)/3 = 33.333333333333336%
196+
});
197+
expect(r.kind).toBe("move");
198+
expect(r.toTweenPct).toBe(33.333);
199+
});
200+
201+
it("never emits more than 3 decimal places", () => {
202+
for (const tweenDuration of [3, 7, 9, 11, 133]) {
203+
const r = resolveKeyframeRetime({
204+
tweenStart: 2,
205+
tweenDuration,
206+
keyframes: KEYFRAMES,
207+
draggedTweenPct: 0,
208+
dropAbsTime: 3,
209+
});
210+
expect(r.kind).toBe("move");
211+
expect(decimals(r.toTweenPct ?? 0)).toBeLessThanOrEqual(3);
212+
}
213+
});
214+
215+
it("leaves an already-short percentage untouched", () => {
216+
const r = resolveKeyframeRetime({
217+
...WINDOW,
218+
keyframes: KEYFRAMES,
219+
draggedTweenPct: 0,
220+
dropAbsTime: 3, // (3-2)/4 = exactly 25%
221+
});
222+
expect(r.toTweenPct).toBe(25);
223+
});
224+
});

‎packages/studio/src/components/editor/keyframeRetime.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ export function resolveKeyframeRetime(opts: {
121121

122122
// Within the tween window → plain move (re-key the tween-%).
123123
if (dropAbsTime >= tweenStart - EPSILON_TIME && dropAbsTime <= tweenEnd + EPSILON_TIME) {
124-
const toTweenPct = clamp(((dropAbsTime - tweenStart) / tweenDuration) * 100, 0, 100);
124+
// Round here, not at the return: the no-op test below and the value written
125+
// to source must be the same number. The resize branch already rounds, so
126+
// this keeps both write paths at the authored 3dp precision.
127+
const toTweenPct = round3(clamp(((dropAbsTime - tweenStart) / tweenDuration) * 100, 0, 100));
125128
if (Math.abs(toTweenPct - draggedTweenPct) < NOOP_EPSILON_PCT) return { kind: "noop" };
126129
return { kind: "move", toTweenPct };
127130
}

‎packages/studio/src/player/components/useTimelineRangeSelection.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ export function useTimelineRangeSelection({
268268
if (!point || !scrollRect || isTimelineRulerPress(e.clientY, scrollRect.top)) {
269269
isDragging.current = true;
270270
setIsScrubbing(true);
271+
// Seed the pending coordinate so a press with no pointermove still
272+
// replays THIS x on pointerup. `updateScrubDrag` is the only other
273+
// writer, so without this a plain click settles on the ref's initial
274+
// 0 and clamps the playhead back to t=0.
275+
pendingClientXRef.current = e.clientX;
271276
seekFromX(e.clientX);
272277
return;
273278
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// @vitest-environment happy-dom
2+
3+
// Regression guard for the ruler-click seek. `handlePointerUp` replays
4+
// `pendingClientXRef`, which only `updateScrubDrag` (pointermove) used to write,
5+
// so a press with no move settled on the ref's initial 0 and clamped the
6+
// playhead to t=0 — silently discarding the correct pointerdown seek.
7+
import React, { act } from "react";
8+
import { afterEach, describe, expect, it, vi } from "vitest";
9+
import { mountReactHarness } from "../../hooks/domSelectionTestHarness";
10+
import { useTimelineRangeSelection } from "./useTimelineRangeSelection";
11+
12+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
13+
14+
/** Scroll container top; a press within RULER_H of this counts as a ruler press. */
15+
const SCROLL_TOP = 100;
16+
/** Comfortably inside the ruler band (RULER_H is 28 at time of writing). */
17+
const RULER_Y = SCROLL_TOP + 4;
18+
19+
type Handlers = ReturnType<typeof useTimelineRangeSelection>;
20+
21+
function makeScrollEl(): HTMLDivElement {
22+
const el = document.createElement("div");
23+
el.getBoundingClientRect = () =>
24+
({ top: SCROLL_TOP, left: 0, width: 1000, height: 400 }) as DOMRect;
25+
return el;
26+
}
27+
28+
function makePointerEvent(clientX: number, clientY: number): React.PointerEvent {
29+
const currentTarget = document.createElement("div");
30+
currentTarget.setPointerCapture = vi.fn();
31+
return {
32+
button: 0,
33+
shiftKey: false,
34+
metaKey: false,
35+
ctrlKey: false,
36+
pointerId: 1,
37+
clientX,
38+
clientY,
39+
target: document.createElement("div"),
40+
currentTarget,
41+
} as unknown as React.PointerEvent;
42+
}
43+
44+
const roots: Array<{ unmount: () => void }> = [];
45+
46+
afterEach(() => {
47+
for (const root of roots.splice(0)) act(() => root.unmount());
48+
});
49+
50+
function setup(): { handlers: () => Handlers; seekFromX: ReturnType<typeof vi.fn> } {
51+
const seekFromX = vi.fn();
52+
const scrollEl = makeScrollEl();
53+
let latest: Handlers | null = null;
54+
55+
// Hoisted so they survive re-renders. The hook mutates `isDragging.current`
56+
// across the press, and a fresh object per render would reset it to false.
57+
const scrollRef = { current: scrollEl };
58+
const ppsRef = { current: 25 };
59+
const dragScrollRaf = { current: 0 };
60+
const isDragging = { current: false };
61+
const elementsRef = { current: [] };
62+
const trackOrderRef = { current: [] };
63+
const rowHeightsRef = { current: [] };
64+
65+
function Probe(): null {
66+
latest = useTimelineRangeSelection({
67+
scrollRef,
68+
ppsRef,
69+
effectiveDuration: 30,
70+
pps: 25,
71+
seekFromX,
72+
autoScrollDuringDrag: vi.fn(),
73+
dragScrollRaf,
74+
isDragging,
75+
setShowPopover: vi.fn(),
76+
elementsRef,
77+
trackOrderRef,
78+
rowHeightsRef,
79+
contentOrigin: 0,
80+
});
81+
return null;
82+
}
83+
84+
roots.push(mountReactHarness(<Probe />));
85+
return {
86+
handlers: () => {
87+
if (!latest) throw new Error("hook did not render");
88+
return latest;
89+
},
90+
seekFromX,
91+
};
92+
}
93+
94+
describe("useTimelineRangeSelection — ruler press seek", () => {
95+
it("replays the pressed x on pointerup when the pointer never moved", () => {
96+
const { handlers, seekFromX } = setup();
97+
98+
act(() => handlers().handlePointerDown(makePointerEvent(1000, RULER_Y)));
99+
act(() => handlers().handlePointerUp());
100+
101+
// Both the press and the settle must land on the SAME x. The bug settled on
102+
// 0, so the LAST call is the one that decides where the playhead ends up.
103+
expect(seekFromX).toHaveBeenCalledWith(1000);
104+
expect(seekFromX).not.toHaveBeenCalledWith(0);
105+
expect(seekFromX.mock.calls.at(-1)).toEqual([1000]);
106+
});
107+
108+
it("does not fall back to x=0 for a press nearer the left edge either", () => {
109+
const { handlers, seekFromX } = setup();
110+
111+
act(() => handlers().handlePointerDown(makePointerEvent(400, RULER_Y)));
112+
act(() => handlers().handlePointerUp());
113+
114+
expect(seekFromX.mock.calls.at(-1)).toEqual([400]);
115+
});
116+
117+
it("settles on the final pointermove x when the pointer did move", () => {
118+
const { handlers, seekFromX } = setup();
119+
120+
act(() => handlers().handlePointerDown(makePointerEvent(700, RULER_Y)));
121+
act(() => handlers().handlePointerMove(makePointerEvent(760, RULER_Y)));
122+
act(() => handlers().handlePointerUp());
123+
124+
// The move must win over the seeded press coordinate.
125+
expect(seekFromX.mock.calls.at(-1)).toEqual([760]);
126+
});
127+
});

0 commit comments

Comments
 (0)