A file's subtitles come onto the timeline that is open
Browse files at this commite17cd0a1236072132576b1ccb59d7fd98b52dfefParent: 3c6b5d9- feat
- a file's subtitles come onto the timeline that is open
4 changed files +197 -12
- crates/app/src/main.rs +74 -4
- crates/app/src/ui/library.rs +41 -7
- crates/engine/src/playback.rs +22 -1
- crates/engine/tests/subtitles.rs +60 -0
crates/app/src/main.rsmodified+74 -4
| @@ -343,7 +343,9 @@ impl LibraryTab { | ||
| 343 | 343 | match self { |
| 344 | 344 | LibraryTab::Media => "No video or stills yet — Import, or drop a file on the window", |
| 345 | 345 | LibraryTab::Audio => "No sound yet — Import, or drop a file on the window", |
| 346 | - LibraryTab::Text => "No subtitles yet — drop an .srt on the window, or + S in the toolbar", | |
| 346 | + LibraryTab::Text => { | |
| 347 | + "No subtitles yet — Add subtitles from a file, or drop an .srt on the window" | |
| 348 | + } | |
| 347 | 349 | } |
| 348 | 350 | } |
| 349 | 351 | } |
| @@ -4808,7 +4810,7 @@ impl Player { | ||
| 4808 | 4810 | let added = this |
| 4809 | 4811 | .session |
| 4810 | 4812 | .as_mut() |
| 4811 | - .map(|session| parsed.map(|tracks| session.add_subtitle_tracks(tracks))); | |
| 4813 | + .map(|session| parsed.and_then(|tracks| pushed(session, &path, tracks))); | |
| 4812 | 4814 | this.landed_subtitles(&path, added, cx); |
| 4813 | 4815 | }) |
| 4814 | 4816 | .ok(); |
| @@ -4826,7 +4828,7 @@ impl Player { | ||
| 4826 | 4828 | let added = self |
| 4827 | 4829 | .session |
| 4828 | 4830 | .as_mut() |
| 4829 | - .map(|session| subs.map(|tracks| session.add_subtitle_tracks(tracks))); | |
| 4831 | + .map(|session| subs.and_then(|tracks| pushed(session, path, tracks))); | |
| 4830 | 4832 | self.landed_subtitles(path, added, cx); |
| 4831 | 4833 | } |
| 4832 | 4834 | |
| @@ -4842,7 +4844,10 @@ impl Player { | ||
| 4842 | 4844 | cx: &mut Context<Self>, |
| 4843 | 4845 | ) { |
| 4844 | 4846 | let text = match added { |
| 4845 | - Some(Ok(0)) => format!("{} is already on the timeline", file_name(path)), | |
| 4847 | + Some(Ok(0)) => format!( | |
| 4848 | + "{}'s subtitles are on the timeline already", | |
| 4849 | + file_name(path) | |
| 4850 | + ), | |
| 4846 | 4851 | Some(Ok(n)) => format!( |
| 4847 | 4852 | "SUBTITLES {} — {n} track(s), showing over the picture, {} hides them", |
| 4848 | 4853 | file_name(path), |
| @@ -6750,6 +6755,33 @@ fn subtitle_tail(session: &mut PlaybackSession, subs: Subs) -> Option<String> { | ||
| 6750 | 6755 | } |
| 6751 | 6756 | } |
| 6752 | 6757 | |
| 6758 | +/// The push both deliberate add-subtitles doors end on -- `+ S` and its key | |
| 6759 | +/// ([`Player::add_subtitles`]) and a dropped or argv'd subtitle file | |
| 6760 | +/// ([`Player::take_subtitles`]) -- so the two cannot come to word the same file | |
| 6761 | +/// differently. | |
| 6762 | +/// | |
| 6763 | +/// The walk having found *nothing* is a refusal and not a count: a container | |
| 6764 | +/// with no subtitle track in it and a file whose tracks are all on the timeline | |
| 6765 | +/// already both push zero rows, and they are opposite answers -- one says look | |
| 6766 | +/// somewhere else, the other says you already have them. The engine's own door | |
| 6767 | +/// draws the same line in the same words | |
| 6768 | +/// ([`PlaybackSession::no_subtitles_in`], asked here because this route splits | |
| 6769 | +/// the walk from the push to keep the walk off the render thread). | |
| 6770 | +/// | |
| 6771 | +/// The file itself joins nothing either way: no library row, no lane, no clip. | |
| 6772 | +/// Subtitles are a list the timeline carries, and this is the only thing that | |
| 6773 | +/// touches it. | |
| 6774 | +fn pushed( | |
| 6775 | + session: &mut PlaybackSession, | |
| 6776 | + path: &std::path::Path, | |
| 6777 | + tracks: Vec<engine::subtitle::SubtitleTrack>, | |
| 6778 | +) -> engine::Result<usize> { | |
| 6779 | + match tracks.is_empty() { | |
| 6780 | + true => Err(PlaybackSession::no_subtitles_in(path)), | |
| 6781 | + false => Ok(session.add_subtitle_tracks(tracks)), | |
| 6782 | + } | |
| 6783 | +} | |
| 6784 | + | |
| 6753 | 6785 | /// What a subtitle row says under its name: how many cues it holds and whether |
| 6754 | 6786 | /// they are pictures ([`engine::subtitle::SubtitleTrack::is_bitmap`]) -- which |
| 6755 | 6787 | /// is the difference between a track an export writes into the file and one it |
| @@ -15511,6 +15543,44 @@ mod tests { | ||
| 15511 | 15543 | assert_eq!(sub_pick_name(&[], 0), None); |
| 15512 | 15544 | } |
| 15513 | 15545 | |
| 15546 | + /// The door this editor answers "don't make me import the film again" with, | |
| 15547 | + /// and it is in the panel where the tracks it adds are listed: the Text | |
| 15548 | + /// tab's own button reads a file's subtitle tracks onto the open timeline | |
| 15549 | + /// -- a release's `.mkv`, an `.srt` beside it -- while the file itself | |
| 15550 | + /// joins nothing. It is the *action* and not a second implementation of it, | |
| 15551 | + /// so the button, the stroke and the actions card cannot drift apart, and | |
| 15552 | + /// it is oracle-gated like every other action button, so with no timeline | |
| 15553 | + /// open it dims and says why instead of opening a chooser for nothing. | |
| 15554 | + #[test] | |
| 15555 | + fn the_text_tab_carries_the_add_subtitles_door() { | |
| 15556 | + let library = include_str!("ui/library.rs"); | |
| 15557 | + let at = library | |
| 15558 | + .find("\"add-subtitles\"") | |
| 15559 | + .expect("no add-subtitles control in the library column"); | |
| 15560 | + let block = &library[at..(at + 1400).min(library.len())]; | |
| 15561 | + assert!( | |
| 15562 | + block.contains("ActionId::AddSubtitleTrack"), | |
| 15563 | + "the button is a door of its own rather than the action: {block}" | |
| 15564 | + ); | |
| 15565 | + assert!( | |
| 15566 | + block.contains("pick_and_add_subtitles"), | |
| 15567 | + "the button does not open the subtitle chooser: {block}" | |
| 15568 | + ); | |
| 15569 | + // `action_control` is the oracle-gated one: dimmed with the refusal in | |
| 15570 | + // the oracle's own words. `control` alone would be lit with no timeline. | |
| 15571 | + assert!( | |
| 15572 | + library[..at].ends_with("self.action_control(\n "), | |
| 15573 | + "the add-subtitles button is not oracle-gated" | |
| 15574 | + ); | |
| 15575 | + // ...and the empty tab names that button, rather than pointing at a | |
| 15576 | + // toolbar the person is not looking at. | |
| 15577 | + assert!( | |
| 15578 | + crate::LibraryTab::Text.empty().contains("Add subtitles"), | |
| 15579 | + "{}", | |
| 15580 | + crate::LibraryTab::Text.empty() | |
| 15581 | + ); | |
| 15582 | + } | |
| 15583 | + | |
| 15514 | 15584 | /// The one thing regrouping must not break: `sub_track` is a flat index |
| 15515 | 15585 | /// into the add-order list, a click sets it and a save writes it into the |
| 15516 | 15586 | /// `.edith`. Every row must still name the track it was made from -- rows |
crates/app/src/ui/library.rsmodified+41 -7
| @@ -280,13 +280,47 @@ impl Player { | ||
| 280 | 280 | ) |
| 281 | 281 | , |
| 282 | 282 | ) |
| 283 | - .child( | |
| 284 | - div().flex_none().child(control( | |
| 283 | + // The tab says what the one full-width control is for. Text holds | |
| 284 | + // no rows of its own ([`LibraryTab::holds`]) -- it is the tracks | |
| 285 | + // under it -- and what a person opens it to do is put subtitles on | |
| 286 | + // this timeline: off a release's `.mkv`, off an `.srt` beside it, | |
| 287 | + // without the file itself joining the library or a lane. So that is | |
| 288 | + // the button there, where the tracks it adds are listed; media goes | |
| 289 | + // on getting in from the other two tabs, from a drop, and from the | |
| 290 | + // actions card. | |
| 291 | + .when(self.library_tab == LibraryTab::Text, |d| { | |
| 292 | + d.child(div().flex_none().child(self.action_control( | |
| 293 | + "add-subtitles", | |
| 294 | + width - 16., | |
| 295 | + match self.session.as_ref().is_some_and(|s| !s.subtitles().is_empty()) { | |
| 296 | + // The primary action of this tab while it has nothing: | |
| 297 | + // the same rule the Import button follows one tab over. | |
| 298 | + false => ACCENT_PRIMARY(), | |
| 299 | + true => BG_RAISED(), | |
| 300 | + }, | |
| 301 | + None, | |
| 302 | + // The column is 128 px at the 640x360 floor and the whole | |
| 303 | + // name does not fit it: a centred label wider than its | |
| 304 | + // button is clipped at *both* ends ("d subtitles from a | |
| 305 | + // fil"), which is a button that says nothing. The short | |
| 306 | + // form there; the full name is what the tooltip, the | |
| 307 | + // actions card and the stroke go on saying either way. | |
| 308 | + match (width - 16.) / LIST_CHAR_W >= 26. { | |
| 309 | + true => "Add subtitles from a file…", | |
| 310 | + false => "Add subtitles", | |
| 311 | + }, | |
| 312 | + "reads a file's subtitle tracks onto this timeline — the file itself \ | |
| 313 | + joins nothing", | |
| 314 | + ActionId::AddSubtitleTrack, | |
| 315 | + cx.listener(|this, _: &ClickEvent, _, cx| this.pick_and_add_subtitles(cx)), | |
| 316 | + ))) | |
| 317 | + }) | |
| 318 | + .when(self.library_tab != LibraryTab::Text, |d| { | |
| 319 | + d.child(div().flex_none().child(control( | |
| 285 | 320 | "import", |
| 286 | 321 | // Full width of the column: the way media gets in is |
| 287 | - // the one affordance in this panel that is always | |
| 288 | - // there, whatever tab is open and however narrow the | |
| 289 | - // window is. | |
| 322 | + // the one affordance of the tabs that hold media, | |
| 323 | + // however narrow the window is. | |
| 290 | 324 | width - 16., |
| 291 | 325 | // Filled with the accent while there is nothing to work |
| 292 | 326 | // on, because with an empty library this *is* the |
| @@ -302,8 +336,8 @@ impl Player { | ||
| 302 | 336 | "adds a file to this list — or drop one on the window".to_string(), |
| 303 | 337 | !exporting, |
| 304 | 338 | cx.listener(|this, _: &ClickEvent, _, cx| this.pick_and_import(cx)), |
| 305 | - )), | |
| 306 | - ) | |
| 339 | + ))) | |
| 340 | + }) | |
| 307 | 341 | .child( |
| 308 | 342 | div() |
| 309 | 343 | .id("library-rows") |
crates/engine/src/playback.rsmodified+22 -1
| @@ -998,7 +998,18 @@ impl PlaybackSession { | ||
| 998 | 998 | /// [`crate::subtitle::open`] gives. Nothing else about the timeline moves: |
| 999 | 999 | /// subtitles are not clips and land on no lane. |
| 1000 | 1000 | pub fn import_subtitles(&mut self, path: &Path) -> crate::Result<usize> { |
| 1001 | - Ok(self.add_subtitle_tracks(Self::parse_subtitles(path)?)) | |
| 1001 | + let tracks = Self::parse_subtitles(path)?; | |
| 1002 | + // A file that was walked and carries none is refused as *that*, because | |
| 1003 | + // the alternative -- 0 added -- is the very sentence a file whose tracks | |
| 1004 | + // are already on this timeline gets, and the two are opposite answers a | |
| 1005 | + // person acts differently on. Said only after the walk actually looked | |
| 1006 | + // ([`crate::subtitle::of_media`]); a *file* that is not a container at | |
| 1007 | + // all never reaches here, [`parse_subtitles`](Self::parse_subtitles) | |
| 1008 | + // refuses it in its own words. | |
| 1009 | + if tracks.is_empty() { | |
| 1010 | + return Err(Self::no_subtitles_in(path)); | |
| 1011 | + } | |
| 1012 | + Ok(self.add_subtitle_tracks(tracks)) | |
| 1002 | 1013 | } |
| 1003 | 1014 | |
| 1004 | 1015 | /// The reading half of [`import_subtitles`](Self::import_subtitles), with no |
| @@ -1039,6 +1050,16 @@ impl PlaybackSession { | ||
| 1039 | 1050 | Ok(tracks) |
| 1040 | 1051 | } |
| 1041 | 1052 | |
| 1053 | + /// What a file the walk found no subtitle track in is refused with, | |
| 1054 | + /// wherever the refusal is worded: the engine's own door | |
| 1055 | + /// ([`import_subtitles`](Self::import_subtitles)) and a front-end that | |
| 1056 | + /// splits the walk from the push ([`parse_subtitles`](Self::parse_subtitles) | |
| 1057 | + /// then [`add_subtitle_tracks`](Self::add_subtitle_tracks)) must not tell a | |
| 1058 | + /// person two different stories about the same file. | |
| 1059 | + pub fn no_subtitles_in(path: &Path) -> crate::Error { | |
| 1060 | + format!("no subtitle tracks in {}", path.display()).into() | |
| 1061 | + } | |
| 1062 | + | |
| 1042 | 1063 | /// Puts tracks already read by [`parse_subtitles`](Self::parse_subtitles) |
| 1043 | 1064 | /// on this timeline, in the order given. Hands back how many actually went |
| 1044 | 1065 | /// on: [`Project::add_subtitles`] drops the ones already here -- same file, |
crates/engine/tests/subtitles.rsmodified+60 -0
| @@ -412,6 +412,66 @@ fn a_subtitle_row_can_be_removed_and_the_survivors_round_trip() { | ||
| 412 | 412 | assert_eq!(session.subtitles().len(), 2, "and nothing moved"); |
| 413 | 413 | } |
| 414 | 414 | |
| 415 | +/// The whole point of the add-subtitles door: another release's subtitle | |
| 416 | +/// tracks go onto the timeline that is already open, and the file they came out | |
| 417 | +/// of joins *nothing* -- no source row, no lane, no clip. Nobody has to import a | |
| 418 | +/// second copy of a film, or break the edit they have, to get its subtitles. | |
| 419 | +/// | |
| 420 | +/// And a file the walk found no subtitle track in is refused as that, in words, | |
| 421 | +/// rather than counted as zero: zero added is what a file whose tracks are here | |
| 422 | +/// already answers, and the two are opposite instructions to the person reading | |
| 423 | +/// them. | |
| 424 | +#[test] | |
| 425 | +fn subtitles_come_off_another_file_without_it_joining_the_timeline() { | |
| 426 | + let media = asset("test_av.mp4"); | |
| 427 | + let mut session = engine::PlaybackSession::open(&media).expect("open the fixture"); | |
| 428 | + session.set_gain(0.0); | |
| 429 | + let sources: Vec<_> = session.sources().iter().map(|s| s.path.clone()).collect(); | |
| 430 | + let lanes = session.lanes(); | |
| 431 | + let clips: Vec<_> = lanes.iter().map(|&l| session.lane_clips(l).len()).collect(); | |
| 432 | + assert_eq!(sources.len(), 1, "one file is open"); | |
| 433 | + | |
| 434 | + // A different file entirely, carrying two subtitle tracks. | |
| 435 | + let other = asset("test_subs.mkv"); | |
| 436 | + assert_eq!( | |
| 437 | + session.import_subtitles(&other).expect("its subtitles come"), | |
| 438 | + 2 | |
| 439 | + ); | |
| 440 | + assert_eq!(session.subtitles().len(), 2); | |
| 441 | + for track in session.subtitles() { | |
| 442 | + assert_eq!(track.path, other, "the rows name the file they came out of"); | |
| 443 | + assert!(!track.cues.is_empty(), "with their cues read"); | |
| 444 | + } | |
| 445 | + // ...and the timeline is exactly the timeline it was. | |
| 446 | + assert_eq!( | |
| 447 | + session.sources().iter().map(|s| s.path.clone()).collect::<Vec<_>>(), | |
| 448 | + sources, | |
| 449 | + "the file whose subtitles these are did not join the library" | |
| 450 | + ); | |
| 451 | + assert_eq!(session.lanes(), lanes, "no lane was added"); | |
| 452 | + assert_eq!( | |
| 453 | + session.lanes().iter().map(|&l| session.lane_clips(l).len()).collect::<Vec<_>>(), | |
| 454 | + clips, | |
| 455 | + "and nothing was cut, moved or dropped on one" | |
| 456 | + ); | |
| 457 | + | |
| 458 | + // The same file again is the same two tracks, not four. | |
| 459 | + assert_eq!(session.import_subtitles(&other).expect("no error"), 0); | |
| 460 | + assert_eq!(session.subtitles().len(), 2); | |
| 461 | + | |
| 462 | + // A container walked and carrying none says so, naming the file -- and the | |
| 463 | + // claim is only made after the walk looked. | |
| 464 | + let why = session | |
| 465 | + .import_subtitles(&media) | |
| 466 | + .expect_err("the fixture carries no subtitle track") | |
| 467 | + .to_string(); | |
| 468 | + assert!( | |
| 469 | + why.contains("no subtitle tracks in") && why.contains("test_av.mp4"), | |
| 470 | + "{why}" | |
| 471 | + ); | |
| 472 | + assert_eq!(session.subtitles().len(), 2, "and nothing changed over it"); | |
| 473 | +} | |
| 474 | + | |
| 415 | 475 | /// A bitmap track survives a save the same way a text one does, and for the |
| 416 | 476 | /// same reason: a `.edith` holds the file and the track number, never a cue, so |
| 417 | 477 | /// what comes back is what the file still says. The pictures are read out of |