forge
Explore
Log in

Don't you dare go Hollow

Close every gap on the track at once

Browse files at this commit

e78c741653d7fb15582cde30fd4696651d38860eAhmet Kaan GümüşParent: 8ce6d6bc1459ca
  • feat
    • close every gap on the track at once
Show full message
✨ feat(timeline): close every gap on the track at once

A second row in the gap menu, named for the lane it will touch, so nobody has
to guess whether it reaches the others. Gaps close right to left, since each
close moves the ones after it, and the whole sweep is one undo press.

A gap a linked take cannot close without losing sync is skipped rather than
forced, and the notice says how many were left and where.
crates/app/src/player/timeline_edit.rsmodified+53 -0
@@ -1100,6 +1100,59 @@ impl Player {
1100 cx.notify();1100 cx.notify();
1101 }1101 }
1102 1102
1103+ /// Closes every bounded gap on `lane`, as one gesture. The scope stays the
1104+ /// clicked track's scope -- not the whole timeline -- and each linked take is
1105+ /// widened only when its other half has the same empty stretch. Unsafe gaps
1106+ /// are left open and counted in the notice, so the sweep never fails as a
1107+ /// silent no-op.
1108+ pub(crate) fn close_all_gaps_on_lane(&mut self, lane: Lane, cx: &mut Context<Self>) {
1109+ let Some(session) = self.session.as_mut() else {
1110+ return;
1111+ };
1112+ let report = match session.close_all_gaps_on_lane(lane) {
1113+ Ok(report) => report,
1114+ Err(e) => {
1115+ self.notify_user(e.to_string().into());
1116+ cx.notify();
1117+ return;
1118+ }
1119+ };
1120+ if report.closed > 0 {
1121+ self.mark_dirty();
1122+ self.selected.clear();
1123+ self.reset_after_reseek();
1124+ }
1125+ let skipped = report.skipped.len();
1126+ let notice = match (report.closed, skipped) {
1127+ (0, 0) => format!("NO GAPS TO CLOSE on {}", lane.label()),
1128+ (0, 1) => format!(
1129+ "NO GAPS CLOSED on {} — 1 skipped at {}; match linked gaps or detach",
1130+ lane.label(),
1131+ report.skipped[0].start
1132+ ),
1133+ (0, n) => format!(
1134+ "NO GAPS CLOSED on {} — {n} skipped; match linked gaps or detach",
1135+ lane.label()
1136+ ),
1137+ (n, 0) => format!(
1138+ "{n} GAPS CLOSED on {} — {} takes them back",
1139+ lane.label(),
1140+ self.keymap.display(ActionId::Undo)
1141+ ),
1142+ (n, 1) => format!(
1143+ "{n} GAPS CLOSED on {}; 1 skipped at {} — match linked gaps or detach",
1144+ lane.label(),
1145+ report.skipped[0].start
1146+ ),
1147+ (n, skipped) => format!(
1148+ "{n} GAPS CLOSED on {}; {skipped} skipped — match linked gaps or detach",
1149+ lane.label()
1150+ ),
1151+ };
1152+ self.notify_user(notice.into());
1153+ cx.notify();
1154+ }
1155+
1103 /// A press on an edge: the start of the drag that changes how much of a1156 /// A press on an edge: the start of the drag that changes how much of a
1104 /// source it plays. It picks the placement as a press anywhere else on the1157 /// source it plays. It picks the placement as a press anywhere else on the
1105 /// box does -- the edge strip covers the box's own listener (`occlude`), so1158 /// box does -- the edge strip covers the box's own listener (`occlude`), so
crates/app/src/ui/overlays.rsmodified+20 -4
@@ -567,21 +567,37 @@ impl Player {
567 );567 );
568 }568 }
569 } else if let MenuOn::Gap(start, frames) = menu.on {569 } else if let MenuOn::Gap(start, frames) = menu.on {
570- // The one item a gap offers: no properties, no clip actions, just570+ // Gap rows are ripples, not clip actions. The first is the exact
571- // the ripple that closes it -- Premiere's Close Gap, Resolve's571+ // hole under the pointer; the second appears only when the same
572- // Delete Gap, per-lane the way `Player::close_gap` documents.572+ // track has another bounded hole too, and says the track in the row
573+ // so "all" cannot read as the whole timeline.
574+ let lane = menu.lane;
573 rows.push(575 rows.push(
574 row(rows.len())576 row(rows.len())
575 .cursor_pointer()577 .cursor_pointer()
576 .hover(|s| s.bg(rgb(BG_HOVER())))578 .hover(|s| s.bg(rgb(BG_HOVER())))
577 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {579 .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
578 this.context_menu = None;580 this.context_menu = None;
579- this.close_gap(menu.lane, start, frames, cx);581+ this.close_gap(lane, start, frames, cx);
580 }))582 }))
581 .child("Close gap")583 .child("Close gap")
582 .child(chord_style(div().text_color(rgb(FG_SECONDARY()))).child("ripples left"))584 .child(chord_style(div().text_color(rgb(FG_SECONDARY()))).child("ripples left"))
583 .into_any_element(),585 .into_any_element(),
584 );586 );
587+ if session.gap_count(lane) > 1 {
588+ rows.push(
589+ row(rows.len())
590+ .cursor_pointer()
591+ .hover(|s| s.bg(rgb(BG_HOVER())))
592+ .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
593+ this.context_menu = None;
594+ this.close_all_gaps_on_lane(lane, cx);
595+ }))
596+ .child(format!("Close all {} gaps", lane.label()))
597+ .child(chord_style(div().text_color(rgb(FG_SECONDARY()))).child("this track"))
598+ .into_any_element(),
599+ );
600+ }
585 } else {601 } else {
586 // A grade on a waveform, an equalizer on a picture, a silence scan602 // A grade on a waveform, an equalizer on a picture, a silence scan
587 // on a still: things that do not exist for what was right-clicked,603 // on a still: things that do not exist for what was right-clicked,
crates/engine/src/playback.rsmodified+21 -1
@@ -26,7 +26,9 @@ use crate::colorspace::{ColorDescription, ContentLight};
26 use crate::decode::{Backend, BackendCell, DecodeSession, Frame, Worker};26 use crate::decode::{Backend, BackendCell, DecodeSession, Frame, Worker};
27 use crate::demux::{Codec, Demuxer, VideoMeta};27 use crate::demux::{Codec, Demuxer, VideoMeta};
28 use crate::eq::EqParams;28 use crate::eq::EqParams;
29-use crate::project::{Clip, Edge, Lane, LaneKind, Project, Rate, Source, Span, Speed, SubClip};29+use crate::project::{
30+ Clip, Edge, GapSweep, Lane, LaneKind, Project, Rate, Source, Span, Speed, SubClip,
31+};
30 use crate::scale::{Composer, FitPolicy};32 use crate::scale::{Composer, FitPolicy};
31 use crate::transform::TransformParams;33 use crate::transform::TransformParams;
32 34
@@ -2035,6 +2037,12 @@ impl PlaybackSession {
2035 self.project.gap_at(lane, frame)2037 self.project.gap_at(lane, frame)
2036 }2038 }
2037 2039
2040+ /// How many bounded gaps that same right-click lane holds. The tail after
2041+ /// the last clip is not counted, because nothing would ripple there.
2042+ pub fn gap_count(&self, lane: Lane) -> usize {
2043+ self.project.gap_count(lane)
2044+ }
2045+
2038 /// Every lane's handle, in display order -- what a front-end lays out top to2046 /// Every lane's handle, in display order -- what a front-end lays out top to
2039 /// bottom, and the list [`add_lane`](Self::add_lane) grows.2047 /// bottom, and the list [`add_lane`](Self::add_lane) grows.
2040 pub fn lanes(&self) -> Vec<Lane> {2048 pub fn lanes(&self) -> Vec<Lane> {
@@ -2458,6 +2466,18 @@ impl PlaybackSession {
2458 self.project.gap_take_scope(lane, start, frames)2466 self.project.gap_take_scope(lane, start, frames)
2459 }2467 }
2460 2468
2469+ /// Closes every bounded gap on one lane, keeping each linked take whole when
2470+ /// its other half has the matching empty stretch and reporting the gaps that
2471+ /// were not safe to close. One undo step for every gap that closed.
2472+ pub fn close_all_gaps_on_lane(&mut self, lane: Lane) -> crate::Result<GapSweep> {
2473+ let report = self.project.close_all_gaps_on_lane(lane)?;
2474+ if report.closed > 0 {
2475+ let now = self.now();
2476+ self.seek(now);
2477+ }
2478+ Ok(report)
2479+ }
2480+
2461 /// Cuts every one of `regions` -- `(start, len)` in timeline frames -- out2481 /// Cuts every one of `regions` -- `(start, len)` in timeline frames -- out
2462 /// of the lanes in `scope` and closes each hole, as **one** edit2482 /// of the lanes in `scope` and closes each hole, as **one** edit
2463 /// ([`Project::cut_regions`]): the jumpcut a silence scan2483 /// ([`Project::cut_regions`]): the jumpcut a silence scan
crates/engine/src/project.rsmodified+183 -0
@@ -872,6 +872,22 @@ pub type Parts = (
872 /// that grows for as long as the session lasts.872 /// that grows for as long as the session lasts.
873 const HISTORY_CAP: usize = 100;873 const HISTORY_CAP: usize = 100;
874 874
875+/// One gap a close-all sweep left open, with the frame it still starts on and
876+/// the same refusal a single [`Project::gap_take_scope`] close would have said.
877+#[derive(Clone, Debug, PartialEq, Eq)]
878+pub struct GapSkip {
879+ pub start: u32,
880+ pub reason: String,
881+}
882+
883+/// What [`Project::close_all_gaps_on_lane`] did: closed gaps count as edits,
884+/// skipped gaps are the take-safety refusals the sweep did not hide.
885+#[derive(Clone, Debug, Default, PartialEq, Eq)]
886+pub struct GapSweep {
887+ pub closed: usize,
888+ pub skipped: Vec<GapSkip>,
889+}
890+
875 /// The edit list plus its undo history.891 /// The edit list plus its undo history.
876 #[derive(Clone, Debug)]892 #[derive(Clone, Debug)]
877 pub struct Project {893 pub struct Project {
@@ -4189,6 +4205,13 @@ impl Project {
4189 gap(self.lane(lane), frame)4205 gap(self.lane(lane), frame)
4190 }4206 }
4191 4207
4208+ /// How many bounded holes `lane` holds. The open end after the last clip is
4209+ /// not counted, like [`gap_at`](Self::gap_at), because closing it would move
4210+ /// nothing.
4211+ pub fn gap_count(&self, lane: Lane) -> usize {
4212+ gaps(self.lane(lane)).len()
4213+ }
4214+
4192 /// The lanes closing the gap `(start, frames)` on `lane` must ripple4215 /// The lanes closing the gap `(start, frames)` on `lane` must ripple
4193 /// together to keep sync -- what a right-click on empty bench space asks4216 /// together to keep sync -- what a right-click on empty bench space asks
4194 /// before the menu offers to close it, and what widens4217 /// before the menu offers to close it, and what widens
@@ -4257,6 +4280,50 @@ impl Project {
4257 Ok(scope)4280 Ok(scope)
4258 }4281 }
4259 4282
4283+ /// Close every bounded gap on `lane`, as one undo step, without making the
4284+ /// lane's scope global. Each gap uses the same take-safety widening
4285+ /// [`gap_take_scope`](Self::gap_take_scope) gives a single close: linked
4286+ /// halves ride along only when their matching lane is empty over that exact
4287+ /// stretch; mismatches are skipped and reported.
4288+ ///
4289+ /// The walk is back-to-front. Closing a gap shifts everything after it, so
4290+ /// later holes must be consumed while their measured starts are still true;
4291+ /// earlier starts are unchanged by cuts to their right. A snapshot is taken
4292+ /// only before the first successful close, so an all-skipped sweep is not an
4293+ /// undo step.
4294+ pub fn close_all_gaps_on_lane(&mut self, lane: Lane) -> crate::Result<GapSweep> {
4295+ let Some(_) = self.index(lane) else {
4296+ return Err("no track to work on".into());
4297+ };
4298+ let mut report = GapSweep::default();
4299+ let mut snapshot = false;
4300+ for (start, frames) in gaps(self.lane(lane)).into_iter().rev() {
4301+ match self
4302+ .gap_take_scope(lane, start, frames)
4303+ .and_then(|scope| {
4304+ let on = self.scoped(&scope)?;
4305+ self.scope_holds_takes_whole(&on, start)?;
4306+ Ok(on)
4307+ }) {
4308+ Ok(on) => {
4309+ if !snapshot {
4310+ self.snapshot();
4311+ snapshot = true;
4312+ }
4313+ ripple(&mut self.lanes, &on, start, frames);
4314+ report.closed += 1;
4315+ }
4316+ Err(e) => report.skipped.push(GapSkip {
4317+ start,
4318+ reason: e.to_string(),
4319+ }),
4320+ }
4321+ }
4322+ report.skipped.reverse();
4323+ debug_assert!(links_are_consistent(&self.lanes).is_ok());
4324+ Ok(report)
4325+ }
4326+
4260 /// Cut **every** one of `regions` -- `(start, len)` in timeline frames --4327 /// Cut **every** one of `regions` -- `(start, len)` in timeline frames --
4261 /// out of the lanes in `scope` and close each hole, as one edit: the4328 /// out of the lanes in `scope` and close each hole, as one edit: the
4262 /// jumpcut a silence scan asks for ([`crate::silence`]).4329 /// jumpcut a silence scan asks for ([`crate::silence`]).
@@ -4991,6 +5058,20 @@ fn gap(clips: &[Clip], frame: u32) -> Option<(u32, u32)> {
4991 Some((start, next.start - start))5058 Some((start, next.start - start))
4992 }5059 }
4993 5060
5061+/// Every bounded hole in `clips`, left to right. The tail after the last clip is
5062+/// deliberately absent: there is no later placement for a ripple to bring back.
5063+fn gaps(clips: &[Clip]) -> Vec<(u32, u32)> {
5064+ let mut out = Vec::new();
5065+ let mut next = 0;
5066+ for c in clips {
5067+ if c.start > next {
5068+ out.push((next, c.start - next));
5069+ }
5070+ next = c.end();
5071+ }
5072+ out
5073+}
5074+
4994 /// Index of the clip `frame` falls *strictly inside*, i.e. the one a split5075 /// Index of the clip `frame` falls *strictly inside*, i.e. the one a split
4995 /// there would cut in two. A placement's own first frame is not inside it.5076 /// there would cut in two. A placement's own first frame is not inside it.
4996 fn splittable(clips: &[Clip], frame: u32) -> Option<usize> {5077 fn splittable(clips: &[Clip], frame: u32) -> Option<usize> {
@@ -7773,6 +7854,108 @@ mod tests {
7773 assert!(err.contains("detach"), "{err}");7854 assert!(err.contains("detach"), "{err}");
7774 }7855 }
7775 7856
7857+
7858+ /// The sweep is the single-gap close repeated on one lane, but measured once
7859+ /// and applied from the right. The differing gap lengths here expose a
7860+ /// front-to-back bug: the last clip would be cut against a stale frame after
7861+ /// the first gap shifted it.
7862+ #[test]
7863+ fn close_all_gaps_on_one_lane_closes_every_bounded_gap_and_undoes_once() {
7864+ let mut p = Project::single(FILE, 1);
7865+ let v = p.add_lane(LaneKind::Video);
7866+ assert!(p.place(v, 2, clip(2, 0, 2, 0)), "head gap [0,2)");
7867+ assert!(p.place(v, 7, clip(7, 2, 4, 0)), "middle gap [4,7)");
7868+ assert!(p.place(v, 14, clip(14, 4, 5, 0)), "middle gap [9,14)");
7869+ let before = shape(&p);
7870+
7871+ assert_eq!(p.gap_count(v), 3);
7872+ let report = p.close_all_gaps_on_lane(v).expect("the lane exists");
7873+ assert_eq!(report.closed, 3);
7874+ assert!(report.skipped.is_empty());
7875+ assert_eq!(
7876+ p.lane(v),
7877+ &[clip(0, 0, 2, 0), clip(2, 2, 4, 0), clip(4, 4, 5, 0)],
7878+ "all gaps closed without eating source frames"
7879+ );
7880+ assert!(p.undo(), "one undo for the whole sweep");
7881+ assert_eq!(shape(&p), before);
7882+ }
7883+
7884+ /// Empty, already-contiguous and tail-only lanes are not controls that do
7885+ /// work. A single clip with a head gap does close, because there is a real
7886+ /// placement to ripple back.
7887+ #[test]
7888+ fn close_all_gaps_on_one_lane_handles_empty_single_contiguous_and_tail_only_lanes() {
7889+ let mut p = Project::single(FILE, 9);
7890+ let v = p.add_lane(LaneKind::Video);
7891+ let history = p.history.len();
7892+ let report = p.close_all_gaps_on_lane(v).expect("empty lane exists");
7893+ assert_eq!(report, GapSweep::default(), "an empty lane has no bounded gap");
7894+ assert_eq!(p.history.len(), history, "no undo step for a no-op");
7895+
7896+ let mut p = Project::single(FILE, 9);
7897+ let v = p.add_lane(LaneKind::Video);
7898+ assert!(p.place(v, 5, clip(5, 0, 3, 0)));
7899+ let before = shape(&p);
7900+ let report = p.close_all_gaps_on_lane(v).expect("single clip lane exists");
7901+ assert_eq!(report.closed, 1, "the head gap closes");
7902+ assert_eq!(p.lane(v), &[clip(0, 0, 3, 0)]);
7903+ assert!(p.undo());
7904+ assert_eq!(shape(&p), before);
7905+
7906+ let mut p = Project::single(FILE, 9);
7907+ let history = p.history.len();
7908+ let report = p.close_all_gaps_on_lane(Lane::V1).expect("V1 exists");
7909+ assert_eq!(report, GapSweep::default(), "contiguous clips have no gaps");
7910+ assert_eq!(p.history.len(), history);
7911+
7912+ let mut p = Project::single(FILE, 9);
7913+ let v = p.add_lane(LaneKind::Video);
7914+ assert!(p.place(v, 0, clip(0, 0, 3, 0)));
7915+ let history = p.history.len();
7916+ let report = p.close_all_gaps_on_lane(v).expect("tail-only lane exists");
7917+ assert_eq!(report, GapSweep::default(), "the open-ended tail is not closed");
7918+ assert_eq!(p.lane(v), &[clip(0, 0, 3, 0)]);
7919+ assert_eq!(p.history.len(), history);
7920+ }
7921+
7922+ /// A sweep closes the gaps that satisfy the linked-take rule and leaves the
7923+ /// rest in place with a count and reason. Here [2,4) matches on both lanes,
7924+ /// while V1's [6,9) has only [6,8) empty on A1.
7925+ #[test]
7926+ fn close_all_gaps_on_one_lane_partially_closes_linked_takes_and_reports_skips() {
7927+ let sources = vec![Source::new(FILE, 0)];
7928+ let video = vec![
7929+ linked(0, 0, 2, 1),
7930+ linked(4, 4, 6, 2),
7931+ clip(9, 9, 11, 0),
7932+ ];
7933+ let audio = vec![
7934+ linked(0, 0, 2, 1),
7935+ linked(4, 4, 6, 2),
7936+ clip(8, 8, 11, 0),
7937+ ];
7938+ let mut p =
7939+ Project::from_parts(sources, two(video, audio), Vec::new(), Vec::new(), Vec::new())
7940+ .expect("valid parts");
7941+ let before = shape(&p);
7942+
7943+ let report = p.close_all_gaps_on_lane(Lane::V1).expect("V1 exists");
7944+ assert_eq!(report.closed, 1);
7945+ assert_eq!(report.skipped.len(), 1);
7946+ assert_eq!(report.skipped[0].start, 6);
7947+ assert!(report.skipped[0].reason.contains("A1"), "{report:?}");
7948+ assert!(report.skipped[0].reason.contains("out of sync"), "{report:?}");
7949+
7950+ assert_eq!(p.lane(Lane::V1)[1].start, 2, "the matched take moved");
7951+ assert_eq!(p.lane(Lane::A1)[1].start, 2, "and its other half stayed in sync");
7952+ assert_eq!(p.lane(Lane::V1)[2].start, 7, "the skipped later gap remains");
7953+ assert_eq!(p.lane(Lane::A1)[2].start, 6, "A1's shorter gap remains shorter");
7954+ links_are_consistent(&p.lanes).expect("every linked take that moved stayed whole");
7955+ assert!(p.undo(), "one undo restores every closed gap");
7956+ assert_eq!(shape(&p), before);
7957+ }
7958+
7776 #[test]7959 #[test]
7777 fn ripple_delete_spans_partial_clips() {7960 fn ripple_delete_spans_partial_clips() {
7778 let mut p = three();7961 let mut p = three();