Skip to content

Commit d787681

Browse files
feat(studio): zoom the timeline to the selected clips (#5824)
* feat(studio): zoom the timeline to the selected clips Toolbar action and Z fit the selected clips to the visible width, with the range margin, and stay disabled until something is selected. * fix(studio): gate zoom-to-selection on a resolvable clip The scroll clamp already lives in the timeline zoom request. Drop the unused copy, and disable the button and Z unless the selection matches a timeline clip with duration. A collapsed timeline cancels zoomTimelineToRange instead of laying out a percent the view cannot show. * fix(studio): keep a zoom step when the timeline is collapsed A collapsed timeline has no track for a range zoom, so zoom in and out step the percent directly. Z is consumed only when that zoom actually starts, and zoom-to-selection no longer builds the span three times. * ci: retrigger checks after workflow approval The approved run was cancelled during checkout, so the required checks never executed.
1 parent c887643 commit d787681

9 files changed

Lines changed: 419 additions & 12 deletions

File tree

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

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,18 @@ describe("TimelineToolbar Fit", () => {
271271
timelineFitPps: 10,
272272
timelinePps: 10,
273273
});
274-
return registerTimelineZoomViewport({ scroll, contentOrigin: 32, publishScroll: () => {} });
274+
return {
275+
scroll,
276+
unregister: registerTimelineZoomViewport({
277+
scroll,
278+
contentOrigin: 32,
279+
publishScroll: () => {},
280+
}),
281+
};
275282
}
276283

