diff --git a/packages/studio/src/player/components/TimelineAutomationLane.test.tsx b/packages/studio/src/player/components/TimelineAutomationLane.test.tsx index 375c3481c8..af73ba64f4 100644 --- a/packages/studio/src/player/components/TimelineAutomationLane.test.tsx +++ b/packages/studio/src/player/components/TimelineAutomationLane.test.tsx @@ -805,6 +805,74 @@ describe("TimelineAutomationLane stretch", () => { expect(t1).toBeLessThanOrEqual(4); }); + it("retimes identically whether the right edge arrives in one move or several", () => { + // moveEdge must always retime from the points snapshotted at arm time, + // never from the live draft — retimeRange is a RELATIVE transform (it + // scales the lane's OWN current point positions by newSpan/oldSpan), so + // feeding it the live draft on every pointermove compounds the scale + // factor instead of applying it once. A real drag fires dozens of moves; + // this asserts the FINAL preview is identical regardless of how many. + const onPreviewSingle = vi.fn(); + const single = mount(stretchable, { + rangeSelection: { t0: 0.5, t1: 2.5 }, + onPreview: onPreviewSingle, + }); + fire(single.svg, "pointerdown", at(2.5, 0.5)); + fire(single.svg, "pointermove", at(3.3, 0.5)); + const singleShot = (onPreviewSingle.mock.calls.at(-1)?.[0] as HfAutomation | undefined) + ?.lanes[0]?.points; + expect(singleShot).toBeDefined(); + + const onPreviewMulti = vi.fn(); + const multi = mount(stretchable, { + rangeSelection: { t0: 0.5, t1: 2.5 }, + onPreview: onPreviewMulti, + }); + fire(multi.svg, "pointerdown", at(2.5, 0.5)); + // At least 3 separate pointermoves crossing the same span, not one jump. + fire(multi.svg, "pointermove", at(2.7, 0.5)); + fire(multi.svg, "pointermove", at(2.9, 0.5)); + fire(multi.svg, "pointermove", at(3.1, 0.5)); + fire(multi.svg, "pointermove", at(3.3, 0.5)); + const afterFourMoves = (onPreviewMulti.mock.calls.at(-1)?.[0] as HfAutomation | undefined) + ?.lanes[0]?.points; + expect(afterFourMoves).toBeDefined(); + + // Both interior points (t=1, t=2) land exactly where a single-shot retime + // puts them — not compounded, and not dropped. + expect(afterFourMoves).toEqual(singleShot); + expect(afterFourMoves?.length).toBe(6); + expect(afterFourMoves?.some((p) => Math.abs(p.t - 1.2) < 0.001 && p.v === 0.5)).toBe(true); + expect(afterFourMoves?.some((p) => Math.abs(p.t - 2.6) < 0.001 && p.v === 0.8)).toBe(true); + }); + + it("retimes identically whether the left edge arrives in one move or several", () => { + const onPreviewSingle = vi.fn(); + const single = mount(stretchable, { + rangeSelection: { t0: 1, t1: 3 }, + onPreview: onPreviewSingle, + }); + fire(single.svg, "pointerdown", at(1, 0.5)); + fire(single.svg, "pointermove", at(0.2, 0.5)); + const singleShot = (onPreviewSingle.mock.calls.at(-1)?.[0] as HfAutomation | undefined) + ?.lanes[0]?.points; + expect(singleShot).toBeDefined(); + + const onPreviewMulti = vi.fn(); + const multi = mount(stretchable, { + rangeSelection: { t0: 1, t1: 3 }, + onPreview: onPreviewMulti, + }); + fire(multi.svg, "pointerdown", at(1, 0.5)); + fire(multi.svg, "pointermove", at(0.7, 0.5)); + fire(multi.svg, "pointermove", at(0.4, 0.5)); + fire(multi.svg, "pointermove", at(0.2, 0.5)); + const afterThreeMoves = (onPreviewMulti.mock.calls.at(-1)?.[0] as HfAutomation | undefined) + ?.lanes[0]?.points; + expect(afterThreeMoves).toBeDefined(); + expect(afterThreeMoves).toEqual(singleShot); + }); + it("shows a resize cursor when hovering an edge with nothing else live", () => { const { svg } = mount(ramp, { rangeSelection: { t0: 1, t1: 3 } }); fire(svg, "pointermove", at(3, 0.5)); // near the right edge, nothing pressed diff --git a/packages/studio/src/player/components/useAutomationLaneGestures.ts b/packages/studio/src/player/components/useAutomationLaneGestures.ts index 54214c6014..2eaf4b9cd0 100644 --- a/packages/studio/src/player/components/useAutomationLaneGestures.ts +++ b/packages/studio/src/player/components/useAutomationLaneGestures.ts @@ -12,7 +12,11 @@ */ import { useCallback, useRef, useState, type PointerEvent as ReactPointerEvent } from "react"; -import type { AutomationRange, HfAutomationLane } from "@hyperframes/core/audio-automation"; +import type { + AutomationRange, + HfAutomationLane, + HfAutomationPoint, +} from "@hyperframes/core/audio-automation"; import { applyShiftConstraint, curveForDrag, @@ -125,12 +129,17 @@ export function useAutomationLaneGestures({ * into an actual range, rather than a click that should just clear one. */ const rangeCrossed = useRef(false); /** An edge-stretch drag in progress: which edge, the selection it started - * from (kept fixed as the retime's untouched anchor), and the edge's own - * live position. */ + * from (kept fixed as the retime's untouched anchor), the edge's own live + * position, and the lane's points as they stood at arm time. `retimeRange` + * is a RELATIVE transform — it scales a lane's own current point positions + * by newSpan/oldSpan — so it must always run against this fixed snapshot, + * never against the live draft: retiming from the draft would compound the + * scale factor on every pointermove instead of applying it once. */ const [edgeDrag, setEdgeDrag] = useState<{ edge: "t0" | "t1"; origin: { t0: number; t1: number }; current: number; + points: HfAutomationPoint[]; } | null>(null); /** Cursor hint: hovering a stretch handle with nothing else live. */ const [edgeHover, setEdgeHover] = useState(false); @@ -210,6 +219,7 @@ export function useAutomationLaneGestures({ edge, origin: rangeSelection, current: edge === "t0" ? rangeSelection.t0 : rangeSelection.t1, + points: lane.points, }); return; } @@ -222,7 +232,7 @@ export function useAutomationLaneGestures({ rangeCrossed.current = false; setRangeDrag({ from: t, to: t }); }, - [edgeAt, rangeSelection, onRangeSelect, pointAt, duration, snapTimes], + [edgeAt, rangeSelection, onRangeSelect, pointAt, duration, snapTimes, lane], ); const onPointerDown = useCallback( @@ -310,20 +320,32 @@ export function useAutomationLaneGestures({ const moveEdge = useCallback( (e: ReactPointerEvent): void => { if (edgeDrag === null) return; - const { edge, origin } = edgeDrag; + const { edge, origin, points } = edgeDrag; const raw = pointAt(e.clientX, e.clientY).t; const clamped = Math.min(duration, Math.max(0, raw)); const current = edge === "t0" ? Math.min(clamped, origin.t1 - POINT_MERGE_SEC) : Math.max(clamped, origin.t0 + POINT_MERGE_SEC); - setEdgeDrag({ edge, origin, current }); + setEdgeDrag({ edge, origin, current, points }); const newT0 = edge === "t0" ? current : origin.t0; const newT1 = edge === "t1" ? current : origin.t1; setHint(`${newT0.toFixed(2)}s → ${newT1.toFixed(2)}s`); - commitPoints(retimeRange({ lane, range, t0: origin.t0, t1: origin.t1, newT0, newT1 }), false); + // Retime from the snapshot taken at arm time, never from `lane` (the + // live draft) — see the state comment above for why. + commitPoints( + retimeRange({ + lane: { target: lane.target, points }, + range, + t0: origin.t0, + t1: origin.t1, + newT0, + newT1, + }), + false, + ); }, - [edgeDrag, pointAt, duration, lane, range, commitPoints], + [edgeDrag, pointAt, duration, lane.target, range, commitPoints], ); /** Update the live range-drag as the pointer moves, firing `onRangeSelect`