Skip to content

Commit 08d3efd

Browse files
committed
refactor(studio): ruler labels follow the ticks' frame rate and fps parsing splits by form
1 parent 87957ca commit 08d3efd

12 files changed

Lines changed: 74 additions & 57 deletions

‎packages/core/src/fps.ts‎

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,40 @@ export type FpsParseResult =
3434
| "ambiguous-decimal";
3535
};
3636

37+
type FpsFailure = Extract<FpsParseResult, { ok: false }>["reason"];
38+
const fail = (reason: FpsFailure): FpsParseResult => ({ ok: false, reason });
39+
40+
function rangedFps(num: number, den: number): FpsParseResult {
41+
if (num <= 0) return fail("non-positive");
42+
const decimal = num / den;
43+
if (decimal < 1 || decimal > 240) return fail("out-of-range");
44+
return { ok: true, value: { num, den } };
45+
}
46+
47+
function numericFps(input: number): FpsParseResult {
48+
if (!Number.isFinite(input)) return fail("not-a-number");
49+
if (!Number.isInteger(input)) return fail("ambiguous-decimal");
50+
return rangedFps(input, 1);
51+
}
52+
53+
function fractionFps(raw: string): FpsParseResult {
54+
const parts = raw.split("/");
55+
if (parts.length !== 2) return fail("invalid-fraction");
56+
const num = Number(parts[0]);
57+
const den = Number(parts[1]);
58+
if (!Number.isFinite(num) || !Number.isFinite(den)) return fail("not-a-number");
59+
if (!Number.isInteger(num) || !Number.isInteger(den) || den <= 0) return fail("invalid-fraction");
60+
return rangedFps(num, den);
61+
}
62+
3763
// Decimals like "29.97" are rejected: FFmpeg reads them as a different rate than 30000/1001.
3864
export function parseFps(input: string | number): FpsParseResult {
39-
if (typeof input === "number") {
40-
if (!Number.isFinite(input)) return { ok: false, reason: "not-a-number" };
41-
if (!Number.isInteger(input)) return { ok: false, reason: "ambiguous-decimal" };
42-
if (input <= 0) return { ok: false, reason: "non-positive" };
43-
if (input > 240) return { ok: false, reason: "out-of-range" };
44-
return { ok: true, value: { num: input, den: 1 } };
45-
}
65+
if (typeof input === "number") return numericFps(input);
4666
const raw = input.trim();
47-
if (raw === "") return { ok: false, reason: "empty" };
48-
49-
if (raw.includes("/")) {
50-
const parts = raw.split("/");
51-
if (parts.length !== 2) return { ok: false, reason: "invalid-fraction" };
52-
const num = Number(parts[0]);
53-
const den = Number(parts[1]);
54-
if (!Number.isFinite(num) || !Number.isFinite(den)) {
55-
return { ok: false, reason: "not-a-number" };
56-
}
57-
if (!Number.isInteger(num) || !Number.isInteger(den)) {
58-
return { ok: false, reason: "invalid-fraction" };
59-
}
60-
if (den <= 0) return { ok: false, reason: "invalid-fraction" };
61-
if (num <= 0) return { ok: false, reason: "non-positive" };
62-
const decimal = num / den;
63-
if (decimal < 1 || decimal > 240) return { ok: false, reason: "out-of-range" };
64-
return { ok: true, value: { num, den } };
65-
}
66-
67-
if (!/^-?\d+$/.test(raw)) {
68-
if (/^-?\d*\.\d+$/.test(raw)) return { ok: false, reason: "ambiguous-decimal" };
69-
return { ok: false, reason: "not-a-number" };
70-
}
71-
const n = Number(raw);
72-
if (n <= 0) return { ok: false, reason: "non-positive" };
73-
if (n > 240) return { ok: false, reason: "out-of-range" };
74-
return { ok: true, value: { num: n, den: 1 } };
67+
if (raw === "") return fail("empty");
68+
if (raw.includes("/")) return fractionFps(raw);
69+
if (/^-?\d+$/.test(raw)) return rangedFps(Number(raw), 1);
70+
return fail(/^-?\d*\.\d+$/.test(raw) ? "ambiguous-decimal" : "not-a-number");
7571
}
7672

