Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

#712: keyboard Slide keeps linked picture and sound aligned at a neighbour limit

vangel76ce6afffc6f0e
diff --git a/crates/edit/src/lib.rs b/crates/edit/src/lib.rs
index 2f5a8382b..554e9e5c7 100644
--- a/crates/edit/src/lib.rs
+++ b/crates/edit/src/lib.rs
@@ -876,6 +876,27 @@ pub fn slide(seq: &mut Sequence, clip: ClipId, delta: Tick, ctx: &mut EditCtx) -
Ok(d)
}
+/// Slide several items (a linked picture/sound pair, a selection) by one common delta: the most
+/// restrictive item's neighbour limit clamps them all, so their relative timing is kept.
+/// Returns the delta applied.
+pub fn slide_items(seq: &mut Sequence, clips: &[ClipId], delta: Tick, ctx: &mut EditCtx) -> Result<Tick> {
+ let mut d = delta;
+ for c in clips {
+ let x = slide(&mut seq.clone(), *c, d, ctx)?;
+ if x.0.signum() != d.0.signum() {
+ d = Tick::ZERO;
+ } else if x.abs() < d.abs() {
+ d = x;
+ }
+ }
+ if d != Tick::ZERO {
+ for c in clips {
+ slide(seq, *c, d, ctx)?;
+ }
+ }
+ Ok(d)
+}
+
/// Rate stretch: change an item's duration by dragging an edge, adjusting speed to keep the same media.
pub fn rate_stretch(seq: &mut Sequence, clip: ClipId, edge: Edge, delta: Tick, ctx: &mut EditCtx) -> Result<f64> {
let (tid, it) = seq.find_item(clip).ok_or(EditError::NoItem(clip))?;
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 0b3a9f8a6..47cc2f181 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -2349,20 +2349,8 @@ fn build() -> Vec<CommandSpec> {
.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]);
- s.edit_sequence("Slide", |q, ctx, _| {
- // clamp across all linked partners, then slide them by the common delta
- let mut dd = d;
- for c in &clips {
- let x = edit::slide(&mut q.clone(), *c, dd, ctx)?;
- if x.abs() < dd.abs() {
- dd = x;
- }
- }
- for c in &clips {
- edit::slide(q, *c, dd, ctx)?;
- }
- Ok(())
- })?;
+ // clamp across all linked partners, then slide them by the common delta
+ s.edit_sequence("Slide", |q, ctx, _| Ok(edit::slide_items(q, &clips, d, ctx)?))?;
Ok(Value::Null)
}),
cmd!("timeline.rateStretch", "Rate Stretch", [], None, r#"{"clip":id,"edge":"in|out","delta":ticks}"#, has_seq, |s, p| {
diff --git a/crates/engine/src/keyboard.rs b/crates/engine/src/keyboard.rs
index d8487251c..55563816f 100644
--- a/crates/engine/src/keyboard.rs
+++ b/crates/engine/src/keyboard.rs
@@ -265,12 +265,8 @@ fn slip_selection(s: &mut Session, frames: i64) -> Result<Value> {
fn slide_selection(s: &mut Session, frames: i64) -> Result<Value> {
let d = s.sequence_rate().tick_of(frames);
let clips = with_links(s, &s.state.selection.clone());
- s.edit_sequence("Slide", |q, ctx, _| {
- for c in &clips {
- edit::slide(q, *c, d, ctx)?;
- }
- Ok(())
- })?;
+ // one common delta, clamped by the most restrictive member, keeps linked partners in sync
+ s.edit_sequence("Slide", |q, ctx, _| Ok(edit::slide_items(q, &clips, d, ctx)?))?;
Ok(Value::Null)
}
diff --git a/crates/engine/src/keyboard_tests.rs b/crates/engine/src/keyboard_tests.rs
index b23a9809d..60e9b7b43 100644
--- a/crates/engine/src/keyboard_tests.rs
+++ b/crates/engine/src/keyboard_tests.rs
@@ -376,3 +376,29 @@ fn reveal_nested_sequence_opens_it_at_the_matching_frame() {
assert_ne!(s.state.active_sequence, Some(outer));
assert_eq!(s.playhead(), nest_clip.source_in + rate.tick_of(7), "{r}");
}
+
+#[test]
+fn keyboard_slide_keeps_linked_partners_aligned_at_a_neighbour_limit() {
+ // #712: a three-frame sound-only follower limits only the sound partner
+ let mut s = Session::default();
+ s.execute("file.newProject", json!({"name": "Slide"})).unwrap();
+ s.execute("file.newSequence", json!({"name": "Sequence", "fps": 25, "video": 1, "audio": 1, "width": 16, "height": 16})).unwrap();
+ let r = s.execute("file.newOfflineFile", json!({"name": "Media", "seconds": 4, "fps": 25, "video": true, "audio": true})).unwrap();
+ let item = r["item"].as_u64().unwrap();
+ let fr = s.sequence_rate().tick_of(1);
+ let r =
+ s.execute("timeline.place", json!({"item": item, "track": "V1", "audioTrack": "A1", "time": 0, "sourceIn": fr.0 * 20, "duration": fr.0 * 60})).unwrap();
+ let (v, a) = (r["clips"][0].as_u64().unwrap(), r["clips"][1].as_u64().unwrap());
+ let r = s.execute("timeline.place", json!({"item": item, "track": "A1", "time": fr.0 * 60, "sourceIn": 0, "duration": fr.0 * 3})).unwrap();
+ let f = r["clips"][0].as_u64().unwrap();
+ s.execute("sequence.linkedSelection", json!({"on": true})).unwrap();
+ s.execute("timeline.select", json!({"clips": [v]})).unwrap();
+ s.execute("timeline.slideRight5", json!({})).unwrap();
+ assert_eq!(clip(&s, v).start, Tick(fr.0 * 2), "the pair moves only as far as the sound may");
+ assert_eq!(clip(&s, a).start, clip(&s, v).start, "picture and sound stay aligned");
+ let fl = clip(&s, f);
+ assert_eq!((fl.start, fl.duration, fl.source_in), (Tick(fr.0 * 62), fr, Tick(fr.0 * 2)));
+ // nothing more to give: a further slide right moves neither partner
+ s.execute("timeline.slideRight", json!({})).unwrap();
+ assert_eq!((clip(&s, v).start, clip(&s, a).start), (Tick(fr.0 * 2), Tick(fr.0 * 2)));
+}