FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
#164: ripple delete says which sync-locked track blocks it; playhead snaps to cuts
carlitosteso3e7e96b247fb2
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rsindex 99df7040d..2699c2593 100644--- a/crates/edit/src/lib.rs+++ b/crates/edit/src/lib.rs@@ -35,8 +35,9 @@ pub enum EditError {NoTrack(TrackId),#[error("track is locked")]Locked,- #[error("this edit would break sync on a sync-locked track")]- SyncLockConflict,+ /// Names the sync-locked track (`A2`) whose clip is in the way.+ #[error("this edit would break sync: {0} is sync-locked and has a clip in the way (turn off its sync lock, or lock the track, to make this edit)")]+ SyncLockConflict(String),#[error("not enough media (handles) for this trim")]NoHandles,#[error("edit would make a clip shorter than one frame")]@@ -381,6 +382,17 @@ fn ripple_delete_spans(seq: &Sequence, items: &[ClipId]) -> Vec<TimeRange> {spans}+/// How the UI names a track: `V1`, `A2` (by position), else its name.+pub fn track_label(seq: &Sequence, id: TrackId) -> String {+ if let Some(i) = seq.video_tracks.iter().position(|t| t.id == id) {+ return format!("V{}", i + 1);+ }+ if let Some(i) = seq.audio_tracks.iter().position(|t| t.id == id) {+ return format!("A{}", i + 1);+ }+ seq.track(id).map(|t| t.name.clone()).unwrap_or_else(|| format!("track {}", id.0))+}+/// `ranges` in time order, with the ones that touch or overlap joined.fn merged(mut ranges: Vec<TimeRange>) -> Vec<TimeRange> {ranges.sort_by_key(|r| r.start);@@ -422,7 +434,7 @@ pub fn ripple_delete_items(seq: &mut Sequence, items: &[ClipId]) -> Result<Vec<Tcontinue;}if !on_affected && !track_range_empty(tr, *span) {- return Err(EditError::SyncLockConflict);+ return Err(EditError::SyncLockConflict(track_label(seq, tr.id)));}shift_track_from(tr, span.end(), -span.duration);}@@ -450,7 +462,7 @@ pub fn close_gap(seq: &mut Sequence, track: TrackId, t: Tick) -> Result<()> {continue;}if !track_range_empty(tr, gap) {- return Err(EditError::SyncLockConflict);+ return Err(EditError::SyncLockConflict(track_label(seq, tr.id)));}}shift_track_from(tr, gap.end(), -gap.duration);@@ -589,7 +601,7 @@ pub fn trim(seq: &mut Sequence, clip: ClipId, edge: Edge, mode: TrimMode, delta:// (the clip's own linked partners are the caller's to trim or leave)let in_the_way = tr.items.iter().any(|i| i.range().overlaps(&closing) && (own_link.is_none() || i.link != own_link));if tr.id != tid && shift < Tick::ZERO && in_the_way {- return Err(EditError::SyncLockConflict);+ return Err(EditError::SyncLockConflict(track_label(seq, tr.id)));}let from = if edge == Edge::In { old_start + Tick(1) } else { old_end };for i in &mut tr.items {@@ -700,7 +712,7 @@ pub fn ripple_trim_group(seq: &mut Sequence, clips: &[ClipId], edge: Edge, deltalet closing = if edge == Edge::In { TimeRange::new(from - Tick(1), -shift) } else { TimeRange::new(from + shift, -shift) };let own = |i: &TrackItem| i.link.is_some_and(|l| own_links.contains(&l));if tr.items.iter().any(|i| i.range().overlaps(&closing) && !follows(i) && !own(i) && !stays(i)) {- return Err(EditError::SyncLockConflict);+ return Err(EditError::SyncLockConflict(track_label(seq, tid)));}}let followers: Vec<ClipId> = tr.items.iter().filter(|i| !clips.contains(&i.id) && follows(i)).map(|i| i.id).collect();@@ -719,7 +731,7 @@ pub fn ripple_trim_group(seq: &mut Sequence, clips: &[ClipId], edge: Edge, delta});// a follower with no room (a clip in its way, or the sequence start) blocks the editif tr.items.first().is_some_and(|i| i.start < Tick::ZERO) || tr.items.windows(2).any(|w| w[0].end() > w[1].start) {- return Err(EditError::SyncLockConflict);+ return Err(EditError::SyncLockConflict(track_label(seq, tr.id)));}}transitions_follow_cuts(seq, &mut work);diff --git a/crates/edit/src/tests.rs b/crates/edit/src/tests.rsindex 6f89c2a0e..4f49f1fad 100644--- a/crates/edit/src/tests.rs+++ b/crates/edit/src/tests.rs@@ -144,7 +144,7 @@ fn ripple_delete_and_conflict() {// audio under the gap blocks the ripplelet b = fx.put(v1, 20, 10, 0);fx.put(a1, 22, 3, 0);- assert_eq!(ripple_delete_items(&mut fx.seq, &[b]), Err(EditError::SyncLockConflict));+ assert_eq!(ripple_delete_items(&mut fx.seq, &[b]), Err(EditError::SyncLockConflict("A1".into())), "names the track in the way");assert_eq!(fx.spans(v1), vec![(0, 10), (20, 10)], "unchanged on failure");}@@ -284,10 +284,10 @@ fn ripple_trim_group_takes_a_split_edit_along() {ripple_trim_group(&mut fx.seq, &[a], Edge::Out, -f(5), &mut Fx::ctx(&mut n)).unwrap();assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 8), (8, 10)], vec![(4, 14)]), "shorter: it follows too");// the sound would start before the sequence does: refused, nothing changes- assert_eq!(ripple_trim_group(&mut fx.seq, &[a], Edge::Out, -f(5), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));+ assert!(matches!(ripple_trim_group(&mut fx.seq, &[a], Edge::Out, -f(5), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict(_))));// another clip right before the sound leaves it no room eitherfx.put(a1, 0, 4, 0);- assert_eq!(ripple_trim_group(&mut fx.seq, &[a], Edge::Out, -f(2), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));+ assert!(matches!(ripple_trim_group(&mut fx.seq, &[a], Edge::Out, -f(2), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict(_))));assert_eq!((fx.spans(v1), fx.spans(a1)), (vec![(0, 8), (8, 10)], vec![(0, 4), (4, 14)]), "unchanged on failure");// a linked clip that ends before the cut is not a split edit and stays where it islet early = fx.put(fx.a(1), 0, 3, 0);@@ -592,7 +592,7 @@ fn shorter_head_into_an_l_cut_keeps_the_earlier_sound_where_it_is() {// later material on A1 moves up with the pictures; landing on the L cut's sound refuseslet later = fx.put(a1, 15, 5, 0);- assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));+ assert!(matches!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict(_))));assert_eq!(fx.spans(a1), vec![(0, 13), (15, 5)], "unchanged on failure");fx.seq.find_item_mut(later).unwrap().1.start = f(20);ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(2), &mut Fx::ctx(&mut n)).unwrap();@@ -605,10 +605,10 @@ fn shorter_head_into_an_l_cut_keeps_the_earlier_sound_where_it_is() {let b = fx.put(v1, 10, 10, 0);fx.put(a1, 0, 13, 0);let mut n = fx.next;- assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));+ assert!(matches!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict(_))));// so does a link with no partner leftfx.seq.track_mut(a1).unwrap().items[0].link = Some(9);- assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));+ assert!(matches!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict(_))));// a linked cutaway across the cut whose own partner reaches past it too is no L cut: it refuseslet mut fx = Fx::new();@@ -622,7 +622,7 @@ fn shorter_head_into_an_l_cut_keeps_the_earlier_sound_where_it_is() {fx.seq.find_item_mut(c).unwrap().1.link = Some(7);}let mut n = fx.next;- assert_eq!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict));+ assert!(matches!(ripple_trim_group(&mut fx.seq, &[b], Edge::In, f(3), &mut Fx::ctx(&mut n)), Err(EditError::SyncLockConflict(_))));}#[test]@@ -651,7 +651,7 @@ fn shorter_head_closes_the_stretch_after_the_cut_on_sync_locked_tracks() {} else {trim(&mut fx.seq, b, Edge::In, TrimMode::Ripple, f(2), &mut Fx::ctx(&mut n))};- assert_eq!(r, Err(EditError::SyncLockConflict), "group {group}");+ assert!(matches!(r, Err(EditError::SyncLockConflict(_))), "group {group}");}// a clip on a sync-locked track that starts at the cut stays put while the pictures after it move// up: only the stretch after the cut sees it (the old stretch, before the cut, let it through)@@ -668,7 +668,7 @@ fn shorter_head_closes_the_stretch_after_the_cut_on_sync_locked_tracks() {} else {trim(&mut fx.seq, b, Edge::In, TrimMode::Ripple, f(3), &mut Fx::ctx(&mut n))};- assert_eq!(r, Err(EditError::SyncLockConflict), "group {group}");+ assert!(matches!(r, Err(EditError::SyncLockConflict(_))), "group {group}");assert_eq!(fx.spans(v1), vec![(0, 10), (10, 10), (20, 10)], "unchanged on failure");}}diff --git a/crates/engine/src/settings.rs b/crates/engine/src/settings.rsindex c9a5d4307..f4216067b 100644--- a/crates/engine/src/settings.rs+++ b/crates/engine/src/settings.rs@@ -441,7 +441,7 @@ impl Default for TimelinePrefs {surround_media_tracks: "useFile".into(),multichannel_mono_media_tracks: "useFile".into(),focus_timeline_on_edit: false,- snap_playhead: false,+ snap_playhead: true,return_to_beginning: true,out_of_sync_unlinked: false,play_after_rendering: true,diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rsindex 5fe7b6ce4..872b4e301 100644--- a/crates/ui-egui/src/panels/timeline.rs+++ b/crates/ui-egui/src/panels/timeline.rs@@ -1404,6 +1404,16 @@ fn edge_geometry(seq: &Sequence, layout: &Layout, track: filmcraft_project::Trac/// Snap `t` to nearby candidates (edits, playhead, markers, in/out). Returns snapped tick.fn snap(app: &mut FilmcraftApp, seq: &Sequence, layout: &Layout, t: Tick, exclude: &[ClipId]) -> Tick {+ snap_to(app, seq, layout, t, exclude, true)+}++/// Where a dragged playhead lands: [`snap`], but not onto itself, or it would stick where it is+/// and only move in jumps of `SNAP_PX` (#164).+fn snap_playhead(app: &mut FilmcraftApp, seq: &Sequence, layout: &Layout, t: Tick) -> Tick {+ snap_to(app, seq, layout, t, &[], false)+}++fn snap_to(app: &mut FilmcraftApp, seq: &Sequence, layout: &Layout, t: Tick, exclude: &[ClipId], to_playhead: bool) -> Tick {if !app.session.state.snapping {return t;}@@ -1417,7 +1427,9 @@ fn snap(app: &mut FilmcraftApp, seq: &Sequence, layout: &Layout, t: Tick, excludcands.push(it.end());}}- cands.push(app.session.playhead());+ if to_playhead {+ cands.push(app.session.playhead());+ }cands.extend(seq.markers.iter().map(|m| m.start));cands.extend(seq.mark_in);cands.extend(seq.mark_out);@@ -1891,7 +1903,10 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &if let Some(d) = started {if matches!(d, Drag::Scrub) {app.stop();- let tt = rate.snap_nearest(layout.tick_at(p.x).max(Tick::ZERO));+ let mut tt = rate.snap_nearest(layout.tick_at(p.x).max(Tick::ZERO));+ if mods.shift || app.session.prefs.timeline.snap_playhead {+ tt = snap_playhead(app, seq, layout, tt);+ }app.session.set_playhead(tt);}if resp.drag_started() {@@ -1911,7 +1926,7 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &let mut tt = rate.snap_nearest(t_here.max(Tick::ZERO));// Shift snaps; Settings ▸ Timeline ▸ "Snap playhead in Timeline when Snap is enabled"if mods.shift || app.session.prefs.timeline.snap_playhead {- tt = snap(app, seq, layout, tt, &[]);+ tt = snap_playhead(app, seq, layout, tt);}app.session.set_playhead(tt);Some(Drag::Scrub)diff --git a/crates/ui-egui/tests/settings_ui.rs b/crates/ui-egui/tests/settings_ui.rsindex d28549eb9..c85136187 100644--- a/crates/ui-egui/tests/settings_ui.rs+++ b/crates/ui-egui/tests/settings_ui.rs@@ -197,7 +197,7 @@ fn checkbox_edits_apply_on_ok_and_not_on_cancel() {d.click("settings.ok");assert!(d.draft().is_null(), "OK closes");assert_eq!(d.pref("trim.selectionToolRollRipple"), true, "edits on several pages apply together");- assert_eq!(d.pref("timeline.snapPlayhead"), true);+ assert_eq!(d.pref("timeline.snapPlayhead"), false, "on by default (as in Premiere), the click turns it off");}#[test]@@ -331,3 +331,33 @@ fn snap_playhead_and_return_to_beginning() {d.ok("ui.playback", json!({"action": "stop"}));assert!(ph.0 < dur.0 / 2, "restarts from the beginning: {ph:?}");}++/// #164: dragging the playhead in the ruler snaps to a cut, and moves freely away from one+/// (it used to snap onto its own position and only move in jumps).+#[test]+fn playhead_drag_snaps_to_cuts() {+ let mut d = Driver::demo();+ d.frames(2);+ let seq = d.harness.state().session.active_sequence().unwrap().clone();+ let v1 = &seq.video_tracks[0];+ let cut = v1.items[1].start;+ let id = v1.items[1].id.0;+ let rect = |d: &mut Driver, id: &str| -> [f64; 4] {+ let v = d.ok("ui.elements", json!({"prefix": id}));+ let e = v.as_array().unwrap().iter().find(|e| e["id"] == id).unwrap_or_else(|| panic!("no element {id}")).clone();+ let r: Vec<f64> = e["rect"].as_array().unwrap().iter().map(|x| x.as_f64().unwrap()).collect();+ [r[0], r[1], r[2], r[3]]+ };+ let clip = rect(&mut d, &format!("timeline.clip.{id}"));+ let ruler = rect(&mut d, "timeline.ruler");+ let y = ruler[1] + ruler[3] / 2.0;+ let (from, near) = (clip[0] + 40.0, clip[0] + 3.0);+ d.ok("ui.drag", json!({"from": {"x": from, "y": y}, "to": {"x": near, "y": y}, "steps": 8}));+ d.frames(2);+ assert_eq!(d.harness.state().session.playhead(), cut, "lands on the cut");+ // a step at a time from the cut, it follows the mouse instead of sticking+ d.ok("ui.drag", json!({"from": {"x": clip[0] + 30.0, "y": y}, "to": {"x": clip[0] + 22.0, "y": y}, "steps": 8}));+ d.frames(2);+ let ph = d.harness.state().session.playhead();+ assert!(ph > cut, "moves off the cut freely: {ph:?} vs {cut:?}");+}