Skip to content

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

fix(engine): Unlink a selection that also holds unlinked clips (#1103)

Sam Sandweiss869e40701ce2
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 58d943b83..7d929e226 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -1865,11 +1865,24 @@ fn build() -> Vec<CommandSpec> {
}),
cmd!("clip.link", "Link", ["Clip"], Some("Cmd+L"), r#"{"clips":[id]?}"#, has_selection, |s, p| {
let sel = clips_p(s, p);
- let linked = sel.iter().all(|c| s.active_sequence().and_then(|q| q.find_item(*c)).is_some_and(|(_, i)| i.link.is_some()));
+ // Unlink when any selected clip is linked, as the Timeline's context menu says (#1103):
+ // a title or a music clip among the selection must not turn Unlink into a re-link
+ let linked = sel.iter().any(|c| s.active_sequence().and_then(|q| q.find_item(*c)).is_some_and(|(_, i)| i.link.is_some()));
s.edit_sequence(if linked { "Unlink" } else { "Link" }, |q, ctx, _| {
if linked {
+ let mut links = Vec::new();
for c in &sel {
if let Some((_, i)) = q.find_item_mut(*c) {
+ links.extend(i.link.take());
+ }
+ }
+ // a partner left alone in its link (one half unlinked on its own) is unlinked
+ // too: a one-member link would read as "linked" with nothing to follow
+ for l in links {
+ let members: Vec<ClipId> = q.all_tracks().flat_map(|t| t.items.iter()).filter(|i| i.link == Some(l)).map(|i| i.id).collect();
+ if let [lonely] = members[..]
+ && let Some((_, i)) = q.find_item_mut(lonely)
+ {
i.link = None;
}
}
diff --git a/crates/engine/src/explicit_targets_tests.rs b/crates/engine/src/explicit_targets_tests.rs
index 2abbcee12..4a80f9261 100644
--- a/crates/engine/src/explicit_targets_tests.rs
+++ b/crates/engine/src/explicit_targets_tests.rs
@@ -307,3 +307,26 @@ fn link_leaves_clips_without_a_partner_unlinked() {
assert_eq!(clip(&s, v1).unwrap().link, None, "v1 has no overlapping audio in the selection");
assert_eq!(clip(&s, a2).unwrap().link, None, "a2 has no overlapping video in the selection");
}
+
+/// #1103: Unlink with a selection that also holds an unlinked clip (a title, a music clip) unlinks
+/// the linked ones, as the Timeline's context menu offers, instead of re-linking them.
+#[test]
+fn unlink_with_an_unlinked_clip_in_the_selection_still_unlinks() {
+ let mut s = demo();
+ let (v, a) = (v1(&s), a1(&s));
+ let (v0, a0, v1, a1) = (v[0].id, a[0].id, v[1].id, a[1].id);
+ assert!(clip(&s, v0).unwrap().link.is_some() && clip(&s, v1).unwrap().link.is_some(), "the demo's clips start linked");
+ // make one clip unlinked on its own: its partner goes with it (no one-member link stays)
+ s.execute("clip.link", json!({"clips": [a1.0]})).unwrap();
+ assert_eq!(clip(&s, a1).unwrap().link, None);
+ assert_eq!(clip(&s, v1).unwrap().link, None, "the partner left alone is unlinked too");
+ s.execute("clip.link", json!({"clips": [v0.0, a0.0]})).unwrap();
+ s.execute("clip.link", json!({"clips": [v0.0, a0.0]})).unwrap();
+ assert!(clip(&s, v0).unwrap().link.is_some(), "v0 and a0 linked again");
+ // v0 + a0 linked, v1 not: Unlink
+ let r = s.execute("clip.link", json!({"clips": [v0.0, a0.0, v1.0]})).unwrap();
+ assert_eq!(r["linked"], false);
+ for c in [v0, a0, v1] {
+ assert_eq!(clip(&s, c).unwrap().link, None, "{c:?}");
+ }
+}