diff --git a/crates/app/src/ui/timeline.rs b/crates/app/src/ui/timeline.rs index 2e1d44e..1d635d9 100644 --- a/crates/app/src/ui/timeline.rs +++ b/crates/app/src/ui/timeline.rs @@ -333,7 +333,11 @@ impl Player { cx: &mut Context, ) -> Option { let tracks = self.session.as_ref()?.subtitles(); - if tracks.is_empty() { + // The rows a removal left behind: still a section, because a list that + // has just been emptied is exactly when the way back has to be on + // screen. + let gone = self.session.as_ref()?.removed_subtitles(); + if tracks.is_empty() && gone.is_empty() { return None; } let groups = subtitle_rows(tracks); @@ -385,7 +389,7 @@ impl Player { // Named, because a × is the same glyph on every row and // the tooltip is what says which track it takes off. let remove_tip: SharedString = - format!("Remove {title} — importing the file again brings it back") + format!("Remove {title} — it drops to the list below, one click back") .into(); div() // The *flat* index into the session's add-order @@ -537,6 +541,99 @@ impl Player { .children(rows) }) .collect(); + // What a removal left behind, under the live rows and dimmed: the same + // row shape with a `+` where the × was, so the way off the timeline and + // the way back on are one thing in one place. The cues are still in + // hand (`engine::PlaybackSession::removed_subtitles`), so this is a + // click and not a re-read of a 25 GB file. + let back = (!gone.is_empty()).then(|| { + let rows: Vec<_> = (0..gone.len()) + .map(|bin| { + let name = sub_pick_name(gone, bin).unwrap_or_else(|| format!("track {bin}")); + let tint = file_tint(self.sources(), &gone[bin].path); + let detail = subtitle_detail(&gone[bin]); + let tip: SharedString = + format!("Bring {name} back — the cues are still here, nothing is re-read") + .into(); + let back_tip = tip.clone(); + div() + .id(("subtitle-restore", bin)) + .flex_none() + .h(px(ROW_H)) + .flex() + .items_center() + .gap(px(6.)) + .pr(px(6.)) + .rounded(px(3.)) + .text_color(rgb(FG_SECONDARY())) + .cursor_pointer() + .hover(|s| s.bg(rgb(BG_HOVER()))) + .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into()) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.restore_subtitle_track(bin, cx); + })) + .child( + div() + .flex_none() + .w(px(SWATCH_W)) + .h_full() + .rounded(px(2.)) + .opacity(0.55) + .when_some(tint, |d, tint| d.bg(rgb(tint))), + ) + .child( + div() + .flex_1() + .min_w(px(0.)) + .flex() + .flex_col() + .child(div().truncate().text_size(px(11.)).child(name)) + .child(div().truncate().text_size(px(10.)).child(detail)), + ) + // The × 's own twin: a `HIT_MIN` target at the same + // edge, and it stops the click there for the same + // reason the × does -- the row under it does the same + // thing, and a doubled restore would be a second + // notice about a row that is already back. + .child( + div() + .id(("subtitle-restore-button", bin)) + .flex_none() + .w(px(HIT_MIN)) + .h_full() + .flex() + .items_center() + .justify_center() + .rounded(px(3.)) + .cursor_pointer() + .hover(|s| s.bg(rgb(BG_HOVER()))) + .tooltip(move |_, cx| cx.new(|_| Tip(back_tip.clone())).into()) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + cx.stop_propagation(); + this.restore_subtitle_track(bin, cx); + })) + .child("+"), + ) + }) + .collect(); + div() + .flex_none() + .flex() + .flex_col() + .gap(px(2.)) + .child( + div() + .flex_none() + .h(px(SUB_HEAD_H)) + .flex() + .items_center() + .truncate() + .text_size(px(10.)) + .text_color(rgb(FG_SECONDARY())) + .child("Removed — click to bring one back"), + ) + .children(rows) + }); Some( div() .flex_none() @@ -568,7 +665,8 @@ impl Player { .gap(px(2.)) .max_h(px(SUB_ROWS_H)) .overflow_y_scroll() - .children(rows), + .children(rows) + .children(back), ), ) } diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 8df37d0..c82d64f 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -1066,12 +1066,27 @@ impl PlaybackSession { /// unlike [`remove_source`](Self::remove_source) this does not reseek. Rows /// past `idx` move down by one: a caller holding a picked row (the export's /// subtitle pick) has to fix it up or drop it. Not an undo step, for the - /// reason [`Project::remove_subtitles`] gives -- the inverse is a - /// re-import. + /// reason [`Project::remove_subtitles`] gives -- and not a loss either: the + /// track keeps its cues in + /// [`removed_subtitles`](Self::removed_subtitles) and + /// [`restore_subtitles`](Self::restore_subtitles) puts it back. pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> { self.project.remove_subtitles(idx) } + /// The tracks removed from this timeline, oldest first: what a front-end + /// lists as the way back ([`Project::removed_subtitles`]). + pub fn removed_subtitles(&self) -> &[crate::subtitle::SubtitleTrack] { + self.project.removed_subtitles() + } + + /// Brings the `idx`th removed track back, cues and all, and hands back + /// which row it is now ([`Project::restore_subtitles`]). Nothing is read + /// off disk: this is the removal undone, not the import repeated. + pub fn restore_subtitles(&mut self, idx: usize) -> crate::Result { + self.project.restore_subtitles(idx) + } + /// Writes the timeline to `path` as a `.edith`, atomically (see /// [`crate::edith`]). Sources no clip plays from are left out, and the /// playhead is saved with it so a reopened project resumes where it stood. diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index e1cab53..9c3b2f6 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -650,6 +650,17 @@ pub struct Project { /// it, and it is saved as a *reference* to the file the cues came out of /// ([`crate::subtitle`]). subtitles: Vec, + /// What [`Project::remove_subtitles`] took off, oldest first, cues and all + /// -- the bin [`Project::restore_subtitles`] puts one back from. A removal + /// keeps the track rather than dropping it because the cues cost a walk of + /// the whole container to read (210 ms on a 25 GB remux) and re-reading the + /// file is the one thing a person who removed a row by mistake should not + /// have to do. + /// + /// Not saved and not on the history's snapshots: a `.edith` is what the + /// timeline *has* ([`crate::edith`]), so the bin is the session's, exactly + /// as a clipboard is. + removed_subs: Vec, /// The master limiter every lane's sound is summed *through* /// ([`Project::limiter`]). Off by default, and not in the lane list, which /// is what the history snapshots hold: it is a setting on the mix, like the @@ -689,6 +700,7 @@ impl Project { history: Vec::new(), next_link: 1, subtitles: Vec::new(), + removed_subs: Vec::new(), limiter: Limiter::default(), tone: crate::tonemap::Preset::default(), } @@ -799,6 +811,7 @@ impl Project { history: Vec::new(), next_link, subtitles: Vec::new(), + removed_subs: Vec::new(), limiter: Limiter::default(), tone: crate::tonemap::Preset::default(), }) @@ -1237,19 +1250,56 @@ impl Project { /// [`crate::export::ExportSettings::subtitles`]) has to fix it up or drop /// it, or it names a different track afterwards. /// - /// ponytail: not an undo step, for the reason the limiter is not -- the - /// history snapshots hold the lane list and subtitles are not on it - /// ([`Project::subtitles`]). The inverse is a re-import, not a ctrl+z, and - /// the upgrade path is the same one: a history entry holding the project's - /// own settings beside the lanes. + /// Not an undo step, for the reason the limiter is not -- the history + /// snapshots hold the lane list and subtitles are not on it + /// ([`Project::subtitles`]). The way back is + /// [`restore_subtitles`](Project::restore_subtitles) and not a re-import: + /// the track goes to [`removed_subtitles`](Project::removed_subtitles) with + /// its cues, so nothing that was read has to be read again. pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> { if idx >= self.subtitles.len() { return Err(format!("there is no subtitle track {idx} to remove").into()); } - self.subtitles.remove(idx); + self.removed_subs.push(self.subtitles.remove(idx)); Ok(()) } + /// The tracks removed so far, oldest first: what a front-end lists beside + /// the live ones so a removal can be taken back + /// ([`restore_subtitles`](Project::restore_subtitles) indexes into this). + pub fn removed_subtitles(&self) -> &[SubtitleTrack] { + &self.removed_subs + } + + /// Puts the `idx`th removed track back on the timeline, cues and all, and + /// hands back which row it is now. The inverse of + /// [`remove_subtitles`](Project::remove_subtitles) and the same thing a + /// re-import of that file would have added -- minus the walk of the + /// container, which is the whole point. + /// + /// Refused by name for a bin this project does not have that many entries + /// in, exactly as a bad removal index is: a front-end holding a stale index + /// has picked the wrong row and needs to hear it. + /// + /// ponytail: it lands at the *end* of the list rather than where it was, + /// which is where a re-import would have put it too. What that costs is the + /// row order after a restore; the upgrade path is remembering the index + /// beside the track in the bin and inserting there. + pub fn restore_subtitles(&mut self, idx: usize) -> crate::Result { + if idx >= self.removed_subs.len() { + return Err(format!("there is no removed subtitle track {idx} to bring back").into()); + } + let track = self.removed_subs.remove(idx); + // `false` when the very same (file, track) was imported again while it + // sat in the bin: it is already back, and the row it is already on is + // the answer -- which the lookup below gives either way. + self.add_subtitles(&track); + self.subtitles + .iter() + .position(|t| t.path == track.path && t.track == track.track) + .ok_or_else(|| format!("{} did not go back on the timeline", track.label).into()) + } + /// What each of [`audio_segments_from`](Project::audio_segments_from)'s /// lanes is multiplied by on its way into the mix: the same lanes in the /// same order, as *amplitudes*, so the mixer never sees a decibel. `1.0` @@ -2738,6 +2788,9 @@ impl Project { history: Vec::new(), next_link: self.next_link, subtitles: self.subtitles.clone(), + // An export renders what is on the timeline: the bin is a way back + // for the window, and no render reads it. + removed_subs: Vec::new(), limiter: self.limiter, tone: self.tone, } diff --git a/crates/engine/tests/subtitles.rs b/crates/engine/tests/subtitles.rs index 02a80c4..49e9300 100644 --- a/crates/engine/tests/subtitles.rs +++ b/crates/engine/tests/subtitles.rs @@ -412,6 +412,82 @@ fn a_subtitle_row_can_be_removed_and_the_survivors_round_trip() { assert_eq!(session.subtitles().len(), 2, "and nothing moved"); } +/// The removal's inverse, which is the whole complaint: a row taken off is *in +/// hand*, not gone, and one call puts it back with the cues it left with. The +/// proof that nothing is re-read is the file itself -- it is deleted from disk +/// before the restore, and the cues still come back whole. A re-import at that +/// point could only have refused. +#[test] +fn a_removed_subtitle_row_comes_back_without_a_reimport() { + let dir = scratch("restore"); + let media = dir.join("test_av.mp4"); + std::fs::copy(asset("test_av.mp4"), &media).expect("copy the media fixture"); + for name in ["a.srt", "b.srt"] { + std::fs::copy(data("test_subs.srt"), dir.join(name)).expect("copy the subtitle fixture"); + } + + let mut session = engine::PlaybackSession::open(&media).expect("open the fixture"); + session.set_gain(0.0); + for name in ["a.srt", "b.srt"] { + session + .import_subtitles(&dir.join(name)) + .expect("the .srt imports"); + } + assert!(session.removed_subtitles().is_empty(), "nothing removed yet"); + + session.remove_subtitles(0).expect("the first row goes"); + assert_eq!(session.subtitles().len(), 1, "one row left on the timeline"); + let bin: Vec<&str> = session + .removed_subtitles() + .iter() + .map(|t| t.label.as_str()) + .collect(); + assert_eq!(bin, vec!["a.srt"], "and the removed one is listed as removed"); + assert_eq!( + session.removed_subtitles()[0].cues, + expected(), + "with its cues, so bringing it back costs no walk of the file" + ); + + // The file is gone: a re-import is not even possible from here, which is + // exactly the state a person who removed the wrong row must not be in. + std::fs::remove_file(dir.join("a.srt")).expect("delete the source file"); + + let track = session.restore_subtitles(0).expect("the row comes back"); + assert_eq!(track, 1, "back on the timeline, where a re-import would land"); + assert!( + session.removed_subtitles().is_empty(), + "and out of the removed list" + ); + let names: Vec = session + .subtitles() + .iter() + .map(|t| t.label.clone()) + .collect(); + assert_eq!(names, vec!["b.srt", "a.srt"]); + assert_eq!( + session.subtitles()[track].cues, + expected(), + "cues and all, off a file that is not there any more" + ); + + // A save writes it like any other row, and a load reads it back -- the + // restore is a track on the timeline, not a session-only ghost. (Its file + // is gone, so the load lists it refused by name, which is what a missing + // subtitle has always been.) + let project = dir.join("back.edith"); + session.save_project(&project).expect("save"); + let text = std::fs::read_to_string(&project).expect("read the project back"); + assert!(text.contains("\nsubtitle - a.srt\n"), "{text}"); + + // A bin index nobody has says so, naming it, exactly as a bad removal does. + let why = session + .restore_subtitles(0) + .expect_err("the bin is empty") + .to_string(); + assert!(why.contains('0') && why.contains("subtitle"), "{why}"); +} + /// A bitmap track survives a save the same way a text one does, and for the /// same reason: a `.edith` holds the file and the track number, never a cue, so /// what comes back is what the file still says. The pictures are read out of