Skip to content

Commit a3b6eec

Browse files
fix(studio): timeline Fit fills the width with the film, and a host can hide keyframe controls (#4680)
* fix(studio): timeline Fit fills the width with the film, and a host can hide keyframe controls * fix(studio): keyframe tests read the claimed K press, and the fit span stays private * fix(studio): an edit that changes the film's length keeps the timeline scale, and Fit is an icon * fix(studio): manual zoom keeps its scale whenever only the film's length changes * fix(studio): drop the width check the kept scale never needed * fix(studio): a composition switch refits, and the kept scale clamps against the new fit * test(studio): a project reset refits the manual zoom
1 parent 73d26a5 commit a3b6eec

6 files changed

Lines changed: 343 additions & 155 deletions

File tree

‎packages/studio/src/components/TimelineToolbar.test.tsx‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,22 @@ vi.mock("../contexts/StudioContext", () => ({
2424

2525
afterEach(() => {
2626
document.body.innerHTML = "";
27-
usePlayerStore.setState({ autoKeyframeEnabled: true, thumbnailMode: "adaptive" });
27+
usePlayerStore.setState({
28+
autoKeyframeEnabled: true,
29+
thumbnailMode: "adaptive",
30+
zoomMode: "fit",
31+
});
2832
});
2933

3034
function renderToolbar(
3135
domEditSession?: React.ComponentProps<typeof TimelineToolbar>["domEditSession"],
36+
props: Partial<React.ComponentProps<typeof TimelineToolbar>> = {},
3237
) {
3338
const host = document.createElement("div");
3439
document.body.append(host);
3540
const root = createRoot(host);
3641
act(() => {
37-
root.render(<TimelineToolbar domEditSession={domEditSession} />);
42+
root.render(<TimelineToolbar domEditSession={domEditSession} {...props} />);
3843
});
3944
return { host, root };
4045
}
@@ -175,6 +180,52 @@ describe("TimelineToolbar — keyframes on audio tracks", () => {
175180
expect(button?.disabled).toBe(false);
176181
act(() => root.unmount());
177182
});
183+
184+
const keyframeControls = (host: HTMLElement) => [
185+
host.querySelector('button[aria-label="Add keyframe at playhead"]'),
186+
host.querySelector('button[aria-label="Auto-record manual edits as keyframes"]'),
187+
];
188+
/** True when the keyframe shortcut claimed K, so playback never saw it. */
189+
const pressK = () => {
190+
const event = new KeyboardEvent("keydown", { key: "k", bubbles: true, cancelable: true });
191+
act(() => {
192+
window.dispatchEvent(event);
193+
});
194+
return event.defaultPrevented;
195+
};
196+
197+
it("shows Add keyframe and auto-record, and K adds a keyframe, by default", () => {
198+
const { host, root } = renderToolbar(sessionFor("div"));
199+
expect(keyframeControls(host).every(Boolean)).toBe(true);
200+
expect(pressK()).toBe(true);
201+
act(() => root.unmount());
202+
});
203+
204+
it("hides both controls for a host without keyframes, turns auto-record off and leaves K alone", () => {
205+
const { host, root } = renderToolbar(sessionFor("div"), { showKeyframes: false });
206+
expect(keyframeControls(host)).toEqual([null, null]);
207+
expect(usePlayerStore.getState().autoKeyframeEnabled).toBe(false);
208+
expect(pressK()).toBe(false);
209+
act(() => root.unmount());
210+
});
211+
});
212+
213+
describe("TimelineToolbar Fit", () => {
214+
it("shows Fit as a named icon and says whether fit is on", () => {
215+
const { host, root } = renderToolbar();
216+
const fit = () => host.querySelector('button[aria-label="Fit timeline to width"]');
217+
expect(fit()?.textContent).toBe("");
218+
expect(fit()?.querySelector("svg")).not.toBeNull();
219+
act(() => fit()?.dispatchEvent(new MouseEvent("click", { bubbles: true })));
220+
expect(fit()?.getAttribute("aria-pressed")).toBe("true");
221+
act(() =>
222+
host
223+
.querySelector('button[aria-label="Zoom in"]')
224+
?.dispatchEvent(new MouseEvent("click", { bubbles: true })),
225+
);
226+
expect(fit()?.getAttribute("aria-pressed")).toBe("false");
227+
act(() => root.unmount());
228+
});
178229
});
179230

180231
describe("TimelineToolbar audio meters", () => {

‎packages/studio/src/components/TimelineToolbar.tsx‎

Lines changed: 119 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { useEffect, useRef } from "react";
2-
import { Image, Magnet, MagnifyingGlassMinus, MagnifyingGlassPlus } from "@phosphor-icons/react";
2+
import {
3+
ArrowsOutLineHorizontal,
4+
Image,
5+
Magnet,
6+
MagnifyingGlassMinus,
7+
MagnifyingGlassPlus,
8+
} from "@phosphor-icons/react";
39
import {
410
useEnableKeyframes,
511
isPlayheadWithinTween,
@@ -38,6 +44,8 @@ export interface TimelineToolbarProps {
3844
onSplitElement?: (element: TimelineElement, splitTime: number) => void;
3945
history?: TimelineHistoryButtonsProps;
4046
showAddBeat?: boolean;
47+
/** Hides Add keyframe and auto-record, and turns off auto-record and the K shortcut with them. */
48+
showKeyframes?: boolean;
4149
}
4250

4351
interface KeyframeToggleState {
@@ -134,6 +142,7 @@ export function TimelineToolbar({
134142
onSplitElement,
135143
history,
136144
showAddBeat = true,
145+
showKeyframes = true,
137146
}: TimelineToolbarProps) {
138147
const activeTool = usePlayerStore((s) => s.activeTool);
139148
const setActiveTool = usePlayerStore((s) => s.setActiveTool);
@@ -174,10 +183,14 @@ export function TimelineToolbar({
174183
// Wire the "Add keyframe (K)" shortcut the toolbar advertises. Active only when
175184
// there's a keyframeable selection; otherwise K stays JKL-pause in playback.
176185
useKeyframeKeyboard({
177-
enabled: Boolean(onToggleKeyframe),
186+
enabled: showKeyframes && Boolean(onToggleKeyframe),
178187
onAddKeyframe: onToggleKeyframe,
179188
});
180189

190+
useEffect(() => {
191+
if (!showKeyframes) setAutoKeyframeEnabled(false);
192+
}, [showKeyframes, setAutoKeyframeEnabled]);
193+
181194
// "N" toggles timeline snapping (industry convention: Resolve/FCP).
182195
// Skip when typing in an input/contenteditable.
183196
useEffect(() => {
@@ -270,107 +283,110 @@ export function TimelineToolbar({
270283
</button>
271284
</Tooltip>
272285
)}
273-
{/* Always rendered (CapCut-style): with no keyframeable selection the
274-
button fades to a disabled state instead of unmounting, so the
275-
toolbar layout never shifts. */}
276-
<Tooltip
277-
label={
278-
keyframePathEndpoint
279-
? "Motion path endpoints cannot be removed"
280-
: !onToggleKeyframe
281-
? "Select an animated element to add keyframes"
282-
: keyframeIsMotionPath
283-
? keyframeWillExtend
284-
? "Extend motion path to playhead (K)"
285-
: keyframeState === "active"
286-
? "Remove waypoint from motion path (K)"
287-
: "Add waypoint to motion path (K)"
288-
: keyframeState === "active"
289-
? "Remove keyframe at playhead (K)"
290-
: keyframeState === "inactive"
286+
{showKeyframes && (
287+
<>
288+
{/* With no keyframeable selection the button fades to disabled instead of
289+
unmounting, so the toolbar layout never shifts (CapCut-style). */}
290+
<Tooltip
291+
label={
292+
keyframePathEndpoint
293+
? "Motion path endpoints cannot be removed"
294+
: !onToggleKeyframe
295+
? "Select an animated element to add keyframes"
296+
: keyframeIsMotionPath
291297
? keyframeWillExtend
292-
? "Add keyframe at playhead, extends animation (K)"
293-
: "Add keyframe at playhead (K)"
294-
: "Add keyframe (K)"
295-
}
296-
>
297-
<button
298-
type="button"
299-
disabled={!onToggleKeyframe}
300-
onClick={onToggleKeyframe}
301-
aria-label={
302-
keyframePathEndpoint
303-
? "Motion path endpoint"
304-
: keyframeIsMotionPath
305-
? keyframeState === "active"
306-
? "Remove motion path waypoint"
307-
: keyframeWillExtend
308-
? "Extend motion path to playhead"
309-
: "Add motion path waypoint"
310-
: keyframeState === "active"
311-
? "Remove keyframe at playhead"
312-
: "Add keyframe at playhead"
313-
}
314-
className={
315-
!onToggleKeyframe
316-
? flatDisabled
317-
: `${flatBtn} active:scale-[0.98] hover:bg-white/6 ${
318-
keyframeState === "active"
319-
? "text-studio-accent"
320-
: keyframeState === "inactive"
321-
? "text-neutral-400 hover:text-studio-accent"
322-
: "text-neutral-600 hover:text-neutral-400"
323-
}`
324-
}
325-
>
326-
<svg width="16" height="16" viewBox="0 0 10 10" fill="currentColor">
327-
{keyframeState === "active" ? (
328-
<path d="M5 0.5L9.5 5L5 9.5L0.5 5Z" />
329-
) : (
330-
<path
331-
d="M5 1.2L8.8 5L5 8.8L1.2 5Z"
332-
fill="none"
333-
stroke="currentColor"
334-
strokeWidth="1.2"
335-
/>
336-
)}
337-
</svg>
338-
</button>
339-
</Tooltip>
340-
<Tooltip
341-
label={
342-
autoKeyframeEnabled
343-
? "Auto-record manual edits as keyframes (click to turn off)"
344-
: "Manual edits will not be recorded as keyframes (click to turn on)"
345-
}
346-
>
347-
<button
348-
type="button"
349-
onClick={() => setAutoKeyframeEnabled(!autoKeyframeEnabled)}
350-
aria-label="Auto-record manual edits as keyframes"
351-
aria-pressed={autoKeyframeEnabled}
352-
className={`${flatBtn} active:scale-[0.98] hover:bg-white/6 ${
353-
autoKeyframeEnabled
354-
? "text-red-400 hover:text-red-300"
355-
: "text-neutral-600 hover:text-neutral-400"
356-
}`}
357-
>
358-
<svg width="16" height="16" viewBox="0 0 10 10" fill="none">
359-
{/* Same diamond outline as the Add-keyframe icon, with a
298+
? "Extend motion path to playhead (K)"
299+
: keyframeState === "active"
300+
? "Remove waypoint from motion path (K)"
301+
: "Add waypoint to motion path (K)"
302+
: keyframeState === "active"
303+
? "Remove keyframe at playhead (K)"
304+
: keyframeState === "inactive"
305+
? keyframeWillExtend
306+
? "Add keyframe at playhead, extends animation (K)"
307+
: "Add keyframe at playhead (K)"
308+
: "Add keyframe (K)"
309+
}
310+
>
311+
<button
312+
type="button"
313+
disabled={!onToggleKeyframe}
314+
onClick={onToggleKeyframe}
315+
aria-label={
316+
keyframePathEndpoint
317+
? "Motion path endpoint"
318+
: keyframeIsMotionPath
319+
? keyframeState === "active"
320+
? "Remove motion path waypoint"
321+
: keyframeWillExtend
322+
? "Extend motion path to playhead"
323+
: "Add motion path waypoint"
324+
: keyframeState === "active"
325+
? "Remove keyframe at playhead"
326+
: "Add keyframe at playhead"
327+
}
328+
className={
329+
!onToggleKeyframe
330+
? flatDisabled
331+
: `${flatBtn} active:scale-[0.98] hover:bg-white/6 ${
332+
keyframeState === "active"
333+
? "text-studio-accent"
334+
: keyframeState === "inactive"
335+
? "text-neutral-400 hover:text-studio-accent"
336+
: "text-neutral-600 hover:text-neutral-400"
337+
}`
338+
}
339+
>
340+
<svg width="16" height="16" viewBox="0 0 10 10" fill="currentColor">
341+
{keyframeState === "active" ? (
342+
<path d="M5 0.5L9.5 5L5 9.5L0.5 5Z" />
343+
) : (
344+
<path
345+
d="M5 1.2L8.8 5L5 8.8L1.2 5Z"
346+
fill="none"
347+
stroke="currentColor"
348+
strokeWidth="1.2"
349+
/>
350+
)}
351+
</svg>
352+
</button>
353+
</Tooltip>
354+
<Tooltip
355+
label={
356+
autoKeyframeEnabled
357+
? "Auto-record manual edits as keyframes (click to turn off)"
358+
: "Manual edits will not be recorded as keyframes (click to turn on)"
359+
}
360+
>
361+
<button
362+
type="button"
363+
onClick={() => setAutoKeyframeEnabled(!autoKeyframeEnabled)}
364+
aria-label="Auto-record manual edits as keyframes"
365+
aria-pressed={autoKeyframeEnabled}
366+
className={`${flatBtn} active:scale-[0.98] hover:bg-white/6 ${
367+
autoKeyframeEnabled
368+
? "text-red-400 hover:text-red-300"
369+
: "text-neutral-600 hover:text-neutral-400"
370+
}`}
371+
>
372+
<svg width="16" height="16" viewBox="0 0 10 10" fill="none">
373+
{/* Same diamond outline as the Add-keyframe icon, with a
360374
record-style dot inside: filled = auto-recording,
361375
hollow = manual edits won't be keyframed. */}
362-
<path d="M5 0.7L9.3 5L5 9.3L0.7 5Z" stroke="currentColor" strokeWidth="1" />
363-
<circle
364-
cx="5"
365-
cy="5"
366-
r="1.8"
367-
fill={autoKeyframeEnabled ? "currentColor" : "none"}
368-
stroke="currentColor"
369-
strokeWidth="1"
370-
/>
371-
</svg>
372-
</button>
373-
</Tooltip>
376+
<path d="M5 0.7L9.3 5L5 9.3L0.7 5Z" stroke="currentColor" strokeWidth="1" />
377+
<circle
378+
cx="5"
379+
cy="5"
380+
r="1.8"
381+
fill={autoKeyframeEnabled ? "currentColor" : "none"}
382+
stroke="currentColor"
383+
strokeWidth="1"
384+
/>
385+
</svg>
386+
</button>
387+
</Tooltip>
388+
</>
389+
)}
374390
{onSplitElement &&
375391
(() => {
376392
// Render the button unconditionally (disabled when unusable):
@@ -495,13 +511,15 @@ export function TimelineToolbar({
495511
<button
496512
type="button"
497513
onClick={() => setZoomMode("fit")}
514+
aria-label="Fit timeline to width"
515+
aria-pressed={zoomMode === "fit"}
498516
className={`h-7 px-2 rounded-md text-[11px] font-medium transition-colors ${
499517
zoomMode === "fit"
500518
? "bg-studio-accent/10 text-studio-accent"
501519
: "text-neutral-400 hover:bg-white/6 hover:text-neutral-200"
502520
}`}
503521
>
504-
Fit
522+
<ArrowsOutLineHorizontal size={16} aria-hidden="true" />
505523
</button>
506524
</Tooltip>
507525
<Tooltip label="Zoom out">

0 commit comments

Comments
 (0)