FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Timeline: dragging a clip above the top track or below the last one adds a new track (#1104)
MarekSpodBiedry92eb83695771
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 6609afddf..73b42b535 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -2279,9 +2279,14 @@ fn build() -> Vec<CommandSpec> {"Move Clips",[],None,- r#"{"moves":[{"clip":id,"track":id|"V2","time":ticks}],"insert":bool,"linked":bool?,"copy":bool?}"#,+ r#"{"moves":[{"clip":id,"track":id|"V2"|"new" (after the last of its kind),"time":ticks}],"insert":bool,"linked":bool?,"copy":bool?}"#,has_seq,- move_clips+ |s, p| {+ if crate::sequence_tools::move_wants_new_track(p) {+ return crate::sequence_tools::move_to_new_tracks(s, p);+ }+ move_clips(s, p)+ }),cmd!("timeline.trim",diff --git a/crates/engine/src/sequence_tools.rs b/crates/engine/src/sequence_tools.rsindex dc6c57953..ddee169d2 100644--- a/crates/engine/src/sequence_tools.rs+++ b/crates/engine/src/sequence_tools.rs@@ -771,6 +771,65 @@ pub(crate) fn place_on_new_tracks(s: &mut Session, p: &Value) -> Result<Value> {}}+/// `timeline.move` names `"new"` as the `track` of a move.+pub(crate) fn move_wants_new_track(p: &Value) -> bool {+ p.get("moves").and_then(Value::as_array).is_some_and(|m| m.iter().any(is_new_move))+}++/// One move of `timeline.move` with `"track":"new"`.+fn is_new_move(m: &Value) -> bool {+ m.get("track").and_then(Value::as_str).is_some_and(|v| v.eq_ignore_ascii_case("new"))+}++/// `timeline.move` with `"track":"new"` on some moves (#1104: a clip dragged into the empty space+/// above the top video track or below the last audio track): one track is added after the last one+/// of each kind the `"new"` clips come from, they all land on it, and the track and the move are+/// one undo step. When the move fails, the new tracks go away again.+pub(crate) fn move_to_new_tracks(s: &mut Session, p: &Value) -> Result<Value> {+ let cmd = "timeline.move";+ let q = s.active_sequence().ok_or(EngineError::NoSequence)?;+ let (mut new_v, mut new_a) = (false, false);+ for m in p.get("moves").and_then(Value::as_array).into_iter().flatten().filter(|m| is_new_move(m)) {+ let clip = crate::commands::clip_p(m, "clip").ok_or_else(|| bad(cmd, "need `clip` in each move"))?;+ let (track, _) = q.find_item(clip).ok_or_else(|| bad(cmd, "no such clip"))?;+ if q.video_tracks.iter().any(|t| t.id == track) {+ new_v = true;+ } else {+ new_a = true;+ }+ }+ let added = add_tracks(s, &json!({"video": u64::from(new_v), "audio": u64::from(new_a)}))?;+ let mark = s.history.undo.len();+ let (video, audio) = (added.get("video").and_then(|v| v.get(0)).cloned(), added.get("audio").and_then(|v| v.get(0)).cloned());+ let mut moves = p.get("moves").and_then(Value::as_array).cloned().unwrap_or_default();+ for m in moves.iter_mut().filter(|m| is_new_move(m)) {+ let on_video = crate::commands::clip_p(m, "clip")+ .and_then(|c| s.active_sequence().and_then(|q| q.find_item(c).map(|(t, _)| q.video_tracks.iter().any(|v| v.id == t))))+ .unwrap_or(true);+ m["track"] = if on_video { video.clone() } else { audio.clone() }.unwrap_or(Value::Null);+ }+ let mut q = p.clone();+ q["moves"] = Value::Array(moves);+ let run = crate::commands::find(cmd).ok_or_else(|| EngineError::UnknownCommand(cmd.into()))?.run;+ match run(s, &q) {+ Ok(v) => {+ // the new tracks and the move are one undo step+ if mark >= 1+ && s.history.undo.len() == mark + 1+ && let (Some((label, _)), Some((_, before))) = (s.history.undo.pop(), s.history.undo.pop())+ {+ s.history.undo.push((label, before));+ }+ Ok(v)+ }+ Err(e) => {+ s.undo();+ s.history.redo.pop();+ Err(e)+ }+ }+}+fn delete_tracks(s: &mut Session, p: &Value) -> Result<Value> {let seq_id = s.state.active_sequence.ok_or(EngineError::NoSequence)?;let q = s.active_sequence().ok_or(EngineError::NoSequence)?;diff --git a/crates/engine/src/sequence_tools_tests.rs b/crates/engine/src/sequence_tools_tests.rsindex d275f9b3e..593a7a150 100644--- a/crates/engine/src/sequence_tools_tests.rs+++ b/crates/engine/src/sequence_tools_tests.rs@@ -607,3 +607,26 @@ fn place_on_new_tracks_adds_them_in_one_undo_step() {assert_eq!(*s.active_sequence().unwrap(), before);assert_eq!(s.history.undo.len(), undo);}++/// #1104: a clip dragged into the empty space past the last track moves onto a new track (with+/// `"new"`), one undo step; a move that fails leaves no new track behind.+#[test]+fn move_to_new_track_adds_it_in_one_undo_step() {+ let (mut s, item) = fresh();+ let clips = place(&mut s, item, "V1", 0, 0, 10);+ let before = s.active_sequence().unwrap().clone();+ let nv = before.video_tracks.len();+ let undo = s.history.undo.len();+ s.execute("timeline.move", json!({"moves": [{"clip": clips[0], "track": "new", "time": 0}], "linked": false})).unwrap();+ let q = s.active_sequence().unwrap();+ assert_eq!(q.video_tracks.len(), nv + 1);+ assert_eq!(q.video_tracks[nv].items.iter().map(|i| i.id.0).collect::<Vec<_>>(), [clips[0]], "the clip is on the new top video track");+ assert!(q.video_tracks[0].items.is_empty());+ assert_eq!(s.history.undo.len(), undo + 1, "one undo step");+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(*s.active_sequence().unwrap(), before);++ assert!(s.execute("timeline.move", json!({"moves": [{"clip": 999_999, "track": "new", "time": 0}]})).is_err());+ assert_eq!(*s.active_sequence().unwrap(), before);+ assert_eq!(s.history.undo.len(), undo);+}diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rsindex d5766c0bb..5f933e0e3 100644--- a/crates/ui-egui/src/panels/timeline.rs+++ b/crates/ui-egui/src/panels/timeline.rs@@ -94,6 +94,9 @@ pub enum Drag {start_track: TrackId,offset: Tick,track_delta: i32,+ /// #1104: the pointer is in the empty space past the last track of the dragged clips' kind:+ /// the drop moves them onto a new track there.+ new_track: Option<(TrackKind, Rect)>,/// Option is held: the drop copies the clips instead of moving them (as of the last/// drag frame, so letting go of both at once still copies).copy: bool,@@ -2137,7 +2140,7 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &}if resp.drag_started() {let clips = app.session.state.selection.clone();- Some(Drag::Move { clips, grab_tick: t, start_track: track, offset: Tick::ZERO, track_delta: 0, copy: mods.alt })+ Some(Drag::Move { clips, grab_tick: t, start_track: track, offset: Tick::ZERO, track_delta: 0, new_track: None, copy: mods.alt })} else {None}@@ -2238,7 +2241,19 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &(Some(c), Some(s)) if c.kind == s.kind => c.index as i32 - s.index as i32,_ => 0,};- Some(Drag::Move { clips, grab_tick, start_track, offset, track_delta, copy: mods.alt })+ // #1104: past the last track of the clips' kind, a new track (only for clips of one track)+ let new_track = start_row+ .filter(|_| clips.iter().all(|c| seq.find_item(*c).is_some_and(|(t, _)| t == start_track)))+ .and_then(|s| {+ let (vh, ah) = (app.ui.timeline.video_track_h, app.ui.timeline.audio_track_h);+ new_track_at(seq, layout, p.y, vh, ah).filter(|(kind, _)| *kind == s.kind)+ });+ if let Some((_, band)) = new_track {+ let (x0, x1) = (layout.x_of((first + offset).max(Tick::ZERO)), layout.x_of((last + offset).max(Tick::ZERO)));+ let r = Rect::from_min_max(pos2(x0, band.min.y + 1.0), pos2(x1, band.max.y - 1.0));+ ui.painter().rect_stroke(r, 3.0, Stroke::new(1.5, Color32::WHITE), StrokeKind::Inside);+ }+ Some(Drag::Move { clips, grab_tick, start_track, offset, track_delta, new_track, copy: mods.alt })}Drag::Trim { clip, edge, mode, from, .. } => seq.find_item(clip).map(|(_, it)| {let base = if edge == filmcraft_edit::Edge::In { it.start } else { it.end() };@@ -2305,10 +2320,12 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &&& let Some(d) = app.tl.drag.take(){let r = match d {- Drag::Move { clips, offset, track_delta, copy, .. } if offset != Tick::ZERO || track_delta != 0 => {+ Drag::Move { clips, offset, track_delta, new_track, copy, .. } if offset != Tick::ZERO || track_delta != 0 || new_track.is_some() => {+ // #1104: dropped past the last track, the clips go onto a new one+ let to = |tid| new_track.map_or_else(|| json!(shift_track(seq, tid, track_delta).unwrap_or(tid).0), |_| json!("new"));let moves: Vec<Value> = clips.iter()- .filter_map(|c| seq.find_item(*c).map(|(tid, it)| json!({"clip": c.0, "track": shift_track(seq, tid, track_delta).unwrap_or(tid).0, "time": (it.start + offset).max(Tick::ZERO).0})))+ .filter_map(|c| seq.find_item(*c).map(|(tid, it)| json!({"clip": c.0, "track": to(tid), "time": (it.start + offset).max(Tick::ZERO).0}))).collect();// Option held while dropping copies (Premiere: Option-drag duplicates)Some(app.session.execute("timeline.move", json!({"moves": moves, "insert": mods.command, "linked": false, "copy": copy || mods.alt})))