Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

feat: timeline tabs reorder by dragging

Jareer89987def6fd2
diff --git a/desktop/src/App.tsx b/desktop/src/App.tsx
index 5bc74fe1..0a03e7e4 100644
--- a/desktop/src/App.tsx
+++ b/desktop/src/App.tsx
@@ -1343,6 +1343,9 @@ function Editor({
onRenameTimeline={(timelineId, name) =>
void dispatch({ op: "renameTimeline", timelineId, name })
}
+ onMoveTimeline={(timelineId, index) =>
+ void dispatch({ op: "moveTimeline", timelineId, index })
+ }
onRequestRemoveTimeline={(timelineId) => {
const meta = project.timelines.find((candidate) => candidate.id === timelineId);
if (meta) setTimelineToDelete({ id: meta.id, name: meta.name });
diff --git a/desktop/src/components/TimelinePanel.tsx b/desktop/src/components/TimelinePanel.tsx
index 9d8b2dbc..6421c888 100644
--- a/desktop/src/components/TimelinePanel.tsx
+++ b/desktop/src/components/TimelinePanel.tsx
@@ -158,6 +158,7 @@ export function TimelinePanel({
onSelectTimeline,
onAddTimeline,
onRenameTimeline,
+ onMoveTimeline,
onRequestRemoveTimeline,
onGestureEnd,
}: {
@@ -208,6 +209,8 @@ export function TimelinePanel({
onSelectTimeline: (timelineId: string) => void;
onAddTimeline: () => void;
onRenameTimeline: (timelineId: string, name: string) => void;
+ /** Drops a dragged tab at a new slot; the index counts the tab as removed. */
+ onMoveTimeline: (timelineId: string, index: number) => void;
/** Asks the app to confirm and delete; the panel never deletes directly. */
onRequestRemoveTimeline: (timelineId: string) => void;
/** A move or trim drag finished; the echoed change becomes one command. */
@@ -757,6 +760,72 @@ export function TimelinePanel({
onTrackScroll(clampTrackScroll(trackScroll + event.deltaY, rows.length, canvasRef.current));
};
+ // ── timeline tab reorder ──────────────────────────────────────────────────
+ // A pointer drag on a tab, not HTML5 drag-and-drop: the webview's OS file
+ // drop handling owns that channel (see the bin drag in App for the same
+ // choice). The tab is pressed, pulled past a small threshold, and dropped
+ // into the slot whose caret is showing; a plain click still selects.
+ const tabStrip = useRef<HTMLDivElement>(null);
+ /** The tab being dragged and the caret position, in strip content pixels. */
+ const [tabDrag, setTabDrag] = useState<{ id: string; x: number } | null>(null);
+ const tabPress = useRef<{ id: string; x: number; dragging: boolean } | null>(null);
+ /** Swallows exactly one click: the one the browser fires after a drop. */
+ const tabDropped = useRef(false);
+
+ /** The insertion slot under the pointer, and where to draw the caret. */
+ const tabSlotAt = (clientX: number): { slot: number; x: number } | null => {
+ const strip = tabStrip.current;
+ if (!strip) return null;
+ const tabs = Array.from(strip.querySelectorAll<HTMLElement>('[role="tab"]'));
+ if (tabs.length === 0) return null;
+ const origin = strip.getBoundingClientRect().left - strip.scrollLeft;
+ for (let slot = 0; slot < tabs.length; slot += 1) {
+ const rect = tabs[slot].getBoundingClientRect();
+ if (clientX < rect.left + rect.width / 2) {
+ return { slot, x: rect.left - origin - 1 };
+ }
+ }
+ const last = tabs[tabs.length - 1].getBoundingClientRect();
+ return { slot: tabs.length, x: last.right - origin + 1 };
+ };
+
+ const onTabPointerDown = (timelineId: string, event: React.PointerEvent) => {
+ if (event.button !== 0) return;
+ event.currentTarget.setPointerCapture(event.pointerId);
+ tabPress.current = { id: timelineId, x: event.clientX, dragging: false };
+ };
+ const onTabPointerMove = (event: React.PointerEvent) => {
+ const press = tabPress.current;
+ if (!press) return;
+ if (!press.dragging && Math.abs(event.clientX - press.x) < 4) return;
+ press.dragging = true;
+ const target = tabSlotAt(event.clientX);
+ if (target) setTabDrag({ id: press.id, x: target.x });
+ };
+ const onTabPointerUp = (event: React.PointerEvent) => {
+ const press = tabPress.current;
+ tabPress.current = null;
+ if (!press?.dragging) return;
+ setTabDrag(null);
+ tabDropped.current = true;
+ const from = project.timelines.findIndex((timeline) => timeline.id === press.id);
+ const target = tabSlotAt(event.clientX);
+ if (from === -1 || !target) return;
+ // The slot counts the dragged tab too; lifted out, everything to its
+ // right shifts one left.
+ const index = target.slot > from ? target.slot - 1 : target.slot;
+ if (index !== from) onMoveTimeline(press.id, index);
+ };
+ const selectUnlessDropped = (timelineId: string) => {
+ // Reordering must not also switch documents: the engine command leaves
+ // the selection alone, so the click that follows a drop is swallowed.
+ if (tabDropped.current) {
+ tabDropped.current = false;
+ return;
+ }
+ onSelectTimeline(timelineId);
+ };
+
return (
<div className={PANEL_SHELL}>
{/*
@@ -766,7 +835,8 @@ export function TimelinePanel({
as changing documents, not changing tools.
*/}
<div
- className="thin-scroll flex h-8 shrink-0 items-end gap-0.5 overflow-x-auto
+ ref={tabStrip}
+ className="thin-scroll relative flex h-8 shrink-0 items-end gap-0.5 overflow-x-auto
overflow-y-hidden border-b border-hairline bg-sunken px-1.5 pt-1"
>
{project.timelines.map((timeline) => (
@@ -775,11 +845,22 @@ export function TimelinePanel({
timeline={timeline}
active={timeline.id === project.activeTimelineId}
closable={project.timelines.length > 1}
- onSelect={onSelectTimeline}
+ dimmed={tabDrag?.id === timeline.id}
+ onSelect={selectUnlessDropped}
onRename={onRenameTimeline}
onRequestRemove={onRequestRemoveTimeline}
+ onDragPointerDown={onTabPointerDown}
+ onDragPointerMove={onTabPointerMove}
+ onDragPointerUp={onTabPointerUp}
/>
))}
+ {tabDrag && (
+ <span
+ aria-hidden
+ className="pointer-events-none absolute bottom-0.5 top-1.5 w-0.5 rounded bg-accent"
+ style={{ left: tabDrag.x }}
+ />
+ )}
<span className="self-center">
<IconButton icon="plus" label="New timeline" size={7} onClick={onAddTimeline} />
</span>
@@ -939,16 +1020,25 @@ function TimelineTab({
timeline,
active,
closable,
+ dimmed,
onSelect,
onRename,
onRequestRemove,
+ onDragPointerDown,
+ onDragPointerMove,
+ onDragPointerUp,
}: {
timeline: TimelineMeta;
active: boolean;
closable: boolean;
+ /** True while this tab is the one being dragged to a new slot. */
+ dimmed: boolean;
onSelect: (timelineId: string) => void;
onRename: (timelineId: string, name: string) => void;
onRequestRemove: (timelineId: string) => void;
+ onDragPointerDown: (timelineId: string, event: React.PointerEvent) => void;
+ onDragPointerMove: (event: React.PointerEvent) => void;
+ onDragPointerUp: (event: React.PointerEvent) => void;
}) {
const [renaming, setRenaming] = useState(false);
@@ -983,6 +1073,9 @@ function TimelineTab({
title={active ? timeline.name : `Switch to ${timeline.name}`}
onClick={() => onSelect(timeline.id)}
onDoubleClick={() => setRenaming(true)}
+ onPointerDown={(event) => onDragPointerDown(timeline.id, event)}
+ onPointerMove={onDragPointerMove}
+ onPointerUp={onDragPointerUp}
// Browser-tab shape: rounded top only, and the active tab overlaps the
// strip's bottom border by a pixel with the panel's own background, so
// it reads as one surface with the tray below rather than a pill
@@ -992,7 +1085,7 @@ function TimelineTab({
active
? "border border-b-0 border-hairline bg-panel text-primary"
: "text-secondary hover:bg-hover hover:text-primary"
- }`}
+ } ${dimmed ? "opacity-40" : ""}`}
>
<span className="truncate">{timeline.name}</span>
{closable && (
diff --git a/desktop/src/lib/generated/Command.ts b/desktop/src/lib/generated/Command.ts
index d168bebe..8f039bb5 100644
--- a/desktop/src/lib/generated/Command.ts
+++ b/desktop/src/lib/generated/Command.ts
@@ -202,7 +202,16 @@ name: string, } | { "op": "selectTimeline",
/**
* The timeline to switch to.
*/
-timelineId: string, } | { "op": "addFont",
+timelineId: string, } | { "op": "moveTimeline",
+/**
+ * The timeline to move. An unknown id is a no-op.
+ */
+timelineId: string,
+/**
+ * Where it lands among its siblings, 0-based, counted with the
+ * timeline already removed from its old slot. Clamped to the end.
+ */
+index: number, } | { "op": "addFont",
/**
* The family name titles will refer to.
*/
diff --git a/engine/crates/wolfcut-project/src/commands.rs b/engine/crates/wolfcut-project/src/commands.rs
index 973ace0a..e38bc83f 100644
--- a/engine/crates/wolfcut-project/src/commands.rs
+++ b/engine/crates/wolfcut-project/src/commands.rs
@@ -404,6 +404,15 @@ pub enum Command {
/// The timeline to switch to.
timeline_id: String,
},
+ /// Moves a timeline tab to a new position in the strip. Which timeline
+ /// is active does not change - only the order of the tabs.
+ MoveTimeline {
+ /// The timeline to move. An unknown id is a no-op.
+ timeline_id: String,
+ /// Where it lands among its siblings, 0-based, counted with the
+ /// timeline already removed from its old slot. Clamped to the end.
+ index: usize,
+ },
/// Registers a font file for titles. A path already registered is a
/// no-op, so re-adding cannot duplicate.
AddFont {
@@ -1243,6 +1252,21 @@ pub fn apply(
Ok(Outcome { created_id: None, applied })
}
+ Command::MoveTimeline { timeline_id, index } => {
+ let Some(from) =
+ project.timelines.iter().position(|timeline| timeline.id == timeline_id)
+ else {
+ return Ok(Outcome::default());
+ };
+ let to = index.min(project.timelines.len() - 1);
+ if to == from {
+ return Ok(Outcome::default());
+ }
+ let timeline = project.timelines.remove(from);
+ project.timelines.insert(to, timeline);
+ Ok(Outcome { created_id: None, applied: true })
+ }
+
Command::AddFont { family, path } => {
if project.fonts.iter().any(|font| font.path == path) {
return Ok(Outcome::default());
diff --git a/engine/crates/wolfcut-project/src/lib.rs b/engine/crates/wolfcut-project/src/lib.rs
index 4e656050..237cd229 100644
--- a/engine/crates/wolfcut-project/src/lib.rs
+++ b/engine/crates/wolfcut-project/src/lib.rs
@@ -206,6 +206,37 @@ mod tests {
assert!(refused.is_err(), "the last timeline cannot be deleted");
}
+ #[test]
+ fn a_timeline_moves_to_a_new_slot_without_stealing_the_selection() {
+ let mut editor = Editor::new();
+ let second = editor.apply(Command::AddTimeline).expect("adds").created_id.expect("id");
+ let third = editor.apply(Command::AddTimeline).expect("adds").created_id.expect("id");
+ editor.apply(Command::SelectTimeline { timeline_id: "TL1".to_owned() }).expect("ok");
+
+ editor
+ .apply(Command::MoveTimeline { timeline_id: third.clone(), index: 0 })
+ .expect("moves");
+ let order: Vec<_> =
+ editor.project().timelines.iter().map(|timeline| timeline.id.clone()).collect();
+ assert_eq!(order, vec![third.clone(), "TL1".to_owned(), second.clone()]);
+ assert_eq!(editor.project().active_timeline_id, "TL1", "moving must not select");
+
+ // An index past the end clamps to the last slot; a same-slot move and
+ // an unknown id apply nothing, so neither records history.
+ editor
+ .apply(Command::MoveTimeline { timeline_id: third.clone(), index: 99 })
+ .expect("ok");
+ let order: Vec<_> =
+ editor.project().timelines.iter().map(|timeline| timeline.id.clone()).collect();
+ assert_eq!(order, vec!["TL1".to_owned(), second, third]);
+ let before_can_undo = editor.can_undo();
+ let outcome = editor
+ .apply(Command::MoveTimeline { timeline_id: "nope".to_owned(), index: 0 })
+ .expect("no-op");
+ assert!(!outcome.applied);
+ assert_eq!(editor.can_undo(), before_can_undo);
+ }
+
#[test]
fn undo_and_redo_walk_the_history() {
let (mut editor, _, clip_id) = fixture();