7773
export function parseFpsWithDefault(input: string | number | undefined): FpsParseResult {

‎packages/studio/src/player/components/PlayerControls.tsx‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ interface PlayerControlsProps {
168168
shortcutSections?: readonly ShortcutSection[];
169169
}
170170

171+
const compositionFps = () => useLinkedClipPreferences.getState().compositionFps;
172+
171173
export const PlayerControls = memo(function PlayerControls({
172174
onTogglePlay,
173175
onSeek,
@@ -197,17 +199,14 @@ export const PlayerControls = memo(function PlayerControls({
197199

198200
const durationRef = useRef(duration);
199201
durationRef.current = duration;
200-
const compositionFps = useLinkedClipPreferences((s) => s.compositionFps);
201-
const compositionFpsRef = useRef(compositionFps);
202-
compositionFpsRef.current = compositionFps;
203202
const controlsDisabled = disabled || !timelineReady;
204203

205204
useEffect(() => {
206205
if (!timeDisplayRef.current) return;
207206
const t = currentTimeRef.current;
208207
timeDisplayRef.current.textContent =
209-
timeDisplayMode === "frame" ? formatFrameTime(t, duration, compositionFps) : formatTime(t);
210-
}, [compositionFps, duration, timeDisplayMode]);
208+
timeDisplayMode === "frame" ? formatFrameTime(t, duration, compositionFps()) : formatTime(t);
209+
}, [duration, timeDisplayMode]);
211210

212211
useMountEffect(() => {
213212
const updateTime = (time: number) => {
@@ -216,7 +215,7 @@ export const PlayerControls = memo(function PlayerControls({
216215
const currentDuration = durationRef.current;
217216
const text =
218217
timeDisplayModeRef.current === "frame"
219-
? formatFrameTime(time, currentDuration, compositionFpsRef.current)
218+
? formatFrameTime(time, currentDuration, compositionFps())
220219
: formatTime(time);
221220
if (timeDisplayRef.current.textContent !== text) timeDisplayRef.current.textContent = text;
222221
};

‎packages/studio/src/player/components/ShortcutsPanel.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { formatTime, frameToSeconds } from "../lib/time";
33
import { Popover, Tooltip } from "../../components/ui";
44
import { flatActive, flatIdle } from "../../components/timelineToolbarStyles";
55
import { usePlayerStore } from "../store/playerStore";
6-
import { useLinkedClipPreferences } from "../../utils/linkedClipPreferences";
6+
import { useCompositionFps } from "../../utils/linkedClipPreferences";
77
import { DEFAULT_SHORTCUT_SECTIONS, type ShortcutSection } from "./studioShortcuts";
88

99
interface ShortcutsPanelProps {
@@ -32,7 +32,7 @@ export const ShortcutsPanel = memo(function ShortcutsPanel({
3232
}: ShortcutsPanelProps) {
3333
const [showShortcuts, setShowShortcuts] = useState(false);
3434
const [jumpFrame, setJumpFrame] = useState("");
35-
const compositionFps = useLinkedClipPreferences((s) => s.compositionFps);
35+
const compositionFps = useCompositionFps();
3636
// Focus lands on the scrolling body, not the frame input, so keyboard users can scroll and read it.
3737
const panelBodyRef = useRef<HTMLDivElement>(null);
3838

‎packages/studio/src/player/components/Timeline.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React, { act } from "react";
44
import { createRoot } from "react-dom/client";
55
import { afterEach } from "vitest";
66
import { describe, it, expect, vi } from "vitest";
7+
import { rulerTickText } from "./timelineRulerGeometry";
78
import {
89
Timeline,
910
formatTimelineTickLabel,
@@ -1367,3 +1368,14 @@ describe("resolveTimelineAssetDrop", () => {
13671368
).toEqual({ start: 1.18, track: 8 });
13681369
});
13691370
});
1371+
1372+
describe("rulerTickText", () => {
1373+
it("counts frames at the rate the ticks were laid out at", () => {
1374+
expect(rulerTickText(2, 25, 10, 1)).toBe(50);
1375+
expect(rulerTickText(2, 30_000 / 1_001, 10, 1)).toBe(60);
1376+
});
1377+
1378+
it("shows time when the ruler is not counting frames", () => {
1379+
expect(rulerTickText(2, undefined, 10, 1)).toBe("00:02");
1380+
});
1381+
});

‎packages/studio/src/player/components/TimelineProvider.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export type TimelineCanvasState = Omit<
4848
totalH: number;
4949
effectiveDuration: number;
5050
majorTickInterval: number;
51+
tickFrameRate: number | undefined;
5152
rangeSelection: TimelineRangeSelection | null;
5253
marqueeRect: Rect | null;
5354
resizingClip: ResizingClipState | null;

‎packages/studio/src/player/components/TimelineRuler.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe("TimelineRuler", () => {
8181
totalH={100}
8282
effectiveDuration={30}
8383
majorTickInterval={10}
84-
theme={{} as TimelineTheme}
84+
theme={{} as TimelineTheme}
8585
contentOrigin={80}
8686
scrollRef={{ current: el }}
8787
/>,

‎packages/studio/src/player/components/TimelineRuler.tsx‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import { memo, type RefObject } from "react";
22
import { useSettledScrollLeft } from "./useSettledScrollLeft";
33
import type { TimelineTheme } from "./timelineTheme";
44
import { RULER_H, getTimelineBeatEntries } from "./timelineLayout";
5-
import { formatTimelineTickLabel } from "./timelineRulerGeometry";
6-
import { usePlayerStore } from "../store/playerStore";
7-
import { secondsToFrame } from "../lib/time";
8-
import { useLinkedClipPreferences } from "../../utils/linkedClipPreferences";
5+
import { rulerTickText } from "./timelineRulerGeometry";
96
import type { MusicBeatAnalysis } from "@hyperframes/core/beats";
107
import type { TimelineTimeRange } from "../lib/timelineClipIndex";
118

@@ -17,6 +14,7 @@ interface TimelineRulerProps {
1714
totalH: number;
1815
effectiveDuration: number;
1916
majorTickInterval: number;
17+
tickFrameRate?: number;
2018
theme: TimelineTheme;
2119
beatAnalysis?: MusicBeatAnalysis | null;
2220
contentOrigin: number;
@@ -85,14 +83,13 @@ export const TimelineRuler = memo(function TimelineRuler({
8583
totalH,
8684
effectiveDuration,
8785
majorTickInterval,
86+
tickFrameRate,
8887
theme,
8988
beatAnalysis,
9089
contentOrigin,
9190
renderTimeRange,
9291
scrollRef,
9392
}: TimelineRulerProps) {
94-
const timeDisplayMode = usePlayerStore((s) => s.timeDisplayMode);
95-
const compositionFps = useLinkedClipPreferences((s) => s.compositionFps);
9693
const beatTimes = beatAnalysis?.beatTimes ?? [];
9794
const beatStrengths = beatAnalysis?.beatStrengths ?? [];
9895
const beatEntries = getTimelineBeatEntries(beatTimes, beatStrengths, renderTimeRange);
@@ -196,9 +193,7 @@ export const TimelineRuler = memo(function TimelineRuler({
196193
fontSize: 10,
197194
}}
198195
>
199-
{timeDisplayMode === "frame"
200-
? secondsToFrame(t, compositionFps)
201-
: formatTimelineTickLabel(t, effectiveDuration, majorTickInterval)}
196+
{rulerTickText(t, tickFrameRate, effectiveDuration, majorTickInterval)}
202197
</span>
203198
<div className="w-px" style={{ height: RULER_H, background: theme.tickMajor }} />
204199
</div>

‎packages/studio/src/player/components/TimelineRulerPart.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export function TimelineRulerPart() {
1313
totalH={props.totalH}
1414
effectiveDuration={props.effectiveDuration}
1515
majorTickInterval={props.majorTickInterval}
16+
tickFrameRate={props.tickFrameRate}
1617
theme={props.theme}
1718
beatAnalysis={props.beatAnalysis}
1819
contentOrigin={props.contentOrigin}

‎packages/studio/src/player/components/timelineRulerGeometry.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { formatTime } from "../lib/time";
1+
import { formatTime, secondsToFrame } from "../lib/time";
22
import type { TimelineTimeDisplayMode } from "../../utils/studioUiPreferences";
33
import type { TimelineTimeRange } from "../lib/timelineClipIndex";
44

@@ -161,3 +161,12 @@ export function formatTimelineTickLabel(
161161
}
162162
return formatTime(safeTime);
163163
}
164+
165+
export function rulerTickText(
166+
time: number,
167+
frameRate: number | undefined,
168+
duration: number,
169+
majorInterval: number,
170+
): string | number {
171+
return frameRate ? secondsToFrame(time, frameRate) : formatTimelineTickLabel(time, duration, majorInterval);
172+
}

‎packages/studio/src/player/components/useTimelineProviderState.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ export function useTimelineProviderState({
444444
dragActive: draggedClip?.started === true || resizingClip != null,
445445
displayDuration,
446446
});
447-
const { major, minor, majorTickInterval } = useTimelineTicks(
447+
const { major, minor, majorTickInterval, frameRate } = useTimelineTicks(
448448
displayDuration,
449449
pps,
450450
timeDisplayMode,
@@ -460,6 +460,7 @@ export function useTimelineProviderState({
460460
totalH: displayLayout.totalH,
461461
effectiveDuration,
462462
majorTickInterval,
463+
tickFrameRate: frameRate,
463464
rangeSelection,
464465
marqueeRect,
465466
laneGapStrips,

0 commit comments

Comments
 (0)