forge
Explore
Log in

Don't you dare go Hollow

A removed track is one click from being back

Browse files at this commit

f41b4c601a91cfc473d13f9a6735d8abfd44fac3Ahmet Kaan GümüşParent: 4ddba0c
  • feat
    • a removed track is one click from being back
Show full message
✨ feat(subtitles): a removed track is one click from being back

Removing a subtitle row dropped the whole track -- cues and all -- and
the only way back was importing the file again, which on a big remux is
a walk of the whole container for something the session had in hand a
second ago. "Bringing back the removed subtitle needs reimport is very
nonsense", and it is: a removal is the one edit here whose inverse cost
more than the edit.

The removal is now non-destructive at the model. `Project` keeps what it
took off in a bin beside the list (`removed_subtitles`), and
`restore_subtitles` puts one back with the cues it left with -- no file
is read, so a row comes back even when its file has gone missing since.
The bin is the session's, not the project's: a `.edith` still writes what
the timeline has, exactly as it did.

The door is the row the removal leaves behind, under the list and dimmed,
with a `+` where the × was: the way off the timeline and the way back on
are one shape in one place. The list is drawn while either half of it has
rows, so removing the last track is exactly when the way back is on
screen rather than the moment the section disappears.

The audio side of the same class was already reversible: a lane removal
is a history snapshot and z restores it. A library row is not, by the
decision recorded in `Project::remove_source`, and is left alone.

