1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
import { useEffect, useRef } from "react";
import { useEditor } from "@/editor/use-editor";
import { useShiftKey } from "@/hooks/use-shift-key";
import { useEdgeAutoScroll } from "@/timeline/hooks/use-edge-auto-scroll";
import { timelineTimeToPixels } from "@/timeline";
import {
PlayheadController,
type PlayheadConfig,
} from "@/timeline/controllers/playhead-controller";
import type { MediaTime } from "@/wasm";
interface UseTimelinePlayheadProps {
zoomLevel: number;
rulerRef: React.RefObject<HTMLDivElement | null>;
rulerScrollRef: React.RefObject<HTMLDivElement | null>;
tracksScrollRef: React.RefObject<HTMLDivElement | null>;
playheadRef?: React.RefObject<HTMLDivElement | null>;
}
export function useTimelinePlayhead({
zoomLevel,
rulerRef,
rulerScrollRef,
tracksScrollRef,
playheadRef,
}: UseTimelinePlayheadProps) {
const editor = useEditor();
const isShiftHeldRef = useShiftKey();
// isScrubbing drives useEdgeAutoScroll — the controller sets it on the editor,
// so this reactive read naturally reflects whether scrubbing is active.
const isScrubbing = useEditor((e) => e.playback.getIsScrubbing());
const config: PlayheadConfig = {
zoomLevel,
duration: editor.timeline.getTotalDuration(),
getActiveProjectFps: () => editor.project.getActive()?.settings.fps ?? null,
isShiftHeld: () => isShiftHeldRef.current,
getIsPlaying: () => editor.playback.getIsPlaying(),
getRulerEl: () => rulerRef.current,
getRulerScrollEl: () => rulerScrollRef.current,
getTracksScrollEl: () => tracksScrollRef.current,
getPlayheadEl: () => playheadRef?.current ?? null,
getSceneTracks: () => editor.scenes.getActiveScene().tracks,
getSceneBookmarks: () => editor.scenes.getActiveScene()?.bookmarks ?? [],
seek: (time) => editor.playback.seek({ time }),
setScrubbing: (scrubbing) =>
editor.playback.setScrubbing({ isScrubbing: scrubbing }),
setTimelineViewState: ({ zoomLevel, scrollLeft, playheadTime }) =>
editor.project.setTimelineViewState({
viewState: {
zoomLevel,
scrollLeft,
playheadTime,
},
}),
};
const configRef = useRef(config);
configRef.current = config;
const ctrlRef = useRef<PlayheadController | null>(null);
if (!ctrlRef.current) {
ctrlRef.current = new PlayheadController({ configRef });
}
const ctrl = ctrlRef.current;
// Scroll → keep playhead position in sync with scroll offset.
useEffect(() => {
const scrollEl = rulerScrollRef.current;
if (!scrollEl) return;
const handler = () =>
ctrl.updatePlayheadLeft(editor.playback.getCurrentTime());
scrollEl.addEventListener("scroll", handler, { passive: true });
return () => scrollEl.removeEventListener("scroll", handler);
}, [ctrl, editor.playback, rulerScrollRef]);
// Playback events → update playhead position and auto-scroll during playback.
useEffect(() => {
const handler = (time: MediaTime) => ctrl.handlePlaybackUpdate(time);
ctrl.updatePlayheadLeft(editor.playback.getCurrentTime());
const unsubscribeUpdate = editor.playback.onUpdate(handler);
const unsubscribeSeek = editor.playback.onSeek(handler);
return () => {
unsubscribeUpdate();
unsubscribeSeek();
};
}, [ctrl, editor.playback]);
useEdgeAutoScroll({
isActive: isScrubbing,
getMouseClientX: () => ctrl.getLastMouseClientX(),
rulerScrollRef,
tracksScrollRef,
contentWidth: timelineTimeToPixels({
time: editor.timeline.getTotalDuration(),
zoomLevel,
}),
});
useEffect(() => () => ctrl.destroy(), [ctrl]);
return {
handlePlayheadMouseDown: ctrl.onPlayheadMouseDown,
handleRulerMouseDown: ctrl.onRulerMouseDown,
};
}
|