From 25855e817cd6c5a8df49b269fbfbc9523f21d255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 15 Aug 2026 20:31:34 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(timeline):=20grouped=20capti?= =?UTF-8?q?on=20edits=20reseek,=20and=20the=20link=20counter=20clears=20ca?= =?UTF-8?q?ption=20ids?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A grouped caption's drag and trim move the group's clips with it -- a timeline->source change -- so PlaybackSession::move_sub and trim_sub now invalidate Dirty::Both when the caption is grouped with clips (the check delete_sub already made, now shared as caption_grouped_with_clips), and the app pays the reset_after_reseek a clip's drag and trim pay on the same paths. The link counter a load seeds now sits past every id the file names: from_parts chains the subs a lane already holds, and with_subs -- the door a load's placements actually come through, after from_parts -- raises the counter past the ids its captions carry. A hand-grouped caption holding the file's highest id could have had it re-issued to the next group that asked, silently grouping it with clips it never met. A palette drop onto a subtitle lane renumbers that lane alone; the picks it held now follow the discipline of every other renumbering edit -- kept off the lane, re-read for the marked caption, other lanes' picks untouched. Also folds in the v16 caption-group round-trip test that had been left out of the previous commit. --- crates/app/src/player/timeline_edit.rs | 57 ++++++++++++++------ crates/engine/src/edith.rs | 64 ++++++++++++++++++++++ crates/engine/src/playback.rs | 47 +++++++++++----- crates/engine/src/project.rs | 75 +++++++++++++++++++++++++- 4 files changed, 211 insertions(+), 32 deletions(-) diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index 4521ad0..1d01ef3 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -396,23 +396,39 @@ impl Player { // ([`Project::place_sub`]'s sorted insert), so one placed *before* // the marked caption slid it one along and the index left behind // names its new neighbour -- the caption the next Delete would take. - // The mark goes back on the caption it was on. + // The mark goes back on the caption it was on, every *other* pick + // this lane held goes with the indices that moved (they name + // neighbours now), and picks on other lanes keep what they name: + // a placement renumbers this lane alone. (None, Some(start)) => { let mark = self .session .as_ref() .and_then(|session| sub_mark(session.sub_lane(to), start)) .map(|i| (to, i)); - self.selected = match mark { - Some(mark) => { - let mut sel = Selection::new(); - sel.set_one(mark); - sel + let mut kept = Selection::new(); + for &(lane, idx) in self.selected.picks() { + if lane != to { + kept.add((lane, idx)); } - None => Selection::new(), - }; + } + if let Some(mark) = mark { + kept.add(mark); + } + self.selected = kept; + } + // Nothing marked on this lane, but the placement renumbered it all + // the same: a pick left on a slid index names the wrong caption, + // and the discipline of every renumbering edit applies. + (None, None) => { + let mut kept = Selection::new(); + for &(lane, idx) in self.selected.picks() { + if lane != to { + kept.add((lane, idx)); + } + } + self.selected = kept; } - (None, None) => {} } cx.notify(); } @@ -458,7 +474,9 @@ impl Player { // A lane holds its captions in start order, so the drop moved the // mark as well as the box: it is re-read off where the caption // landed rather than left on the index it had, which after the move - // is a neighbour's. + // is a neighbour's. A grouped caption dragged its clips with it -- + // the engine reseeks itself, and this owes the flag reset a clip's + // drop pays at the same moment. Some(Ok(())) => { let mark = self .session @@ -473,6 +491,9 @@ impl Player { } None => Selection::new(), }; + if drag.sub.link.is_some() { + self.reset_after_reseek(); + } } None => {} } @@ -1103,15 +1124,19 @@ impl Player { // A caption's edge reaches the engine through its own door, at this // timeline's rate. An `Ok` is *not* "something changed" -- an edge that // stopped at a wall it already stood against is `Ok` with no undo step - // ([`Project::trim_sub`]) -- so only the refusal is ever said out loud, - // and nothing reseeks: cues are drawn over whatever is decoded. + // ([`Project::trim_sub`]) -- so only the refusal is ever said out loud. + // A lone caption's trim moves nothing that plays, and nothing reseeks; + // a grouped one trimmed its clips with it, and owes the same flag + // reset a clip's trim pays. if trim.lane.kind == LaneKind::Subtitle { - if let Some(Err(e)) = self + let trimmed = self .session .as_mut() - .map(|session| session.trim_sub(trim.lane, trim.idx, trim.edge, trim.to)) - { - self.notify_user(format!("NOT TRIMMED — {e}").into()); + .map(|session| session.trim_sub(trim.lane, trim.idx, trim.edge, trim.to)); + match trimmed { + Some(Err(e)) => self.notify_user(format!("NOT TRIMMED — {e}").into()), + Some(Ok(())) if trim.link.is_some() => self.reset_after_reseek(), + _ => {} } cx.notify(); return; diff --git a/crates/engine/src/edith.rs b/crates/engine/src/edith.rs index e5831b6..6dfa3b0 100644 --- a/crates/engine/src/edith.rs +++ b/crates/engine/src/edith.rs @@ -1485,6 +1485,70 @@ mod tests { } } + /// The caption's group field (v16): written only when the caption is in a + /// group -- so a project nobody grouped a caption in is the same bytes it + /// was in v15 -- read back as the id, and refused in an older dialect, + /// which never wrote one. + #[test] + fn a_caption_group_round_trips_and_an_old_dialect_refuses_the_field() { + let dir = PathBuf::from("/proj"); + let caption = |start: u32, frames: u32, link| SubClip { + start, + frames, + track: 0, + in_us: 0, + out_us: i64::from(frames) * 1_000_000, + link, + }; + let lanes = vec![ + (LaneKind::Video, vec![clip(0, 0, 30, 0, Some(7))]), + (LaneKind::Audio, vec![clip(0, 0, 30, 0, Some(7))]), + (LaneKind::Subtitle, Vec::new()), + ]; + let subs = vec![vec![], vec![], vec![caption(0, 30, Some(7)), caption(40, 10, None)]]; + let bytes = super::emit( + &dir, + &doc().1, + &lanes, + &[], + &subs, + &[subtitle("/proj/subs.srt", None)], + &[], + &[], + (1280, 720), + None, + crate::tonemap::Preset::default(), + false, + true, + crate::export::EncoderSeat::default(), + Limiter::default(), + 0, + ); + let text = String::from_utf8_lossy(&bytes); + assert!( + text.contains("sub 1 0 30 0 0 30000000 7\n"), + "the grouped caption carries its id: {text}" + ); + assert!( + text.contains("sub 1 40 10 0 0 10000000\n"), + "a caption in no group writes no field: {text}" + ); + let back = parse(&bytes, &dir).expect("v16 parses"); + assert_eq!(back.subs[2][0].link, Some(7)); + assert_eq!(back.subs[2][1].link, None); + + // The older dialect never wrote a sixth field, and one claiming to be + // it is a file this parser refuses by name. + let v15 = parse( + b"edith 15\nplayhead 0\nresolution 1280 720\nsource 0 a.mp4\nsubtitle - subs.srt\n\ + sub 1 0 30 0 0 30000000 7\n", + &dir, + ) + .unwrap_err() + .to_string(); + assert_eq!(v15, "line 6: sub wants 5 fields, found 6"); + } + /// The v12 line: whether the project is cut on the stand-ins, written only /// when it is -- and the dialect before it, which had no stand-ins and /// whose bytes are therefore unchanged bar the version. diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 1b3ec6f..3ea8eb5 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -1753,9 +1753,16 @@ impl PlaybackSession { /// Drags a placement to another subtitle lane or another frame /// ([`Project::move_sub`]). One undo step, and `Ok` with none for a drop - /// that changed nothing. + /// that changed nothing. A caption in a group drags the group's clips with + /// it -- a mapping change -- so this reseeks like a clip's drag does; a + /// caption in no group moves nothing that plays and disturbs nothing. pub fn move_sub(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> crate::Result<()> { - self.project.move_sub(from, idx, to, start) + let grouped = self.caption_grouped_with_clips(from, idx); + let moved = self.project.move_sub(from, idx, to, start); + if moved.is_ok() && grouped { + self.invalidate(Dirty::Both); + } + moved } /// Drags one edge of a placement to timeline frame `to` @@ -1763,9 +1770,16 @@ impl PlaybackSession { /// placement's position is in frames while its words are in microseconds, /// and the rate that joins them is the one thing a [`Project`] does not /// know. That is why a front-end trims through this door and never converts - /// a cue's microseconds itself. One undo step. + /// a cue's microseconds itself. One undo step. A caption in a group trims + /// the group's clips with it, and that half reseeked; a lone caption's trim + /// changes only the words, which are drawn over whatever is decoded. pub fn trim_sub(&mut self, lane: Lane, idx: usize, edge: Edge, to: u32) -> crate::Result<()> { - self.project.trim_sub(lane, idx, edge, to, self.meta.frame_rate) + let grouped = self.caption_grouped_with_clips(lane, idx); + let trimmed = self.project.trim_sub(lane, idx, edge, to, self.meta.frame_rate); + if trimmed.is_ok() && grouped { + self.invalidate(Dirty::Both); + } + trimmed } /// How far that edge may travel, `(first, last)` timeline frame inclusive @@ -1841,8 +1855,20 @@ impl PlaybackSession { pub fn delete_sub(&mut self, lane: Lane, idx: usize) -> bool { // Which of the two, asked before anything moves: the reseek a grouped // delete owes is the whole difference between the paths. - let grouped = self - .project + match self.caption_grouped_with_clips(lane, idx) { + true => self.edit(Dirty::Both, |p| p.delete_sub_in(lane, idx)), + // A caption in no group -- or one grouped only with other captions, + // which lift and move nothing that plays -- is the lift it always + // was. + false => self.project.delete_sub_in(lane, idx), + } + } + + /// Whether the caption at `idx` of `lane` is grouped with clips on other + /// lanes: the one question every caption edit that can move media has to + // ask first, because the answer is whether the edit owes a reseek. + fn caption_grouped_with_clips(&self, lane: Lane, idx: usize) -> bool { + self.project .sub_lane(lane) .get(idx) .and_then(|s| s.link) @@ -1851,14 +1877,7 @@ impl PlaybackSession { .lanes() .into_iter() .any(|l| l != lane && self.project.lane(l).iter().any(|c| c.link == Some(id))) - }); - match grouped { - true => self.edit(Dirty::Both, |p| p.delete_sub_in(lane, idx)), - // A caption in no group -- or one grouped only with other captions, - // which lift and move nothing that plays -- is the lift it always - // was. - false => self.project.delete_sub_in(lane, idx), - } + }) } /// What the clip at `idx` of `lane` is equalized with, or `None` for one diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index ccf4c1b..7a26cf5 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -919,10 +919,17 @@ impl Project { } } links_are_consistent(&lanes)?; + // The counter sits past every id the file names, on either of a lane's + // lists: a v16 caption may carry the highest link there is, and a + // re-issued id would silently group it with the next placement that + // asks for one -- the loader refusing a save the editor just wrote. let next_link = lanes .iter() - .flat_map(|l| &l.clips) - .filter_map(|c| c.link) + .flat_map(|l| { + let clips = l.clips.iter().filter_map(|c| c.link); + let subs = l.subs.iter().filter_map(|s| s.link); + clips.chain(subs) + }) .max() // Saturating so a crafted file cannot make the counter wrap: at the // ceiling ids stop being fresh, which loses grouping, not memory. @@ -1445,6 +1452,17 @@ impl Project { return Err(format!("the {name} lane is out of order or overlaps itself").into()); } data.subs = list; + // The counter also has to clear the ids these captions carry: this + // is the door a load's placements come through, *after* + // [`from_parts`](Project::from_parts) seeded it from the clips, and + // a hand-grouped caption may carry the highest id in the file. + self.next_link = self.next_link.max( + data.subs + .iter() + .filter_map(|s| s.link) + .max() + .map_or(0, |m| m.saturating_add(1)), + ); } Ok(self) } @@ -4802,6 +4820,59 @@ mod tests { assert_eq!(shape(&p), before); } + /// The link counter a load seeds sits past every id the file names, + /// captions included: a hand-grouped caption may carry the highest id there + /// is, and a counter that read the clips alone would hand it to the next + /// group that asked -- silently grouping the caption with clips it never + /// met. + #[test] + fn the_link_counter_sits_past_a_caption_only_id() { + let track = || SubtitleTrack { + path: FILE.into(), + track: None, + language: "eng".into(), + name: String::new(), + label: "eng".into(), + cues: Vec::new(), + bitmap: false, + refused: None, + }; + let mut p = Project::single(FILE, 9); + p.add_lane(LaneKind::Subtitle); + let caption = SubClip { + start: 0, + frames: 6, + track: 0, + in_us: 0, + out_us: 6_000_000, + // The highest id in the file, and only a caption carries it. + link: Some(1), + }; + let mut p = p + .with_subtitles(vec![track()]) + .with_subs(vec![Vec::new(), Vec::new(), vec![caption]]) + .expect("a caption may carry the file's highest id"); + + // A new group on lanes the caption is not on mints the next id -- which + // must not be the caption's. + let v2 = p.add_lane(LaneKind::Video); + assert!(p.place(v2, 0, clip(0, 0, 9, 0))); + p.group_all(&[(Lane::V1, 0), (v2, 0)]) + .expect("the clips group"); + let minted = p.lane(Lane::V1)[0].link.expect("grouped"); + assert_ne!( + minted, + 1, + "the caption's id is not re-issued to clips" + ); + assert_eq!( + p.sub_lane(Lane::new(LaneKind::Subtitle, 0))[0].link, + Some(1), + "the caption keeps its own group" + ); + assert!(links_are_consistent(&p.lanes).is_ok()); + } + /// The caption a hand groups: `group_all` over clips *and* a caption, the /// mates riding along, the offsets kept, the refusals in words. #[test]