From 791ea9f0037e1f3587d8154ca66e45bb59d9c14f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 19 Aug 2026 20:38:24 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(app):=20a=20library=20full=20o?= =?UTF-8?q?f=20subtitles=20reads=20as=20its=20films=20again?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Group headers in the Text tab used to vanish under sub_headers_fit's viewport gate, which was the one thing carrying "whose tracks these are" once a window got short -- so a short window left every track looking like it belonged to whichever film happened to be scrolled into view. The row list already scrolls past SUB_ROWS_H, so the gate was solving a problem the scrollbar had already solved: headers are now drawn whenever there is more than one source, full stop. Each header now also says how many tracks it holds and folds its rows shut on a click (Player::sub_folded, session-lifetime, never saved) -- a plain toggle and not the click-cycling pattern this codebase has already thrown out once. Its hit target moved up to HIT_MIN to earn the click. A standalone .srt was already its own named group (by its own filename) rather than a sourceless catch-all, so no "External" bucket was needed for it. --- crates/app/src/layout.rs | 15 +++---- crates/app/src/library_meta.rs | 13 ------ crates/app/src/main.rs | 10 ++++- crates/app/src/render.rs | 1 - crates/app/src/tests/layout.rs | 76 ++++++++++++++++++++++++++++++++++ crates/app/src/tests/media.rs | 18 +++----- crates/app/src/tests/mod.rs | 2 +- crates/app/src/ui/library.rs | 4 +- crates/app/src/ui/timeline.rs | 59 ++++++++++++++++++-------- 9 files changed, 142 insertions(+), 56 deletions(-) diff --git a/crates/app/src/layout.rs b/crates/app/src/layout.rs index 4dd784e..03f398c 100644 --- a/crates/app/src/layout.rs +++ b/crates/app/src/layout.rs @@ -173,15 +173,12 @@ pub(crate) const SUB_CUE_MIN_W: f32 = 2.; /// How much of the subtitle list in the library column is on screen at once, /// past which it scrolls -- the media list above it is what keeps the height. pub(crate) const SUB_ROWS_H: f32 = 3. * ROW_H; -/// The row naming the file a block of tracks came out of. Shorter than a track -/// row because nothing on it is clicked -- `HIT_MIN` binds targets and a header -/// is a label, not a way in -- and drawn at all only where the column has the -/// height to show tracks under it ([`sub_headers_fit`]). -pub(crate) const SUB_HEAD_H: f32 = 18.; -/// How much of the subtitle list is on screen at the 640x360 floor: one row, -/// measured -- the section's own heading and the Add button under it take the -/// rest of the 84 px the column has there. -pub(crate) const SUB_ROWS_AT_FLOOR: f32 = ROW_H; +/// The row naming the file a block of tracks came out of. Always drawn where +/// there is more than one file -- the list already scrolls past `SUB_ROWS_H`, +/// so a short window loses tracks under the fold rather than the name saying +/// whose they are. A click folds it, so it binds `HIT_MIN` like every other +/// target. +pub(crate) const SUB_HEAD_H: f32 = HIT_MIN; /// How much of a subtitle row's width the file's name in front of the label may /// take. Half: which file and which language are both worth reading, and a name /// given the whole row is a row where the language is what gets truncated. diff --git a/crates/app/src/library_meta.rs b/crates/app/src/library_meta.rs index b24e7b8..581b9dd 100644 --- a/crates/app/src/library_meta.rs +++ b/crates/app/src/library_meta.rs @@ -370,19 +370,6 @@ pub(crate) fn clip_middle(name: &str, width: f32) -> String { ) } -/// Whether the subtitle list has the height to name each file over its tracks. -/// A header is worth its own height only where there are still tracks under it -/// to read: at the 640x360 floor the list is one row tall, and a header there -/// would name a film and show none of it -- so at the floor there are no -/// headers and the rows say which file they came from themselves. -/// -/// The window's height answers it on its own: the row above the picture and the -/// panel under it are fixed, so every pixel the window gains past the floor is a -/// pixel this column gains. -pub(crate) fn sub_headers_fit(viewport_h: f32) -> bool { - SUB_ROWS_AT_FLOOR + (viewport_h - 360.).max(0.) >= SUB_HEAD_H + 2. * ROW_H -} - /// What the window is called: the program, and what is open in it. The name is /// what the header shows, so an empty window says the program alone rather than /// "no file open — edith". diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 111eeb6..3dcab48 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -43,7 +43,7 @@ use ui::widgets::*; use keymap::{ActionId, Keymap}; use std::cell::Cell; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::path::{Path, PathBuf}; use std::rc::Rc; use std::sync::Arc; @@ -266,6 +266,13 @@ struct Player { /// like every other index here -- track 2 of one project is not track 2 of /// the next. sub_track: usize, + /// Which files' subtitle groups in the library are folded shut, keyed by + /// the group's own path -- the same key [`subtitle_rows`] groups on, so a + /// fold survives a track being added or removed off the file it belongs + /// to. Session-lifetime only: a fold is a "not looking at this one right + /// now", not a project setting, so it is never saved and every file opens + /// unfolded. + sub_folded: HashSet, /// Which subtitle lane is *shown*: one of them and never two, the way a /// player has one subtitle track chosen at a time -- two hundred lanes of /// different words all drawn at once is two hundred plates over one @@ -701,6 +708,7 @@ fn main() { snap: true, subs_on: true, sub_track: 0, + sub_folded: HashSet::new(), sub_lane: None, snap_cue: None, ghost: None, diff --git a/crates/app/src/render.rs b/crates/app/src/render.rs index 1ac0ee0..bdb6570 100644 --- a/crates/app/src/render.rs +++ b/crates/app/src/render.rs @@ -556,7 +556,6 @@ impl Render for Player { .flex() .child(self.library( self.split_px(Split::Library, window.viewport_size()), - f32::from(window.viewport_size().height), cx, )) // The seams, one per pair of regions: what a hand drags to diff --git a/crates/app/src/tests/layout.rs b/crates/app/src/tests/layout.rs index 9844ab9..4751983 100644 --- a/crates/app/src/tests/layout.rs +++ b/crates/app/src/tests/layout.rs @@ -1124,6 +1124,46 @@ fn subtitle_rows_group_a_source_however_they_were_added() { "the group carries the path the tint is asked by" ); assert_eq!(file_tint(&sources, &groups[2].path), None); + // What a header's "N tracks" says: the group's own row count, which is + // 2, 2, 1 here -- a film that gave several tracks and a standalone + // `.srt` that gave exactly one. + assert_eq!( + groups.iter().map(|g| g.rows.len()).collect::>(), + [2, 2, 1] + ); + // A standalone `.srt` is already its own group, named after its own + // file ("late", not lumped under some catch-all "External" bucket) -- + // there is no sourceless case for a header to special-case. + assert_eq!(groups[2].name, "late"); + assert_eq!(groups[2].path, PathBuf::from("/subs/late.srt")); +} + +/// The fold a click on a header sets is keyed by [`SubGroup::path`] +/// (`Player::sub_folded`), so it has to survive the very thing regrouping +/// is for: a second track landing on a file already in the list, or one +/// being removed from it. The group's path is the fold's whole identity, +/// so it must not move under either. +#[test] +fn a_groups_fold_key_survives_a_track_arriving_or_leaving_its_file() { + let before = [ + sub("/films/a.mkv", Some(1), "eng"), + sub("/films/b.mkv", Some(1), "eng"), + ]; + let after_add = [ + sub("/films/a.mkv", Some(1), "eng"), + sub("/films/b.mkv", Some(1), "eng"), + sub("/films/a.mkv", Some(2), "fre"), + ]; + let (before_groups, after_groups) = (subtitle_rows(&before), subtitle_rows(&after_add)); + assert_eq!(before_groups[0].path, after_groups[0].path, "a's key held"); + assert_eq!(before_groups[1].path, after_groups[1].path, "b's key held"); + // The group a second track landed on grew; the other did not move. + assert_eq!(after_groups[0].rows.len(), 2); + assert_eq!(after_groups[1].rows.len(), 1); + // Removing that same track back off leaves the original key and count. + let after_remove = subtitle_rows(&before); + assert_eq!(after_remove[0].path, before_groups[0].path); + assert_eq!(after_remove[0].rows.len(), 1); } /// What the strip header, the section heading and the toggle's notice all @@ -1219,6 +1259,42 @@ fn the_text_tab_carries_the_add_subtitles_door() { ); } +/// A source's group header in the library must not vanish on a short +/// window: it is drawn whenever there is more than one source +/// (`several_files`), never gated on the viewport's height, and the list +/// under it is what scrolls instead ([`SUB_ROWS_H`]). And it has to be a +/// real fold, not the click-cycling pattern this codebase has already +/// thrown out once: one click toggles `sub_folded` shut or open, it never +/// steps through more than those two states. +#[test] +fn a_subtitle_group_header_is_never_gated_on_window_height() { + let timeline = src_text("ui/timeline.rs"); + assert!( + !timeline.contains("sub_headers_fit"), + "the header is still gated on the viewport's height" + ); + let at = timeline + .find("let headed = ") + .expect("no `headed` computed in subtitle_section"); + let line = &timeline[at..(at + 80).min(timeline.len())]; + assert!( + line.contains("several_files") && !line.contains("viewport"), + "headed depends on something other than the file count: {line}" + ); + // The header is a click target that flips membership in a set -- + // `remove` else `insert` -- not a value stepped through several states. + let head_at = timeline + .find("subtitle-group-head") + .expect("no id on the group header"); + let block = &timeline[head_at..(head_at + 2400).min(timeline.len())]; + assert!(block.contains("sub_folded.remove"), "no fold-open path: {block}"); + assert!(block.contains("sub_folded.insert"), "no fold-shut path: {block}"); + assert!( + block.contains("\"1 track\"") || block.contains("tracks"), + "the header does not say how many tracks it holds: {block}" + ); +} + /// 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/tests/media.rs b/crates/app/src/tests/media.rs index cc700ba..e021f9b 100644 --- a/crates/app/src/tests/media.rs +++ b/crates/app/src/tests/media.rs @@ -831,19 +831,13 @@ fn the_subtitle_plate_and_lanes_fit_the_smallest_window() { // taking the media list's room. assert_eq!(SUB_ROWS_H / ROW_H, 3.); assert!(ROW_H >= HIT_MIN, "a subtitle row is clicked to pick it"); - // At the floor there are no group headers at all: the list is one row - // tall there, and a header would name a film and then show none of it. - // The rows go on naming their own file, so nothing is lost by it. - assert!(!sub_headers_fit(360.), "a header at the floor eats the track"); - // At the size the window opens on there is room for a header and the - // tracks under it, which is the only condition it is drawn on. - assert!(sub_headers_fit(720.)); - // ...and where it is drawn it fits inside the same capped list, header - // and two rows under it, without the tracks losing their budget. + // A header is always drawn with more than one file, floor included: the + // list scrolls past `SUB_ROWS_H` instead of a short window losing the + // name saying whose tracks these are. assert!(SUB_HEAD_H + 2. * ROW_H <= SUB_ROWS_H); - // A header takes no click -- there is nothing to fold and nothing to - // aim at -- which is the only reason it may be under `HIT_MIN`. - assert!(SUB_HEAD_H < HIT_MIN && SUB_HEAD_H > 0.); + // A header folds its group on a click now, so it binds `HIT_MIN` like + // every other target rather than being let off it as a bare label. + assert!(SUB_HEAD_H >= HIT_MIN); } /// What the rows of both lists do with a name too long for the column: two diff --git a/crates/app/src/tests/mod.rs b/crates/app/src/tests/mod.rs index c1e391b..004773d 100644 --- a/crates/app/src/tests/mod.rs +++ b/crates/app/src/tests/mod.rs @@ -57,7 +57,7 @@ use super::{ use super::{ SUB_BOTTOM, SUB_CUE_MIN_W, SUB_HEAD_H, SUB_LINE_H, SUB_ROWS_H, SUB_STEM_SHARE, SUB_TEXT, clip_middle, cue_box, sub_bottom, - carries_subtitles, cues_at, file_tint, is_subtitle, lang_human, row_text_w, sub_headers_fit, + carries_subtitles, cues_at, file_tint, is_subtitle, lang_human, row_text_w, Subs, subtitle_detail, subtitle_notice, subtitle_tail, sub_pick_name, subtitle_rows, subtitle_toggle_notice, walk_subtitles, }; diff --git a/crates/app/src/ui/library.rs b/crates/app/src/ui/library.rs index e57427f..0e22828 100644 --- a/crates/app/src/ui/library.rs +++ b/crates/app/src/ui/library.rs @@ -15,7 +15,7 @@ impl Player { /// the rest of this window: nothing in it takes focus, so the root keeps /// the keyboard and the play key still works after a row is clicked /// (ledger:182). - pub(crate) fn library(&self, width: f32, viewport_h: f32, cx: &mut Context) -> impl IntoElement { + pub(crate) fn library(&self, width: f32, cx: &mut Context) -> impl IntoElement { let exporting = self.exporting().is_some(); let sources = self .session @@ -484,7 +484,7 @@ impl Player { // it goes on no lane and is dragged nowhere -- but it is a thing the // timeline holds, and this is the list of those. .when(self.library_tab == LibraryTab::Text, |d| { - d.children(self.subtitle_section(width, viewport_h, cx)) + d.children(self.subtitle_section(width, cx)) }) .child(control( "add-asset", diff --git a/crates/app/src/ui/timeline.rs b/crates/app/src/ui/timeline.rs index 30529c6..b80e269 100644 --- a/crates/app/src/ui/timeline.rs +++ b/crates/app/src/ui/timeline.rs @@ -485,20 +485,17 @@ impl Player { /// Every row names the file it came out of itself, in words and in the tint /// that file's clips wear on the lanes -- but only where there is more than /// one file to tell apart, the way [`row_name`] numbers audio streams only - /// where a file gave several. Where the window is tall enough for it - /// ([`sub_headers_fit`]) each file's block is headed by its name as well: a - /// label and nothing more, no click and nothing to fold, so the rows under - /// it are the only things anybody has to aim at. At the 640x360 floor the - /// headers are gone and the rows still say whose they are. + /// where a file gave several. There, each file's block is headed by its + /// name as well, its swatch and its track count -- always, whatever the + /// window's height, since the list under it scrolls (`SUB_ROWS_H`) rather + /// than the header being the thing a short window drops. A click on a + /// header folds its rows shut (`Player::sub_folded`) and a second one opens + /// them back -- a fold and not a cycle, so what a click does never depends + /// on what the last one did. /// /// `None` when there are none -- an empty heading is a section about /// nothing. - pub(crate) fn subtitle_section( - &self, - width: f32, - viewport_h: f32, - cx: &mut Context, - ) -> Option { + pub(crate) fn subtitle_section(&self, width: f32, cx: &mut Context) -> Option { let tracks = self.session.as_ref()?.subtitles(); if tracks.is_empty() { return None; @@ -510,16 +507,20 @@ impl Player { // One file's tracks need no prefix saying which file: it is the only // one, and every row would carry the same word. let several_files = groups.len() > 1; - let headed = several_files && sub_headers_fit(viewport_h); + let headed = several_files; let text_w = row_text_w(width); let rows: Vec<_> = groups .into_iter() - .map(|SubGroup { name, path, rows }| { + .enumerate() + .map(|(group_ord, SubGroup { name, path, rows })| { // The file's own colour, the one its media rows and its clips // wear -- and none at all for a standalone `.srt`, which came // off no file on this timeline. let tint = file_tint(self.sources(), &path); + let track_count = rows.len(); let numbered = rows.len() > 1; + let folded = self.sub_folded.contains(&path); + let fold_path = path.clone(); // The name twice over: all of it the header can hold, and the // share of a row a prefix may take in front of the label. let head = clip_middle(&name, text_w); @@ -697,11 +698,13 @@ impl Player { .flex_col() .gap(px(2.)) // The header: which film these came out of, in words and not - // in colour alone. No id, no click, nothing to fold -- a - // label, which is why it is allowed under `HIT_MIN`. + // in colour alone -- a click folds its rows shut and a + // second click opens them, so it is its own `HIT_MIN` + // target rather than a bare label. .when(headed, |d| { d.child( div() + .id(("subtitle-group-head", group_ord)) .flex_none() .h(px(SUB_HEAD_H)) .flex() @@ -709,6 +712,24 @@ impl Player { .gap(px(6.)) .text_size(px(10.)) .text_color(rgb(FG_SECONDARY())) + .rounded(px(3.)) + .cursor_pointer() + .hover(|s| s.bg(rgb(BG_HOVER()))) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + match this.sub_folded.remove(&fold_path) { + true => {} + false => { + this.sub_folded.insert(fold_path.clone()); + } + } + cx.notify(); + })) + // Folded / open, in the same glyph a fold uses + // everywhere else on this row list. + .child(match folded { + true => "▸", + false => "▾", + }) .when_some(tint, |d, tint| { d.child( div() @@ -719,10 +740,14 @@ impl Player { .bg(rgb(tint)), ) }) - .child(div().flex_1().min_w(px(0.)).truncate().child(head)), + .child(div().flex_1().min_w(px(0.)).truncate().child(head)) + .child(match track_count { + 1 => "1 track".to_string(), + n => format!("{n} tracks"), + }), ) }) - .children(rows) + .when(!(headed && folded), |d| d.children(rows)) }) .collect(); Some(