FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#483: media dropped past the last track goes on a new track
vangel76cca7d5ef0ea3
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 33e2ee85f..d49361f05 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -2110,9 +2110,12 @@ fn build() -> Vec<CommandSpec> {"Place Clip",[],None,- r#"{"item":id,"track":"V1"|id|"A1" (sound only)?,"audioTrack":"A1"|id?,"time":ticks|"frame":i64|"seconds":f64,"insert":bool,"sourceIn":ticks?,"duration":ticks?,"video":bool=true,"audio":bool=true}"#,+ r#"{"item":id,"track":"V1"|id|"A1" (sound only)|"new" (a video track after the last)?,"audioTrack":"A1"|id|"new"?,"time":ticks|"frame":i64|"seconds":f64,"insert":bool,"sourceIn":ticks?,"duration":ticks?,"video":bool=true,"audio":bool=true}"#,has_seq,|s, p| {+ if crate::sequence_tools::wants_new_track(p) {+ return crate::sequence_tools::place_on_new_tracks(s, p);+ }let stream_flag = |key: &str| match p.get(key) {None => Ok(true),Some(Value::Bool(v)) => Ok(*v),diff --git a/crates/engine/src/sequence_tools.rs b/crates/engine/src/sequence_tools.rsindex a0027913d..dc6c57953 100644--- a/crates/engine/src/sequence_tools.rs+++ b/crates/engine/src/sequence_tools.rs@@ -723,6 +723,54 @@ pub(crate) fn add_tracks(s: &mut Session, p: &Value) -> Result<Value> {Ok(json!({"video": video, "audio": audio, "submix": submix}))}+/// `timeline.place` names `"new"` as its `track` or `audioTrack`.+pub(crate) fn wants_new_track(p: &Value) -> bool {+ ["track", "audioTrack"].iter().any(|k| p.get(*k).and_then(Value::as_str).is_some_and(|v| v.eq_ignore_ascii_case("new")))+}++/// `timeline.place` with `"track":"new"` and/or `"audioTrack":"new"` (#483: media dropped in the+/// empty space above the video tracks or below the audio tracks): a track is added after the+/// last one of its kind for each stream the item carries (and the call enables), then the clip+/// goes on it. One undo step; when the placement fails, the new tracks go away again.+pub(crate) fn place_on_new_tracks(s: &mut Session, p: &Value) -> Result<Value> {+ let cmd = "timeline.place";+ let item = crate::commands::item_p(p, "item").ok_or_else(|| bad(cmd, "need `item`"))?;+ let pi = s.project.item(item).ok_or_else(|| bad(cmd, "no such item"))?;+ let is_new = |k: &str| p.get(k).and_then(Value::as_str).is_some_and(|v| v.eq_ignore_ascii_case("new"));+ let enabled = |k: &str| p.get(k).and_then(Value::as_bool).unwrap_or(true);+ let new_v = is_new("track") && pi.has_video() && enabled("video");+ let new_a = is_new("audioTrack") && pi.has_audio() && enabled("audio");+ if !new_v && !new_a {+ return Err(bad(cmd, "no new track to add: the item has no picture or sound for it"));+ }+ let added = add_tracks(s, &json!({"video": u64::from(new_v), "audio": u64::from(new_a)}))?;+ let mark = s.history.undo.len();+ let mut q = p.clone();+ for (key, kind) in [("track", "video"), ("audioTrack", "audio")] {+ if is_new(key) {+ q[key] = added.get(kind).and_then(|ids| ids.get(0)).cloned().unwrap_or(Value::Null);+ }+ }+ let place = crate::commands::find(cmd).ok_or_else(|| EngineError::UnknownCommand(cmd.into()))?.run;+ match place(s, &q) {+ Ok(v) => {+ // the new tracks and the clip 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 9e164ce2c..d275f9b3e 100644--- a/crates/engine/src/sequence_tools_tests.rs+++ b/crates/engine/src/sequence_tools_tests.rs@@ -575,3 +575,35 @@ fn go_to_in_out_without_marks_go_to_the_sequence_ends() {s.execute("markers.goToIn", json!({})).unwrap();assert_eq!(s.playhead(), s.active_sequence().unwrap().mark_in.unwrap());}++/// #483: media dropped in the empty space past the last track goes on a new track (with+/// `"new"`), one undo step; a placement that fails leaves no new track behind.+#[test]+fn place_on_new_tracks_adds_them_in_one_undo_step() {+ let (mut s, item) = fresh();+ let before = s.active_sequence().unwrap().clone();+ let (nv, na) = (before.video_tracks.len(), before.audio_tracks.len());+ let undo = s.history.undo.len();+ let r = s.execute("timeline.place", json!({"item": item, "track": "new", "audioTrack": "new", "time": 0})).unwrap();+ let q = s.active_sequence().unwrap();+ assert_eq!((q.video_tracks.len(), q.audio_tracks.len()), (nv + 1, na + 1));+ let clips: Vec<ClipId> = r["clips"].as_array().unwrap().iter().map(|c| ClipId(c.as_u64().unwrap())).collect();+ assert_eq!(clips.len(), 2);+ assert_eq!(q.video_tracks[nv].items.iter().map(|i| i.id).collect::<Vec<_>>(), [clips[0]], "the picture is on the new top video track");+ assert_eq!(q.audio_tracks[na].items.iter().map(|i| i.id).collect::<Vec<_>>(), [clips[1]], "the sound is on the new last audio track");+ assert_eq!(s.history.undo.len(), undo + 1, "one undo step");+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(*s.active_sequence().unwrap(), before);++ // only the picture gets a new track; the sound follows the given track+ s.execute("timeline.place", json!({"item": item, "track": "new", "audioTrack": "A1", "time": 0})).unwrap();+ let q = s.active_sequence().unwrap();+ assert_eq!((q.video_tracks.len(), q.audio_tracks.len()), (nv + 1, na));+ assert_eq!((q.video_tracks[nv].items.len(), q.audio_tracks[0].items.len()), (1, 1));+ s.execute("edit.undo", json!({})).unwrap();++ // a placement that fails after the tracks were added: nothing changes+ assert!(s.execute("timeline.place", json!({"item": item, "track": "new", "audioTrack": "A99", "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 17a5eef96..2b1e8fef2 100644--- a/crates/ui-egui/src/panels/timeline.rs+++ b/crates/ui-egui/src/panels/timeline.rs@@ -189,6 +189,26 @@ impl Layout {}}+/// #483: where a drop at `y` adds a track: the empty space above the top video track (a video+/// track) or below the last audio track (an audio track), never over a track, the caption tracks+/// or the Mix row. Gives the kind and the band the new track would take (`vh` / `ah` high).+fn new_track_at(seq: &Sequence, layout: &Layout, y: f32, vh: f32, ah: f32) -> Option<(TrackKind, Rect)> {+ if layout.row_at(y).is_some() {+ return None;+ }+ let cap_n = seq.caption_tracks.len();+ let tracks_top = layout.content.min.y + if cap_n > 0 { cap_n as f32 * super::timeline_captions::ROW_H + DIVIDER_H } else { 0.0 };+ let band = |y0: f32, y1: f32| Rect::from_min_max(pos2(layout.content.min.x, y0), pos2(layout.content.max.x, y1));+ if y < layout.split_y {+ let top = layout.rows.iter().filter(|r| r.kind == TrackKind::Video).map(|r| r.rect.min.y).fold(layout.split_y, f32::min);+ (y >= tracks_top && y < top).then(|| (TrackKind::Video, band((top - vh).max(tracks_top), top)))+ } else {+ let bottom = layout.rows.iter().filter(|r| r.kind == TrackKind::Audio).map(|r| r.rect.max.y).fold(layout.split_y + DIVIDER_H, f32::max);+ let mix = layout.content.max.y - MASTER_H;+ (y >= bottom && y < mix).then(|| (TrackKind::Audio, band(bottom, (bottom + ah).min(mix))))+ }+}+/// How far the Timeline reaches, in seconds: ten minutes past the end of the sequence. Premiere/// Pro's Timeline does the same (on a 6 s sequence its view stops with 00:10:06 at the right/// edge). The view cannot be scrolled past it and the scroll bar spans it.@@ -2380,21 +2400,34 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &let dur = source.map(|s| s.range.duration).unwrap_or_else(|| app.session.project.item(item).map(|i| i.duration()).filter(|d| d.0 > 0 && !is_still).unwrap_or(still));- if let Some(row) = &row {- let r = Rect::from_min_max(pos2(layout.x_of(t), row.rect.min.y + 1.0), pos2(layout.x_of(t + dur), row.rect.max.y - 1.0));+ // #483: in the empty space above the top video track or below the last audio track, a new track+ let new_track = if row.is_some() { None } else { new_track_at(seq, layout, p.y, app.ui.timeline.video_track_h, app.ui.timeline.audio_track_h) }.filter(+ |(kind, _)| {+ let carries = app.session.project.item(item).is_some_and(|i| if *kind == TrackKind::Video { i.has_video() } else { i.has_audio() });+ carries && source.is_none_or(|s| if *kind == TrackKind::Video { s.video } else { s.audio })+ },+ );+ if let Some(band) = row.as_ref().map(|r| r.rect).or(new_track.map(|(_, band)| band)) {+ let r = Rect::from_min_max(pos2(layout.x_of(t), band.min.y + 1.0), pos2(layout.x_of(t + dur), band.max.y - 1.0));ui.painter().rect_filled(r, 3.0, Color32::from_white_alpha(40));ui.painter().rect_stroke(r, 3.0, Stroke::new(1.5, Color32::WHITE), StrokeKind::Inside);if mods.command {ui.painter().text(r.left_top() + vec2(4.0, -2.0), Align2::LEFT_BOTTOM, tl!("Insert"), Tokens::ui(10.0), Color32::WHITE);}}+ let target = match (&row, new_track) {+ (Some(row), _) => Some(match row.kind {+ TrackKind::Video => (json!(row.track.0), json!(seq.audio_tracks.get(row.index).or(seq.audio_tracks.first()).map(|t| t.id.0))),+ TrackKind::Audio => (json!(seq.video_tracks.get(row.index).or(seq.video_tracks.first()).map(|t| t.id.0)), json!(row.track.0)),+ }),+ // the other stream goes on the track of the same number, or on a new one as well+ (None, Some((TrackKind::Video, _))) => Some((json!("new"), seq.audio_tracks.get(seq.video_tracks.len()).map_or(json!("new"), |t| json!(t.id.0)))),+ (None, Some((TrackKind::Audio, _))) => Some((seq.video_tracks.get(seq.audio_tracks.len()).map_or(json!("new"), |t| json!(t.id.0)), json!("new"))),+ (None, None) => None,+ };if ctx.input(|i| i.pointer.any_released())- && let Some(row) = row+ && let Some((vt, at)) = target{- let (vt, at) = match row.kind {- TrackKind::Video => (Some(row.track.0), seq.audio_tracks.get(row.index).or(seq.audio_tracks.first()).map(|t| t.id.0)),- TrackKind::Audio => (seq.video_tracks.get(row.index).or(seq.video_tracks.first()).map(|t| t.id.0), Some(row.track.0)),- };let mut params = json!({"item": item.0, "track": vt, "audioTrack": at, "time": t.0, "insert": mods.command});if let Some(source) = source {params["sourceIn"] = json!(source.range.start.0);@@ -2648,3 +2681,40 @@ mod thumbnail_tiles_tests {assert!(thumbnail_tiles(ThumbnailMode::Continuous, 0.0, 0.0, 80.0, (0.0, 800.0), dur, FRAME).is_empty());}}++#[cfg(test)]+mod new_track_tests {+ use super::*;+ use filmcraft_project::{Project, SequenceSettings};++ /// #483: a drop above the top video track adds a video track, one below the last audio track an+ /// audio track; over a track, between the areas or on the Mix row it adds none.+ #[test]+ fn empty_space_past_the_tracks_adds_a_track() {+ let mut p = Project::new("t");+ let id = p.new_sequence("s", SequenceSettings::default(), 1, 1, None);+ let seq = p.sequence(id).cloned().expect("sequence");+ // video area 0..200 (V1 at 150..200), divider, audio area 205..466 (A1 at 205..255), Mix 466..500+ let row = |kind, track: &filmcraft_project::Track, y0: f32| Row {+ track: track.id,+ kind,+ index: 0,+ rect: Rect::from_min_max(pos2(0.0, y0), pos2(800.0, y0 + 50.0)),+ lane: false,+ };+ let layout = Layout {+ content: Rect::from_min_max(pos2(0.0, 0.0), pos2(800.0, 500.0)),+ ruler: Rect::from_min_max(pos2(0.0, -20.0), pos2(800.0, 0.0)),+ rows: vec![row(TrackKind::Video, &seq.video_tracks[0], 150.0), row(TrackKind::Audio, &seq.audio_tracks[0], 205.0)],+ pps: 100.0,+ scroll: 0.0,+ split_y: 200.0,+ };+ let at = |y| new_track_at(&seq, &layout, y, 50.0, 50.0);+ assert_eq!(at(40.0), Some((TrackKind::Video, Rect::from_min_max(pos2(0.0, 100.0), pos2(800.0, 150.0)))));+ assert_eq!(at(300.0), Some((TrackKind::Audio, Rect::from_min_max(pos2(0.0, 255.0), pos2(800.0, 305.0)))));+ for y in [160.0, 202.0, 230.0, 480.0] {+ assert_eq!(at(y), None, "y = {y}");+ }+ }+}