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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
import type { MouseEvent as ReactMouseEvent } from "react";
import { snappedSeekTime, type FrameRate } from "opencut-wasm";
import { TICKS_PER_SECOND } from "@/wasm";
import {
buildTimelineSnapPoints,
getTimelineSnapThresholdInTicks,
resolveTimelineSnap,
} from "@/timeline/snapping";
import { getBookmarkSnapPoints } from "@/timeline/bookmarks/index";
import { getElementEdgeSnapPoints } from "@/timeline/element-snap-source";
import { getAnimationKeyframeSnapPointsForTimeline } from "@/animation/timeline-snap-points";
import {
getCenteredLineLeft,
timelineTimeToPixels,
timelineTimeToSnappedPixels,
} from "@/timeline";
import { BASE_TIMELINE_PIXELS_PER_SECOND } from "@/timeline/scale";
import type { Bookmark, SceneTracks } from "@/timeline";
// --- Session ---
interface ScrubSession {
kind: "scrubbing";
/** True when scrub started from a ruler click (not the playhead handle). */
didStartFromRuler: boolean;
/** True once the mouse has moved during a ruler drag. */
hasMoved: boolean;
/** Most recent frame-snapped time set by scrub(). */
currentTime: number | null;
}
type Session = { kind: "idle" } | ScrubSession;
// --- Config ---
export interface PlayheadConfig {
zoomLevel: number;
duration: number;
getActiveProjectFps: () => FrameRate | null;
isShiftHeld: () => boolean;
getIsPlaying: () => boolean;
getRulerEl: () => HTMLDivElement | null;
getRulerScrollEl: () => HTMLDivElement | null;
getTracksScrollEl: () => HTMLDivElement | null;
getPlayheadEl: () => HTMLDivElement | null;
getSceneTracks: () => SceneTracks;
getSceneBookmarks: () => Bookmark[];
seek: (time: number) => void;
setScrubbing: (isScrubbing: boolean) => void;
setTimelineViewState: (viewState: {
zoomLevel: number;
scrollLeft: number;
playheadTime: number;
}) => void;
}
export interface PlayheadConfigRef {
readonly current: PlayheadConfig;
}
// --- Pure helpers (px → logical) ---
function pixelToTime({
clientX,
rulerEl,
zoomLevel,
duration,
}: {
clientX: number;
rulerEl: HTMLDivElement;
zoomLevel: number;
duration: number;
}): number {
const rulerRect = rulerEl.getBoundingClientRect();
const contentWidth = timelineTimeToPixels({ time: duration, zoomLevel });
const clampedX = Math.max(
0,
Math.min(contentWidth, clientX - rulerRect.left),
);
const seconds = Math.max(
0,
Math.min(
duration / TICKS_PER_SECOND,
clampedX / (BASE_TIMELINE_PIXELS_PER_SECOND * zoomLevel),
),
);
return Math.round(seconds * TICKS_PER_SECOND);
}
// --- Controller ---
export class PlayheadController {
private lastMouseClientX = 0;
private session: Session = { kind: "idle" };
private readonly configRef: PlayheadConfigRef;
constructor(deps: { configRef: PlayheadConfigRef }) {
this.configRef = deps.configRef;
this.onPlayheadMouseDown = this.onPlayheadMouseDown.bind(this);
this.onRulerMouseDown = this.onRulerMouseDown.bind(this);
this.handleMouseMove = this.handleMouseMove.bind(this);
this.handleMouseUp = this.handleMouseUp.bind(this);
}
private get config(): PlayheadConfig {
return this.configRef.current;
}
get isActive(): boolean {
return this.session.kind !== "idle";
}
getLastMouseClientX(): number {
return this.lastMouseClientX;
}
destroy(): void {
this.deactivate();
}
// --- Public event handlers (bound, stable references) ---
onPlayheadMouseDown(event: ReactMouseEvent): void {
event.preventDefault();
event.stopPropagation();
this.session = {
kind: "scrubbing",
didStartFromRuler: false,
hasMoved: false,
currentTime: null,
};
this.config.setScrubbing(true);
this.scrub({ event, isElementSnappingEnabled: true });
this.activate();
}
onRulerMouseDown(event: ReactMouseEvent): void {
if (event.button !== 0) return;
if (this.config.getPlayheadEl()?.contains(event.target as Node)) return;
event.preventDefault();
this.session = {
kind: "scrubbing",
didStartFromRuler: true,
hasMoved: false,
currentTime: null,
};
this.config.setScrubbing(true);
// No element-edge snapping on initial ruler click — avoids a jarring jump.
this.scrub({ event, isElementSnappingEnabled: false });
this.activate();
}
// --- Public non-session methods ---
/**
* Imperatively updates the playhead DOM element's `left` style.
* Called on scroll and playback events to avoid React re-renders
* during animation frame updates.
*/
updatePlayheadLeft(time: number): void {
const playheadEl = this.config.getPlayheadEl();
if (!playheadEl) return;
const centerPixel = timelineTimeToSnappedPixels({
time,
zoomLevel: this.config.zoomLevel,
});
const scrollLeft = this.config.getRulerScrollEl()?.scrollLeft ?? 0;
playheadEl.style.left = `${getCenteredLineLeft({ centerPixel }) - scrollLeft}px`;
}
/**
* Updates the playhead position and auto-scrolls to keep the playhead
* visible during playback.
*/
handlePlaybackUpdate(time: number): void {
this.updatePlayheadLeft(time);
// Auto-scroll only during playback, not while scrubbing.
if (!this.config.getIsPlaying() || this.session.kind === "scrubbing")
return;
const rulerViewport = this.config.getRulerScrollEl();
const tracksViewport = this.config.getTracksScrollEl();
if (!rulerViewport || !tracksViewport) return;
const playheadPixels = timelineTimeToPixels({
time,
zoomLevel: this.config.zoomLevel,
});
const viewportWidth = rulerViewport.clientWidth;
const isOutOfView =
playheadPixels < rulerViewport.scrollLeft ||
playheadPixels > rulerViewport.scrollLeft + viewportWidth;
if (isOutOfView) {
const desiredScroll = Math.max(
0,
Math.min(
rulerViewport.scrollWidth - viewportWidth,
playheadPixels - viewportWidth / 2,
),
);
rulerViewport.scrollLeft = tracksViewport.scrollLeft = desiredScroll;
}
}
// --- Private ---
private activate(): void {
window.addEventListener("mousemove", this.handleMouseMove);
window.addEventListener("mouseup", this.handleMouseUp);
}
private deactivate(): void {
window.removeEventListener("mousemove", this.handleMouseMove);
window.removeEventListener("mouseup", this.handleMouseUp);
}
/**
* Converts pointer position to a frame-snapped timeline time and seeks.
* `isElementSnappingEnabled` controls element-edge snapping; frame-level snapping
* is always applied.
*/
private scrub({
event,
isElementSnappingEnabled,
}: {
event: MouseEvent | ReactMouseEvent;
isElementSnappingEnabled: boolean;
}): void {
const ruler = this.config.getRulerEl();
if (!ruler) return;
const fps = this.config.getActiveProjectFps();
if (!fps) return;
const { zoomLevel, duration } = this.config;
const rawTime = pixelToTime({
clientX: event.clientX,
rulerEl: ruler,
zoomLevel,
duration,
});
const frameTime =
snappedSeekTime({ time: rawTime, duration, rate: fps }) ?? rawTime;
const time = (() => {
if (!isElementSnappingEnabled || this.config.isShiftHeld())
return frameTime;
const snapPoints = buildTimelineSnapPoints({
sources: [
() =>
getElementEdgeSnapPoints({ tracks: this.config.getSceneTracks() }),
() =>
getBookmarkSnapPoints({
bookmarks: this.config.getSceneBookmarks(),
}),
() =>
getAnimationKeyframeSnapPointsForTimeline({
tracks: this.config.getSceneTracks(),
}),
],
});
const result = resolveTimelineSnap({
targetTime: frameTime,
snapPoints,
maxSnapDistance: getTimelineSnapThresholdInTicks({ zoomLevel }),
});
return result.snapPoint ? result.snappedTime : frameTime;
})();
if (this.session.kind === "scrubbing") {
this.session.currentTime = time;
}
this.config.seek(time);
this.lastMouseClientX = event.clientX;
}
private handleMouseMove(event: MouseEvent): void {
if (this.session.kind !== "scrubbing") return;
this.scrub({ event, isElementSnappingEnabled: true });
if (this.session.didStartFromRuler) {
this.session.hasMoved = true;
}
}
private handleMouseUp(event: MouseEvent): void {
if (this.session.kind !== "scrubbing") return;
const session = this.session;
this.config.setScrubbing(false);
if (session.currentTime !== null) {
this.config.seek(session.currentTime);
this.config.setTimelineViewState({
zoomLevel: this.config.zoomLevel,
scrollLeft: this.config.getTracksScrollEl()?.scrollLeft ?? 0,
playheadTime: session.currentTime,
});
}
// Ruler click without drag: snap to clicked position on mouseup.
if (session.didStartFromRuler && !session.hasMoved) {
this.scrub({ event, isElementSnappingEnabled: false });
}
this.session = { kind: "idle" };
this.deactivate();
}
}
|