Skip to content

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

fix(engine): Close Gap with no gap selected closes the sequence's gaps (#1986)

Sam Sandweiss1a946e764b56
diff --git a/crates/engine/src/commands.rs b/crates/engine/src/commands.rs
index 58d943b83..920defd85 100644
--- a/crates/engine/src/commands.rs
+++ b/crates/engine/src/commands.rs
@@ -2136,12 +2136,14 @@ fn build() -> Vec<CommandSpec> {
remove_transitions(s, &ids, "Clear Transition")
}),
cmd!("sequence.closeGap", "Close Gap", ["Sequence"], None, r#"{"track":"V1"|id?,"time":ticks?}"#, has_seq, |s, p| {
- // Shortcuts and menus have no parameters. Resolve that form from the selected gap
- // (#1135); explicit requests keep their existing track/time validation.
+ // Shortcuts and menus have no parameters. That form closes the selected gap (#1135),
+ // or with none selected every gap between In and Out, as Premiere's Sequence ▸ Close
+ // Gap does (#1986); explicit requests keep their existing track/time validation.
if p.as_object().is_some_and(|p| p.is_empty()) {
- let gap = s.state.gap_selection.ok_or_else(|| bad("sequence.closeGap", "select a gap or provide `track`"))?;
- ripple_delete_gap(s, gap)?;
- return Ok(Value::Null);
+ return match s.state.gap_selection {
+ Some(gap) => ripple_delete_gap(s, gap).map(|_| Value::Null),
+ None => close_sequence_gaps(s),
+ };
}
let tr = track_p(s, p, "track", "sequence.closeGap")?.ok_or_else(|| bad("sequence.closeGap", "need `track`"))?;
let t = time_p(s, p, "").unwrap_or(s.playhead());
@@ -4037,6 +4039,28 @@ fn ripple_delete_gap(s: &mut Session, g: crate::GapSelection) -> Result<Value> {
Ok(json!({"closed": g.range.duration.0}))
}
+/// `sequence.closeGap` with no gap selected: every span where no track has anything, between In
+/// and Out (the whole sequence without them), closes in one undo step (#1986).
+fn close_sequence_gaps(s: &mut Session) -> Result<Value> {
+ let within = in_out_range(s)?;
+ let gaps = s.active_sequence().map(|q| crate::sequence_extras::sequence_gaps(q, Some(within))).unwrap_or_default();
+ let Some(first) = gaps.first().copied() else {
+ return Err(bad("sequence.closeGap", "no gaps to close"));
+ };
+ s.edit_sequence("Close Gap", |q, _, st| {
+ // a locked track would stay put while everything else moved past it
+ if q.all_tracks().any(|t| t.locked && t.items.iter().any(|i| i.start >= first.end())) {
+ return Err(edit::EditError::Locked.into());
+ }
+ for g in gaps.iter().rev() {
+ crate::sequence_extras::close_sequence_gap(q, *g, st.ripple_sequence_markers);
+ }
+ Ok(())
+ })?;
+ let closed = gaps.iter().fold(Tick::ZERO, |sum, g| sum + g.duration);
+ Ok(json!({"gaps": gaps.len(), "closed": closed.0}))
+}
+
/// `sequence.closeGap`: ripple the gap on `track` at `t` closed (one undo step, "Ripple Delete").
fn close_gap(s: &mut Session, tr: TrackId, t: Tick) -> Result<()> {
s.edit_sequence("Ripple Delete", |q, _, st| {
diff --git a/crates/engine/src/gap_selection_tests.rs b/crates/engine/src/gap_selection_tests.rs
index 19abd7c4c..74d62d329 100644
--- a/crates/engine/src/gap_selection_tests.rs
+++ b/crates/engine/src/gap_selection_tests.rs
@@ -211,7 +211,6 @@ fn close_gap_rejects_invalid_context_and_explicit_parameters_without_editing() {
two_clips(&mut s, [0, 96]);
let before = s.project.clone();
let undo = s.history.undo.len();
- assert!(s.execute("sequence.closeGap", json!({})).is_err(), "a shortcut needs a selected gap");
s.execute("timeline.selectGap", json!({"track":"V1","frame":60})).unwrap();
for params in [json!({"track":null}), json!({"track":false}), json!({"track":"V99","frame":60}), json!({"time":1})] {
assert!(s.execute("sequence.closeGap", params).is_err(), "invalid explicit requests must not fall back to the selected gap");
@@ -232,3 +231,76 @@ fn close_gap_rejects_invalid_context_and_explicit_parameters_without_editing() {
assert!(std::sync::Arc::ptr_eq(&before, &s.project));
assert_eq!(s.history.undo.len(), undo);
}
+
+/// 48-frame clips (pictures on V1, linked sounds on A1) placed at `frames`, in that order.
+fn clips_at(s: &mut Session, frames: &[i64]) -> Vec<(ClipId, ClipId)> {
+ s.execute("file.openDemoProject", json!({})).unwrap();
+ s.execute("file.newSequence", json!({"name": "Gaps", "video": 1, "audio": 1})).unwrap();
+ let rate = s.sequence_rate();
+ let item = s.project.items.values().find(|i| i.has_video() && i.has_audio() && i.as_media().is_some()).map(|i| i.id).unwrap();
+ for f in frames {
+ s.execute("timeline.place", json!({"item": item.0, "frame": f, "sourceIn": rate.tick_of(48).0, "duration": rate.tick_of(48).0})).unwrap();
+ }
+ s.execute("edit.deselectAll", json!({})).unwrap();
+ let q = s.active_sequence().unwrap();
+ (0..frames.len()).map(|n| (q.video_tracks[0].items[n].id, q.audio_tracks[0].items[n].id)).collect()
+}
+
+/// #1986: Sequence ▸ Close Gap with no gap selected closes every gap in the sequence, on picture and
+/// sound alike, in one undo step (Premiere's Close Gap).
+#[test]
+fn close_gap_without_a_selected_gap_closes_every_gap_in_one_undo_step() {
+ let mut s = Session::default();
+ let clips = clips_at(&mut s, &[24, 96, 200]);
+ let rate = s.sequence_rate();
+ let at = |f: i64| rate.tick_of(f);
+ let (zero, f48, f96) = (Tick::ZERO, at(48), at(96));
+ let before = serde_json::to_value(&s.project).unwrap();
+ let undo = s.history.undo.len();
+ let r = s.execute("sequence.closeGap", json!({})).unwrap();
+ assert_eq!(r["gaps"], json!(3), "the leading gap and the two between the clips: {r}");
+ for ((picture, sound), start) in clips.iter().zip([zero, f48, f96]) {
+ assert_eq!(range(&s, *picture).0, start);
+ assert_eq!(range(&s, *sound).0, start, "sound stays with its picture");
+ }
+ assert_eq!(s.history.undo.len(), undo + 1, "one undo step");
+ s.undo();
+ assert_eq!(serde_json::to_value(&s.project).unwrap(), before);
+}
+
+/// With In and Out set, only the gaps that lie between them close.
+#[test]
+fn close_gap_without_a_selected_gap_only_closes_gaps_between_in_and_out() {
+ let mut s = Session::default();
+ let clips = clips_at(&mut s, &[0, 96, 200]);
+ let rate = s.sequence_rate();
+ let at = |f: i64| rate.tick_of(f);
+ s.execute("markers.markIn", json!({"time": at(140).0})).unwrap();
+ s.execute("markers.markOut", json!({"time": at(260).0})).unwrap();
+ let r = s.execute("sequence.closeGap", json!({})).unwrap();
+ assert_eq!(r["gaps"], json!(1), "{r}");
+ assert_eq!(range(&s, clips[0].0).0, Tick::ZERO);
+ assert_eq!(range(&s, clips[1].0).0, at(96), "the gap before In stays");
+ assert_eq!(range(&s, clips[2].0).0, at(144), "the gap between In and Out closed");
+}
+
+/// Nothing to close, or a locked track that would be left behind: an error and no edit.
+#[test]
+fn close_gap_without_a_selected_gap_refuses_without_editing() {
+ let mut s = Session::default();
+ clips_at(&mut s, &[0, 48]);
+ let before = s.project.clone();
+ let undo = s.history.undo.len();
+ assert!(s.execute("sequence.closeGap", json!({})).is_err(), "no gaps");
+ assert!(std::sync::Arc::ptr_eq(&before, &s.project));
+ assert_eq!(s.history.undo.len(), undo);
+
+ let mut s = Session::default();
+ clips_at(&mut s, &[0, 96]);
+ s.execute("timeline.setTrack", json!({"track":"A1","locked":true})).unwrap();
+ let before = s.project.clone();
+ let undo = s.history.undo.len();
+ assert!(s.execute("sequence.closeGap", json!({})).is_err(), "A1 is locked and has a clip after the gap");
+ assert!(std::sync::Arc::ptr_eq(&before, &s.project));
+ assert_eq!(s.history.undo.len(), undo);
+}
diff --git a/crates/engine/src/sequence_extras.rs b/crates/engine/src/sequence_extras.rs
index 093ce0f29..54449fa0c 100644
--- a/crates/engine/src/sequence_extras.rs
+++ b/crates/engine/src/sequence_extras.rs
@@ -413,27 +413,8 @@ pub fn simplify_sequence(proj: &filmcraft_project::Project, q: &mut Sequence, o:
// close gaps: spans where no track has anything
let mut closed = Tick::ZERO;
if o.close_gaps {
- let mut spans: Vec<TimeRange> = q.all_tracks().flat_map(|t| t.items.iter().map(|i| i.range())).collect();
- spans.extend(q.caption_tracks.iter().flat_map(|t| t.captions.iter().map(|c| c.range())));
- spans.sort_by_key(|r| r.start);
- let mut gaps: Vec<TimeRange> = Vec::new();
- let mut cursor = Tick::ZERO;
- for r in &spans {
- if r.start > cursor {
- gaps.push(TimeRange::from_bounds(cursor, r.start));
- }
- cursor = cursor.max(r.end());
- }
- for g in gaps.iter().rev() {
- for t in q.video_tracks.iter_mut().chain(q.audio_tracks.iter_mut()) {
- edit::shift_track_from(t, g.end(), Tick::ZERO - g.duration);
- }
- for t in &mut q.caption_tracks {
- for c in t.captions.iter_mut().filter(|c| c.start >= g.end()) {
- c.start -= g.duration;
- }
- }
- crate::sequence_tools::ripple_markers(&mut q.markers, g.end(), Tick::ZERO - g.duration);
+ for g in sequence_gaps(q, None).iter().rev() {
+ close_sequence_gap(q, *g, true);
closed += g.duration;
}
}
@@ -456,6 +437,43 @@ pub fn simplify_sequence(proj: &filmcraft_project::Project, q: &mut Sequence, o:
json!({"removedClips": removed_clips, "removedEffects": removed_effects, "movedClips": moved, "closedGaps": closed.0, "removedTracks": removed_tracks})
}
+/// Spans where no track has anything (clips or captions), in time order: the gaps before, between
+/// and inside the material, never the open time after the last clip. With `within`, only the gaps
+/// that lie entirely inside it (Close Gap between In and Out).
+pub(crate) fn sequence_gaps(q: &Sequence, within: Option<TimeRange>) -> Vec<TimeRange> {
+ let mut spans: Vec<TimeRange> = q.all_tracks().flat_map(|t| t.items.iter().map(|i| i.range())).collect();
+ spans.extend(q.caption_tracks.iter().flat_map(|t| t.captions.iter().map(|c| c.range())));
+ spans.sort_by_key(|r| r.start);
+ let mut gaps: Vec<TimeRange> = Vec::new();
+ let mut cursor = Tick::ZERO;
+ for r in &spans {
+ if r.start > cursor {
+ gaps.push(TimeRange::from_bounds(cursor, r.start));
+ }
+ cursor = cursor.max(r.end());
+ }
+ if let Some(w) = within {
+ gaps.retain(|g| g.start >= w.start && g.end() <= w.end());
+ }
+ gaps
+}
+
+/// Ripple one of [`sequence_gaps`] closed: everything after it, on every track, moves left by its
+/// length (it is empty on all of them, so nothing can collide). Close later gaps first.
+pub(crate) fn close_sequence_gap(q: &mut Sequence, g: TimeRange, markers: bool) {
+ for t in q.video_tracks.iter_mut().chain(q.audio_tracks.iter_mut()) {
+ edit::shift_track_from(t, g.end(), Tick::ZERO - g.duration);
+ }
+ for t in &mut q.caption_tracks {
+ for c in t.captions.iter_mut().filter(|c| c.start >= g.end()) {
+ c.start -= g.duration;
+ }
+ }
+ if markers {
+ crate::sequence_tools::ripple_markers(&mut q.markers, g.end(), Tick::ZERO - g.duration);
+ }
+}
+
fn simplify(s: &mut Session, p: &Value) -> Result<Value> {
let o = SimplifyOptions::from_params(p)?;
let seq_id = s.state.active_sequence.ok_or(EngineError::NoSequence)?;