forge
Explore
Log in

Don't you dare go Hollow

The last clip deletes and the timeline empties

Browse files at this commit

9a6ef8129737597c7f8c06669fd11db2e0da33d4Ahmet Kaan GümüşParent: 99fb07c
  • feat
    • the last clip deletes and the timeline empties
Show full message
✨ feat(timeline): the last clip deletes and the timeline empties

The never-empty-TIMELINE invariant is gone: an emptied timeline is now a
state of the model, not an edge case a refusal kept unreachable. Emptiness
is honest all the way down -- `PlaybackSession::span` is an `Option<Span>`
whose `None` *is* the empty timeline, and `start_span` feeds it one black
frame, so the viewer shows the nothing that is there rather than the last
picture of the clip that was deleted.

* `Project::lift` and `Project::ripple_delete` refuse only what is not an
  edit (a bad index, an empty range, a delete with nothing left to take --
  the last so a no-op still costs no undo step);
* `Project::from_parts` and the `.edith` loader take an all-empty project
  and refuse a *laneless* one instead, which is the invariant that was
  really being defended;
* `without_orphan_sources` keeps source 0 when no clip names anything: it
  carries the frame rate the timeline counts in, and pruning it wrote a
  project that could not be loaded back;
* the app's two "the timeline cannot be emptied" notices are gone, and an
  export of an empty timeline is refused by name instead of writing a file
  of no frames.

