Skip to content

Write HTML. Render video. Built for agents.

fix(studio): a playhead scrolled off to the left no longer draws over the track names (#4436)

Miguel Ángel225c22b6b981
diff --git a/packages/studio/src/player/components/PlayheadIndicator.tsx b/packages/studio/src/player/components/PlayheadIndicator.tsx
index ea79037c7f..50c0d8a1c8 100644
--- a/packages/studio/src/player/components/PlayheadIndicator.tsx
+++ b/packages/studio/src/player/components/PlayheadIndicator.tsx
@@ -11,6 +11,9 @@
*/
import { PLAYHEAD_HEAD_W } from "./timelineLayout";
+/** Widest part of the playhead, centred on its line. */
+export const PLAYHEAD_GLOW_W = 13;
+
interface PlayheadIndicatorProps {
/** CSS color, defaults to the HF accent variable */
color?: string;
@@ -50,7 +53,7 @@ export function PlayheadIndicator({
className="absolute top-0 bottom-0"
style={{
left: "50%",
- width: 13,
+ width: PLAYHEAD_GLOW_W,
transform: "translateX(-50%)",
background: `radial-gradient(closest-side, ${glowColor}, transparent)`,
}}
diff --git a/packages/studio/src/player/components/Timeline.test.ts b/packages/studio/src/player/components/Timeline.test.ts
index e465dab913..eebbe565c0 100644
--- a/packages/studio/src/player/components/Timeline.test.ts
+++ b/packages/studio/src/player/components/Timeline.test.ts
@@ -88,9 +88,7 @@ function getHorizontalGeometry(host: HTMLElement, clipId: string, tickLabel: str
if (!ruler) throw new Error("Missing timeline ruler");
const rulerOrigin = ruler.previousElementSibling;
if (!(rulerOrigin instanceof HTMLElement)) throw new Error("Missing timeline ruler origin");
- const playhead = Array.from(host.querySelectorAll<HTMLElement>("div")).find(
- (node) => node.style.zIndex === "100",
- );
+ const playhead = host.querySelector<HTMLElement>("[data-timeline-playhead-layer] > div");
if (!playhead) throw new Error("Missing timeline playhead");
return { clip, trackHeader, rulerTick, rulerOrigin, playhead };
}
diff --git a/packages/studio/src/player/components/Timeline.virtualization.test.tsx b/packages/studio/src/player/components/Timeline.virtualization.test.tsx
index 130e9682d5..ca8a5c8499 100644
--- a/packages/studio/src/player/components/Timeline.virtualization.test.tsx
+++ b/packages/studio/src/player/components/Timeline.virtualization.test.tsx
@@ -3,6 +3,7 @@
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
+import { TIMELINE_SCROLL_SETTLE_MS } from "./useTimelineScrollViewport";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -430,6 +431,26 @@ describe("Timeline without row virtualization", { timeout: 30_000 }, () => {
};
}
+ it("clips a scrolled-off playhead at the track headers once the scroll settles", async () => {
+ const { host, dispose } = await renderUnvirtualizedTimeline();
+ try {
+ const scroller = host.querySelector<HTMLElement>("[data-timeline-scroll-viewport]")!;
+ const layer = () => host.querySelector<HTMLElement>("[data-timeline-playhead-layer]");
+ await scrollTimelineHorizontally(scroller, 166);
+ expect(layer()?.style.clipPath).toBe("");
+ await act(async () => {
+ await new Promise((resolve) => setTimeout(resolve, TIMELINE_SCROLL_SETTLE_MS + 20));
+ });
+ // The ruler's corner is as wide as the track headers; the glow may overhang by 6.5px.
+ const headerWidth = parseFloat(
+ host.querySelector<HTMLElement>(".sticky.left-0")!.style.width,
+ );
+ expect(layer()?.style.clipPath).toContain(`${166 + headerWidth - 6.5}px)`);
+ } finally {
+ dispose();
+ }
+ });
+
it("mounts every clip rather than a window", async () => {
const { host, dispose } = await renderUnvirtualizedTimeline();
try {
diff --git a/packages/studio/src/player/components/TimelineCanvas.tsx b/packages/studio/src/player/components/TimelineCanvas.tsx
index 74f11dd062..31cb23a33e 100644
--- a/packages/studio/src/player/components/TimelineCanvas.tsx
+++ b/packages/studio/src/player/components/TimelineCanvas.tsx
@@ -1,6 +1,7 @@
import { memo } from "react";
import { TimelineRulerPart } from "./TimelineRulerPart";
import { PlayheadIndicator } from "./PlayheadIndicator";
+import { TimelinePlayheadLayer } from "./TimelinePlayheadLayer";
import {
RULER_H,
CLIP_Y,
@@ -236,18 +237,19 @@ export const TimelineCanvas = memo(function TimelineCanvas() {
the half-head offset baked into getTimelinePlayheadLeft keep the
inner 1px line's CENTER exactly on contentOrigin + t * pps (the ruler
ticks' center), instead of relying on shrink-wrap sizing. */}
- <div
- ref={props.playheadRef}
- className="absolute top-0 bottom-0 pointer-events-none"
- style={{
- left: `${getTimelinePlayheadLeft(0, 0, props.contentOrigin)}px`,
- width: PLAYHEAD_HEAD_W,
- zIndex: 100,
- display: beatDragging ? "none" : undefined,
- }}
- >
- <PlayheadIndicator scrubbing={props.isScrubbing} />
- </div>
+ <TimelinePlayheadLayer scrollRef={props.scrollRef} contentOrigin={props.contentOrigin}>
+ <div
+ ref={props.playheadRef}
+ className="absolute top-0 bottom-0 pointer-events-none"
+ style={{
+ left: `${getTimelinePlayheadLeft(0, 0, props.contentOrigin)}px`,
+ width: PLAYHEAD_HEAD_W,
+ display: beatDragging ? "none" : undefined,
+ }}
+ >
+ <PlayheadIndicator scrubbing={props.isScrubbing} />
+ </div>
+ </TimelinePlayheadLayer>
</div>
);
});
diff --git a/packages/studio/src/player/components/TimelinePlayheadLayer.tsx b/packages/studio/src/player/components/TimelinePlayheadLayer.tsx
new file mode 100644
index 0000000000..5af1bd131f
--- /dev/null
+++ b/packages/studio/src/player/components/TimelinePlayheadLayer.tsx
@@ -0,0 +1,33 @@
+import type { ReactNode, RefObject } from "react";
+import { PLAYHEAD_GLOW_W } from "./PlayheadIndicator";
+import { useSettledScrollLeft } from "./useSettledScrollLeft";
+
+// Clips the playhead at the track headers' edge once a scroll settles, so one scrolled off to
+// the left does not draw over the track names. Half the glow may pass the edge so 00:00 looks
+// as before; mid-scroll it is not clipped.
+export function TimelinePlayheadLayer({
+ scrollRef,
+ contentOrigin,
+ children,
+}: {
+ scrollRef: RefObject<HTMLDivElement | null>;
+ contentOrigin: number;
+ children: ReactNode;
+}) {
+ const settledScrollLeft = useSettledScrollLeft(scrollRef);
+ return (
+ <div
+ data-timeline-playhead-layer=""
+ className="absolute inset-0 pointer-events-none"
+ style={{
+ zIndex: 100,
+ clipPath:
+ settledScrollLeft === null
+ ? undefined
+ : `inset(0 0 0 ${settledScrollLeft + contentOrigin - PLAYHEAD_GLOW_W / 2}px)`,
+ }}
+ >
+ {children}
+ </div>
+ );
+}
diff --git a/packages/studio/src/player/components/TimelineRuler.tsx b/packages/studio/src/player/components/TimelineRuler.tsx
index a80a8c2472..0f2a7bcd71 100644
--- a/packages/studio/src/player/components/TimelineRuler.tsx
+++ b/packages/studio/src/player/components/TimelineRuler.tsx
@@ -1,5 +1,5 @@
-import { memo, useCallback, useRef, useSyncExternalStore, type RefObject } from "react";
-import { TIMELINE_SCROLL_SETTLE_MS } from "./useTimelineScrollViewport";
+import { memo, type RefObject } from "react";
+import { useSettledScrollLeft } from "./useSettledScrollLeft";
import type { TimelineTheme } from "./timelineTheme";
import { RULER_H, getTimelineBeatEntries } from "./timelineLayout";
import { formatTimelineTickLabel } from "./timelineRulerGeometry";
@@ -41,11 +41,8 @@ function tickIndexUnderHeader(major: number[], pps: number, scrollLeft: number):
return found;
}
-/**
- * Paints over the one label the header corner would slice, once a scroll settles. Moving it
- * on every tick crossing cost 2-3x the scroll budget on a large timeline, so mid-scroll a
- * fragment can still show.
- */
+// Paints over the one label the header corner would slice, once a scroll settles; mid-scroll a
+// fragment can still show.
const HeaderSlicedLabelMask = memo(function HeaderSlicedLabelMask({
scrollRef,
major,
@@ -57,39 +54,9 @@ const HeaderSlicedLabelMask = memo(function HeaderSlicedLabelMask({
pps: number;
background: string;
}) {
- const scrollingRef = useRef(false);
- const subscribe = useCallback(
- (onChange: () => void) => {
- const el = scrollRef.current;
- let settle: ReturnType<typeof setTimeout> | undefined;
- const onScroll = () => {
- if (!scrollingRef.current) {
- scrollingRef.current = true;
- onChange();
- }
- clearTimeout(settle);
- settle = setTimeout(() => {
- scrollingRef.current = false;
- onChange();
- }, TIMELINE_SCROLL_SETTLE_MS);
- };
- el?.addEventListener("scroll", onScroll, { passive: true });
- return () => {
- clearTimeout(settle);
- scrollingRef.current = false;
- el?.removeEventListener("scroll", onScroll);
- };
- },
- [scrollRef],
- );
- const index = useSyncExternalStore(
- subscribe,
- () =>
- scrollingRef.current
- ? -1
- : tickIndexUnderHeader(major, pps, scrollRef.current?.scrollLeft ?? 0),
- () => -1,
- );
+ const settledScrollLeft = useSettledScrollLeft(scrollRef);
+ const index =
+ settledScrollLeft === null ? -1 : tickIndexUnderHeader(major, pps, settledScrollLeft);
if (index < 0) return null;
const labelLeft = major[index]! * pps - 0.5 + TICK_LABEL_INSET_PX;
const next = major[index + 1];
diff --git a/packages/studio/src/player/components/useSettledScrollLeft.ts b/packages/studio/src/player/components/useSettledScrollLeft.ts
new file mode 100644
index 0000000000..ec106b32a3
--- /dev/null
+++ b/packages/studio/src/player/components/useSettledScrollLeft.ts
@@ -0,0 +1,38 @@
+import { useCallback, useRef, useSyncExternalStore, type RefObject } from "react";
+import { TIMELINE_SCROLL_SETTLE_MS } from "./useTimelineScrollViewport";
+
+// The timeline viewport's scrollLeft once a scroll settles, or null while it is scrolling. Readers
+// re-render twice per gesture, never per frame: per-frame work cost 2-3x the viewport gate's budget
+// on a large unvirtualized timeline.
+export function useSettledScrollLeft(scrollRef: RefObject<HTMLDivElement | null>): number | null {
+ const scrollingRef = useRef(false);
+ const subscribe = useCallback(
+ (onChange: () => void) => {
+ const el = scrollRef.current;
+ let settle: ReturnType<typeof setTimeout> | undefined;
+ const onScroll = () => {
+ if (!scrollingRef.current) {
+ scrollingRef.current = true;
+ onChange();
+ }
+ clearTimeout(settle);
+ settle = setTimeout(() => {
+ scrollingRef.current = false;
+ onChange();
+ }, TIMELINE_SCROLL_SETTLE_MS);
+ };
+ el?.addEventListener("scroll", onScroll, { passive: true });
+ return () => {
+ clearTimeout(settle);
+ scrollingRef.current = false;
+ el?.removeEventListener("scroll", onScroll);
+ };
+ },
+ [scrollRef],
+ );
+ return useSyncExternalStore(
+ subscribe,
+ () => (scrollingRef.current ? null : (scrollRef.current?.scrollLeft ?? 0)),
+ () => null,
+ );
+}