From 03073e6900975f36fd60e8568dc5c2d302a87433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 14 Aug 2026 05:48:01 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(engine):=20a=20palette=20row?= =?UTF-8?q?=20that=20goes=20takes=20the=20placements'=20indexes=20with=20i?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A `SubClip` names its subtitle track by index into the project's palette, exactly as a `Clip` names its source -- so `remove_subtitles` owed `remove_source`'s rule and did not have it: a row removed above a placement left every clip past it playing the *next* track's words. The probe that found it: three rows, the middle one placed, remove the first -- the caption on the lane silently became another language. Now it follows that rule whole. Refused, naming the lane and the frame up to three of them, while anything on a subtitle lane plays *this* row; allowed otherwise, walking every placement past it down one; and the undo history is cleared, because its snapshots hold the indexes as they were before the walk. The refusal reaches a person already -- the x on a subtitle row shows the engine's words verbatim. Five smaller things the same read turned up, each its own hunk: - a drag that changes nothing is `Ok` and no step, not a refusal: a caption picked up and put back, and an edge pulled past a wall it already stands against, are both a hand that did nothing wrong, and a front-end shows every `Err` as a notice. - `remove_lane` names the captions it still holds and the way out -- three names, a count, "or drag them to another track" -- instead of "1 subtitle(s), the first at frame 0". - its "only track" sentence gets its own arm for a subtitle lane, so the word stays true if the guard that makes it unreachable is relaxed. - `place_sub` says in words that `at` is where it lands and `sub.start` is ignored, which is `place`'s own contract for a clip. - `track_name` is the one place a refusal turns an index into the name a panel shows. --- crates/engine/src/project.rs | 107 ++++++++++++++++++++++---- crates/engine/tests/lanes.rs | 6 +- crates/engine/tests/subtitles.rs | 125 ++++++++++++++++++++++++++++++- 3 files changed, 219 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index bccbdbf..50d95f7 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -981,17 +981,33 @@ impl Project { lane.label(), match lane.kind { LaneKind::Video => "video", - LaneKind::Audio | LaneKind::Subtitle => "audio", + LaneKind::Audio => "audio", + // Unreachable while the guard above lets the last subtitle + // lane go, and its own word here so the sentence stays true + // if that rule is ever relaxed. + LaneKind::Subtitle => "subtitle", } ) .into()); } - if let Some(sub) = self.lanes[i].subs.first() { + if !self.lanes[i].subs.is_empty() { + // Three names and a count, exactly as the clips below get: the way + // out of the refusal is the drag, so the words say so. + let named: Vec = self.lanes[i] + .subs + .iter() + .take(3) + .map(|s| format!("{} at frame {}", self.track_name(s.track), s.start)) + .collect(); + let rest = match self.lanes[i].subs.len().saturating_sub(named.len()) { + 0 => String::new(), + n => format!(" and {n} more"), + }; return Err(format!( - "{} still holds {} subtitle(s), the first at frame {}: delete those first", + "{} still holds {}{rest}: delete those captions (or drag them to \ + another track) first", lane.label(), - self.lanes[i].subs.len(), - sub.start + named.join(", ") ) .into()); } @@ -1431,7 +1447,18 @@ impl Project { /// Every index past `idx` moves down by one, as [`remove_source`]( /// Project::remove_source)'s do: a caller holding a picked row (an export's /// [`crate::export::ExportSettings::subtitles`]) has to fix it up or drop - /// it, or it names a different track afterwards. + /// it, or it names a different track afterwards. The placements on the + /// lanes are *not* a caller's to fix: a [`SubClip`] names its track by + /// index into this very list, so every one past `idx` is walked down with + /// it here -- the reindex [`remove_source`](Project::remove_source) does + /// for its clips, for its reason. + /// + /// Refused, changing nothing, while anything placed on a subtitle lane + /// plays *this* row: the placement would have to be deleted or retargeted, + /// and either one is an edit nobody asked for. The refusal names the lane + /// and the frame it is at, up to three of them -- + /// [`remove_source`](Project::remove_source) refuses the same way for the + /// same silent corruption. /// /// corner-cut: not an undo step, for the reason the limiter is not -- the /// history snapshots hold the lane list and subtitles are not on it @@ -1439,15 +1466,51 @@ impl Project { /// on -- [`crate::PlaybackSession::import_subtitles`], the panel's own /// door, which reads the subtitles of a file and nothing else -- and not a /// ctrl+z; the upgrade path is the same one the limiter has: a history - /// entry holding the project's own settings beside the lanes. + /// entry holding the project's own settings beside the lanes. The history + /// is *cleared* rather than kept, as a source removal clears it: the + /// snapshots hold the indexes as they were before the reindex, so an undo + /// into one would put every placement past `idx` on the wrong track. pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> { if idx >= self.subtitles.len() { return Err(format!("there is no subtitle track {idx} to remove").into()); } + let placed: Vec = handles(&self.lanes) + .into_iter() + .zip(&self.lanes) + .flat_map(|(lane, data)| { + data.subs + .iter() + .filter(move |s| s.track == idx) + .map(move |s| format!("{} at frame {}", lane.label(), s.start)) + }) + .take(3) + .collect(); + if !placed.is_empty() { + return Err(format!( + "{} is on the timeline ({}): delete those clips first", + self.track_name(idx), + placed.join(", ") + ) + .into()); + } self.subtitles.remove(idx); + for s in self.lanes.iter_mut().flat_map(|l| &mut l.subs) { + if s.track > idx { + s.track -= 1; + } + } + self.history.clear(); Ok(()) } + /// What a refusal calls a palette row: the name the panel shows, and the + /// index alone for a row that is not there. + fn track_name(&self, track: usize) -> String { + self.subtitles + .get(track) + .map_or_else(|| format!("subtitle track {track}"), |t| t.label.clone()) + } + /// The subtitle lanes in display order -- what a front-end lays out and /// what [`sub_lane_cues`](Project::sub_lane_cues) is asked of, the twin of /// [`audio_lanes`](Project::audio_lanes). @@ -1469,6 +1532,13 @@ impl Project { /// twin, with one deliberate difference: it **refuses** an overlap instead /// of overwriting what it lands on. /// + /// `at` is where it lands and [`sub.start`](SubClip::start) is ignored, + /// exactly as [`place`](Project::place) ignores a [`Clip`]'s: what a drag + /// carries is a *window of a track* and where the hand let go, and the + /// second of those is the argument. So a caller builds the placement once + /// (from the palette row it dragged) and drops it at as many frames as it + /// likes without rewriting the field each time. + /// /// A picture placed over a picture hides it and the hidden one is still /// there to drag back out; two captions in one frame are two lines nobody /// asked to stack, and the words that lost are gone from the screen with @@ -1548,7 +1618,9 @@ impl Project { /// /// One undo step on success; nothing changed and no step on a refusal -- /// an index that is not there, a lane that is not a subtitle lane or is not - /// there, an overlap, and a drop that changes neither lane nor frame. + /// there, and an overlap. A drop that changes neither lane nor frame is + /// `Ok` and no step: a hand that picked a caption up and put it back has + /// done nothing wrong, and a front-end showing every `Err` would say so. pub fn move_sub(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> crate::Result<()> { let Some(sub) = self.sub_lane(from).get(idx).copied() else { return Err(format!("there is no subtitle {idx} on {}", from.label()).into()); @@ -1564,7 +1636,10 @@ impl Project { return Err(format!("there is no {} to move onto", to.label()).into()); }; if (here, start) == (dest, sub.start) { - return Err("that subtitle is already there".into()); + // A pick-up-put-back: the drag ended where it began, which is not a + // mistake to name -- `Ok`, and no undo step, because nothing + // changed. A refusal here would toast a hand that did nothing. + return Ok(()); } if start.checked_add(sub.frames).is_none() { return Err( @@ -1620,7 +1695,10 @@ impl Project { /// caller's table for, and which this one can simply read. /// /// `Err`, with nothing changed and no undo step, for an index that is not - /// there, an unusable `fps`, and an edge already where it was asked to go. + /// there and an unusable `fps`. An edge that ends up where it already was + /// -- the hand did not move it, or the clamp stopped it at a wall it stood + /// against -- is `Ok` and no step, for [`move_sub`](Project::move_sub)'s + /// reason: a drag that stops at a wall is not a mistake to report. pub fn trim_sub( &mut self, lane: Lane, @@ -1644,10 +1722,11 @@ impl Project { Edge::End => sub.end(), }; if to == at { - return Err(format!( - "that edge is at frame {at} already -- it cannot go past frames {lo}..{hi}" - ) - .into()); + // The edge is where it was asked to go -- either the hand did not + // move it, or the clamp above stopped it at a wall it was already + // standing against. Both are a drag that changed nothing: `Ok`, no + // undo step, and no toast for a box that simply stopped moving. + return Ok(()); } self.snapshot(); let i = self.index(lane).expect("the subtitle was found on it"); diff --git a/crates/engine/tests/lanes.rs b/crates/engine/tests/lanes.rs index da79ff7..c53784b 100644 --- a/crates/engine/tests/lanes.rs +++ b/crates/engine/tests/lanes.rs @@ -508,7 +508,11 @@ fn a_subtitle_lane_is_a_peer_and_every_media_path_refuses_it() { // The lane's own life cycle, which is the machinery it *does* use. let held = project.remove_lane(s1).expect_err("it still holds a caption"); - assert!(held.to_string().contains("still holds 1 subtitle(s)"), "{held}"); + assert!( + held.to_string().contains("at frame 0") + && held.to_string().contains("drag them to another track"), + "the refusal names the caption and the way out: {held}" + ); assert!(project.lift_sub(s1, 0), "the caption comes off"); project.remove_lane(s1).expect("and then so does the lane"); assert_eq!(project.lanes(), [Lane::V1, Lane::A1], "the last one may go"); diff --git a/crates/engine/tests/subtitles.rs b/crates/engine/tests/subtitles.rs index 08cb1fc..b1c78b9 100644 --- a/crates/engine/tests/subtitles.rs +++ b/crates/engine/tests/subtitles.rs @@ -11,7 +11,7 @@ use std::path::PathBuf; use engine::project::{Edge, Lane, LaneKind, SubClip}; use engine::scratch::Scratch; -use engine::subtitle::{self, Cue}; +use engine::subtitle::{self, Cue, SubtitleTrack}; fn asset(name: &str) -> PathBuf { PathBuf::from(env!("CARGO_MANIFEST_DIR")) @@ -939,9 +939,21 @@ fn a_lane_maps_to_cues_clipped_to_the_window_and_shifted_onto_the_timeline() { "both cues clipped to the window and shifted to the placement" ); - // A placement whose track has been taken off the palette shows nothing -- - // never another track's words. - project.remove_subtitles(0).expect("the row comes off"); + // The palette row cannot be taken off under the placement that plays it -- + // the refusal names the lane and the frame it is at. + let why = project + .remove_subtitles(0) + .expect_err("a placed row does not come off") + .to_string(); + assert!( + why.contains("S1 at frame 0") && why.contains("delete those clips first"), + "the refusal names where it is placed: {why}" + ); + + // A placement whose track is gone all the same -- a load that put a shorter + // palette back, which is the only door left -- shows nothing, never another + // track's words. + let project = project.with_subtitles(Vec::new()); assert!( project.sub_lane_cues(lane, LANE_FPS).is_empty(), "a placement with no track left shows nothing" @@ -952,6 +964,111 @@ fn a_lane_maps_to_cues_clipped_to_the_window_and_shifted_onto_the_timeline() { ); } +/// A palette row taken off while *another* row is placed: the bug a +/// placement-that-names-an-index invites, and the rule +/// [`engine::Project::remove_source`] already follows -- the rows below the one +/// that went move down, so every clip that named one has to move with them or +/// it plays somebody else's words. +#[test] +fn a_removed_palette_row_walks_the_placements_below_it_down() { + let mut project = engine::Project::single(FILE, 150); + let base = subtitle::open(&data("test_subs.srt"), None); + // Three rows of one file: `add_subtitles` dedupes by (path, track), so + // three track numbers are three rows without three files. + for (n, label) in [(0u64, "eng"), (1, "fra - Signs"), (2, "spa")] { + assert!( + project.add_subtitles(&SubtitleTrack { + track: Some(n), + label: label.into(), + ..base.clone() + }), + "row {label} goes on the palette" + ); + } + let lane = project.add_lane(LaneKind::Subtitle); + project + .place_sub(lane, 30, whole(1)) + .expect("the middle row is placed"); + assert_eq!( + project.subtitles()[project.sub_lane(lane)[0].track].label, + "fra - Signs" + ); + + // The row above it comes off -- nothing plays it -- and the placement is + // walked down with the palette, so it still says the same words. + project.remove_subtitles(0).expect("an unplaced row comes off"); + assert_eq!( + project.subtitles().iter().map(|t| &*t.label).collect::>(), + ["fra - Signs", "spa"], + "the survivors keep their order" + ); + assert_eq!(project.sub_lane(lane)[0].track, 0, "the placement moved down"); + assert_eq!( + project.subtitles()[project.sub_lane(lane)[0].track].label, + "fra - Signs", + "and names the very track it named before" + ); + + // The history went with it: its snapshots hold the indexes as they were + // before the reindex, so an undo into one would put the placement back on a + // track that is no longer there. + assert!( + !project.undo(), + "no step survives a reindex the snapshots do not know about" + ); + + // ...and now that it *is* placed, the same row does not come off at all: + // the refusal names the lane, the frame, and the way out. + let why = project + .remove_subtitles(0) + .expect_err("a placed row stays") + .to_string(); + assert!( + why.contains("fra - Signs") && why.contains("S1 at frame 30"), + "the refusal names the row and where it plays: {why}" + ); + assert_eq!(project.subtitles().len(), 2, "and nothing came off"); +} + +/// A drag that changes nothing is not a mistake to report: a caption picked up +/// and put back down, and an edge pulled past a wall it already stands against, +/// both come back `Ok` and cost no undo step -- a front-end shows every `Err` +/// as a notice, and neither of these two is worth one. +#[test] +fn a_drag_that_changes_nothing_is_ok_and_costs_no_step() { + let (mut project, lane) = with_subtitle_lane(); + project + .place_sub(lane, 30, SubClip { frames: 60, ..whole(0) }) + .expect("it goes down"); + let placed = project.sub_lane(lane)[0]; + + project + .move_sub(lane, 0, lane, 30) + .expect("put back where it was picked up"); + assert_eq!(project.sub_lane(lane)[0], placed, "and nothing moved"); + + let (_lo, hi) = project + .trim_sub_room(lane, 0, Edge::End, LANE_FPS) + .expect("the room the tail has"); + project + .trim_sub(lane, 0, Edge::End, hi, LANE_FPS) + .expect("out to the wall"); + let at_wall = project.sub_lane(lane)[0]; + project + .trim_sub(lane, 0, Edge::End, hi + 5, LANE_FPS) + .expect("and past it, which is the wall again"); + assert_eq!(project.sub_lane(lane)[0], at_wall, "the edge stayed put"); + + // Two edits happened, so two steps exist: the trim, then the placement. + assert!(project.undo(), "the trim walks back"); + assert_eq!(project.sub_lane(lane)[0], placed); + assert!(project.undo(), "and the placement"); + assert!( + project.sub_lane(lane).is_empty(), + "the two no-ops left no step of their own" + ); +} + /// The razor and the ripple, which is the whole reason a caption is a lane and /// not a setting: a cut through the picture cuts the words over it, and the /// windows follow by proportion -- no frame rate anywhere in that arithmetic.