Skip to content

The truly free, open-source, local-first, cross-platform CapCut replacement. Scriptable through a JSON-RPC API & MCP.

feat(timeline): ripple delete, ⇧⌫ closes the gap a deletion leaves

Jareer4695733e3a47
diff --git a/src/crates/concat-host/tests/export.rs b/src/crates/concat-host/tests/export.rs
index 0fe5c901..2c2977c7 100644
--- a/src/crates/concat-host/tests/export.rs
+++ b/src/crates/concat-host/tests/export.rs
@@ -685,6 +685,7 @@ fn every_edit_still_exports() {
exported.expect_tone(2.75);
studio.apply(Command::RemoveClips {
+ ripple: false,
clip_ids: vec![peek_clip.clone()],
});
let exported = studio.export("head removed");
diff --git a/src/crates/concat-perf/src/main.rs b/src/crates/concat-perf/src/main.rs
index 02040d85..b864d128 100644
--- a/src/crates/concat-perf/src/main.rs
+++ b/src/crates/concat-perf/src/main.rs
@@ -66,6 +66,7 @@ fn main() {
let mut results = vec![
plan_200_clips(),
undo_200_edits(),
+ ripple_delete_of_200(),
document_round_trip(),
publish_window_of_200(),
waveform_of_an_hour(),
@@ -143,6 +144,78 @@ fn timeline_of(clips: usize, tracks: usize) -> Timeline {
timeline
}
+/// Ripple-deleting every other clip of a four-hundred-clip cut, on four
+/// lanes: the gap-closing walks every survivor against every removed span
+/// on its lane, and a rough cut does this after every single deletion.
+/// https://github.com/jub0t/Concat/issues/106
+fn ripple_delete_of_200() -> Measure {
+ let mut editor = Editor::new();
+ let media_id = editor
+ .apply(Command::AddMedia {
+ item: concat_project::commands::NewMedia {
+ path: "/perf/ripple.mp4".to_owned(),
+ name: "ripple.mp4".to_owned(),
+ duration: Some(1.0),
+ kind: concat_project::model::MediaKind::Video,
+ width: Some(1920),
+ height: Some(1080),
+ frame_rate: Some(30.0),
+ frame_rate_fraction: Some("30/1".to_owned()),
+ video_codec: Some("h264".to_owned()),
+ audio_codec: None,
+ has_audio: false,
+ audio_tracks: Vec::new(),
+ },
+ })
+ .expect("adds media")
+ .created_id
+ .expect("a media id");
+ let tracks: Vec<String> = editor
+ .project()
+ .active()
+ .tracks
+ .iter()
+ .map(|track| track.id.clone())
+ .collect();
+ let mut ids = Vec::with_capacity(400);
+ for index in 0..400usize {
+ let id = editor
+ .apply(Command::AddClip {
+ media_id: media_id.clone(),
+ track_id: tracks[index % tracks.len()].clone(),
+ start: (index / tracks.len()) as f64,
+ ripple: false,
+ })
+ .expect("adds a clip")
+ .created_id
+ .expect("a clip id");
+ ids.push(id);
+ }
+ let doomed: Vec<String> = ids.iter().step_by(2).cloned().collect();
+ let started = Instant::now();
+ editor
+ .apply(Command::RemoveClips {
+ clip_ids: doomed,
+ ripple: true,
+ })
+ .expect("removes");
+ let millis = started.elapsed().as_secs_f64() * 1e3;
+ let last = editor
+ .project()
+ .active()
+ .clips
+ .iter()
+ .map(|clip| clip.start + clip.duration)
+ .fold(0.0_f64, f64::max);
+ Measure {
+ name: "ripple delete of 200 clips out of 400",
+ value: millis,
+ unit: "ms",
+ budget: Budget::AtMost(20.0),
+ note: format!("the cut ends at {last}s, was 100s"),
+ }
+}
+
/// Planning one frame of a two-hundred-clip cut: microseconds, and the
/// thing every scrub and every export frame starts with.
fn plan_200_clips() -> Measure {
diff --git a/src/crates/concat-project/src/commands/clips.rs b/src/crates/concat-project/src/commands/clips.rs
index a5602a79..2db31e28 100644
--- a/src/crates/concat-project/src/commands/clips.rs
+++ b/src/crates/concat-project/src/commands/clips.rs
@@ -472,14 +472,31 @@ pub(super) fn apply(
})
}
- Command::RemoveClips { clip_ids } => {
+ Command::RemoveClips { clip_ids, ripple } => {
let timeline = project.active_mut();
let doomed: HashSet<&str> = clip_ids.iter().map(String::as_str).collect();
+ // The spans going, per track, taken before they are gone: the
+ // ripple closes exactly these.
+ let removed: Vec<(String, f64, f64)> = timeline
+ .clips
+ .iter()
+ .filter(|clip| doomed.contains(clip.id.as_str()))
+ .map(|clip| {
+ (
+ clip.track_id.clone(),
+ clip.start,
+ clip.start + clip.duration,
+ )
+ })
+ .collect();
let clip_count = timeline.clips.len();
timeline
.clips
.retain(|clip| !doomed.contains(clip.id.as_str()));
let applied = timeline.clips.len() != clip_count;
+ if ripple && applied {
+ close_gaps(timeline, &removed);
+ }
Ok(Outcome {
created_id: None,
applied,
@@ -493,6 +510,41 @@ pub(super) fn apply(
/// How far apart two clips may sit and still count as touching, in seconds.
const JOIN_EPSILON: f64 = 1e-6;
+/// The ripple of a delete: pulls every clip that sits after a removed
+/// span left by the length of the removed spans before it, on that clip's
+/// own track. Spans that overlap count once - a clip behind two doomed
+/// clips that shared five seconds moves by their union, not their sum, or
+/// it would land in front of what was in front of it. A span reaching past
+/// the clip's start counts only up to it, and nothing is pulled before
+/// zero.
+/// https://github.com/jub0t/Concat/issues/106
+fn close_gaps(timeline: &mut Timeline, removed: &[(String, f64, f64)]) {
+ for clip in timeline.clips_mut() {
+ let mut spans: Vec<(f64, f64)> = removed
+ .iter()
+ .filter(|(track, start, _)| *track == clip.track_id && *start < clip.start)
+ .map(|(_, start, end)| (*start, end.min(clip.start)))
+ .collect();
+ if spans.is_empty() {
+ continue;
+ }
+ spans.sort_by(|a, b| a.0.total_cmp(&b.0));
+ let mut gap = 0.0;
+ let (mut from, mut to) = spans[0];
+ for (start, end) in spans.into_iter().skip(1) {
+ if start <= to {
+ to = to.max(end);
+ } else {
+ gap += to - from;
+ from = start;
+ to = end;
+ }
+ }
+ gap += to - from;
+ clip.start = (clip.start - gap).max(0.0);
+ }
+}
+
fn default_clip(id: String, track_id: String, media: &MediaItem, start: f64) -> Clip {
let kind = match media.kind {
MediaKind::Video => ClipKind::Video,
diff --git a/src/crates/concat-project/src/commands/mod.rs b/src/crates/concat-project/src/commands/mod.rs
index eadacb9e..6fac7d2d 100644
--- a/src/crates/concat-project/src/commands/mod.rs
+++ b/src/crates/concat-project/src/commands/mod.rs
@@ -503,6 +503,15 @@ pub enum Command {
RemoveClips {
/// The clips to delete.
clip_ids: Vec<String>,
+ /// When true the deletion leaves no gap: on each track, every clip
+ /// that starts after a removed span moves left by the length of the
+ /// removed spans before it. A track the deletion never touched
+ /// stays where it is, so a picture going from one lane does not
+ /// pull the sound on another - CapCut's magnetic track, which is
+ /// what was asked for. False leaves the hole.
+ /// https://github.com/jub0t/Concat/issues/106
+ #[serde(default)]
+ ripple: bool,
},
/// Applies a [`ClipPatch`]: only the fields present change, with the
/// clamps documented on the patch. An unknown clip is a no-op.
diff --git a/src/crates/concat-project/src/lib.rs b/src/crates/concat-project/src/lib.rs
index 77f473ca..5adfdd3b 100644
--- a/src/crates/concat-project/src/lib.rs
+++ b/src/crates/concat-project/src/lib.rs
@@ -156,6 +156,219 @@ mod tests {
(editor, media_id, clip_id)
}
+ /// Clips of `media_id`, ten seconds each, at these starts on this
+ /// track, in this order. The ids come back in the same order.
+ fn lane(editor: &mut Editor, media_id: &str, track_id: &str, starts: &[f64]) -> Vec<String> {
+ starts
+ .iter()
+ .map(|start| {
+ editor
+ .apply(Command::AddClip {
+ media_id: media_id.to_owned(),
+ track_id: track_id.to_owned(),
+ start: *start,
+ ripple: false,
+ })
+ .expect("adds")
+ .created_id
+ .expect("id")
+ })
+ .collect()
+ }
+
+ fn start_of(editor: &Editor, clip_id: &str) -> f64 {
+ editor
+ .project()
+ .active()
+ .clip(clip_id)
+ .expect("the clip is still there")
+ .start
+ }
+
+ // ── ripple delete: https://github.com/jub0t/Concat/issues/106 ──
+
+ #[test]
+ fn ripple_delete_closes_the_gap_on_its_own_track_only() {
+ let (mut editor, media_id, first) = fixture();
+ let (video, sound) = {
+ let tracks = &editor.project().active().tracks;
+ (tracks[0].id.clone(), tracks[1].id.clone())
+ };
+ let rest = lane(&mut editor, &media_id, &video, &[10.0, 20.0]);
+ let other = lane(&mut editor, &media_id, &sound, &[15.0]);
+
+ let outcome = editor
+ .apply(Command::RemoveClips {
+ clip_ids: vec![rest[0].clone()],
+ ripple: true,
+ })
+ .expect("removes");
+ assert!(outcome.applied);
+ assert!(editor.project().active().clip(&rest[0]).is_none());
+ assert_eq!(start_of(&editor, &first), 0.0, "what was in front stays");
+ assert_eq!(
+ start_of(&editor, &rest[1]),
+ 10.0,
+ "what was behind closes up"
+ );
+ assert_eq!(
+ start_of(&editor, &other[0]),
+ 15.0,
+ "another lane is none of this deletion's business"
+ );
+ }
+
+ #[test]
+ fn a_plain_delete_still_leaves_the_hole() {
+ let (mut editor, media_id, _) = fixture();
+ let video = editor.project().active().tracks[0].id.clone();
+ let rest = lane(&mut editor, &media_id, &video, &[10.0, 20.0]);
+ editor
+ .apply(Command::RemoveClips {
+ clip_ids: vec![rest[0].clone()],
+ ripple: false,
+ })
+ .expect("removes");
+ assert_eq!(start_of(&editor, &rest[1]), 20.0);
+ }
+
+ #[test]
+ fn ripple_delete_of_several_clips_moves_each_survivor_by_what_was_before_it() {
+ let (mut editor, media_id, first) = fixture();
+ let video = editor.project().active().tracks[0].id.clone();
+ let rest = lane(&mut editor, &media_id, &video, &[10.0, 20.0, 30.0]);
+ // The first and the third go: the second moves by one span, the
+ // fourth by two.
+ editor
+ .apply(Command::RemoveClips {
+ clip_ids: vec![first, rest[1].clone()],
+ ripple: true,
+ })
+ .expect("removes");
+ assert_eq!(start_of(&editor, &rest[0]), 0.0);
+ assert_eq!(start_of(&editor, &rest[2]), 10.0);
+ assert_eq!(editor.project().active().clips.len(), 2);
+ }
+
+ #[test]
+ fn ripple_delete_counts_overlapping_spans_once() {
+ let (mut editor, media_id, first) = fixture();
+ let video = editor.project().active().tracks[0].id.clone();
+ // Two doomed clips sharing five seconds - [0, 10) and [5, 15) -
+ // and a survivor at twenty. Their union is fifteen, so it lands at
+ // five; their sum is twenty, which would send it to zero. Away
+ // from zero on purpose: the floor there would hide the difference.
+ let more = lane(&mut editor, &media_id, &video, &[5.0, 20.0]);
+ editor
+ .apply(Command::RemoveClips {
+ clip_ids: vec![first, more[0].clone()],
+ ripple: true,
+ })
+ .expect("removes");
+ assert_eq!(start_of(&editor, &more[1]), 5.0);
+ }
+
+ #[test]
+ fn ripple_delete_of_a_span_reaching_past_a_survivor_pulls_only_up_to_it() {
+ let (mut editor, media_id, first) = fixture();
+ let video = editor.project().active().tracks[0].id.clone();
+ // A doomed clip at [10, 20) with a survivor stacked inside its span
+ // at fourteen: the survivor moves by the four seconds of span in
+ // front of it, to ten, not by the whole ten, which would put it at
+ // four - in front of where the doomed clip began.
+ let more = lane(&mut editor, &media_id, &video, &[10.0, 14.0]);
+ editor
+ .apply(Command::RemoveClips {
+ clip_ids: vec![more[0].clone()],
+ ripple: true,
+ })
+ .expect("removes");
+ assert_eq!(start_of(&editor, &first), 0.0);
+ assert_eq!(start_of(&editor, &more[1]), 10.0);
+ }
+
+ #[test]
+ fn ripple_delete_leaves_a_clip_stacked_at_the_same_start_alone() {
+ let (mut editor, media_id, first) = fixture();
+ let video = editor.project().active().tracks[0].id.clone();
+ let twin = lane(&mut editor, &media_id, &video, &[0.0]);
+ editor
+ .apply(Command::RemoveClips {
+ clip_ids: vec![first],
+ ripple: true,
+ })
+ .expect("removes");
+ assert_eq!(
+ start_of(&editor, &twin[0]),
+ 0.0,
+ "nothing was in front of it"
+ );
+ }
+
+ #[test]
+ fn ripple_delete_of_unknown_ids_moves_nothing_and_records_nothing() {
+ let (mut editor, media_id, _) = fixture();
+ let video = editor.project().active().tracks[0].id.clone();
+ let rest = lane(&mut editor, &media_id, &video, &[10.0]);
+ let before_can_undo = editor.can_undo();
+ let outcome = editor
+ .apply(Command::RemoveClips {
+ clip_ids: vec!["nobody".to_owned()],
+ ripple: true,
+ })
+ .expect("tolerated");
+ assert!(!outcome.applied);
+ assert_eq!(start_of(&editor, &rest[0]), 10.0);
+ assert_eq!(editor.can_undo(), before_can_undo);
+ }
+
+ #[test]
+ fn ripple_delete_is_one_undo_step_that_puts_everything_back() {
+ let (mut editor, media_id, first) = fixture();
+ let video = editor.project().active().tracks[0].id.clone();
+ let rest = lane(&mut editor, &media_id, &video, &[10.0, 20.0]);
+ editor
+ .apply(Command::RemoveClips {
+ clip_ids: vec![first.clone(), rest[0].clone()],
+ ripple: true,
+ })
+ .expect("removes");
+ assert_eq!(start_of(&editor, &rest[1]), 0.0);
+ assert!(editor.undo());
+ assert_eq!(start_of(&editor, &first), 0.0);
+ assert_eq!(start_of(&editor, &rest[0]), 10.0);
+ assert_eq!(start_of(&editor, &rest[1]), 20.0);
+ assert!(editor.redo());
+ assert_eq!(start_of(&editor, &rest[1]), 0.0);
+ }
+
+ #[test]
+ fn ripple_delete_of_everything_on_a_lane_is_just_a_delete() {
+ let (mut editor, media_id, first) = fixture();
+ let video = editor.project().active().tracks[0].id.clone();
+ let rest = lane(&mut editor, &media_id, &video, &[10.0]);
+ editor
+ .apply(Command::RemoveClips {
+ clip_ids: vec![first, rest[0].clone()],
+ ripple: true,
+ })
+ .expect("removes");
+ assert!(editor.project().active().clips.is_empty());
+ }
+
+ #[test]
+ fn a_remove_clips_document_without_the_ripple_field_still_parses_as_a_plain_delete() {
+ let command: Command = serde_json::from_value(json!({
+ "op": "removeClips",
+ "clipIds": ["c1"]
+ }))
+ .expect("the field is new; old callers do not send it");
+ match command {
+ Command::RemoveClips { ripple, .. } => assert!(!ripple),
+ _ => panic!("not a remove"),
+ }
+ }
+
#[test]
fn a_new_project_has_one_timeline_and_four_lanes() {
let editor = Editor::new();
@@ -460,7 +673,10 @@ mod tests {
.collect();
assert_eq!(sound.len(), 1);
editor
- .apply(Command::RemoveClips { clip_ids: sound })
+ .apply(Command::RemoveClips {
+ clip_ids: sound,
+ ripple: false,
+ })
.expect("removes");
assert_eq!(
editor
@@ -2676,6 +2892,7 @@ mod tests {
},
Command::RemoveClips {
clip_ids: vec!["c1".to_owned()],
+ ripple: false,
},
Command::UpdateClip {
clip_id: "c1".to_owned(),
diff --git a/src/crates/concat/locales/en.json b/src/crates/concat/locales/en.json
index e244e6bd..8614d583 100644
--- a/src/crates/concat/locales/en.json
+++ b/src/crates/concat/locales/en.json
@@ -520,6 +520,8 @@
"RGB split": "RGB split",
"Richer colour where it is muted, without blowing out what is already strong.": "Richer colour where it is muted, without blowing out what is already strong.",
"Right": "Right",
+ "Ripple delete": "Ripple delete",
+ "Ripple delete {0} clips": "Ripple delete {0} clips",
"Robot": "Robot",
"Room": "Room",
"Rotation": "Rotation",
diff --git a/src/crates/concat/src/lib.rs b/src/crates/concat/src/lib.rs
index 3de60ddb..4adc9c25 100644
--- a/src/crates/concat/src/lib.rs
+++ b/src/crates/concat/src/lib.rs
@@ -1184,6 +1184,7 @@ pub fn run() -> Result<(), slint::PlatformError> {
state.seek(end);
}
"delete" => state.delete_selected(),
+ "ripple-delete" => state.ripple_delete_selected(),
"split" => {
let at = state.playhead;
state.split_at(at, false);
diff --git a/src/crates/concat/src/studio.rs b/src/crates/concat/src/studio.rs
index 6c890575..f84082ef 100644
--- a/src/crates/concat/src/studio.rs
+++ b/src/crates/concat/src/studio.rs
@@ -4227,7 +4227,19 @@ impl Studio {
}
}
+ /// Delete: the selection goes and the hole stays.
pub fn delete_selected(&mut self) {
+ self.remove_selected(false);
+ }
+
+ /// Ripple delete (⇧⌫): the selection goes and each lane closes behind
+ /// it, so a rough cut needs no dragging-left after every deletion.
+ /// https://github.com/jub0t/Concat/issues/106
+ pub fn ripple_delete_selected(&mut self) {
+ self.remove_selected(true);
+ }
+
+ fn remove_selected(&mut self, ripple: bool) {
let doomed: Vec<String> = self
.selection
.iter()
@@ -4238,7 +4250,10 @@ impl Studio {
.cloned()
.collect();
if !doomed.is_empty() {
- self.apply(Command::RemoveClips { clip_ids: doomed });
+ self.apply(Command::RemoveClips {
+ clip_ids: doomed,
+ ripple,
+ });
}
self.selection.clear();
}
@@ -5819,6 +5834,17 @@ impl Studio {
checkable: false,
checked: false,
});
+ rows.push(MenuItemData {
+ id: "ripple-delete".into(),
+ label: t("Ripple delete").into(),
+ kind: MenuRow::Action,
+ glyph: Glyph::Trash,
+ shortcut: "⇧⌫".into(),
+ enabled: !locked,
+ danger: true,
+ checkable: false,
+ checked: false,
+ });
rows
}
@@ -5941,6 +5967,22 @@ impl Studio {
checkable: false,
checked: false,
},
+ MenuItemData {
+ id: "ripple-delete".into(),
+ label: if selected > 1 {
+ tf("Ripple delete {0} clips", &[&selected])
+ } else {
+ t("Ripple delete")
+ }
+ .into(),
+ kind: MenuRow::Action,
+ glyph: Glyph::Trash,
+ shortcut: "⇧⌫".into(),
+ enabled: selected > 0,
+ danger: true,
+ checkable: false,
+ checked: false,
+ },
rule(),
MenuItemData {
id: "snap".into(),
@@ -6137,6 +6179,8 @@ impl Studio {
self.split_at(at, true);
}
"select-all" => self.select_all(),
+ // ⇧⌫, from the key table; plain ⌫ comes in as its own callback.
+ "ripple-delete" => self.ripple_delete_selected(),
"copy" | "duplicate" | "mute" => {
if let Some(id) = self.sole_selection() {
self.clip_action(&id, action);
@@ -6223,12 +6267,14 @@ impl Studio {
"lock" => self.toggle_lock(&clip.track_id),
// A clip that is part of the selection takes the selection with
// it: Delete on one of five selected clips means the five.
- "delete" => {
+ "delete" | "ripple-delete" => {
+ let ripple = action == "ripple-delete";
if self.selection.len() > 1 && self.selection.iter().any(|held| held == id) {
- self.delete_selected();
+ self.remove_selected(ripple);
} else {
self.apply(Command::RemoveClips {
clip_ids: vec![id.to_owned()],
+ ripple,
});
}
self.menu_target = None;
diff --git a/src/crates/concat/ui/app.slint b/src/crates/concat/ui/app.slint
index 0f65d735..8d972feb 100644
--- a/src/crates/concat/ui/app.slint
+++ b/src/crates/concat/ui/app.slint
@@ -355,8 +355,15 @@ export component App inherits Window {
return reject;
}
if (event.text == Key.Space) { Editor.play-toggled(); return accept; }
+ // Shift closes the gap the deletion leaves - the ripple delete
+ // of every other editor, on the key they all use for it.
+ // https://github.com/jub0t/Concat/issues/106
if (event.text == Key.Delete || event.text == Key.Backspace) {
- Editor.delete-selected();
+ if (event.modifiers.shift) {
+ Editor.shortcut("ripple-delete");
+ } else {
+ Editor.delete-selected();
+ }
return accept;
}
if (event.text == Key.Home) { Editor.shortcut("start"); return accept; }