diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index d24950d..45dfb02 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -1100,6 +1100,59 @@ impl Player { cx.notify(); } + /// Closes every bounded gap on `lane`, as one gesture. The scope stays the + /// clicked track's scope -- not the whole timeline -- and each linked take is + /// widened only when its other half has the same empty stretch. Unsafe gaps + /// are left open and counted in the notice, so the sweep never fails as a + /// silent no-op. + pub(crate) fn close_all_gaps_on_lane(&mut self, lane: Lane, cx: &mut Context) { + let Some(session) = self.session.as_mut() else { + return; + }; + let report = match session.close_all_gaps_on_lane(lane) { + Ok(report) => report, + Err(e) => { + self.notify_user(e.to_string().into()); + cx.notify(); + return; + } + }; + if report.closed > 0 { + self.mark_dirty(); + self.selected.clear(); + self.reset_after_reseek(); + } + let skipped = report.skipped.len(); + let notice = match (report.closed, skipped) { + (0, 0) => format!("NO GAPS TO CLOSE on {}", lane.label()), + (0, 1) => format!( + "NO GAPS CLOSED on {} — 1 skipped at {}; match linked gaps or detach", + lane.label(), + report.skipped[0].start + ), + (0, n) => format!( + "NO GAPS CLOSED on {} — {n} skipped; match linked gaps or detach", + lane.label() + ), + (n, 0) => format!( + "{n} GAPS CLOSED on {} — {} takes them back", + lane.label(), + self.keymap.display(ActionId::Undo) + ), + (n, 1) => format!( + "{n} GAPS CLOSED on {}; 1 skipped at {} — match linked gaps or detach", + lane.label(), + report.skipped[0].start + ), + (n, skipped) => format!( + "{n} GAPS CLOSED on {}; {skipped} skipped — match linked gaps or detach", + lane.label() + ), + }; + self.notify_user(notice.into()); + cx.notify(); + } + /// A press on an edge: the start of the drag that changes how much of a /// source it plays. It picks the placement as a press anywhere else on the /// box does -- the edge strip covers the box's own listener (`occlude`), so diff --git a/crates/app/src/ui/overlays.rs b/crates/app/src/ui/overlays.rs index 66c2db4..f793656 100644 --- a/crates/app/src/ui/overlays.rs +++ b/crates/app/src/ui/overlays.rs @@ -567,21 +567,37 @@ impl Player { ); } } else if let MenuOn::Gap(start, frames) = menu.on { - // The one item a gap offers: no properties, no clip actions, just - // the ripple that closes it -- Premiere's Close Gap, Resolve's - // Delete Gap, per-lane the way `Player::close_gap` documents. + // Gap rows are ripples, not clip actions. The first is the exact + // hole under the pointer; the second appears only when the same + // track has another bounded hole too, and says the track in the row + // so "all" cannot read as the whole timeline. + let lane = menu.lane; rows.push( row(rows.len()) .cursor_pointer() .hover(|s| s.bg(rgb(BG_HOVER()))) .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { this.context_menu = None; - this.close_gap(menu.lane, start, frames, cx); + this.close_gap(lane, start, frames, cx); })) .child("Close gap") .child(chord_style(div().text_color(rgb(FG_SECONDARY()))).child("ripples left")) .into_any_element(), ); + if session.gap_count(lane) > 1 { + rows.push( + row(rows.len()) + .cursor_pointer() + .hover(|s| s.bg(rgb(BG_HOVER()))) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.context_menu = None; + this.close_all_gaps_on_lane(lane, cx); + })) + .child(format!("Close all {} gaps", lane.label())) + .child(chord_style(div().text_color(rgb(FG_SECONDARY()))).child("this track")) + .into_any_element(), + ); + } } else { // A grade on a waveform, an equalizer on a picture, a silence scan // on a still: things that do not exist for what was right-clicked, diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index e7d2448..b2b6aaa 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -26,7 +26,9 @@ use crate::colorspace::{ColorDescription, ContentLight}; use crate::decode::{Backend, BackendCell, DecodeSession, Frame, Worker}; use crate::demux::{Codec, Demuxer, VideoMeta}; use crate::eq::EqParams; -use crate::project::{Clip, Edge, Lane, LaneKind, Project, Rate, Source, Span, Speed, SubClip}; +use crate::project::{ + Clip, Edge, GapSweep, Lane, LaneKind, Project, Rate, Source, Span, Speed, SubClip, +}; use crate::scale::{Composer, FitPolicy}; use crate::transform::TransformParams; @@ -2035,6 +2037,12 @@ impl PlaybackSession { self.project.gap_at(lane, frame) } + /// How many bounded gaps that same right-click lane holds. The tail after + /// the last clip is not counted, because nothing would ripple there. + pub fn gap_count(&self, lane: Lane) -> usize { + self.project.gap_count(lane) + } + /// Every lane's handle, in display order -- what a front-end lays out top to /// bottom, and the list [`add_lane`](Self::add_lane) grows. pub fn lanes(&self) -> Vec { @@ -2458,6 +2466,18 @@ impl PlaybackSession { self.project.gap_take_scope(lane, start, frames) } + /// Closes every bounded gap on one lane, keeping each linked take whole when + /// its other half has the matching empty stretch and reporting the gaps that + /// were not safe to close. One undo step for every gap that closed. + pub fn close_all_gaps_on_lane(&mut self, lane: Lane) -> crate::Result { + let report = self.project.close_all_gaps_on_lane(lane)?; + if report.closed > 0 { + let now = self.now(); + self.seek(now); + } + Ok(report) + } + /// Cuts every one of `regions` -- `(start, len)` in timeline frames -- out /// of the lanes in `scope` and closes each hole, as **one** edit /// ([`Project::cut_regions`]): the jumpcut a silence scan diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 07e7301..74d4166 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -872,6 +872,22 @@ pub type Parts = ( /// that grows for as long as the session lasts. const HISTORY_CAP: usize = 100; +/// One gap a close-all sweep left open, with the frame it still starts on and +/// the same refusal a single [`Project::gap_take_scope`] close would have said. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct GapSkip { + pub start: u32, + pub reason: String, +} + +/// What [`Project::close_all_gaps_on_lane`] did: closed gaps count as edits, +/// skipped gaps are the take-safety refusals the sweep did not hide. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct GapSweep { + pub closed: usize, + pub skipped: Vec, +} + /// The edit list plus its undo history. #[derive(Clone, Debug)] pub struct Project { @@ -4189,6 +4205,13 @@ impl Project { gap(self.lane(lane), frame) } + /// How many bounded holes `lane` holds. The open end after the last clip is + /// not counted, like [`gap_at`](Self::gap_at), because closing it would move + /// nothing. + pub fn gap_count(&self, lane: Lane) -> usize { + gaps(self.lane(lane)).len() + } + /// The lanes closing the gap `(start, frames)` on `lane` must ripple /// together to keep sync -- what a right-click on empty bench space asks /// before the menu offers to close it, and what widens @@ -4257,6 +4280,50 @@ impl Project { Ok(scope) } + /// Close every bounded gap on `lane`, as one undo step, without making the + /// lane's scope global. Each gap uses the same take-safety widening + /// [`gap_take_scope`](Self::gap_take_scope) gives a single close: linked + /// halves ride along only when their matching lane is empty over that exact + /// stretch; mismatches are skipped and reported. + /// + /// The walk is back-to-front. Closing a gap shifts everything after it, so + /// later holes must be consumed while their measured starts are still true; + /// earlier starts are unchanged by cuts to their right. A snapshot is taken + /// only before the first successful close, so an all-skipped sweep is not an + /// undo step. + pub fn close_all_gaps_on_lane(&mut self, lane: Lane) -> crate::Result { + let Some(_) = self.index(lane) else { + return Err("no track to work on".into()); + }; + let mut report = GapSweep::default(); + let mut snapshot = false; + for (start, frames) in gaps(self.lane(lane)).into_iter().rev() { + match self + .gap_take_scope(lane, start, frames) + .and_then(|scope| { + let on = self.scoped(&scope)?; + self.scope_holds_takes_whole(&on, start)?; + Ok(on) + }) { + Ok(on) => { + if !snapshot { + self.snapshot(); + snapshot = true; + } + ripple(&mut self.lanes, &on, start, frames); + report.closed += 1; + } + Err(e) => report.skipped.push(GapSkip { + start, + reason: e.to_string(), + }), + } + } + report.skipped.reverse(); + debug_assert!(links_are_consistent(&self.lanes).is_ok()); + Ok(report) + } + /// Cut **every** one of `regions` -- `(start, len)` in timeline frames -- /// out of the lanes in `scope` and close each hole, as one edit: the /// jumpcut a silence scan asks for ([`crate::silence`]). @@ -4991,6 +5058,20 @@ fn gap(clips: &[Clip], frame: u32) -> Option<(u32, u32)> { Some((start, next.start - start)) } +/// Every bounded hole in `clips`, left to right. The tail after the last clip is +/// deliberately absent: there is no later placement for a ripple to bring back. +fn gaps(clips: &[Clip]) -> Vec<(u32, u32)> { + let mut out = Vec::new(); + let mut next = 0; + for c in clips { + if c.start > next { + out.push((next, c.start - next)); + } + next = c.end(); + } + out +} + /// Index of the clip `frame` falls *strictly inside*, i.e. the one a split /// there would cut in two. A placement's own first frame is not inside it. fn splittable(clips: &[Clip], frame: u32) -> Option { @@ -7773,6 +7854,108 @@ mod tests { assert!(err.contains("detach"), "{err}"); } + + /// The sweep is the single-gap close repeated on one lane, but measured once + /// and applied from the right. The differing gap lengths here expose a + /// front-to-back bug: the last clip would be cut against a stale frame after + /// the first gap shifted it. + #[test] + fn close_all_gaps_on_one_lane_closes_every_bounded_gap_and_undoes_once() { + let mut p = Project::single(FILE, 1); + let v = p.add_lane(LaneKind::Video); + assert!(p.place(v, 2, clip(2, 0, 2, 0)), "head gap [0,2)"); + assert!(p.place(v, 7, clip(7, 2, 4, 0)), "middle gap [4,7)"); + assert!(p.place(v, 14, clip(14, 4, 5, 0)), "middle gap [9,14)"); + let before = shape(&p); + + assert_eq!(p.gap_count(v), 3); + let report = p.close_all_gaps_on_lane(v).expect("the lane exists"); + assert_eq!(report.closed, 3); + assert!(report.skipped.is_empty()); + assert_eq!( + p.lane(v), + &[clip(0, 0, 2, 0), clip(2, 2, 4, 0), clip(4, 4, 5, 0)], + "all gaps closed without eating source frames" + ); + assert!(p.undo(), "one undo for the whole sweep"); + assert_eq!(shape(&p), before); + } + + /// Empty, already-contiguous and tail-only lanes are not controls that do + /// work. A single clip with a head gap does close, because there is a real + /// placement to ripple back. + #[test] + fn close_all_gaps_on_one_lane_handles_empty_single_contiguous_and_tail_only_lanes() { + let mut p = Project::single(FILE, 9); + let v = p.add_lane(LaneKind::Video); + let history = p.history.len(); + let report = p.close_all_gaps_on_lane(v).expect("empty lane exists"); + assert_eq!(report, GapSweep::default(), "an empty lane has no bounded gap"); + assert_eq!(p.history.len(), history, "no undo step for a no-op"); + + let mut p = Project::single(FILE, 9); + let v = p.add_lane(LaneKind::Video); + assert!(p.place(v, 5, clip(5, 0, 3, 0))); + let before = shape(&p); + let report = p.close_all_gaps_on_lane(v).expect("single clip lane exists"); + assert_eq!(report.closed, 1, "the head gap closes"); + assert_eq!(p.lane(v), &[clip(0, 0, 3, 0)]); + assert!(p.undo()); + assert_eq!(shape(&p), before); + + let mut p = Project::single(FILE, 9); + let history = p.history.len(); + let report = p.close_all_gaps_on_lane(Lane::V1).expect("V1 exists"); + assert_eq!(report, GapSweep::default(), "contiguous clips have no gaps"); + assert_eq!(p.history.len(), history); + + let mut p = Project::single(FILE, 9); + let v = p.add_lane(LaneKind::Video); + assert!(p.place(v, 0, clip(0, 0, 3, 0))); + let history = p.history.len(); + let report = p.close_all_gaps_on_lane(v).expect("tail-only lane exists"); + assert_eq!(report, GapSweep::default(), "the open-ended tail is not closed"); + assert_eq!(p.lane(v), &[clip(0, 0, 3, 0)]); + assert_eq!(p.history.len(), history); + } + + /// A sweep closes the gaps that satisfy the linked-take rule and leaves the + /// rest in place with a count and reason. Here [2,4) matches on both lanes, + /// while V1's [6,9) has only [6,8) empty on A1. + #[test] + fn close_all_gaps_on_one_lane_partially_closes_linked_takes_and_reports_skips() { + let sources = vec![Source::new(FILE, 0)]; + let video = vec![ + linked(0, 0, 2, 1), + linked(4, 4, 6, 2), + clip(9, 9, 11, 0), + ]; + let audio = vec![ + linked(0, 0, 2, 1), + linked(4, 4, 6, 2), + clip(8, 8, 11, 0), + ]; + let mut p = + Project::from_parts(sources, two(video, audio), Vec::new(), Vec::new(), Vec::new()) + .expect("valid parts"); + let before = shape(&p); + + let report = p.close_all_gaps_on_lane(Lane::V1).expect("V1 exists"); + assert_eq!(report.closed, 1); + assert_eq!(report.skipped.len(), 1); + assert_eq!(report.skipped[0].start, 6); + assert!(report.skipped[0].reason.contains("A1"), "{report:?}"); + assert!(report.skipped[0].reason.contains("out of sync"), "{report:?}"); + + assert_eq!(p.lane(Lane::V1)[1].start, 2, "the matched take moved"); + assert_eq!(p.lane(Lane::A1)[1].start, 2, "and its other half stayed in sync"); + assert_eq!(p.lane(Lane::V1)[2].start, 7, "the skipped later gap remains"); + assert_eq!(p.lane(Lane::A1)[2].start, 6, "A1's shorter gap remains shorter"); + links_are_consistent(&p.lanes).expect("every linked take that moved stayed whole"); + assert!(p.undo(), "one undo restores every closed gap"); + assert_eq!(shape(&p), before); + } + #[test] fn ripple_delete_spans_partial_clips() { let mut p = three();