FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Alt-drag of a clip edge trims only that side of a link (#3301)
NetterSilvere6ce9d0680ee
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rsindex 411f7b69e..39e61ffaa 100644--- a/crates/engine/src/commands.rs+++ b/crates/engine/src/commands.rs@@ -627,12 +627,24 @@ fn clamp_trim_linked(q: &filmcraft_project::Sequence, clips: &[ClipId], edge: Ed/// trim drag shows this, so the edge stops at the end of the media or the neighbouring clip/// instead of following the pointer past it (#653).pub fn trim_delta(s: &Session, clip: ClipId, edge: Edge, mode: TrimMode, delta: Tick) -> Result<Tick> {+ trim_delta_linked(s, clip, edge, mode, delta, true)+}++/// [`trim_delta`] for a trim that, with `linked` false (Alt held), leaves the clip's linked+/// partners alone (#3300).+pub fn trim_delta_linked(s: &Session, clip: ClipId, edge: Edge, mode: TrimMode, delta: Tick, linked: bool) -> Result<Tick> {let q = s.active_sequence().ok_or(EngineError::NoSequence)?;let durations = |id: ItemId| crate::media_duration(&s.project, &s.media, id);let starts = |id: ItemId| crate::media_start(&s.project, id);let mut next = s.project.next_id;let ctx = edit::EditCtx { next_id: &mut next, media_duration: &durations, media_start: &starts, min_duration: q.settings.frame_rate.frame_duration() };- clamp_trim_linked(q, &with_links(s, &[clip]), edge, mode, delta, &ctx)+ let clips = if linked { with_links(s, &[clip]) } else { vec![clip] };+ clamp_trim_linked(q, &clips, edge, mode, delta, &ctx)+}++/// `clip` and, unless `p` has `"linked": false` (Alt held), its linked partners.+fn with_links_p(s: &Session, p: &Value, clip: ClipId) -> Vec<ClipId> {+ if bool_p(p, "linked").unwrap_or(true) { with_links(s, &[clip]) } else { vec![clip] }}/// The delta a roll of every cut in `pairs` can apply together: `delta` clamped by each pair's@@ -2758,7 +2770,7 @@ fn build() -> Vec<CommandSpec> {"Trim Edit",[],None,- r#"{"clip":id,"edge":"in|out","mode":"regular|ripple","delta":ticks|"deltaFrames":i64}"#,+ r#"{"clip":id,"edge":"in|out","mode":"regular|ripple","delta":ticks|"deltaFrames":i64,"linked":bool?}"#,has_seq,|s, p| {let c = clip_p(p, "clip").ok_or_else(|| bad("timeline.trim", "need `clip`"))?;@@ -2770,7 +2782,7 @@ fn build() -> Vec<CommandSpec> {.map(Tick).or_else(|| p.get("deltaFrames").and_then(Value::as_i64).map(|f| s.sequence_rate().tick_of(f))).unwrap_or_default();- let clips = with_links(s, &[c]);+ let clips = with_links_p(s, p, c);let applied = s.edit_sequence(if mode == TrimMode::Ripple { "Ripple Trim" } else { "Trim" }, |q, ctx, st| {if q.find_item(c).is_some_and(|(t, _)| q.track(t).is_some_and(|tr| tr.locked)) {return Err(edit::EditError::Locked.into());@@ -2944,7 +2956,7 @@ fn build() -> Vec<CommandSpec> {})?;Ok(Value::Null)}),- cmd!("timeline.rateStretch", "Rate Stretch", [], None, r#"{"clip":id,"edge":"in|out","delta":ticks}"#, has_seq, |s, p| {+ cmd!("timeline.rateStretch", "Rate Stretch", [], None, r#"{"clip":id,"edge":"in|out","delta":ticks,"linked":bool?}"#, has_seq, |s, p| {let c = clip_p(p, "clip").ok_or_else(|| bad("timeline.rateStretch", "need `clip`"))?;let edge = if str_p(p, "edge") == Some("in") { Edge::In } else { Edge::Out };let d = p@@ -2953,7 +2965,7 @@ fn build() -> Vec<CommandSpec> {.map(Tick).or_else(|| p.get("deltaFrames").and_then(Value::as_i64).map(|f| s.sequence_rate().tick_of(f))).unwrap_or_default();- let clips = with_links(s, &[c]);+ let clips = with_links_p(s, p, c);let sp = s.edit_sequence("Rate Stretch", |q, ctx, _| Ok(edit::rate_stretch_items(q, &clips, edge, d, ctx)?))?;Ok(json!({"speed": sp}))}),diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 4a1aed9a6..6b430e0ac 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -1297,6 +1297,8 @@ mod keyboard_tests;#[cfg(test)]mod linked_speed_tests;#[cfg(test)]+mod linked_trim_tests;+#[cfg(test)]mod locked_clip_edits_tests;#[cfg(test)]mod masks_tests;diff --git a/crates/engine/src/linked_trim_tests.rs b/crates/engine/src/linked_trim_tests.rsnew file mode 100644index 000000000..e1e722a31--- /dev/null+++ b/crates/engine/src/linked_trim_tests.rs@@ -0,0 +1,59 @@+//! Trimming and rate-stretching one side of a linked clip (#3300): `"linked": false` (Alt held+//! in the Timeline) edits only the given clip, while the default still edits its partners too.++use crate::Session;+use filmcraft_project::ClipId;+use filmcraft_time::Tick;+use serde_json::json;++fn linked_pair() -> (Session, ClipId, ClipId) {+ let mut s = Session::default();+ s.execute("file.openDemoProject", json!({})).unwrap();+ s.execute("file.newSequence", json!({"name": "Linked trim", "video": 1, "audio": 1})).unwrap();+ let item = s.project.items.values().find(|i| i.has_video() && i.has_audio() && i.as_media().is_some()).unwrap().id;+ s.execute("timeline.place", json!({"item": item.0, "frame": 0, "duration": s.sequence_rate().tick_of(48).0})).unwrap();+ let q = s.active_sequence().unwrap();+ let (video, sound) = (q.video_tracks[0].items[0].id, q.audio_tracks[0].items[0].id);+ assert!(q.find_item(video).unwrap().1.link.is_some(), "the pair is linked");+ (s, video, sound)+}++fn state(s: &Session, id: ClipId) -> (Tick, Tick, Tick) {+ let (_, it) = s.active_sequence().unwrap().find_item(id).unwrap();+ (it.start, it.duration, it.source_in)+}++#[test]+fn unlinked_trim_leaves_the_partner_alone() {+ for mode in ["regular", "ripple"] {+ for edge in ["in", "out"] {+ let (mut s, video, sound) = linked_pair();+ let (before_v, before_a) = (state(&s, video), state(&s, sound));+ let delta = if edge == "in" { 10 } else { -10 };+ s.execute("timeline.trim", json!({"clip": video.0, "edge": edge, "mode": mode, "deltaFrames": delta, "linked": false})).unwrap();+ assert_eq!(state(&s, video).1, before_v.1 - s.sequence_rate().tick_of(10), "{mode} {edge}: the video is trimmed");+ assert_eq!(state(&s, sound), before_a, "{mode} {edge}: the linked audio keeps its range");+ assert!(s.state.linked_selection, "the global toggle stays on");++ // without the flag, the partner follows as before+ let (mut s, video, sound) = linked_pair();+ s.execute("timeline.trim", json!({"clip": video.0, "edge": edge, "mode": mode, "deltaFrames": delta})).unwrap();+ assert_eq!(state(&s, video).1, before_v.1 - s.sequence_rate().tick_of(10));+ assert_eq!(state(&s, sound).1, before_a.1 - s.sequence_rate().tick_of(10), "{mode} {edge}: the linked audio follows");+ }+ }+}++#[test]+fn unlinked_rate_stretch_leaves_the_partner_alone() {+ let (mut s, video, sound) = linked_pair();+ let (before_v, before_a) = (state(&s, video), state(&s, sound));+ s.execute("timeline.rateStretch", json!({"clip": video.0, "edge": "out", "deltaFrames": 12, "linked": false})).unwrap();+ assert_eq!(state(&s, video).1, before_v.1 + s.sequence_rate().tick_of(12));+ assert_eq!(state(&s, sound), before_a, "the linked audio keeps its range");++ let (mut s, video, sound) = linked_pair();+ s.execute("timeline.rateStretch", json!({"clip": video.0, "edge": "out", "deltaFrames": 12})).unwrap();+ assert_eq!(state(&s, video).1, before_v.1 + s.sequence_rate().tick_of(12));+ assert_eq!(state(&s, sound).1, before_a.1 + s.sequence_rate().tick_of(12), "the linked audio follows");+}diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rsindex 8e984d28c..0da423ab7 100644--- a/crates/ui-egui/src/panels/timeline.rs+++ b/crates/ui-egui/src/panels/timeline.rs@@ -179,6 +179,8 @@ pub enum Drag {/// The time under the pointer when the button went down: the edge moves by how far the/// pointer has moved since (#259).from: Tick,+ /// Linked partners trim along; false when Alt was held as the drag started (#3300).+ linked: bool,},Roll {left: ClipId,@@ -203,6 +205,8 @@ pub enum Drag {delta: Tick,/// As in [`Drag::Trim`].from: Tick,+ /// As in [`Drag::Trim`].+ linked: bool,},/// Remix tool: drag a music clip's Out edge; on release `clip.remix` re-plans it to the new duration.Remix {@@ -2039,16 +2043,17 @@ fn preview_drag(app: &FilmcraftApp, seq: &Sequence, out: &mut HashMap<ClipId, (Tout.insert(*clip, (it.start, it.duration + *delta, None));}}- Drag::Trim { clip, edge, delta, .. } => {- for c in filmcraft_engine::commands::with_links(&app.session, &[*clip]) {+ Drag::Trim { clip, edge, delta, linked, .. } => {+ let ids = if *linked { filmcraft_engine::commands::with_links(&app.session, &[*clip]) } else { vec![*clip] };+ for c in ids {if let Some((_, it)) = seq.find_item(c) {moved(it, *edge, *delta);}}}// a rate stretch keeps the same media in a new length: that one does squeeze- Drag::Stretch { clip, edge, delta, .. } => {- let ids = filmcraft_engine::commands::with_links(&app.session, &[*clip]);+ Drag::Stretch { clip, edge, delta, linked, .. } => {+ let ids = if *linked { filmcraft_engine::commands::with_links(&app.session, &[*clip]) } else { vec![*clip] };for c in ids {if let Some((_, it)) = seq.find_item(c) {let v = match edge {@@ -2631,10 +2636,10 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &None}(_, Grab::Edge { clip, edge, kind, .. }) => Some(match kind {- EdgeKind::Trim => Drag::Trim { clip, edge, mode: filmcraft_edit::TrimMode::Regular, delta: Tick::ZERO, from: t },- EdgeKind::Ripple => Drag::Trim { clip, edge, mode: filmcraft_edit::TrimMode::Ripple, delta: Tick::ZERO, from: t },+ EdgeKind::Trim => Drag::Trim { clip, edge, mode: filmcraft_edit::TrimMode::Regular, delta: Tick::ZERO, from: t, linked: !mods.alt },+ EdgeKind::Ripple => Drag::Trim { clip, edge, mode: filmcraft_edit::TrimMode::Ripple, delta: Tick::ZERO, from: t, linked: !mods.alt },EdgeKind::Roll { left, right } => Drag::Roll { left, right, delta: Tick::ZERO, from: t },- EdgeKind::Stretch => Drag::Stretch { clip, edge, delta: Tick::ZERO, from: t },+ EdgeKind::Stretch => Drag::Stretch { clip, edge, delta: Tick::ZERO, from: t, linked: !mods.alt },EdgeKind::Remix => Drag::Remix { clip, delta: Tick::ZERO, from: t },}),(_, Grab::Volume { clip, .. }) if resp.drag_started() => super::timeline_volume::start(seq, layout, clip, p).map(Drag::Volume),@@ -2798,21 +2803,21 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &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)| {+ Drag::Trim { clip, edge, mode, from, linked, .. } => seq.find_item(clip).map(|(_, it)| {let base = if edge == filmcraft_edit::Edge::In { it.start } else { it.end() };let target = snap(app, seq, layout, rate.snap_nearest(base + (t_here - from)), &[clip]);// stop at the end of the media (or the neighbour), as the trim on release will (#653)- let delta = filmcraft_engine::commands::trim_delta(&app.session, clip, edge, mode, target - base).unwrap_or(target - base);- Drag::Trim { clip, edge, mode, delta, from }+ let delta = filmcraft_engine::commands::trim_delta_linked(&app.session, clip, edge, mode, target - base, linked).unwrap_or(target - base);+ Drag::Trim { clip, edge, mode, delta, from, linked }}),Drag::Remix { clip, from, .. } => seq.find_item(clip).map(|(_, it)| {let target = rate.snap_nearest(it.end() + (t_here - from)).max(it.start + rate.frame_duration());Drag::Remix { clip, delta: target - it.end(), from }}),- Drag::Stretch { clip, edge, from, .. } => seq.find_item(clip).map(|(_, it)| {+ Drag::Stretch { clip, edge, from, linked, .. } => seq.find_item(clip).map(|(_, it)| {let base = if edge == filmcraft_edit::Edge::In { it.start } else { it.end() };let target = snap(app, seq, layout, rate.snap_nearest(base + (t_here - from)), &[clip]);- Drag::Stretch { clip, edge, delta: target - base, from }+ Drag::Stretch { clip, edge, delta: target - base, from, linked }}),Drag::Roll { left, right, from, .. } => seq.find_item(left).map(|(_, l)| {let target = snap(app, seq, layout, rate.snap_nearest(l.end() + (t_here - from)), &[left, right]);@@ -2909,11 +2914,11 @@ fn interact(app: &mut FilmcraftApp, ui: &mut egui::Ui, seq: &Sequence, layout: &// 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})))}- Drag::Trim { clip, edge, mode, delta, .. } if delta != Tick::ZERO => Some(app.session.execute(+ Drag::Trim { clip, edge, mode, delta, linked, .. } if delta != Tick::ZERO => Some(app.session.execute("timeline.trim",- json!({"clip": clip.0, "edge": if edge == filmcraft_edit::Edge::In {"in"} else {"out"}, "mode": if mode == filmcraft_edit::TrimMode::Ripple {"ripple"} else {"regular"}, "delta": delta.0}),+ json!({"clip": clip.0, "edge": if edge == filmcraft_edit::Edge::In {"in"} else {"out"}, "mode": if mode == filmcraft_edit::TrimMode::Ripple {"ripple"} else {"regular"}, "delta": delta.0, "linked": linked}),)),- Drag::Stretch { clip, edge, delta, .. } if delta != Tick::ZERO => Some(app.session.execute("timeline.rateStretch", json!({"clip": clip.0, "edge": if edge == filmcraft_edit::Edge::In {"in"} else {"out"}, "delta": delta.0}))),+ Drag::Stretch { clip, edge, delta, linked, .. } if delta != Tick::ZERO => Some(app.session.execute("timeline.rateStretch", json!({"clip": clip.0, "edge": if edge == filmcraft_edit::Edge::In {"in"} else {"out"}, "delta": delta.0, "linked": linked}))),Drag::Remix { clip, delta, .. } if delta != Tick::ZERO => {let d = seq.find_item(clip).map(|(_, it)| it.duration + delta).unwrap_or(Tick::ZERO);Some(app.session.execute("clip.remix", json!({"clip": clip.0, "duration": d.0})))@@ -3396,7 +3401,7 @@ mod trim_preview_tests {let music = seq.audio_tracks.iter().flat_map(|t| &t.items).find(|i| i.name.contains("Ambient")).unwrap().clone();for (edge, delta) in [(filmcraft_edit::Edge::In, 5), (filmcraft_edit::Edge::Out, -5), (filmcraft_edit::Edge::In, -1)] {let delta = Tick(delta * filmcraft_time::TICKS_PER_SECOND);- app.tl.drag = Some(Drag::Trim { clip: music.id, edge, mode: filmcraft_edit::TrimMode::Regular, delta, from: Tick::ZERO });+ app.tl.drag = Some(Drag::Trim { clip: music.id, edge, mode: filmcraft_edit::TrimMode::Regular, delta, from: Tick::ZERO, linked: true });let (mut out, mut edited) = (HashMap::new(), HashMap::new());preview_drag(&app, &seq, &mut out, &mut edited);let shown = edited.get(&music.id).expect("the trimmed clip is drawn from its edited item");@@ -3427,7 +3432,7 @@ mod trim_preview_tests {assert_same_media(&right, &edited[&right.id]);assert_eq!(edited[&right.id].start, right.start + delta);// Rate Stretch keeps the same media in a new length: drawn from the original item- app.tl.drag = Some(Drag::Stretch { clip: left.id, edge: filmcraft_edit::Edge::Out, delta, from: Tick::ZERO });+ app.tl.drag = Some(Drag::Stretch { clip: left.id, edge: filmcraft_edit::Edge::Out, delta, from: Tick::ZERO, linked: true });let (mut out, mut edited) = (HashMap::new(), HashMap::new());preview_drag(&app, &seq, &mut out, &mut edited);assert!(edited.is_empty() && out.contains_key(&left.id));diff --git a/crates/ui-egui/tests/it/timeline_trim_ui.rs b/crates/ui-egui/tests/it/timeline_trim_ui.rsindex 8e859c09a..ea30dc80b 100644--- a/crates/ui-egui/tests/it/timeline_trim_ui.rs+++ b/crates/ui-egui/tests/it/timeline_trim_ui.rs@@ -700,3 +700,33 @@ fn linked_video_drag_adds_video_and_audio_tracks() {fn linked_audio_drag_adds_audio_and_video_tracks() {move_existing_clip_to_a_new_track(true, true);}++/// #3300: with Alt held, dragging the edge of one side of a linked clip trims only that side; the+/// linked audio keeps its range. Without Alt the partner still follows.+#[test]+fn alt_dragging_an_edge_trims_only_that_side_of_a_link() {+ for alt in [true, false] {+ let mut d = Driver::demo();+ let seq = d.app().session.active_sequence().expect("sequence");+ let (video, sound) = seq.video_tracks[0]+ .items+ .iter()+ .find_map(|v| {+ let a = seq.audio_tracks.iter().flat_map(|t| &t.items).find(|a| v.link.is_some() && a.link == v.link)?;+ Some((v.id.0, a.id.0))+ })+ .expect("a linked clip on V1");+ let (v0, a0) = (d.span(video), d.span(sound));+ let (_, x1, y) = d.edges(video);+ d.ok("ui.drag", json!({"from": {"x": x1 - 3.0, "y": y}, "to": {"x": x1 - 63.0, "y": y}, "steps": 20, "modifiers": {"alt": alt}}));+ d.frames(2);+ let v1 = d.span(video);+ assert_eq!(v1.0, v0.0, "alt={alt}: the In point stays");+ assert!(v1.1 < v0.1, "alt={alt}: the video's Out edge was trimmed: {v0:?} -> {v1:?}");+ if alt {+ assert_eq!(d.span(sound), a0, "Alt-trim leaves the linked audio alone");+ } else {+ assert_eq!(d.span(sound).1, a0.1 - (v0.1 - v1.1), "the linked audio follows a plain trim");+ }+ }+}