Skip to content

Commit 6b59004

Browse files
feat(studio): one timeline tool picker with select leftward and rightward (#4714)
* feat(studio): one timeline tool picker with select leftward and rightward The Selection and Razor buttons become one button showing the active tool, with a menu: Select (V), Split (B), Select leftward ([) and Select rightward (]). Leftward and rightward select every clip, on every track, that starts before or at/after the playhead. * test(studio): toolbar order and label follow the tool picker * fix(studio): select leftward and rightward use the live playhead and the usual selection resets * style(studio): keep playerStore's comment share at its base
1 parent 00a907d commit 6b59004

7 files changed

Lines changed: 190 additions & 31 deletions

File tree

‎packages/studio/src/components/TimelineHistoryButtons.dom.test.tsx‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,25 @@ it("leaves enabled buttons without the disabled attribute", () => {
9696
expect(el.className).not.toContain("cursor-not-allowed");
9797
});
9898

99-
it("orders the toolbar Undo, Redo, Select, Razor", () => {
99+
it("orders the toolbar Undo, Redo, then the tool picker showing the active tool", () => {
100100
const host = mount(<TimelineToolbar />);
101101
const labels = Array.from(host.querySelectorAll("button"))
102102
.map((b) => b.getAttribute("aria-label"))
103103
.filter((l) => l !== null)
104-
.slice(0, 4);
104+
.slice(0, 3);
105105

106-
expect(labels).toEqual(["Undo", "Redo", "Selection tool", "Razor tool"]);
106+
expect(labels).toEqual(["Undo", "Redo", "Timeline tool: Select"]);
107+
});
108+
109+
it("shows the Split tool on the picker while the razor is active", async () => {
110+
const { usePlayerStore } = await import("../player");
111+
usePlayerStore.setState({ activeTool: "razor" });
112+
try {
113+
const host = mount(<TimelineToolbar />);
114+
expect(host.querySelector('button[aria-label="Timeline tool: Split"]')).not.toBeNull();
115+
} finally {
116+
usePlayerStore.setState({ activeTool: "select" });
117+
}
107118
});
108119

109120
it("classifies Undo and Redo for the hotkey filters at their new location (KTD13)", () => {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import type { ReactNode } from "react";
2+
import { ArrowLineLeft, ArrowLineRight, CaretDown } from "@phosphor-icons/react";
3+
import { usePlayerStore } from "../player";
4+
import { Scissors } from "../icons/SystemIcons";
5+
import { Menu, MenuItem } from "./ui";
6+
import { cn } from "./ui/cn";
7+
import { flatIdle } from "./timelineToolbarStyles";
8+
9+
function SelectIcon() {
10+
return (
11+
<svg width="16" height="16" viewBox="0 0 12 12" fill="currentColor" aria-hidden="true">
12+
<path d="M2 0.5L10 6L6.5 6.5L8.5 11L6.5 11.5L4.5 7L2 9Z" />
13+
</svg>
14+
);
15+
}
16+
17+
interface ToolRow {
18+
label: string;
19+
shortcut: string;
20+
icon: ReactNode;
21+
active: boolean;
22+
run: () => void;
23+
}
24+
25+
/** One button for the active timeline tool; its menu switches tools or selects clips around the playhead. */
26+
export function TimelineToolPicker() {
27+
const activeTool = usePlayerStore((s) => s.activeTool);
28+
const store = () => usePlayerStore.getState();
29+
const rows: ToolRow[] = [
30+
{
31+
label: "Select",
32+
shortcut: "V",
33+
icon: <SelectIcon />,
34+
active: activeTool === "select",
35+
run: () => store().setActiveTool("select"),
36+
},
37+
{
38+
label: "Split",
39+
shortcut: "B",
40+
icon: <Scissors size={16} />,
41+
active: activeTool === "razor",
42+
run: () => store().setActiveTool("razor"),
43+
},
44+
{
45+
label: "Select leftward",
46+
shortcut: "[",
47+
icon: <ArrowLineLeft size={16} aria-hidden="true" />,
48+
active: false,
49+
run: () => store().selectLeftward(),
50+
},
51+
{
52+
label: "Select rightward",
53+
shortcut: "]",
54+
icon: <ArrowLineRight size={16} aria-hidden="true" />,
55+
active: false,
56+
run: () => store().selectRightward(),
57+
},
58+
];
59+
const current = rows.find((row) => row.active) ?? rows[0];
60+
return (
61+
<Menu
62+
aria-label="Timeline tools"
63+
trigger={
64+
<button
65+
type="button"
66+
aria-label={`Timeline tool: ${current.label}`}
67+
className={cn(flatIdle, "gap-0.5")}
68+
>
69+
{current.icon}
70+
<CaretDown size={10} weight="bold" aria-hidden="true" />
71+
</button>
72+
}
73+
>
74+
{rows.map((row) => (
75+
<MenuItem
76+
key={row.label}
77+
shortcut={row.shortcut}
78+
onClick={row.run}
79+
data-active={row.active || undefined}
80+
className={row.active ? "bg-neutral-800 text-white" : undefined}
81+
>
82+
<span className="flex items-center gap-2">
83+
{row.icon}
84+
{row.label}
85+
</span>
86+
</MenuItem>
87+
))}
88+
</Menu>
89+
);
90+
}

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

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { AudioMetersIcon } from "./icons/AudioMetersIcon";
2626
import { RippleEditIcon } from "./icons/RippleEditIcon";
2727
import { flatActive, flatBtn, flatDisabled, flatIdle } from "./timelineToolbarStyles";
2828
import { TimelineHistoryButtons, type TimelineHistoryButtonsProps } from "./TimelineHistoryButtons";
29-
import { Scissors } from "../icons/SystemIcons";
29+
import { TimelineToolPicker } from "./TimelineToolPicker";
3030
import type { GsapAnimation } from "@hyperframes/core/gsap-parser";
3131
import type { DomEditSelection } from "./editor/domEditingTypes";
3232
import { canSplitElement } from "../utils/timelineElementSplit";
@@ -144,8 +144,6 @@ export function TimelineToolbar({
144144
showAddBeat = true,
145145
showKeyframes = true,
146146
}: TimelineToolbarProps) {
147-
const activeTool = usePlayerStore((s) => s.activeTool);
148-
const setActiveTool = usePlayerStore((s) => s.setActiveTool);
149147
const timelineSnapEnabled = usePlayerStore((s) => s.timelineSnapEnabled);
150148
const setTimelineSnapEnabled = usePlayerStore((s) => s.setTimelineSnapEnabled);
151149
const rippleEditEnabled = usePlayerStore((s) => s.rippleEditEnabled);
@@ -216,30 +214,7 @@ export function TimelineToolbar({
216214
<div className="flex items-center justify-between px-2 py-0.5">
217215
<div className="flex items-center gap-0.5">
218216
<TimelineHistoryButtons {...history} />
219-
<Tooltip label="Selection tool (V)">
220-
<button
221-
type="button"
222-
onClick={() => setActiveTool("select")}
223-
aria-label="Selection tool"
224-
aria-pressed={activeTool === "select"}
225-
className={activeTool === "select" ? flatActive : flatIdle}
226-
>
227-
<svg width="16" height="16" viewBox="0 0 12 12" fill="currentColor">
228-
<path d="M2 0.5L10 6L6.5 6.5L8.5 11L6.5 11.5L4.5 7L2 9Z" />
229-
</svg>
230-
</button>
231-
</Tooltip>
232-
<Tooltip label="Razor tool (B) — Shift+click splits all tracks">
233-
<button
234-
type="button"
235-
onClick={() => setActiveTool("razor")}
236-
aria-label="Razor tool"
237-
aria-pressed={activeTool === "razor"}
238-
className={activeTool === "razor" ? flatActive : flatIdle}
239-
>
240-
<Scissors size={16} />
241-
</button>
242-
</Tooltip>
217+
<TimelineToolPicker />
243218
{/* Divider: tool-mode | editing-actions */}
244219
<div aria-hidden="true" className="mx-1 h-4 w-px bg-neutral-800" />
245220
<Tooltip label={timelineSnapEnabled ? "Snapping on (N)" : "Snapping off (N)"}>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ export function dispatchPlainKey(event: KeyboardEvent, key: string, cb: HotkeyCa
207207
return;
208208
}
209209

210+
if ((key === "[" || key === "]") && !event.shiftKey && !event.altKey) {
211+
event.preventDefault();
212+
const store = usePlayerStore.getState();
213+
if (key === "[") store.selectLeftward();
214+
else store.selectRightward();
215+
return;
216+
}
217+
210218
// CapCut returns to the select tool with "A". Scoped to the razor being
211219
// armed; usePlaybackKeyboard.ts's own "A" (seek to in-point) checks the
212220
// same activeTool to stay out of the way.

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @vitest-environment happy-dom
22
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
33
import { dispatchModifierKey, dispatchPlainKey, type HotkeyCallbacks } from "./appHotkeysDispatch";
4-
import { usePlayerStore } from "../player/store/playerStore";
4+
import { liveTime, usePlayerStore } from "../player/store/playerStore";
55
import type { DomEditSelection } from "../components/editor/domEditing";
66
import { clearAutomationClipboard, copyRange } from "../player/components/automationClipboard";
77
import { VOLUME_RANGE } from "@hyperframes/core/audio-automation";
@@ -60,6 +60,63 @@ afterEach(() => {
6060
});
6161
});
6262

63+
describe("dispatchPlainKey — select leftward / rightward", () => {
64+
const clips = [
65+
{ ...bgmElement, id: "early", key: "early", start: 0, track: 0 },
66+
{ ...bgmElement, id: "at", key: "at", start: 4, track: 1 },
67+
{ ...bgmElement, id: "late", key: "late", start: 7, track: 2 },
68+
];
69+
beforeEach(() => usePlayerStore.setState({ elements: clips, currentTime: 4 }));
70+
71+
it("[ selects every clip starting before the playhead, on every track", () => {
72+
const event = press("[");
73+
dispatchPlainKey(event, "[", callbacks());
74+
expect([...usePlayerStore.getState().selectedElementIds]).toEqual(["early"]);
75+
expect(usePlayerStore.getState().selectedElementId).toBe("early");
76+
expect(event.defaultPrevented).toBe(true);
77+
});
78+
79+
it("uses the live playhead while playing, not the time stored at play start", () => {
80+
usePlayerStore.setState({ isPlaying: true, currentTime: 0 });
81+
liveTime.notify(8);
82+
try {
83+
dispatchPlainKey(press("["), "[", callbacks());
84+
expect([...usePlayerStore.getState().selectedElementIds].sort()).toEqual([
85+
"at",
86+
"early",
87+
"late",
88+
]);
89+
} finally {
90+
usePlayerStore.setState({ isPlaying: false });
91+
liveTime.notify(0);
92+
}
93+
});
94+
95+
it("clears a clicked keyframe like any other selection change", () => {
96+
usePlayerStore.setState({ activeKeyframePct: 50 });
97+
dispatchPlainKey(press("]"), "]", callbacks());
98+
expect(usePlayerStore.getState().activeKeyframePct).toBeNull();
99+
});
100+
101+
it("] selects every clip starting at or after the playhead, on every track", () => {
102+
dispatchPlainKey(press("]"), "]", callbacks());
103+
const { selectedElementIds, selectedElementId } = usePlayerStore.getState();
104+
expect([...selectedElementIds].sort()).toEqual(["at", "late"]);
105+
expect(selectedElementId).toBe("at");
106+
});
107+
108+
it("selects nothing when no clip is on that side", () => {
109+
usePlayerStore.setState({
110+
currentTime: 0,
111+
selectedElementId: "late",
112+
selectedElementIds: new Set(["late"]),
113+
});
114+
dispatchPlainKey(press("["), "[", callbacks());
115+
expect(usePlayerStore.getState().selectedElementIds.size).toBe(0);
116+
expect(usePlayerStore.getState().selectedElementId).toBeNull();
117+
});
118+
});
119+
63120
describe("dispatchPlainKey — Delete arbitration", () => {
64121
const selectBgm = () =>
65122
usePlayerStore.setState({ elements: [bgmElement], selectedElementId: "bgm" });

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export const DEFAULT_SHORTCUT_SECTIONS: readonly ShortcutSection[] = [
5252
{ key: "⌘X", label: "Cut element" },
5353
{ key: hintKey(STUDIO_PLAIN_KEYS.split), label: "Split clip at playhead" },
5454
{ key: "⇧Click", label: "Razor tool: split all tracks" },
55+
{ key: "[", label: "Select clips starting before the playhead" },
56+
{ key: "]", label: "Select clips starting at or after the playhead" },
5557
{ key: "⌘G", label: "Group elements" },
5658
{ key: "⌘⇧G", label: "Ungroup" },
5759
{ key: "Del", label: "Delete selected element (no keyframe selected)" },

‎packages/studio/src/player/store/playerStore.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { createRangeSelectionSlice, type RangeSelectionSlice } from "./rangeSele
2323
import { createTimelineResetState } from "./timelineResetState";
2424
export type { KeyframeCacheEntry } from "./keyframeSlice";
2525
export { liveTime } from "./liveTime";
26+
import { liveTime } from "./liveTime";
2627
export { createTimelineResetState };
2728

2829
import type {
@@ -96,6 +97,8 @@ interface PlayerState extends PlayerStoreSlices {
9697

9798
activeTool: TimelineTool;
9899
setActiveTool: (tool: TimelineTool) => void;
100+
selectLeftward: () => void;
101+
selectRightward: () => void;
99102

100103
/** Tween-relative percentage of the last-clicked keyframe diamond. Operations
101104
* (drag, resize, rotate) target this instead of recomputing from playhead. */
@@ -250,6 +253,17 @@ interface BeatHistoryEntry {
250253
label: string;
251254
}
252255

256+
/** Selects like the marquee: the primary first, so its resets run, then the whole set. */
257+
function selectAroundPlayhead(
258+
state: PlayerState,
259+
keep: (start: number, playhead: number) => boolean,
260+
): void {
261+
const playhead = state.isPlaying ? liveTime.latest() : state.currentTime;
262+
const ids = state.elements.filter((el) => keep(el.start, playhead)).map((el) => el.key ?? el.id);
263+
state.setSelectedElementId(ids[0] ?? null);
264+
state.setSelectedElementIds(new Set(ids));
265+
}
266+
253267
export const usePlayerStore = create<PlayerState>((set, get) => ({
254268
isPlaying: false,
255269
currentTime: 0,
@@ -273,6 +287,8 @@ export const usePlayerStore = create<PlayerState>((set, get) => ({
273287

274288
activeTool: "select",
275289
setActiveTool: (tool) => set({ activeTool: tool }),
290+
selectLeftward: () => selectAroundPlayhead(get(), (start, playhead) => start < playhead),
291+
selectRightward: () => selectAroundPlayhead(get(), (start, playhead) => start >= playhead),
276292

277293
...createKeyframeSlice(set, () => ({
278294
timelineProjectId: get().timelineProjectId,

0 commit comments

Comments
 (0)