277284
it("moves the slider and its readout with a zoom while it is previewed", () => {
278-
const unregister = mountTimelineViewport();
285+
const { unregister } = mountTimelineViewport();
279286
const { host, root } = renderToolbar();
280287
const slider = host.querySelector<HTMLInputElement>('input[aria-label="Timeline zoom"]')!;
281288
const before = slider.value;
@@ -296,7 +303,7 @@ describe("TimelineToolbar Fit", () => {
296303
});
297304

298305
it("keeps Fit when it is picked while a zoom waits to be laid out", () => {
299-
const unregister = mountTimelineViewport();
306+
const { unregister } = mountTimelineViewport();
300307
const { host, root } = renderToolbar();
301308
act(() =>
302309
host
@@ -315,6 +322,113 @@ describe("TimelineToolbar Fit", () => {
315322
act(() => unregister());
316323
});
317324

325+
it("disables zoom-to-selection until a clip is selected", () => {
326+
usePlayerStore.setState({ selectedElementId: null, selectedElementIds: new Set() });
327+
const { host, root } = renderToolbar();
328+
const button = host.querySelector<HTMLButtonElement>(
329+
'button[aria-label="Select a clip to zoom the timeline"]',
330+
);
331+
expect(button?.disabled).toBe(true);
332+
act(() => root.unmount());
333+
});
334+
335+
it("disables zoom-to-selection when the selection is not a timeline clip", () => {
336+
usePlayerStore.setState({
337+
elements: [{ id: "a", key: "a", tag: "div", start: 0, duration: 4, track: 0 }],
338+
selectedElementId: "scene.html#child",
339+
selectedElementIds: new Set(["scene.html#child"]),
340+
});
341+
const { host, root } = renderToolbar();
342+
expect(
343+
host.querySelector<HTMLButtonElement>(
344+
'button[aria-label="Select a clip to zoom the timeline"]',
345+
)?.disabled,
346+
).toBe(true);
347+
act(() => root.unmount());
348+
});
349+
350+
it("disables zoom-to-selection when every selected clip has no duration", () => {
351+
usePlayerStore.setState({
352+
elements: [{ id: "a", key: "a", tag: "div", start: 2, duration: 0, track: 0 }],
353+
selectedElementId: "a",
354+
selectedElementIds: new Set(["a"]),
355+
});
356+
const { host, root } = renderToolbar();
357+
expect(
358+
host.querySelector<HTMLButtonElement>(
359+
'button[aria-label="Select a clip to zoom the timeline"]',
360+
)?.disabled,
361+
).toBe(true);
362+
act(() => root.unmount());
363+
});
364+
365+
function clickZoom(host: HTMLElement) {
366+
act(() => {
367+
host
368+
.querySelector('button[aria-label="Zoom to selected clips"]')
369+
?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
370+
});
371+
act(() => {
372+
for (let i = 0; i < 30; i++) vi.advanceTimersToNextFrame();
373+
});
374+
}
375+
376+
it("zooms the mounted timeline so the selected clips fill the width between the margins", () => {
377+
const { scroll, unregister } = mountTimelineViewport();
378+
usePlayerStore.setState({
379+
elements: [{ id: "a", key: "a", tag: "div", start: 10, duration: 20, track: 0 }],
380+
selectedElementId: "a",
381+
selectedElementIds: new Set(["a"]),
382+
});
383+
const { host, root } = renderToolbar();
384+
clickZoom(host);
385+
const pps = usePlayerStore.getState().timelinePps;
386+
expect(pps).toBeCloseTo(50);
387+
expect(32 + 10 * pps - scroll.scrollLeft).toBeCloseTo(56);
388+
expect(32 + 30 * pps - scroll.scrollLeft).toBeCloseTo(1056);
389+
act(() => root.unmount());
390+
act(() => unregister());
391+
usePlayerStore.setState({
392+
elements: [],
393+
selectedElementId: null,
394+
selectedElementIds: new Set(),
395+
});
396+
});
397+
398+
it("keeps a clip at the start on screen without scrolling past zero", () => {
399+
const { scroll, unregister } = mountTimelineViewport();
400+
usePlayerStore.setState({
401+
elements: [{ id: "a", key: "a", tag: "div", start: 0, duration: 10, track: 0 }],
402+
selectedElementId: "a",
403+
selectedElementIds: new Set(["a"]),
404+
});
405+
const { host, root } = renderToolbar();
406+
clickZoom(host);
407+
expect(scroll.scrollLeft).toBe(0);
408+
const pps = usePlayerStore.getState().timelinePps;
409+
expect(32 + 10 * pps).toBeLessThanOrEqual(1080);
410+
act(() => root.unmount());
411+
act(() => unregister());
412+
});
413+
414+
it("keeps a clip at the end of the fit span inside the viewport", () => {
415+
const end = (1080 - 32 - 2) / 10;
416+
const { scroll, unregister } = mountTimelineViewport();
417+
usePlayerStore.setState({
418+
elements: [{ id: "a", key: "a", tag: "div", start: end - 10, duration: 10, track: 0 }],
419+
selectedElementId: "a",
420+
selectedElementIds: new Set(["a"]),
421+
});
422+
const { host, root } = renderToolbar();
423+
clickZoom(host);
424+
const pps = usePlayerStore.getState().timelinePps;
425+
expect(scroll.scrollLeft).toBeGreaterThan(0);
426+
expect(32 + (end - 10) * pps - scroll.scrollLeft).toBeGreaterThanOrEqual(0);
427+
expect(32 + end * pps - scroll.scrollLeft).toBeLessThanOrEqual(1080);
428+
act(() => root.unmount());
429+
act(() => unregister());
430+
});
431+
318432
it("shows Fit as a named icon and says whether fit is on", () => {
319433
const { host, root } = renderToolbar();
320434
const fit = () => host.querySelector('button[aria-label="Fit timeline to width"]');

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { memo, useEffect, useRef, type ReactNode } from "react";
22
import {
33
ArrowsOutLineHorizontal,
4+
FrameCorners,
45
Image,
56
Magnet,
67
MagnifyingGlassMinus,
@@ -19,8 +20,10 @@ import {
1920
} from "../player/components/timelineZoom";
2021
import { useTimelineZoom } from "../player/components/useTimelineZoom";
2122
import {
23+
canZoomToSelectedClips,
2224
useShownTimelineZoomPercent,
2325
zoomTimelineStep,
26+
zoomTimelineToSelectedClips,
2427
} from "../player/components/timelineZoomInput";
2528
import { usePlayerStore, type TimelineElement } from "../player";
2629
import { Tooltip } from "./ui";
@@ -37,7 +40,10 @@ import { LinkedSelectionToggle } from "./LinkedSelectionToggle";
3740
import { useProjectHasAudio } from "../utils/audioMeterMath";
3841
import { canAddBeatAt, addBeatAtCompositionTime } from "../utils/beatEditActions";
3942
import { isTypingTarget } from "../utils/typingTarget";
40-
import { SPLIT_SHORTCUT_HINT } from "../player/components/studioShortcuts";
43+
import {
44+
SPLIT_SHORTCUT_HINT,
45+
ZOOM_SELECTION_SHORTCUT_HINT,
46+
} from "../player/components/studioShortcuts";
4147
import type { TimelineProps } from "../player/components/TimelineTypes";
4248

4349
interface DomEditSessionSlice extends EnableKeyframesSession {
@@ -174,7 +180,13 @@ export const TimelineToolbar = memo(function TimelineToolbar({
174180
const beatAnalysisReady = usePlayerStore((s) => s.beatAnalysis !== null);
175181
// Subscribed, so the split button follows the selection, not only playhead ticks.
176182
const selectedElementId = usePlayerStore((s) => s.selectedElementId);
183+
const selectedElementIds = usePlayerStore((s) => s.selectedElementIds);
177184
const elements = usePlayerStore((s) => s.elements);
185+
const canZoomSelection = canZoomToSelectedClips({
186+
elements,
187+
selectedElementId,
188+
selectedElementIds,
189+
});
178190
const timelineFitPps = usePlayerStore((s) => s.timelineFitPps);
179191
const { zoomMode, setZoomMode, setManualZoomPercent } = useTimelineZoom();
180192
const displayedTimelineZoomPercent = useShownTimelineZoomPercent();
@@ -487,6 +499,25 @@ export const TimelineToolbar = memo(function TimelineToolbar({
487499
<Image size={16} aria-hidden="true" />
488500
</button>
489501
</Tooltip>
502+
<Tooltip
503+
label={
504+
canZoomSelection
505+
? `Zoom to selected clips (${ZOOM_SELECTION_SHORTCUT_HINT})`
506+
: "Select a clip to zoom the timeline"
507+
}
508+
>
509+
<button
510+
type="button"
511+
disabled={!canZoomSelection}
512+
aria-label={
513+
canZoomSelection ? "Zoom to selected clips" : "Select a clip to zoom the timeline"
514+
}
515+
onClick={() => zoomTimelineToSelectedClips()}
516+
className={canZoomSelection ? flatIdle : flatDisabled}
517+
>
518+
<FrameCorners size={16} aria-hidden="true" />
519+
</button>
520+
</Tooltip>
490521
<Tooltip label="Fit timeline to width">
491522
<button
492523
type="button"

‎packages/studio/src/hooks/appHotkeysDispatch.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { shouldIgnoreHistoryShortcut } from "../utils/studioHelpers";
99
import { canSplitElement } from "../utils/timelineElementSplit";
1010
import { trackStudioEvent } from "../utils/studioTelemetry";
1111
import { STUDIO_PLAIN_KEYS } from "../player/components/studioShortcuts";
12+
import { zoomTimelineToSelectedClips } from "../player/components/timelineZoomInput";
1213
import { openAudioGainDialog } from "../player/components/audioGainDialogStore";
1314
import type { LinkShortcutCallbacks } from "./linkShortcuts";
1415

@@ -156,6 +157,12 @@ export function dispatchModifierKey(
156157
* be asserted without standing up the whole hook. */
157158
export function dispatchPlainKey(event: KeyboardEvent, key: string, cb: HotkeyCallbacks): void {
158159
if (ownsPlainKeys(event.target)) return;
160+
if (key === STUDIO_PLAIN_KEYS.zoomSelection && !event.shiftKey && !event.altKey) {
161+
if (!zoomTimelineToSelectedClips()) return;
162+
event.preventDefault();
163+
return;
164+
}
165+
159166
if (key === STUDIO_PLAIN_KEYS.fullscreen && !event.shiftKey && !event.altKey) {
160167
event.preventDefault();
161168
if (document.fullscreenElement) void document.exitFullscreen();

‎packages/studio/src/hooks/useAppHotkeys.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import { clearAutomationClipboard, copyRange } from "../player/components/automa
77
import { VOLUME_RANGE } from "@hyperframes/core/audio-automation";
88
import type { TimelineElement } from "../player/store/timelineElement";
99
import { useAudioGainDialogStore } from "../player/components/audioGainDialogStore";
10+
import {
11+
cancelTimelineZoom,
12+
registerTimelineZoomViewport,
13+
} from "../player/components/timelineZoomInput";
1014

1115
/** Minimal valid fixture — TimelineElement only requires these five fields. */
1216
const bgmElement: TimelineElement = {
@@ -500,6 +504,77 @@ describe("dispatchPlainKey — G opens Audio Gain", () => {
500504
expect(event.defaultPrevented).toBe(true);
501505
});
502506

507+
it("Z zooms when a clip is selected and does not take the key otherwise", () => {
508+
const scroll = document.createElement("div");
509+
Object.defineProperties(scroll, {
510+
clientWidth: { value: 1080 },
511+
scrollWidth: { value: 1080 },
512+
scrollLeft: { value: 0, writable: true },
513+
});
514+
const unregister = registerTimelineZoomViewport({
515+
scroll,
516+
contentOrigin: 32,
517+
publishScroll: () => {},
518+
});
519+
usePlayerStore.setState({
520+
elements: [bgmElement],
521+
selectedElementId: "bgm",
522+
selectedElementIds: new Set(["bgm"]),
523+
});
524+
const event = press("z");
525+
dispatchPlainKey(event, "z", callbacks());
526+
expect(event.defaultPrevented).toBe(true);
527+
cancelTimelineZoom();
528+
unregister();
529+
530+
const unmounted = press("z");
531+
dispatchPlainKey(unmounted, "z", callbacks());
532+
expect(unmounted.defaultPrevented).toBe(false);
533+
534+
const collapsedScroll = document.createElement("div");
535+
Object.defineProperties(collapsedScroll, {
536+
clientWidth: { value: 32 },
537+
scrollWidth: { value: 32 },
538+
scrollLeft: { value: 0, writable: true },
539+
});
540+
const unregisterCollapsed = registerTimelineZoomViewport({
541+
scroll: collapsedScroll,
542+
contentOrigin: 32,
543+
publishScroll: () => {},
544+
});
545+
const collapsed = press("z");
546+
dispatchPlainKey(collapsed, "z", callbacks());
547+
expect(collapsed.defaultPrevented).toBe(false);
548+
unregisterCollapsed();
549+
550+
usePlayerStore.setState({ selectedElementId: null, selectedElementIds: new Set() });
551+
const idle = press("z");
552+
dispatchPlainKey(idle, "z", callbacks());
553+
expect(idle.defaultPrevented).toBe(false);
554+
555+
const shifted = new KeyboardEvent("keydown", { key: "z", shiftKey: true, bubbles: true });
556+
dispatchPlainKey(shifted, "z", callbacks());
557+
expect(shifted.defaultPrevented).toBe(false);
558+
559+
usePlayerStore.setState({
560+
elements: [bgmElement],
561+
selectedElementId: "scene.html#child",
562+
selectedElementIds: new Set(["scene.html#child"]),
563+
});
564+
const child = press("z");
565+
dispatchPlainKey(child, "z", callbacks());
566+
expect(child.defaultPrevented).toBe(false);
567+
568+
usePlayerStore.setState({
569+
elements: [{ ...bgmElement, duration: 0 }],
570+
selectedElementId: "bgm",
571+
selectedElementIds: new Set(["bgm"]),
572+
});
573+
const zero = press("z");
574+
dispatchPlainKey(zero, "z", callbacks());
575+
expect(zero.defaultPrevented).toBe(false);
576+
});
577+
503578
it("leaves G to the grid toggle when nothing selected has sound", () => {
504579
usePlayerStore.setState({ elements: [video], selectedElementId: "b-roll" });
505580
const event = press("g");

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const STUDIO_PLAIN_KEYS = {
55
record: "r",
66
addKeyframe: "k",
77
audioGain: "g",
8+
zoomSelection: "z",
89
} as const;
910

1011
export interface ShortcutHint {
@@ -20,6 +21,7 @@ export interface ShortcutSection {
2021
const hintKey = (key: string) => key.toUpperCase();
2122

2223
export const SPLIT_SHORTCUT_HINT = hintKey(STUDIO_PLAIN_KEYS.split);
24+
export const ZOOM_SELECTION_SHORTCUT_HINT = hintKey(STUDIO_PLAIN_KEYS.zoomSelection);
2325

2426
/** What PlayerControls' shortcuts panel lists unless an embedder passes its own sections. */
2527
export const DEFAULT_SHORTCUT_SECTIONS: readonly ShortcutSection[] = [
@@ -54,6 +56,7 @@ export const DEFAULT_SHORTCUT_SECTIONS: readonly ShortcutSection[] = [
5456
{ key: "⌘V", label: "Paste element" },
5557
{ key: "⌘X", label: "Cut element" },
5658
{ key: SPLIT_SHORTCUT_HINT, label: "Split clip at playhead" },
59+
{ key: ZOOM_SELECTION_SHORTCUT_HINT, label: "Zoom timeline to selected clips" },
5760
{ key: hintKey(STUDIO_PLAIN_KEYS.audioGain), label: "Audio Gain for clips with sound" },
5861
{ key: "⇧Click", label: "Razor tool: split all tracks" },
5962
{ key: "[", label: "Select clips starting before the playhead" },

0 commit comments

Comments
 (0)