A removed track is one click from being back
Browse files at this commitf41b4c601a91cfc473d13f9a6735d8abfd44fac3Parent: 4ddba0c- feat
- a removed track is one click from being back
4 changed files +253 -11
- crates/app/src/ui/timeline.rs +101 -3
- crates/engine/src/playback.rs +17 -2
- crates/engine/src/project.rs +59 -6
- crates/engine/tests/subtitles.rs +76 -0
crates/app/src/ui/timeline.rsmodified+101 -3
| @@ -333,7 +333,11 @@ impl Player { | |||
| 333 | cx: &mut Context<Self>, | 333 | cx: &mut Context<Self>, |
| 334 | ) -> Option<impl IntoElement> { | 334 | ) -> Option<impl IntoElement> { |
| 335 | let tracks = self.session.as_ref()?.subtitles(); | 335 | 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() { | ||
| 337 | return None; | 341 | return None; |
| 338 | } | 342 | } |
| 339 | let groups = subtitle_rows(tracks); | 343 | let groups = subtitle_rows(tracks); |
| @@ -385,7 +389,7 @@ impl Player { | |||
| 385 | // Named, because a × is the same glyph on every row and | 389 | // Named, because a × is the same glyph on every row and |
| 386 | // the tooltip is what says which track it takes off. | 390 | // the tooltip is what says which track it takes off. |
| 387 | let remove_tip: SharedString = | 391 | 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") |
| 389 | .into(); | 393 | .into(); |
| 390 | div() | 394 | div() |
| 391 | // The *flat* index into the session's add-order | 395 | // The *flat* index into the session's add-order |
| @@ -537,6 +541,99 @@ impl Player { | |||
| 537 | .children(rows) | 541 | .children(rows) |
| 538 | }) | 542 | }) |
| 539 | .collect(); | 543 | .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 | + }); | ||
| 540 | Some( | 637 | Some( |
| 541 | div() | 638 | div() |
| 542 | .flex_none() | 639 | .flex_none() |
| @@ -568,7 +665,8 @@ impl Player { | |||
| 568 | .gap(px(2.)) | 665 | .gap(px(2.)) |
| 569 | .max_h(px(SUB_ROWS_H)) | 666 | .max_h(px(SUB_ROWS_H)) |
| 570 | .overflow_y_scroll() | 667 | .overflow_y_scroll() |
| 571 | - .children(rows), | 668 | + .children(rows) |
| 669 | + .children(back), | ||
| 572 | ), | 670 | ), |
| 573 | ) | 671 | ) |
| 574 | } | 672 | } |
crates/engine/src/playback.rsmodified+17 -2
| @@ -1066,12 +1066,27 @@ impl PlaybackSession { | |||
| 1066 | /// unlike [`remove_source`](Self::remove_source) this does not reseek. Rows | 1066 | /// unlike [`remove_source`](Self::remove_source) this does not reseek. Rows |
| 1067 | /// past `idx` move down by one: a caller holding a picked row (the export's | 1067 | /// past `idx` move down by one: a caller holding a picked row (the export's |
| 1068 | /// subtitle pick) has to fix it up or drop it. Not an undo step, for the | 1068 | /// 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 | 1069 | + /// reason [`Project::remove_subtitles`] gives -- and not a loss either: the |
| 1070 | - /// re-import. | 1070 | + /// track keeps its cues in |
| 1071 | + /// [`removed_subtitles`](Self::removed_subtitles) and | ||
| 1072 | + /// [`restore_subtitles`](Self::restore_subtitles) puts it back. | ||
| 1071 | pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> { | 1073 | pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> { |
| 1072 | self.project.remove_subtitles(idx) | 1074 | self.project.remove_subtitles(idx) |
| 1073 | } | 1075 | } |
| 1074 | 1076 | ||
| 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 | + | ||
| 1075 | /// Writes the timeline to `path` as a `.edith`, atomically (see | 1090 | /// Writes the timeline to `path` as a `.edith`, atomically (see |
| 1076 | /// [`crate::edith`]). Sources no clip plays from are left out, and the | 1091 | /// [`crate::edith`]). Sources no clip plays from are left out, and the |
| 1077 | /// playhead is saved with it so a reopened project resumes where it stood. | 1092 | /// 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 { | |||
| 650 | /// it, and it is saved as a *reference* to the file the cues came out of | 650 | /// it, and it is saved as a *reference* to the file the cues came out of |
| 651 | /// ([`crate::subtitle`]). | 651 | /// ([`crate::subtitle`]). |
| 652 | subtitles: Vec<SubtitleTrack>, | 652 | 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>, | ||
| 653 | /// The master limiter every lane's sound is summed *through* | 664 | /// The master limiter every lane's sound is summed *through* |
| 654 | /// ([`Project::limiter`]). Off by default, and not in the lane list, which | 665 | /// ([`Project::limiter`]). Off by default, and not in the lane list, which |
| 655 | /// is what the history snapshots hold: it is a setting on the mix, like the | 666 | /// is what the history snapshots hold: it is a setting on the mix, like the |
| @@ -689,6 +700,7 @@ impl Project { | |||
| 689 | history: Vec::new(), | 700 | history: Vec::new(), |
| 690 | next_link: 1, | 701 | next_link: 1, |
| 691 | subtitles: Vec::new(), | 702 | subtitles: Vec::new(), |
| 703 | + removed_subs: Vec::new(), | ||
| 692 | limiter: Limiter::default(), | 704 | limiter: Limiter::default(), |
| 693 | tone: crate::tonemap::Preset::default(), | 705 | tone: crate::tonemap::Preset::default(), |
| 694 | } | 706 | } |
| @@ -799,6 +811,7 @@ impl Project { | |||
| 799 | history: Vec::new(), | 811 | history: Vec::new(), |
| 800 | next_link, | 812 | next_link, |
| 801 | subtitles: Vec::new(), | 813 | subtitles: Vec::new(), |
| 814 | + removed_subs: Vec::new(), | ||
| 802 | limiter: Limiter::default(), | 815 | limiter: Limiter::default(), |
| 803 | tone: crate::tonemap::Preset::default(), | 816 | tone: crate::tonemap::Preset::default(), |
| 804 | }) | 817 | }) |
| @@ -1237,19 +1250,56 @@ impl Project { | |||
| 1237 | /// [`crate::export::ExportSettings::subtitles`]) has to fix it up or drop | 1250 | /// [`crate::export::ExportSettings::subtitles`]) has to fix it up or drop |
| 1238 | /// it, or it names a different track afterwards. | 1251 | /// it, or it names a different track afterwards. |
| 1239 | /// | 1252 | /// |
| 1240 | - /// ponytail: not an undo step, for the reason the limiter is not -- the | 1253 | + /// Not an undo step, for the reason the limiter is not -- the history |
| 1241 | - /// history snapshots hold the lane list and subtitles are not on it | 1254 | + /// 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 | 1255 | + /// ([`Project::subtitles`]). The way back is |
| 1243 | - /// the upgrade path is the same one: a history entry holding the project's | 1256 | + /// [`restore_subtitles`](Project::restore_subtitles) and not a re-import: |
| 1244 | - /// own settings beside the lanes. | 1257 | + /// the track goes to [`removed_subtitles`](Project::removed_subtitles) with |
| 1258 | + /// its cues, so nothing that was read has to be read again. | ||
| 1245 | pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> { | 1259 | pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> { |
| 1246 | if idx >= self.subtitles.len() { | 1260 | if idx >= self.subtitles.len() { |
| 1247 | return Err(format!("there is no subtitle track {idx} to remove").into()); | 1261 | return Err(format!("there is no subtitle track {idx} to remove").into()); |
| 1248 | } | 1262 | } |
| 1249 | - self.subtitles.remove(idx); | 1263 | + self.removed_subs.push(self.subtitles.remove(idx)); |
| 1250 | Ok(()) | 1264 | Ok(()) |
| 1251 | } | 1265 | } |
| 1252 | 1266 | ||
| 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 | + | ||
| 1253 | /// What each of [`audio_segments_from`](Project::audio_segments_from)'s | 1303 | /// What each of [`audio_segments_from`](Project::audio_segments_from)'s |
| 1254 | /// lanes is multiplied by on its way into the mix: the same lanes in the | 1304 | /// lanes is multiplied by on its way into the mix: the same lanes in the |
| 1255 | /// same order, as *amplitudes*, so the mixer never sees a decibel. `1.0` | 1305 | /// same order, as *amplitudes*, so the mixer never sees a decibel. `1.0` |
| @@ -2738,6 +2788,9 @@ impl Project { | |||
| 2738 | history: Vec::new(), | 2788 | history: Vec::new(), |
| 2739 | next_link: self.next_link, | 2789 | next_link: self.next_link, |
| 2740 | subtitles: self.subtitles.clone(), | 2790 | 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(), | ||
| 2741 | limiter: self.limiter, | 2794 | limiter: self.limiter, |
| 2742 | tone: self.tone, | 2795 | tone: self.tone, |
| 2743 | } | 2796 | } |
crates/engine/tests/subtitles.rsmodified+76 -0
| @@ -412,6 +412,82 @@ fn a_subtitle_row_can_be_removed_and_the_survivors_round_trip() { | |||
| 412 | assert_eq!(session.subtitles().len(), 2, "and nothing moved"); | 412 | assert_eq!(session.subtitles().len(), 2, "and nothing moved"); |
| 413 | } | 413 | } |
| 414 | 414 | ||
| 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 | + | ||
| 415 | /// A bitmap track survives a save the same way a text one does, and for the | 491 | /// A bitmap track survives a save the same way a text one does, and for the |
| 416 | /// same reason: a `.edith` holds the file and the track number, never a cue, so | 492 | /// same reason: a `.edith` holds the file and the track number, never a cue, so |
| 417 | /// what comes back is what the file still says. The pictures are read out of | 493 | /// what comes back is what the file still says. The pictures are read out of |