The app half of this slice (`Player::restore_subtitle_track`, the notice
and its test) is in 4420513: the theme work committed the whole tree
while both were in flight.
crates/app/src/ui/timeline.rsmodified+101 -3
@@ -333,7 +333,11 @@ impl Player {
333333 cx: &mut Context<Self>,
334334 ) -> Option<impl IntoElement> {
335335 let tracks = self.session.as_ref()?.subtitles();
336- if tracks.is_empty() {
336+ // The rows a removal left behind: still a section, because a list that
337+ // has just been emptied is exactly when the way back has to be on
338+ // screen.
339+ let gone = self.session.as_ref()?.removed_subtitles();
340+ if tracks.is_empty() && gone.is_empty() {
337341 return None;
338342 }
339343 let groups = subtitle_rows(tracks);
@@ -385,7 +389,7 @@ impl Player {
385389 // Named, because a × is the same glyph on every row and
386390 // the tooltip is what says which track it takes off.
387391 let remove_tip: SharedString =
388- format!("Remove {title} — importing the file again brings it back")
392+ format!("Remove {title} — it drops to the list below, one click back")
389393 .into();
390394 div()
391395 // The *flat* index into the session's add-order
@@ -537,6 +541,99 @@ impl Player {
537541 .children(rows)
538542 })
539543 .collect();
544+ // What a removal left behind, under the live rows and dimmed: the same
545+ // row shape with a `+` where the × was, so the way off the timeline and
546+ // the way back on are one thing in one place. The cues are still in
547+ // hand (`engine::PlaybackSession::removed_subtitles`), so this is a
548+ // click and not a re-read of a 25 GB file.
549+ let back = (!gone.is_empty()).then(|| {
550+ let rows: Vec<_> = (0..gone.len())
551+ .map(|bin| {
552+ let name = sub_pick_name(gone, bin).unwrap_or_else(|| format!("track {bin}"));
553+ let tint = file_tint(self.sources(), &gone[bin].path);
554+ let detail = subtitle_detail(&gone[bin]);
555+ let tip: SharedString =
556+ format!("Bring {name} back — the cues are still here, nothing is re-read")
557+ .into();
558+ let back_tip = tip.clone();
559+ div()
560+ .id(("subtitle-restore", bin))
561+ .flex_none()
562+ .h(px(ROW_H))
563+ .flex()
564+ .items_center()
565+ .gap(px(6.))
566+ .pr(px(6.))
567+ .rounded(px(3.))
568+ .text_color(rgb(FG_SECONDARY()))
569+ .cursor_pointer()
570+ .hover(|s| s.bg(rgb(BG_HOVER())))
571+ .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into())
572+ .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
573+ this.restore_subtitle_track(bin, cx);
574+ }))
575+ .child(
576+ div()
577+ .flex_none()
578+ .w(px(SWATCH_W))
579+ .h_full()
580+ .rounded(px(2.))
581+ .opacity(0.55)
582+ .when_some(tint, |d, tint| d.bg(rgb(tint))),
583+ )
584+ .child(
585+ div()
586+ .flex_1()
587+ .min_w(px(0.))
588+ .flex()
589+ .flex_col()
590+ .child(div().truncate().text_size(px(11.)).child(name))
591+ .child(div().truncate().text_size(px(10.)).child(detail)),
592+ )
593+ // The × 's own twin: a `HIT_MIN` target at the same
594+ // edge, and it stops the click there for the same
595+ // reason the × does -- the row under it does the same
596+ // thing, and a doubled restore would be a second
597+ // notice about a row that is already back.
598+ .child(
599+ div()
600+ .id(("subtitle-restore-button", bin))
601+ .flex_none()
602+ .w(px(HIT_MIN))
603+ .h_full()
604+ .flex()
605+ .items_center()
606+ .justify_center()
607+ .rounded(px(3.))
608+ .cursor_pointer()
609+ .hover(|s| s.bg(rgb(BG_HOVER())))
610+ .tooltip(move |_, cx| cx.new(|_| Tip(back_tip.clone())).into())
611+ .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| {
612+ cx.stop_propagation();
613+ this.restore_subtitle_track(bin, cx);
614+ }))
615+ .child("+"),
616+ )
617+ })
618+ .collect();
619+ div()
620+ .flex_none()
621+ .flex()
622+ .flex_col()
623+ .gap(px(2.))
624+ .child(
625+ div()
626+ .flex_none()
627+ .h(px(SUB_HEAD_H))
628+ .flex()
629+ .items_center()
630+ .truncate()
631+ .text_size(px(10.))
632+ .text_color(rgb(FG_SECONDARY()))
633+ .child("Removed — click to bring one back"),
634+ )
635+ .children(rows)
636+ });
540637 Some(
541638 div()
542639 .flex_none()
@@ -568,7 +665,8 @@ impl Player {
568665 .gap(px(2.))
569666 .max_h(px(SUB_ROWS_H))
570667 .overflow_y_scroll()
571- .children(rows),
668+ .children(rows)
669+ .children(back),
572670 ),
573671 )
574672 }
crates/engine/src/playback.rsmodified+17 -2
@@ -1066,12 +1066,27 @@ impl PlaybackSession {
10661066 /// unlike [`remove_source`](Self::remove_source) this does not reseek. Rows
10671067 /// past `idx` move down by one: a caller holding a picked row (the export's
10681068 /// subtitle pick) has to fix it up or drop it. Not an undo step, for the
1069- /// reason [`Project::remove_subtitles`] gives -- the inverse is a
1070- /// re-import.
1069+ /// reason [`Project::remove_subtitles`] gives -- and not a loss either: the
1070+ /// track keeps its cues in
1071+ /// [`removed_subtitles`](Self::removed_subtitles) and
1072+ /// [`restore_subtitles`](Self::restore_subtitles) puts it back.
10711073 pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> {
10721074 self.project.remove_subtitles(idx)
10731075 }
10741076
1077+ /// The tracks removed from this timeline, oldest first: what a front-end
1078+ /// lists as the way back ([`Project::removed_subtitles`]).
1079+ pub fn removed_subtitles(&self) -> &[crate::subtitle::SubtitleTrack] {
1080+ self.project.removed_subtitles()
1081+ }
1082+
1083+ /// Brings the `idx`th removed track back, cues and all, and hands back
1084+ /// which row it is now ([`Project::restore_subtitles`]). Nothing is read
1085+ /// off disk: this is the removal undone, not the import repeated.
1086+ pub fn restore_subtitles(&mut self, idx: usize) -> crate::Result<usize> {
1087+ self.project.restore_subtitles(idx)
1088+ }
1089+
10751090 /// Writes the timeline to `path` as a `.edith`, atomically (see
10761091 /// [`crate::edith`]). Sources no clip plays from are left out, and the
10771092 /// playhead is saved with it so a reopened project resumes where it stood.
crates/engine/src/project.rsmodified+59 -6
@@ -650,6 +650,17 @@ pub struct Project {
650650 /// it, and it is saved as a *reference* to the file the cues came out of
651651 /// ([`crate::subtitle`]).
652652 subtitles: Vec<SubtitleTrack>,
653+ /// What [`Project::remove_subtitles`] took off, oldest first, cues and all
654+ /// -- the bin [`Project::restore_subtitles`] puts one back from. A removal
655+ /// keeps the track rather than dropping it because the cues cost a walk of
656+ /// the whole container to read (210 ms on a 25 GB remux) and re-reading the
657+ /// file is the one thing a person who removed a row by mistake should not
658+ /// have to do.
659+ ///
660+ /// Not saved and not on the history's snapshots: a `.edith` is what the
661+ /// timeline *has* ([`crate::edith`]), so the bin is the session's, exactly
662+ /// as a clipboard is.
663+ removed_subs: Vec<SubtitleTrack>,
653664 /// The master limiter every lane's sound is summed *through*
654665 /// ([`Project::limiter`]). Off by default, and not in the lane list, which
655666 /// is what the history snapshots hold: it is a setting on the mix, like the
@@ -689,6 +700,7 @@ impl Project {
689700 history: Vec::new(),
690701 next_link: 1,
691702 subtitles: Vec::new(),
703+ removed_subs: Vec::new(),
692704 limiter: Limiter::default(),
693705 tone: crate::tonemap::Preset::default(),
694706 }
@@ -799,6 +811,7 @@ impl Project {
799811 history: Vec::new(),
800812 next_link,
801813 subtitles: Vec::new(),
814+ removed_subs: Vec::new(),
802815 limiter: Limiter::default(),
803816 tone: crate::tonemap::Preset::default(),
804817 })
@@ -1237,19 +1250,56 @@ impl Project {
12371250 /// [`crate::export::ExportSettings::subtitles`]) has to fix it up or drop
12381251 /// it, or it names a different track afterwards.
12391252 ///
1240- /// ponytail: not an undo step, for the reason the limiter is not -- the
1241- /// history snapshots hold the lane list and subtitles are not on it
1242- /// ([`Project::subtitles`]). The inverse is a re-import, not a ctrl+z, and
1243- /// the upgrade path is the same one: a history entry holding the project's
1244- /// own settings beside the lanes.
1253+ /// Not an undo step, for the reason the limiter is not -- the history
1254+ /// snapshots hold the lane list and subtitles are not on it
1255+ /// ([`Project::subtitles`]). The way back is
1256+ /// [`restore_subtitles`](Project::restore_subtitles) and not a re-import:
1257+ /// the track goes to [`removed_subtitles`](Project::removed_subtitles) with
1258+ /// its cues, so nothing that was read has to be read again.
12451259 pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> {
12461260 if idx >= self.subtitles.len() {
12471261 return Err(format!("there is no subtitle track {idx} to remove").into());
12481262 }
1249- self.subtitles.remove(idx);
1263+ self.removed_subs.push(self.subtitles.remove(idx));
12501264 Ok(())
12511265 }
12521266
1267+ /// The tracks removed so far, oldest first: what a front-end lists beside
1268+ /// the live ones so a removal can be taken back
1269+ /// ([`restore_subtitles`](Project::restore_subtitles) indexes into this).
1270+ pub fn removed_subtitles(&self) -> &[SubtitleTrack] {
1271+ &self.removed_subs
1272+ }
1273+
1274+ /// Puts the `idx`th removed track back on the timeline, cues and all, and
1275+ /// hands back which row it is now. The inverse of
1276+ /// [`remove_subtitles`](Project::remove_subtitles) and the same thing a
1277+ /// re-import of that file would have added -- minus the walk of the
1278+ /// container, which is the whole point.
1279+ ///
1280+ /// Refused by name for a bin this project does not have that many entries
1281+ /// in, exactly as a bad removal index is: a front-end holding a stale index
1282+ /// has picked the wrong row and needs to hear it.
1283+ ///
1284+ /// ponytail: it lands at the *end* of the list rather than where it was,
1285+ /// which is where a re-import would have put it too. What that costs is the
1286+ /// row order after a restore; the upgrade path is remembering the index
1287+ /// beside the track in the bin and inserting there.
1288+ pub fn restore_subtitles(&mut self, idx: usize) -> crate::Result<usize> {
1289+ if idx >= self.removed_subs.len() {
1290+ return Err(format!("there is no removed subtitle track {idx} to bring back").into());
1291+ }
1292+ let track = self.removed_subs.remove(idx);
1293+ // `false` when the very same (file, track) was imported again while it
1294+ // sat in the bin: it is already back, and the row it is already on is
1295+ // the answer -- which the lookup below gives either way.
1296+ self.add_subtitles(&track);
1297+ self.subtitles
1298+ .iter()
1299+ .position(|t| t.path == track.path && t.track == track.track)
1300+ .ok_or_else(|| format!("{} did not go back on the timeline", track.label).into())
1301+ }
1302+
12531303 /// What each of [`audio_segments_from`](Project::audio_segments_from)'s
12541304 /// lanes is multiplied by on its way into the mix: the same lanes in the
12551305 /// same order, as *amplitudes*, so the mixer never sees a decibel. `1.0`
@@ -2738,6 +2788,9 @@ impl Project {
27382788 history: Vec::new(),
27392789 next_link: self.next_link,
27402790 subtitles: self.subtitles.clone(),
2791+ // An export renders what is on the timeline: the bin is a way back
2792+ // for the window, and no render reads it.
2793+ removed_subs: Vec::new(),
27412794 limiter: self.limiter,
27422795 tone: self.tone,
27432796 }
crates/engine/tests/subtitles.rsmodified+76 -0
@@ -412,6 +412,82 @@ fn a_subtitle_row_can_be_removed_and_the_survivors_round_trip() {
412412 assert_eq!(session.subtitles().len(), 2, "and nothing moved");
413413 }
414414
415+/// The removal's inverse, which is the whole complaint: a row taken off is *in
416+/// hand*, not gone, and one call puts it back with the cues it left with. The
417+/// proof that nothing is re-read is the file itself -- it is deleted from disk
418+/// before the restore, and the cues still come back whole. A re-import at that
419+/// point could only have refused.
420+#[test]
421+fn a_removed_subtitle_row_comes_back_without_a_reimport() {
422+ let dir = scratch("restore");
423+ let media = dir.join("test_av.mp4");
424+ std::fs::copy(asset("test_av.mp4"), &media).expect("copy the media fixture");
425+ for name in ["a.srt", "b.srt"] {
426+ std::fs::copy(data("test_subs.srt"), dir.join(name)).expect("copy the subtitle fixture");
427+ }
428+
429+ let mut session = engine::PlaybackSession::open(&media).expect("open the fixture");
430+ session.set_gain(0.0);
431+ for name in ["a.srt", "b.srt"] {
432+ session
433+ .import_subtitles(&dir.join(name))
434+ .expect("the .srt imports");
435+ }
436+ assert!(session.removed_subtitles().is_empty(), "nothing removed yet");
437+
438+ session.remove_subtitles(0).expect("the first row goes");
439+ assert_eq!(session.subtitles().len(), 1, "one row left on the timeline");
440+ let bin: Vec<&str> = session
441+ .removed_subtitles()
442+ .iter()
443+ .map(|t| t.label.as_str())
444+ .collect();
445+ assert_eq!(bin, vec!["a.srt"], "and the removed one is listed as removed");
446+ assert_eq!(
447+ session.removed_subtitles()[0].cues,
448+ expected(),
449+ "with its cues, so bringing it back costs no walk of the file"
450+ );
451+
452+ // The file is gone: a re-import is not even possible from here, which is
453+ // exactly the state a person who removed the wrong row must not be in.
454+ std::fs::remove_file(dir.join("a.srt")).expect("delete the source file");
455+
456+ let track = session.restore_subtitles(0).expect("the row comes back");
457+ assert_eq!(track, 1, "back on the timeline, where a re-import would land");
458+ assert!(
459+ session.removed_subtitles().is_empty(),
460+ "and out of the removed list"
461+ );
462+ let names: Vec<String> = session
463+ .subtitles()
464+ .iter()
465+ .map(|t| t.label.clone())
466+ .collect();
467+ assert_eq!(names, vec!["b.srt", "a.srt"]);
468+ assert_eq!(
469+ session.subtitles()[track].cues,
470+ expected(),
471+ "cues and all, off a file that is not there any more"
472+ );
473+
474+ // A save writes it like any other row, and a load reads it back -- the
475+ // restore is a track on the timeline, not a session-only ghost. (Its file
476+ // is gone, so the load lists it refused by name, which is what a missing
477+ // subtitle has always been.)
478+ let project = dir.join("back.edith");
479+ session.save_project(&project).expect("save");
480+ let text = std::fs::read_to_string(&project).expect("read the project back");
481+ assert!(text.contains("\nsubtitle - a.srt\n"), "{text}");
482+
483+ // A bin index nobody has says so, naming it, exactly as a bad removal does.
484+ let why = session
485+ .restore_subtitles(0)
486+ .expect_err("the bin is empty")
487+ .to_string();
488+ assert!(why.contains('0') && why.contains("subtitle"), "{why}");
489+}
490+
415491 /// A bitmap track survives a save the same way a text one does, and for the
416492 /// same reason: a `.edith` holds the file and the track number, never a cue, so
417493 /// what comes back is what the file still says. The pictures are read out of