forge
Explore
Log in

Don't you dare go Hollow

A file's subtitles come onto the timeline that is open

Browse files at this commit

e17cd0a1236072132576b1ccb59d7fd98b52dfefAhmet Kaan GümüşParent: 3c6b5d9
  • feat
    • a file's subtitles come onto the timeline that is open
Show full message
✨ feat(subtitles): a file's subtitles come onto the timeline that is open

Importing only the subtitles from a file should not mean reimporting
the whole media or breaking the timeline. The engine could already do exactly that -- the walk
reads a container's subtitle tracks and pushes them onto the timeline's
own list, and subtitles are on no lane -- but the only doors to it were
a toolbar glyph and a stroke, while the panel where the tracks are
listed offered a media import. So the capability was there and the door
was not, which is the same thing as not having it.

The Text tab's own button is now that door: "Add subtitles from a
file…", the action itself (so the button, the stroke and the actions
card cannot drift), oracle-gated like every other action button, and
accented while the tab has nothing on it -- the primary action of the
tab it is in. The tab's empty line names the button rather than
pointing at a toolbar. At the 640x360 floor the column is 128 px and a
centred label wider than its button is clipped at both ends, so the
button says the short form there and the full name stays in the
tooltip, the card and the stroke.

Whatever the file is: a release's `.mkv`, an `.mp4`, an `.srt` beside
it. The media itself joins nothing -- no library row, no lane, no clip,
nothing on the edit list moves -- and the same file twice is the same
tracks, not two of each.

A file the walk found no subtitle track in is now refused as that, in
words, naming the file. It used to answer "0 tracks added", which is
the sentence a file whose tracks are already on the timeline gets --
two opposite answers wearing one string. The line is drawn once
(`PlaybackSession::no_subtitles_in`) and used by both the engine's own
door and the front-end's split walk-then-push, and the claim is only
ever made after the walk actually looked. A plain media *open* is
untouched: nothing is said there about a file with no subtitles,
because nobody asked.
crates/app/src/main.rsmodified+74 -4
@@ -343,7 +343,9 @@ impl LibraryTab {
343343 match self {
344344 LibraryTab::Media => "No video or stills yet — Import, or drop a file on the window",
345345 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+ }
347349 }
348350 }
349351 }
@@ -4808,7 +4810,7 @@ impl Player {
48084810 let added = this
48094811 .session
48104812 .as_mut()
4811- .map(|session| parsed.map(|tracks| session.add_subtitle_tracks(tracks)));
4813+ .map(|session| parsed.and_then(|tracks| pushed(session, &path, tracks)));
48124814 this.landed_subtitles(&path, added, cx);
48134815 })
48144816 .ok();
@@ -4826,7 +4828,7 @@ impl Player {
48264828 let added = self
48274829 .session
48284830 .as_mut()
4829- .map(|session| subs.map(|tracks| session.add_subtitle_tracks(tracks)));
4831+ .map(|session| subs.and_then(|tracks| pushed(session, path, tracks)));
48304832 self.landed_subtitles(path, added, cx);
48314833 }
48324834
@@ -4842,7 +4844,10 @@ impl Player {
48424844 cx: &mut Context<Self>,
48434845 ) {
48444846 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+ ),
48464851 Some(Ok(n)) => format!(
48474852 "SUBTITLES {} — {n} track(s), showing over the picture, {} hides them",
48484853 file_name(path),
@@ -6750,6 +6755,33 @@ fn subtitle_tail(session: &mut PlaybackSession, subs: Subs) -> Option<String> {
67506755 }
67516756 }
67526757
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+
67536785 /// What a subtitle row says under its name: how many cues it holds and whether
67546786 /// they are pictures ([`engine::subtitle::SubtitleTrack::is_bitmap`]) -- which
67556787 /// is the difference between a track an export writes into the file and one it
@@ -15511,6 +15543,44 @@ mod tests {
1551115543 assert_eq!(sub_pick_name(&[], 0), None);
1551215544 }
1551315545
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+
1551415584 /// The one thing regrouping must not break: `sub_track` is a flat index
1551515585 /// into the add-order list, a click sets it and a save writes it into the
1551615586 /// `.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 {
280280 )
281281 ,
282282 )
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(
285320 "import",
286321 // 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.
290324 width - 16.,
291325 // Filled with the accent while there is nothing to work
292326 // on, because with an empty library this *is* the
@@ -302,8 +336,8 @@ impl Player {
302336 "adds a file to this list — or drop one on the window".to_string(),
303337 !exporting,
304338 cx.listener(|this, _: &ClickEvent, _, cx| this.pick_and_import(cx)),
305- )),
306- )
339+ )))
340+ })
307341 .child(
308342 div()
309343 .id("library-rows")
crates/engine/src/playback.rsmodified+22 -1
@@ -998,7 +998,18 @@ impl PlaybackSession {
998998 /// [`crate::subtitle::open`] gives. Nothing else about the timeline moves:
999999 /// subtitles are not clips and land on no lane.
10001000 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))
10021013 }
10031014
10041015 /// The reading half of [`import_subtitles`](Self::import_subtitles), with no
@@ -1039,6 +1050,16 @@ impl PlaybackSession {
10391050 Ok(tracks)
10401051 }
10411052
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+
10421063 /// Puts tracks already read by [`parse_subtitles`](Self::parse_subtitles)
10431064 /// on this timeline, in the order given. Hands back how many actually went
10441065 /// 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() {
412412 assert_eq!(session.subtitles().len(), 2, "and nothing moved");
413413 }
414414
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+
415475 /// A bitmap track survives a save the same way a text one does, and for the
416476 /// same reason: a `.edith` holds the file and the track number, never a cue, so
417477 /// what comes back is what the file still says. The pictures are read out of