Skip to content

Commit 5965304

Browse files
thecodacusclaude
andcommitted
fix(studio): play after switching compositions and timeline scroll bounce
Play button: the Player is keyed by composition id, so switching mounts a fresh instance whose initial 'pause'/'frameupdate' events fire from its own mount effects before Preview's effect subscribes. Preview kept isPlaying=true from the previous composition, so the play button called pause() on an already-paused player and nothing happened. Preview now seeds isPlaying and the current frame from the new player when it subscribes. Timeline scroll: the track area's scrollLeft was mirrored into React state, written back to the element from a passive effect, and fed again from onScroll. When the element moved between a state update and its effect (scrollbar drag, momentum, long frames during playback) the effect wrote the older value back and the scrollbar bounced. Scroll position now lives only in the DOM: wheel scrolling moves the element directly, and a zoom anchor is applied once in a layout effect after the new width renders. Consecutive ctrl+wheel events also continue from the pending zoom instead of a stale pixels-per-frame, so a fast zoom no longer drops steps. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6d6aca2 commit 5965304

3 files changed

Lines changed: 48 additions & 21 deletions

File tree

‎packages/studio/ui/Preview.tsx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ export const Preview: React.FC<PreviewProps> = React.memo(({
112112
player.addEventListener('frameupdate', onFrame);
113113
player.addEventListener('play', onPlay);
114114
player.addEventListener('pause', onPause);
115+
116+
// The Player is keyed by composition.id, so switching compositions mounts a
117+
// fresh instance. Its initial 'frameupdate'/'pause' events fire from its own
118+
// mount effects, which run before this (parent) effect subscribes, so they
119+
// never reach us. Seed local state from the new instance; otherwise
120+
// isPlaying stays true from the previous composition and the play button
121+
// calls pause() on a player that is already paused.
122+
const frame = player.getCurrentFrame();
123+
setIsPlaying(player.isPlaying());
124+
setCurrentFrame(frame);
125+
onFrameUpdate?.(frame);
126+
115127
return () => {
116128
player.removeEventListener('frameupdate', onFrame);
117129
player.removeEventListener('play', onPlay);

‎packages/studio/ui/TimelineEditor.tsx‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const TimelineEditor: React.FC<TimelineEditorProps> = ({
5959
const [speedEditPath, setSpeedEditPath] = useState<string | null>(null);
6060
const [contextMenu, setContextMenu] = useState<{ x: number; y: number; namePath: string; entry: { from: number; durationInFrames: number; playbackRate?: number } } | null>(null);
6161

62-
const { pixelsPerFrame, scrollLeft, setScrollLeft, setPixelsPerFrame, handleWheel } = useTimelineZoom({
62+
const { pixelsPerFrame, setPixelsPerFrame, handleWheel } = useTimelineZoom({
6363
totalFrames,
6464
containerRef: trackAreaRef,
6565
});
@@ -108,12 +108,6 @@ export const TimelineEditor: React.FC<TimelineEditorProps> = ({
108108
return () => el.removeEventListener('wheel', handleWheel);
109109
}, [handleWheel]);
110110

111-
// Sync scroll position
112-
useEffect(() => {
113-
const el = trackAreaRef.current;
114-
if (el) el.scrollLeft = scrollLeft;
115-
}, [scrollLeft]);
116-
117111
// Close context menu on click elsewhere
118112
useEffect(() => {
119113
if (!contextMenu) return;
@@ -270,7 +264,6 @@ export const TimelineEditor: React.FC<TimelineEditorProps> = ({
270264
ref={trackAreaRef}
271265
style={trackAreaContainerStyle}
272266
onClick={handleTrackAreaClick}
273-
onScroll={(e) => setScrollLeft((e.target as HTMLElement).scrollLeft)}
274267
>
275268
{/* Ruler */}
276269
<div style={{ ...rulerStyle, width: totalWidth }}>
Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState, useCallback, useRef, useEffect } from 'react';
1+
import { useState, useCallback, useRef, useEffect, useLayoutEffect } from 'react';
22

33
interface UseTimelineZoomOptions {
44
totalFrames: number;
@@ -7,31 +7,50 @@ interface UseTimelineZoomOptions {
77

88
interface UseTimelineZoomReturn {
99
pixelsPerFrame: number;
10-
scrollLeft: number;
11-
setScrollLeft: (v: number) => void;
1210
setPixelsPerFrame: (v: number) => void;
1311
handleWheel: (e: WheelEvent) => void;
1412
}
1513

1614
const MIN_PX_PER_FRAME = 0.5;
1715
const MAX_PX_PER_FRAME = 20;
1816

17+
/**
18+
* Zoom state for the timeline track area.
19+
*
20+
* The horizontal scroll position lives only in the DOM (`el.scrollLeft`). It is
21+
* not mirrored into React state: a state copy that is written back to the
22+
* element from an effect and fed again from `onScroll` has two writers, and
23+
* whenever the element scrolls between a state update and its effect (scrollbar
24+
* drag, trackpad momentum, browser clamping, long frames during playback) the
25+
* effect writes the older value back and the scrollbar bounces between them.
26+
*/
1927
export function useTimelineZoom({ totalFrames, containerRef }: UseTimelineZoomOptions): UseTimelineZoomReturn {
2028
const [pixelsPerFrame, setPixelsPerFrame] = useState(() => {
2129
// Start at a reasonable default — will be adjusted on mount
2230
return 3;
2331
});
24-
const [scrollLeft, setScrollLeft] = useState(0);
2532
const ppfRef = useRef(pixelsPerFrame);
2633
ppfRef.current = pixelsPerFrame;
2734

35+
// Scroll offset to apply once a zoom has re-rendered the track area at its
36+
// new width. Setting it earlier would be clamped to the old content width.
37+
const pendingScrollRef = useRef<number | null>(null);
38+
39+
useLayoutEffect(() => {
40+
const el = containerRef.current;
41+
if (!el || pendingScrollRef.current === null) return;
42+
el.scrollLeft = pendingScrollRef.current;
43+
pendingScrollRef.current = null;
44+
}, [pixelsPerFrame, containerRef]);
45+
2846
// Fit to width on mount
2947
useEffect(() => {
3048
const el = containerRef.current;
3149
if (!el || totalFrames <= 0) return;
3250
const fitPpf = Math.max(MIN_PX_PER_FRAME, el.clientWidth / totalFrames);
51+
pendingScrollRef.current = null;
52+
el.scrollLeft = 0;
3353
setPixelsPerFrame(Math.min(fitPpf, MAX_PX_PER_FRAME));
34-
setScrollLeft(0);
3554
}, [totalFrames, containerRef]);
3655

3756
const handleWheel = useCallback((e: WheelEvent) => {
@@ -46,20 +65,23 @@ export function useTimelineZoom({ totalFrames, containerRef }: UseTimelineZoomOp
4665
const oldPpf = ppfRef.current;
4766
const zoomFactor = e.deltaY < 0 ? 1.15 : 1 / 1.15;
4867
const newPpf = Math.min(MAX_PX_PER_FRAME, Math.max(MIN_PX_PER_FRAME, oldPpf * zoomFactor));
68+
if (newPpf === oldPpf) return;
4969

50-
// Keep the frame under the cursor in place
51-
const frameAtCursor = (cursorX + el.scrollLeft) / oldPpf;
52-
const newScrollLeft = Math.max(0, frameAtCursor * newPpf - cursorX);
53-
70+
// Keep the frame under the cursor in place. Several wheel events can
71+
// arrive before React re-renders, so continue from the not-yet-applied
72+
// zoom rather than the stale DOM offset.
73+
const oldScrollLeft = pendingScrollRef.current ?? el.scrollLeft;
74+
const frameAtCursor = (cursorX + oldScrollLeft) / oldPpf;
75+
pendingScrollRef.current = Math.max(0, frameAtCursor * newPpf - cursorX);
76+
ppfRef.current = newPpf;
5477
setPixelsPerFrame(newPpf);
55-
setScrollLeft(newScrollLeft);
5678
} else {
57-
// Horizontal scroll
79+
// Horizontal scroll: move the element directly; the browser clamps it.
5880
e.preventDefault();
5981
const delta = e.deltaX !== 0 ? e.deltaX : e.deltaY;
60-
setScrollLeft((prev) => Math.max(0, prev + delta));
82+
el.scrollLeft += delta;
6183
}
6284
}, [containerRef]);
6385

64-
return { pixelsPerFrame, scrollLeft, setScrollLeft, setPixelsPerFrame, handleWheel };
86+
return { pixelsPerFrame, setPixelsPerFrame, handleWheel };
6587
}

0 commit comments

Comments
 (0)