From e17cd0a1236072132576b1ccb59d7fd98b52dfef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 13 Aug 2026 04:30:32 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(subtitles):=20a=20file's=20sub?= =?UTF-8?q?titles=20come=20onto=20the=20timeline=20that=20is=20open?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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.rs | 78 ++++++++++++++++++++++++++++++-- crates/app/src/ui/library.rs | 48 +++++++++++++++++--- crates/engine/src/playback.rs | 23 +++++++++- crates/engine/tests/subtitles.rs | 60 ++++++++++++++++++++++++ 4 files changed, 197 insertions(+), 12 deletions(-) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 590db8f..b2db0c1 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -343,7 +343,9 @@ impl LibraryTab { match self { LibraryTab::Media => "No video or stills yet — Import, or drop a file on the window", LibraryTab::Audio => "No sound yet — Import, or drop a file on the window", - LibraryTab::Text => "No subtitles yet — drop an .srt on the window, or + S in the toolbar", + LibraryTab::Text => { + "No subtitles yet — Add subtitles from a file, or drop an .srt on the window" + } } } } @@ -4808,7 +4810,7 @@ impl Player { let added = this .session .as_mut() - .map(|session| parsed.map(|tracks| session.add_subtitle_tracks(tracks))); + .map(|session| parsed.and_then(|tracks| pushed(session, &path, tracks))); this.landed_subtitles(&path, added, cx); }) .ok(); @@ -4826,7 +4828,7 @@ impl Player { let added = self .session .as_mut() - .map(|session| subs.map(|tracks| session.add_subtitle_tracks(tracks))); + .map(|session| subs.and_then(|tracks| pushed(session, path, tracks))); self.landed_subtitles(path, added, cx); } @@ -4842,7 +4844,10 @@ impl Player { cx: &mut Context, ) { let text = match added { - Some(Ok(0)) => format!("{} is already on the timeline", file_name(path)), + Some(Ok(0)) => format!( + "{}'s subtitles are on the timeline already", + file_name(path) + ), Some(Ok(n)) => format!( "SUBTITLES {} — {n} track(s), showing over the picture, {} hides them", file_name(path), @@ -6750,6 +6755,33 @@ fn subtitle_tail(session: &mut PlaybackSession, subs: Subs) -> Option { } } +/// The push both deliberate add-subtitles doors end on -- `+ S` and its key +/// ([`Player::add_subtitles`]) and a dropped or argv'd subtitle file +/// ([`Player::take_subtitles`]) -- so the two cannot come to word the same file +/// differently. +/// +/// The walk having found *nothing* is a refusal and not a count: a container +/// with no subtitle track in it and a file whose tracks are all on the timeline +/// already both push zero rows, and they are opposite answers -- one says look +/// somewhere else, the other says you already have them. The engine's own door +/// draws the same line in the same words +/// ([`PlaybackSession::no_subtitles_in`], asked here because this route splits +/// the walk from the push to keep the walk off the render thread). +/// +/// The file itself joins nothing either way: no library row, no lane, no clip. +/// Subtitles are a list the timeline carries, and this is the only thing that +/// touches it. +fn pushed( + session: &mut PlaybackSession, + path: &std::path::Path, + tracks: Vec, +) -> engine::Result { + match tracks.is_empty() { + true => Err(PlaybackSession::no_subtitles_in(path)), + false => Ok(session.add_subtitle_tracks(tracks)), + } +} + /// What a subtitle row says under its name: how many cues it holds and whether /// they are pictures ([`engine::subtitle::SubtitleTrack::is_bitmap`]) -- which /// is the difference between a track an export writes into the file and one it @@ -15511,6 +15543,44 @@ mod tests { assert_eq!(sub_pick_name(&[], 0), None); } + /// The door this editor answers "don't make me import the film again" with, + /// and it is in the panel where the tracks it adds are listed: the Text + /// tab's own button reads a file's subtitle tracks onto the open timeline + /// -- a release's `.mkv`, an `.srt` beside it -- while the file itself + /// joins nothing. It is the *action* and not a second implementation of it, + /// so the button, the stroke and the actions card cannot drift apart, and + /// it is oracle-gated like every other action button, so with no timeline + /// open it dims and says why instead of opening a chooser for nothing. + #[test] + fn the_text_tab_carries_the_add_subtitles_door() { + let library = include_str!("ui/library.rs"); + let at = library + .find("\"add-subtitles\"") + .expect("no add-subtitles control in the library column"); + let block = &library[at..(at + 1400).min(library.len())]; + assert!( + block.contains("ActionId::AddSubtitleTrack"), + "the button is a door of its own rather than the action: {block}" + ); + assert!( + block.contains("pick_and_add_subtitles"), + "the button does not open the subtitle chooser: {block}" + ); + // `action_control` is the oracle-gated one: dimmed with the refusal in + // the oracle's own words. `control` alone would be lit with no timeline. + assert!( + library[..at].ends_with("self.action_control(\n "), + "the add-subtitles button is not oracle-gated" + ); + // ...and the empty tab names that button, rather than pointing at a + // toolbar the person is not looking at. + assert!( + crate::LibraryTab::Text.empty().contains("Add subtitles"), + "{}", + crate::LibraryTab::Text.empty() + ); + } + /// The one thing regrouping must not break: `sub_track` is a flat index /// into the add-order list, a click sets it and a save writes it into the /// `.edith`. Every row must still name the track it was made from -- rows diff --git a/crates/app/src/ui/library.rs b/crates/app/src/ui/library.rs index 32370a2..343b0e1 100644 --- a/crates/app/src/ui/library.rs +++ b/crates/app/src/ui/library.rs @@ -280,13 +280,47 @@ impl Player { ) , ) - .child( - div().flex_none().child(control( + // The tab says what the one full-width control is for. Text holds + // no rows of its own ([`LibraryTab::holds`]) -- it is the tracks + // under it -- and what a person opens it to do is put subtitles on + // this timeline: off a release's `.mkv`, off an `.srt` beside it, + // without the file itself joining the library or a lane. So that is + // the button there, where the tracks it adds are listed; media goes + // on getting in from the other two tabs, from a drop, and from the + // actions card. + .when(self.library_tab == LibraryTab::Text, |d| { + d.child(div().flex_none().child(self.action_control( + "add-subtitles", + width - 16., + match self.session.as_ref().is_some_and(|s| !s.subtitles().is_empty()) { + // The primary action of this tab while it has nothing: + // the same rule the Import button follows one tab over. + false => ACCENT_PRIMARY(), + true => BG_RAISED(), + }, + None, + // The column is 128 px at the 640x360 floor and the whole + // name does not fit it: a centred label wider than its + // button is clipped at *both* ends ("d subtitles from a + // fil"), which is a button that says nothing. The short + // form there; the full name is what the tooltip, the + // actions card and the stroke go on saying either way. + match (width - 16.) / LIST_CHAR_W >= 26. { + true => "Add subtitles from a file…", + false => "Add subtitles", + }, + "reads a file's subtitle tracks onto this timeline — the file itself \ + joins nothing", + ActionId::AddSubtitleTrack, + cx.listener(|this, _: &ClickEvent, _, cx| this.pick_and_add_subtitles(cx)), + ))) + }) + .when(self.library_tab != LibraryTab::Text, |d| { + d.child(div().flex_none().child(control( "import", // Full width of the column: the way media gets in is - // the one affordance in this panel that is always - // there, whatever tab is open and however narrow the - // window is. + // the one affordance of the tabs that hold media, + // however narrow the window is. width - 16., // Filled with the accent while there is nothing to work // on, because with an empty library this *is* the @@ -302,8 +336,8 @@ impl Player { "adds a file to this list — or drop one on the window".to_string(), !exporting, cx.listener(|this, _: &ClickEvent, _, cx| this.pick_and_import(cx)), - )), - ) + ))) + }) .child( div() .id("library-rows") diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index f420f54..c47dc47 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -998,7 +998,18 @@ impl PlaybackSession { /// [`crate::subtitle::open`] gives. Nothing else about the timeline moves: /// subtitles are not clips and land on no lane. pub fn import_subtitles(&mut self, path: &Path) -> crate::Result { - Ok(self.add_subtitle_tracks(Self::parse_subtitles(path)?)) + let tracks = Self::parse_subtitles(path)?; + // A file that was walked and carries none is refused as *that*, because + // the alternative -- 0 added -- is the very sentence a file whose tracks + // are already on this timeline gets, and the two are opposite answers a + // person acts differently on. Said only after the walk actually looked + // ([`crate::subtitle::of_media`]); a *file* that is not a container at + // all never reaches here, [`parse_subtitles`](Self::parse_subtitles) + // refuses it in its own words. + if tracks.is_empty() { + return Err(Self::no_subtitles_in(path)); + } + Ok(self.add_subtitle_tracks(tracks)) } /// The reading half of [`import_subtitles`](Self::import_subtitles), with no @@ -1039,6 +1050,16 @@ impl PlaybackSession { Ok(tracks) } + /// What a file the walk found no subtitle track in is refused with, + /// wherever the refusal is worded: the engine's own door + /// ([`import_subtitles`](Self::import_subtitles)) and a front-end that + /// splits the walk from the push ([`parse_subtitles`](Self::parse_subtitles) + /// then [`add_subtitle_tracks`](Self::add_subtitle_tracks)) must not tell a + /// person two different stories about the same file. + pub fn no_subtitles_in(path: &Path) -> crate::Error { + format!("no subtitle tracks in {}", path.display()).into() + } + /// Puts tracks already read by [`parse_subtitles`](Self::parse_subtitles) /// on this timeline, in the order given. Hands back how many actually went /// on: [`Project::add_subtitles`] drops the ones already here -- same file, diff --git a/crates/engine/tests/subtitles.rs b/crates/engine/tests/subtitles.rs index 02a80c4..dd41649 100644 --- a/crates/engine/tests/subtitles.rs +++ b/crates/engine/tests/subtitles.rs @@ -412,6 +412,66 @@ fn a_subtitle_row_can_be_removed_and_the_survivors_round_trip() { assert_eq!(session.subtitles().len(), 2, "and nothing moved"); } +/// The whole point of the add-subtitles door: another release's subtitle +/// tracks go onto the timeline that is already open, and the file they came out +/// of joins *nothing* -- no source row, no lane, no clip. Nobody has to import a +/// second copy of a film, or break the edit they have, to get its subtitles. +/// +/// And a file the walk found no subtitle track in is refused as that, in words, +/// rather than counted as zero: zero added is what a file whose tracks are here +/// already answers, and the two are opposite instructions to the person reading +/// them. +#[test] +fn subtitles_come_off_another_file_without_it_joining_the_timeline() { + let media = asset("test_av.mp4"); + let mut session = engine::PlaybackSession::open(&media).expect("open the fixture"); + session.set_gain(0.0); + let sources: Vec<_> = session.sources().iter().map(|s| s.path.clone()).collect(); + let lanes = session.lanes(); + let clips: Vec<_> = lanes.iter().map(|&l| session.lane_clips(l).len()).collect(); + assert_eq!(sources.len(), 1, "one file is open"); + + // A different file entirely, carrying two subtitle tracks. + let other = asset("test_subs.mkv"); + assert_eq!( + session.import_subtitles(&other).expect("its subtitles come"), + 2 + ); + assert_eq!(session.subtitles().len(), 2); + for track in session.subtitles() { + assert_eq!(track.path, other, "the rows name the file they came out of"); + assert!(!track.cues.is_empty(), "with their cues read"); + } + // ...and the timeline is exactly the timeline it was. + assert_eq!( + session.sources().iter().map(|s| s.path.clone()).collect::>(), + sources, + "the file whose subtitles these are did not join the library" + ); + assert_eq!(session.lanes(), lanes, "no lane was added"); + assert_eq!( + session.lanes().iter().map(|&l| session.lane_clips(l).len()).collect::>(), + clips, + "and nothing was cut, moved or dropped on one" + ); + + // The same file again is the same two tracks, not four. + assert_eq!(session.import_subtitles(&other).expect("no error"), 0); + assert_eq!(session.subtitles().len(), 2); + + // A container walked and carrying none says so, naming the file -- and the + // claim is only made after the walk looked. + let why = session + .import_subtitles(&media) + .expect_err("the fixture carries no subtitle track") + .to_string(); + assert!( + why.contains("no subtitle tracks in") && why.contains("test_av.mp4"), + "{why}" + ); + assert_eq!(session.subtitles().len(), 2, "and nothing changed over it"); +} + /// 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