The never-empty-CLIP invariant (len >= 1) is untouched.
crates/app/src/main.rsmodified+9 -2
@@ -929,7 +929,7 @@ impl Player {
929 _ => false,929 _ => false,
930 };930 };
931 if selected.is_some() && !deleted {931 if selected.is_some() && !deleted {
932- self.notice = Some("NOTHING DELETED — the timeline cannot be emptied".into());932+ self.notice = Some("NOTHING DELETED — that clip is no longer there".into());
933 }933 }
934 if deleted {934 if deleted {
935 self.reset_after_reseek();935 self.reset_after_reseek();
@@ -949,7 +949,7 @@ impl Player {
949 if session.lift_clip(lane, idx) {949 if session.lift_clip(lane, idx) {
950 self.reset_after_reseek();950 self.reset_after_reseek();
951 } else {951 } else {
952- self.notice = Some("NOTHING LIFTED — the timeline cannot be emptied".into());952+ self.notice = Some("NOTHING LIFTED — that half is no longer there".into());
953 }953 }
954 }954 }
955 (Some(_), None) => {955 (Some(_), None) => {
@@ -1503,6 +1503,13 @@ impl Player {
1503 cx.notify();1503 cx.notify();
1504 return;1504 return;
1505 };1505 };
1506+ // An emptied timeline is a timeline; it is simply not a file. Refused by
1507+ // name here rather than written as a project of no frames.
1508+ if session.is_empty() {
1509+ self.notice = Some("NOTHING TO EXPORT — the timeline is empty".into());
1510+ cx.notify();
1511+ return;
1512+ }
1506 session.pause();1513 session.pause();
1507 self.export = Some(session.export_to_with(&self.export_path, &settings));1514 self.export = Some(session.export_to_with(&self.export_path, &settings));
1508 // The card has been answered; the progress line takes the panel from1515 // The card has been answered; the progress line takes the panel from
crates/engine/src/edith.rsmodified+25 -19
@@ -504,9 +504,9 @@ fn parse(data: &[u8], dir: &Path) -> crate::Result<Document> {
504 }504 }
505 }505 }
506 }506 }
507- if doc.lanes.iter().all(|(_, clips)| clips.is_empty()) {507+ // A file whose lanes are all empty *is* a project -- the emptied timeline,
508- return Err("no clips: an empty timeline is not a project".into());508+ // saved. What it may not be is laneless; [`crate::Project::from_parts`]
509- }509+ // refuses that one, by the name it has there.
510 Ok(doc)510 Ok(doc)
511 }511 }
512 512
@@ -870,13 +870,10 @@ mod tests {
870 ] {870 ] {
871 assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want);871 assert_eq!(parse(file.as_bytes(), &dir).unwrap_err().to_string(), want);
872 }872 }
873- // Lanes and nothing on them is not a project.873+ // Lanes and nothing on them is the emptied timeline, saved: a project.
874- assert_eq!(874+ let empty = parse(b"edith 4\nsource 0 a.mp4\nvideo 1\naudio 1\n", &dir).expect("parse");
875- parse(b"edith 4\nsource 0 a.mp4\nvideo 1\naudio 1\n", &dir)875+ assert!(empty.lanes.iter().all(|(_, clips)| clips.is_empty()));
876- .unwrap_err()876+ assert_eq!(empty.lanes.len(), 2, "and its lanes survive the round trip");
877- .to_string(),
878- "no clips: an empty timeline is not a project"
879- );
880 }877 }
881 878
882 /// One band, spelled the way an eq line spells it.879 /// One band, spelled the way an eq line spells it.
@@ -1539,19 +1536,28 @@ mod tests {
1539 }1536 }
1540 }1537 }
1541 1538
1539+ /// The emptied timeline writes and reads back: every lane declared, no clip
1540+ /// on any of them, and the source that gave the project its frame rate still
1541+ /// named. Nothing about the format changes for it -- an empty lane already
1542+ /// had a line of its own.
1542 #[test]1543 #[test]
1543- fn a_project_without_clips_is_not_a_project() {1544+ fn a_project_without_clips_round_trips() {
1544 let dir = PathBuf::from("/proj");1545 let dir = PathBuf::from("/proj");
1546+ let source = Source {
1547+ path: PathBuf::from("/proj/a.mp4"),
1548+ audio_stream: 0,
1549+ };
1550+ let bytes = flat(&dir, &[source.clone()], &two(Vec::new(), Vec::new()), 0);
1551+ let back = parse(&bytes, &dir).expect("an emptied timeline is a project");
1552+ assert_eq!(back.sources, vec![source]);
1553+ assert_eq!(back.lanes, two(Vec::new(), Vec::new()));
1554+ assert_eq!(back.playhead, 0);
1555+ // Older dialects, where the two lanes are implied rather than declared.
1545 assert_eq!(1556 assert_eq!(
1546- parse(b"edith 1\nsource a.mp4\n", &dir)1557+ parse(b"edith 1\nsource a.mp4\n", &dir).expect("v1").lanes,
1547- .unwrap_err()1558+ two(Vec::new(), Vec::new())
1548- .to_string(),
1549- "no clips: an empty timeline is not a project"
1550- );
1551- assert_eq!(
1552- parse(b"edith 1\n", &dir).unwrap_err().to_string(),
1553- "no clips: an empty timeline is not a project"
1554 );1559 );
1560+ assert!(parse(b"edith 1\n", &dir).expect("a project of nothing").sources.is_empty());
1555 }1561 }
1556 1562
1557 /// Every truncation of a good file either parses or refuses; none panics.1563 /// Every truncation of a good file either parses or refuses; none panics.
crates/engine/src/playback.rsmodified+60 -28
@@ -128,7 +128,13 @@ pub struct PlaybackSession {
128 /// clip index: a `split` cuts the clip under a running worker, and only the128 /// clip index: a `split` cuts the clip under a running worker, and only the
129 /// mapping survives that. A span with no source is a *gap*, and the worker129 /// mapping survives that. A span with no source is a *gap*, and the worker
130 /// feeding it emits black frames indexed from zero.130 /// feeding it emits black frames indexed from zero.
131- span: Span,131+ ///
132+ /// `None` is the *emptied* timeline -- no clip on any lane, so there is no
133+ /// stretch to be inside of. It is a state, not a failure: the picture is
134+ /// black ([`start_span`](Self::start_span) feeds one black frame for it),
135+ /// the sound is silence, the duration is zero, and placing anything reseeks
136+ /// straight back out of it.
137+ span: Option<Span>,
132 /// The last clip has been played out; see [`PlaybackSession::is_eos`].138 /// The last clip has been played out; see [`PlaybackSession::is_eos`].
133 eos: bool,139 eos: bool,
134 }140 }
@@ -174,7 +180,7 @@ impl PlaybackSession {
174 // One clip per lane covering the file, so timeline == source until the180 // One clip per lane covering the file, so timeline == source until the
175 // first edit -- and the range opened above is exactly that clip's.181 // first edit -- and the range opened above is exactly that clip's.
176 let project = Project::single(&path, meta.frame_count);182 let project = Project::single(&path, meta.frame_count);
177- let span = project.composite_span_at(0).expect("never empty");183+ let span = project.composite_span_at(0);
178 Ok(Self {184 Ok(Self {
179 meta,185 meta,
180 native: (meta.width, meta.height),186 native: (meta.width, meta.height),
@@ -290,7 +296,7 @@ impl PlaybackSession {
290 296
291 let playhead = doc.playhead;297 let playhead = doc.playhead;
292 let project = Project::from_parts(doc.sources, doc.lanes, doc.eq, doc.color)?;298 let project = Project::from_parts(doc.sources, doc.lanes, doc.eq, doc.color)?;
293- let span = project.composite_span_at(0).expect("never empty");299+ let span = project.composite_span_at(0);
294 // Last, because it is the one thing here that cannot be taken back: the300 // Last, because it is the one thing here that cannot be taken back: the
295 // feeder thread outlives the `Audio` value (it holds its own clones) and301 // feeder thread outlives the `Audio` value (it holds its own clones) and
296 // only a session's `drop` retires it, so a refusal above this line would302 // only a session's `drop` retires it, so a refusal above this line would
@@ -370,9 +376,12 @@ impl PlaybackSession {
370 match self.frames.try_recv() {376 match self.frames.try_recv() {
371 Ok(mut frame) => {377 Ok(mut frame) => {
372 // A gap's worker indexes from zero, a decoder's from its in378 // A gap's worker indexes from zero, a decoder's from its in
373- // point: `base` is whichever this span started at.379+ // point: `base` is whichever this span started at. An empty
374- let base = self.span.from.map_or(0, |(_, in_frame)| in_frame);380+ // timeline has no span, and its one black frame is frame 0.
375- frame.index = self.span.start + frame.index.saturating_sub(base);381+ let (start, base) = self
382+ .span
383+ .map_or((0, 0), |s| (s.start, s.from.map_or(0, |(_, i)| i)));
384+ frame.index = start + frame.index.saturating_sub(base);
376 return Some(frame);385 return Some(frame);
377 }386 }
378 Err(TryRecvError::Empty) => return None,387 Err(TryRecvError::Empty) => return None,
@@ -415,35 +424,47 @@ impl PlaybackSession {
415 /// than remembered as a clip index, because a `split` while playing cuts the424 /// than remembered as a clip index, because a `split` while playing cuts the
416 /// clip under the running worker and only the mapping stays true.425 /// clip under the running worker and only the mapping stays true.
417 fn next_clip(&mut self) -> bool {426 fn next_clip(&mut self) -> bool {
418- let next = self.span.end();427+ // No span is the emptied timeline: it was played out the moment its one
428+ // black frame went by, and there is no "next" to walk to.
429+ let next = match self.span {
430+ Some(span) => span.end(),
431+ None => return false,
432+ };
419 let Some(span) = self.project.composite_span_at(next) else {433 let Some(span) = self.project.composite_span_at(next) else {
420 return false;434 return false;
421 };435 };
422 // We only get here on a disconnect, so the old worker has already436 // We only get here on a disconnect, so the old worker has already
423 // returned; cancel anyway, so `retire` treats every path alike.437 // returned; cancel anyway, so `retire` treats every path alike.
424 self.worker.cancel();438 self.worker.cancel();
425- self.start_span(span);439+ self.start_span(Some(span));
426 true440 true
427 }441 }
428 442
429 /// Points the video worker at `span`: a decoder over its source range, or a443 /// Points the video worker at `span`: a decoder over its source range, or a
430- /// black-frame generator for a gap. The old worker must already have been444+ /// black-frame generator for a gap -- and for `None`, the emptied timeline,
431- /// cancelled -- this is the half both `seek` and `next_clip` share.445+ /// which is one frame of black so the viewer shows the nothing that is
446+ /// there rather than the last picture of the clip that was deleted. The old
447+ /// worker must already have been cancelled -- this is the half both `seek`
448+ /// and `next_clip` share.
432 ///449 ///
433 /// A source that will not open leaves the *span* installed anyway: the450 /// A source that will not open leaves the *span* installed anyway: the
434 /// timeline still moves, there are simply no more pictures, and the451 /// timeline still moves, there are simply no more pictures, and the
435 /// disconnected receiver carries the session on to the next span.452 /// disconnected receiver carries the session on to the next span.
436- fn start_span(&mut self, span: Span) {453+ fn start_span(&mut self, span: Option<Span>) {
437- let opened = match span.from {454+ let opened = match span {
438 // The grade is the composite's at this frame -- the same clip the455 // The grade is the composite's at this frame -- the same clip the
439 // span itself came from -- and it is constant across the span, so456 // span itself came from -- and it is constant across the span, so
440 // the worker carries it and every frame it converts wears it.457 // the worker carries it and every frame it converts wears it.
441- Some((source, in_frame)) => DecodeSession::open_worker(458+ Some(Span {
459+ start,
460+ len,
461+ from: Some((source, in_frame)),
462+ }) => DecodeSession::open_worker(
442 &self.project.sources()[source].path,463 &self.project.sources()[source].path,
443 in_frame,464 in_frame,
444- in_frame + span.len,465+ in_frame + len,
445 self.project466 self.project
446- .composite_color_at(span.start)467+ .composite_color_at(start)
447 .copied()468 .copied()
448 .unwrap_or_default(),469 .unwrap_or_default(),
449 // ...and the canvas it is placed on: the project's resolution470 // ...and the canvas it is placed on: the project's resolution
@@ -452,15 +473,18 @@ impl PlaybackSession {
452 Composer::new(473 Composer::new(
453 self.meta.width,474 self.meta.width,
454 self.meta.height,475 self.meta.height,
455- self.project.composite_fit_at(span.start),476+ self.project.composite_fit_at(start),
456 ),477 ),
457 )478 )
458 .map(|(_, stream)| stream)479 .map(|(_, stream)| stream)
459- .inspect_err(|e| eprintln!("timeline frame {}: video open failed: {e}", span.start)),480+ .inspect_err(|e| eprintln!("timeline frame {start}: video open failed: {e}")),
460- None => Ok(DecodeSession::open_black(481+ // A gap: black for as long as it runs. An emptied timeline has no
482+ // span at all and gets one frame of it -- enough to put black on
483+ // screen, and it ends where the timeline does, at once.
484+ gap => Ok(DecodeSession::open_black(
461 self.meta.width,485 self.meta.width,
462 self.meta.height,486 self.meta.height,
463- span.len,487+ gap.map_or(1, |s| s.len),
464 )),488 )),
465 };489 };
466 if let Ok(stream) = opened {490 if let Ok(stream) = opened {
@@ -480,6 +504,14 @@ impl PlaybackSession {
480 f64::from(self.project.timeline_frames()) / self.meta.frame_rate504 f64::from(self.project.timeline_frames()) / self.meta.frame_rate
481 }505 }
482 506
507+ /// Whether no lane holds anything: the emptied timeline, which plays black
508+ /// and silent and is zero seconds long. A state, not a failure -- but the
509+ /// one a caller with nothing to render (an export) has to refuse by name
510+ /// rather than write a file of no frames.
511+ pub fn is_empty(&self) -> bool {
512+ self.project.timeline_frames() == 0
513+ }
514+
483 /// `(start, len)` per clip in timeline seconds, in order: what a clips lane515 /// `(start, len)` per clip in timeline seconds, in order: what a clips lane
484 /// needs to lay itself out. Selection is the caller's business.516 /// needs to lay itself out. Selection is the caller's business.
485 pub fn clip_spans(&self) -> Vec<(f64, f64)> {517 pub fn clip_spans(&self) -> Vec<(f64, f64)> {
@@ -575,8 +607,9 @@ impl PlaybackSession {
575 607
576 /// Lifts one lane's clip out, leaving a gap: black frames on the video lane,608 /// Lifts one lane's clip out, leaving a gap: black frames on the video lane,
577 /// silence on the audio one, and nothing else moves. Reseeks, because what609 /// silence on the audio one, and nothing else moves. Reseeks, because what
578- /// the playhead sits on has changed. `false` for a bad index and for the610+ /// the playhead sits on has changed. `false` for a bad index -- the lift of
579- /// lift that would leave the whole timeline empty.611+ /// the last placement there is empties the timeline, which is a state
612+ /// ([`is_empty`](Self::is_empty)) and one undo away.
580 pub fn lift_clip(&mut self, lane: Lane, idx: usize) -> bool {613 pub fn lift_clip(&mut self, lane: Lane, idx: usize) -> bool {
581 self.edit(|p| p.lift(lane, idx))614 self.edit(|p| p.lift(lane, idx))
582 }615 }
@@ -775,7 +808,8 @@ impl PlaybackSession {
775 /// on every lane. Unlike a split this *does* move every following frame, so808 /// on every lane. Unlike a split this *does* move every following frame, so
776 /// the session reseeks to wherever the playhead now points.809 /// the session reseeks to wherever the playhead now points.
777 /// [`lift_clip`](Self::lift_clip) is the one that leaves a hole instead.810 /// [`lift_clip`](Self::lift_clip) is the one that leaves a hole instead.
778- /// `false` for a bad index or the last remaining clip.811+ /// `false` for a bad index; the last remaining clip goes like any other and
812+ /// leaves the timeline empty ([`is_empty`](Self::is_empty)).
779 ///813 ///
780 /// The lane travels because the index is a lane's own: `V2`'s third clip is814 /// The lane travels because the index is a lane's own: `V2`'s third clip is
781 /// not `V1`'s, and a front-end that could only say "the third clip" would815 /// not `V1`'s, and a front-end that could only say "the third clip" would
@@ -966,12 +1000,10 @@ impl PlaybackSession {
966 // [`retire`](Self::retire), which is what keeps a scrub off the price1000 // [`retire`](Self::retire), which is what keeps a scrub off the price
967 // of a VA-API init.1001 // of a VA-API init.
968 self.worker.cancel();1002 self.worker.cancel();
969- // `target` is inside the timeline (never empty), so this always spans;1003+ // The span runs from `target` to the end of whatever it landed in -- a
970- // the span runs from there to the end of whatever it landed in -- a1004+ // clip, or a gap, which starts a black-frame worker instead. `None` is
971- // clip, or a gap, which starts a black-frame worker instead.1005+ // the emptied timeline, black too, and `start_span` says so.
972- if let Some(span) = self.project.composite_span_at(target) {1006+ self.start_span(self.project.composite_span_at(target));
973- self.start_span(span);
974- }
975 self.eos = false;1007 self.eos = false;
976 1008
977 let mut audio_running = false;1009 let mut audio_running = false;
crates/engine/src/project.rsmodified+67 -32
@@ -299,10 +299,16 @@ impl Project {
299 /// [`Project::undo`] is `false` until the first edit of the new session.299 /// [`Project::undo`] is `false` until the first edit of the new session.
300 /// This is the one door untrusted parts come in through, so every invariant300 /// This is the one door untrusted parts come in through, so every invariant
301 /// every other constructor keeps is checked here, by name and in release:301 /// every other constructor keeps is checked here, by name and in release:
302- /// every lane empty, an empty clip, a clip naming a source (or an equalizer,302+ /// no lanes at all, an empty clip, a clip naming a source (or an equalizer,
303 /// or a colour) that is not there, a clip whose end overflows, a lane that is303 /// or a colour) that is not there, a clip whose end overflows, a lane that is
304 /// unsorted or self-overlapping, and the grouping rules of [`Clip::link`]304 /// unsorted or self-overlapping, and the grouping rules of [`Clip::link`]
305 /// below.305 /// below.
306+ ///
307+ /// A project whose lanes are all *empty* is not among them: an emptied
308+ /// timeline is a project like any other -- it plays black and silent, it
309+ /// saves, and it loads back. What a project cannot be is laneless, because
310+ /// [`Project::lanes`] is what a front-end lays out and what every `Lane`
311+ /// handle indexes into.
306 pub fn from_parts(312 pub fn from_parts(
307 sources: Vec<Source>,313 sources: Vec<Source>,
308 lanes: Vec<(LaneKind, Vec<Clip>)>,314 lanes: Vec<(LaneKind, Vec<Clip>)>,
@@ -315,8 +321,8 @@ impl Project {
315 if let Some(bad) = color.iter().position(|p| !color_finite(p)) {321 if let Some(bad) = color.iter().position(|p| !color_finite(p)) {
316 return Err(format!("color {bad} holds a value that is not a finite number").into());322 return Err(format!("color {bad} holds a value that is not a finite number").into());
317 }323 }
318- if lanes.iter().all(|(_, clips)| clips.is_empty()) {324+ if lanes.is_empty() {
319- return Err("every lane is empty: that is not a project".into());325+ return Err("no lanes at all: that is not a project".into());
320 }326 }
321 let lanes: Vec<LaneData> = lanes327 let lanes: Vec<LaneData> = lanes
322 .into_iter()328 .into_iter()
@@ -421,9 +427,8 @@ impl Project {
421 /// that could not be taken back would be the one edit with no way out.427 /// that could not be taken back would be the one edit with no way out.
422 /// Nothing plays differently until something is placed on it.428 /// Nothing plays differently until something is placed on it.
423 ///429 ///
424- /// The new lane being empty is fine even though [`Project::from_parts`]430+ /// An empty lane is a lane like any other -- so is a whole project of them
425- /// refuses an all-empty project: the lanes that were there still hold the431+ /// ([`Project::from_parts`]); nothing plays until something is placed.
426- /// timeline, and "no lane holds anything" is what that door refuses.
427 pub fn add_lane(&mut self, kind: LaneKind) -> Lane {432 pub fn add_lane(&mut self, kind: LaneKind) -> Lane {
428 self.snapshot();433 self.snapshot();
429 self.lanes.push(LaneData {434 self.lanes.push(LaneData {
@@ -525,6 +530,11 @@ impl Project {
525 /// [`set_color`](Project::set_color) leaves settings nothing plays behind,530 /// [`set_color`](Project::set_color) leaves settings nothing plays behind,
526 /// and this is the one moment they can go -- the indexes that survive it are531 /// and this is the one moment they can go -- the indexes that survive it are
527 /// only the ones a clip names.532 /// only the ones a clip names.
533+ ///
534+ /// One exception, and it is the emptied timeline's: a project no clip plays
535+ /// from still keeps source 0. It is the file a session is scaffolded from --
536+ /// its frame rate is the timeline's and is written nowhere else -- so a save
537+ /// that pruned it would write a project that cannot be loaded back at all.
528 pub fn without_orphan_sources(&self) -> Parts {538 pub fn without_orphan_sources(&self) -> Parts {
529 let mut moved = vec![None; self.sources.len()];539 let mut moved = vec![None; self.sources.len()];
530 let mut sources = Vec::new();540 let mut sources = Vec::new();
@@ -566,6 +576,9 @@ impl Project {
566 });576 });
567 }577 }
568 }578 }
579+ if sources.is_empty() {
580+ sources.extend(self.sources.first().cloned());
581+ }
569 (582 (
570 sources,583 sources,
571 lanes.into_iter().map(|l| (l.kind, l.clips)).collect(),584 lanes.into_iter().map(|l| (l.kind, l.clips)).collect(),
@@ -1069,13 +1082,12 @@ impl Project {
1069 }1082 }
1070 1083
1071 /// Lift the clip at `idx` out of `lane`, leaving a gap: black frames or1084 /// Lift the clip at `idx` out of `lane`, leaving a gap: black frames or
1072- /// silence, and nothing else moves. Refused for an out-of-range index (which1085+ /// silence, and nothing else moves. Refused only for an out-of-range index
1073- /// a lane that is not there always is) and for the lift that would leave1086+ /// (which a lane that is not there always is) -- lifting the last placement
1074- /// *every* lane empty -- an empty timeline is the front-end's state, not a1087+ /// there is leaves an *empty* timeline, which is a state the project holds
1075- /// project's (see the never-empty invariant).1088+ /// like any other and an undo brings back.
1076 pub fn lift(&mut self, lane: Lane, idx: usize) -> bool {1089 pub fn lift(&mut self, lane: Lane, idx: usize) -> bool {
1077- let placed: usize = self.lanes.iter().map(|l| l.clips.len()).sum();1090+ if idx >= self.lane(lane).len() {
1078- if idx >= self.lane(lane).len() || placed == 1 {
1079 return false;1091 return false;
1080 }1092 }
1081 self.snapshot();1093 self.snapshot();
@@ -1085,23 +1097,17 @@ impl Project {
1085 1097
1086 /// Cut the timeline frames `[at, at + len)` out of *every* lane and close1098 /// Cut the timeline frames `[at, at + len)` out of *every* lane and close
1087 /// the hole: everything after slides back by `len`. The rippling delete --1099 /// the hole: everything after slides back by `len`. The rippling delete --
1088- /// [`lift`](Project::lift) is the one that leaves a gap. Refused for an1100+ /// [`lift`](Project::lift) is the one that leaves a gap. Refused only for an
1089- /// empty range and when it would leave every lane empty.1101+ /// empty range; a delete that leaves nothing behind empties the timeline,
1102+ /// which is a state like any other and one undo away.
1090 pub fn ripple_delete(&mut self, at: u32, len: u32) -> bool {1103 pub fn ripple_delete(&mut self, at: u32, len: u32) -> bool {
1091 if len == 0 {1104 if len == 0 {
1092 return false;1105 return false;
1093 }1106 }
1094- let survivors: usize = self1107+ // Nothing reaches past `at`: there is nothing to cut and nothing to
1095- .lanes1108+ // slide back, and a delete that changes nothing must not cost an undo
1096- .iter()1109+ // step (see [`snapshot`](Project::snapshot)).
1097- .map(|l| {1110+ if !self.lanes.iter().flat_map(|l| &l.clips).any(|c| c.end() > at) {
1098- l.clips
1099- .iter()
1100- .filter(|c| c.start < at || c.end() > at + len)
1101- .count()
1102- })
1103- .sum();
1104- if survivors == 0 {
1105 return false;1111 return false;
1106 }1112 }
1107 self.snapshot();1113 self.snapshot();
@@ -1892,7 +1898,11 @@ mod tests {
1892 1898
1893 assert!(!p.delete(2), "index past the end");1899 assert!(!p.delete(2), "index past the end");
1894 assert!(p.delete(1));1900 assert!(p.delete(1));
1895- assert!(!p.delete(0), "the last remaining clip stays");1901+ assert!(p.delete(0), "and the last remaining clip goes too");
1902+ assert_eq!(p.clips().len(), 0);
1903+ assert_eq!(p.timeline_frames(), 0, "the timeline is emptiable");
1904+ assert!(!p.delete(0), "there is nothing left to delete");
1905+ assert!(p.undo(), "one gesture, one undo");
1896 assert_eq!(p.clips().len(), 1);1906 assert_eq!(p.clips().len(), 1);
1897 }1907 }
1898 1908
@@ -1943,13 +1953,28 @@ mod tests {
1943 }1953 }
1944 }1954 }
1945 1955
1956+ /// The timeline is emptiable: the last placement comes off like any other,
1957+ /// the project holds "nothing on any lane" as a state, and one undo per
1958+ /// gesture brings it back.
1946 #[test]1959 #[test]
1947- fn the_last_clip_of_the_last_lane_cannot_be_lifted() {1960+ fn the_last_clip_of_the_last_lane_lifts_and_undoes() {
1948 let mut p = Project::single(FILE, 9);1961 let mut p = Project::single(FILE, 9);
1949 assert!(p.lift(Lane::A1, 0), "a silent timeline is fine");1962 assert!(p.lift(Lane::A1, 0), "a silent timeline is fine");
1950- assert!(!p.lift(Lane::V1, 0), "an empty one is not");1963+ assert!(p.lift(Lane::V1, 0), "and an empty one is a timeline too");
1964+ assert_eq!(p.timeline_frames(), 0);
1965+ assert_eq!(p.composite_span_at(0), None, "nothing to show");
1966+ assert!(p.audio_segments_from(0, FPS).len() == 1, "one all-gap list");
1951 assert!(!p.lift(Lane::A1, 0), "index past the end");1967 assert!(!p.lift(Lane::A1, 0), "index past the end");
1952- assert_eq!(p.timeline_frames(), 9);1968+ assert!(p.undo(), "and the last lift comes back");
1969+ assert_eq!(p.lane_spans(Lane::V1), vec![(0, 9)]);
1970+ // A saved-and-loaded empty timeline is a project like any other, and it
1971+ // still names the file its frame rate came from.
1972+ assert!(p.lift(Lane::V1, 0));
1973+ let (sources, lanes, eq, color) = p.without_orphan_sources();
1974+ assert_eq!(sources.len(), 1, "source 0 survives an emptied timeline");
1975+ let back = Project::from_parts(sources, lanes, eq, color).expect("an empty project loads");
1976+ assert_eq!(back.timeline_frames(), 0);
1977+ assert_eq!(back.lanes().len(), 2, "and it kept its lanes");
1953 }1978 }
1954 1979
1955 /// What a keyboard selection walks: one answer per lane, gaps included --1980 /// What a keyboard selection walks: one answer per lane, gaps included --
@@ -2021,7 +2046,16 @@ mod tests {
2021 assert_eq!(shape(&p)[1], shape(&p)[0]);2046 assert_eq!(shape(&p)[1], shape(&p)[0]);
2022 assert_eq!(p.timeline_frames(), 5);2047 assert_eq!(p.timeline_frames(), 5);
2023 assert!(!p.ripple_delete(0, 0), "an empty range is not a delete");2048 assert!(!p.ripple_delete(0, 0), "an empty range is not a delete");
2024- assert!(!p.ripple_delete(0, 100), "and one that empties every lane");2049+ assert!(p.undo());
2050+ assert_eq!(shape(&p), shape(&three()));
2051+ // One that takes everything is a delete like any other: the timeline
2052+ // empties, and the undo it cost brings it back whole.
2053+ assert!(p.ripple_delete(0, 100), "the timeline is emptiable");
2054+ assert_eq!(p.timeline_frames(), 0);
2055+ assert!(
2056+ !p.ripple_delete(0, 100),
2057+ "and a delete with nothing left to take is not an undo step"
2058+ );
2025 assert!(p.undo());2059 assert!(p.undo());
2026 assert_eq!(shape(&p), shape(&three()));2060 assert_eq!(shape(&p), shape(&three()));
2027 }2061 }
@@ -2472,7 +2506,8 @@ mod tests {
2472 assert!(p.split(4), "...and is editable from there");2506 assert!(p.split(4), "...and is editable from there");
2473 assert!(p.undo());2507 assert!(p.undo());
2474 2508
2475- // A lane may be empty; every lane may not, and neither may no lane.2509+ // A lane may be empty, and so may every lane -- an emptied timeline is a
2510+ // project. No lane at all is not one: there would be nothing to place on.
2476 assert!(2511 assert!(
2477 Project::from_parts(2512 Project::from_parts(
2478 sources.clone(),2513 sources.clone(),
@@ -2489,7 +2524,7 @@ mod tests {
2489 Vec::new(),2524 Vec::new(),
2490 Vec::new(),2525 Vec::new(),
2491 )2526 )
2492- .is_err()2527+ .is_ok()
2493 );2528 );
2494 assert!(Project::from_parts(sources.clone(), Vec::new(), Vec::new(), Vec::new()).is_err());2529 assert!(Project::from_parts(sources.clone(), Vec::new(), Vec::new(), Vec::new()).is_err());
2495 let bad: [Vec<Clip>; 5] = [2530 let bad: [Vec<Clip>; 5] = [
crates/engine/tests/playback.rsmodified+71 -0
@@ -1122,3 +1122,74 @@ fn an_audio_gap_is_silent_and_the_clock_keeps_counting() {
1122 );1122 );
1123 eprintln!("audio gap: clock at {now:.3}s of {duration:.3}s, frame {last_index:?}");1123 eprintln!("audio gap: clock at {now:.3}s of {duration:.3}s, frame {last_index:?}");
1124 }1124 }
1125+
1126+/// The emptied timeline, end to end: the last clip deletes like any other, what
1127+/// is left plays as black and silence with a duration of zero, it saves and
1128+/// loads back as the project it is, and one undo brings the clip back. Silent
1129+/// fixture, so this runs anywhere.
1130+#[test]
1131+fn an_emptied_timeline_plays_black_saves_loads_and_undoes() {
1132+ let path = asset("test_baseline.mp4");
1133+ let mut session = open(&path);
1134+ let whole = session.timeline_duration();
1135+ assert!(whole > 0.0);
1136+
1137+ // The sole take goes -- picture and sound of it, on every lane at once.
1138+ assert!(
1139+ session.delete_clip(Lane::V1, 0),
1140+ "the last clip deletes like any other"
1141+ );
1142+ assert!(session.is_empty(), "and the timeline is empty");
1143+ assert_eq!(session.timeline_duration(), 0.0);
1144+ assert!(session.clip_spans().is_empty());
1145+ assert_eq!(session.lanes().len(), 2, "the lanes are still there");
1146+
1147+ // It scrubs: every seek lands at zero and shows black rather than the
1148+ // picture of the clip that was deleted.
1149+ for t in [0.0, -1.0, whole, 1e9] {
1150+ session.seek(t);
1151+ assert_eq!(session.now(), 0.0, "seek to {t} on an empty timeline");
1152+ let deadline = Instant::now() + Duration::from_secs(5);
1153+ let mut black = None;
1154+ while black.is_none() && Instant::now() < deadline {
1155+ session.tick();
1156+ black = session.try_frame();
1157+ sleep(Duration::from_millis(4));
1158+ }
1159+ let frame = black.expect("an empty timeline shows one black frame");
1160+ assert_eq!(frame.index, 0);
1161+ assert!(
1162+ frame.bgra.chunks_exact(4).all(|p| p[..3] == [0, 0, 0]),
1163+ "and it is black"
1164+ );
1165+ }
1166+ // It plays: nothing to show, so it is at its end at once, and nothing hangs.
1167+ session.play();
1168+ let mut last_index = None;
1169+ run_for(&mut session, &mut last_index, Duration::from_millis(200));
1170+ assert!(session.is_eos(), "an empty timeline is played out at once");
1171+ session.pause();
1172+
1173+ // It saves and loads back -- still a project, still two lanes, and still
1174+ // naming the file whose frame rate the timeline counts in.
1175+ let dir = std::env::temp_dir().join(format!("ve_empty_{}", std::process::id()));
1176+ let _ = std::fs::remove_dir_all(&dir);
1177+ std::fs::create_dir_all(&dir).expect("scratch dir");
1178+ let project = dir.join("empty.edith");
1179+ session
1180+ .save_project(&project)
1181+ .expect("save an empty timeline");
1182+ let loaded = PlaybackSession::open_project(&project).expect("load it back");
1183+ assert!(loaded.is_empty(), "what came back is the empty timeline");
1184+ assert_eq!(loaded.lanes().len(), 2);
1185+ assert_eq!(loaded.sources().len(), 1, "source 0 is kept, orphan or not");
1186+ assert_eq!(loaded.meta().frame_rate, session.meta().frame_rate);
1187+ drop(loaded);
1188+ let _ = std::fs::remove_dir_all(&dir);
1189+
1190+ // ...and one gesture is one undo: the take comes back whole.
1191+ assert!(session.undo(), "the delete undoes");
1192+ assert!(!session.is_empty());
1193+ assert!((session.timeline_duration() - whole).abs() < 1e-9);
1194+ assert_eq!(session.clip_spans().len(), 1);
1195+}