forge
Explore
Log in

Don't you dare go Hollow

A palette row that goes takes the placements' indexes with it

Browse files at this commit

03073e6900975f36fd60e8568dc5c2d302a87433Ahmet Kaan GümüşParent: 6a02fb1
  • fix
    • a palette row that goes takes the placements' indexes with it
Show full message
🐛 fix(engine): a palette row that goes takes the placements' indexes with it

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.rsmodified+93 -14
@@ -981,17 +981,33 @@ impl Project {
981 lane.label(),981 lane.label(),
982 match lane.kind {982 match lane.kind {
983 LaneKind::Video => "video",983 LaneKind::Video => "video",
984- LaneKind::Audio | LaneKind::Subtitle => "audio",984+ LaneKind::Audio => "audio",
985+ // Unreachable while the guard above lets the last subtitle
986+ // lane go, and its own word here so the sentence stays true
987+ // if that rule is ever relaxed.
988+ LaneKind::Subtitle => "subtitle",
985 }989 }
986 )990 )
987 .into());991 .into());
988 }992 }
989- if let Some(sub) = self.lanes[i].subs.first() {993+ if !self.lanes[i].subs.is_empty() {
994+ // Three names and a count, exactly as the clips below get: the way
995+ // out of the refusal is the drag, so the words say so.
996+ let named: Vec<String> = self.lanes[i]
997+ .subs
998+ .iter()
999+ .take(3)
1000+ .map(|s| format!("{} at frame {}", self.track_name(s.track), s.start))
1001+ .collect();
1002+ let rest = match self.lanes[i].subs.len().saturating_sub(named.len()) {
1003+ 0 => String::new(),
1004+ n => format!(" and {n} more"),
1005+ };
990 return Err(format!(1006 return Err(format!(
991- "{} still holds {} subtitle(s), the first at frame {}: delete those first",1007+ "{} still holds {}{rest}: delete those captions (or drag them to \
1008+ another track) first",
992 lane.label(),1009 lane.label(),
993- self.lanes[i].subs.len(),1010+ named.join(", ")
994- sub.start
995 )1011 )
996 .into());1012 .into());
997 }1013 }
@@ -1431,7 +1447,18 @@ impl Project {
1431 /// Every index past `idx` moves down by one, as [`remove_source`](1447 /// Every index past `idx` moves down by one, as [`remove_source`](
1432 /// Project::remove_source)'s do: a caller holding a picked row (an export's1448 /// Project::remove_source)'s do: a caller holding a picked row (an export's
1433 /// [`crate::export::ExportSettings::subtitles`]) has to fix it up or drop1449 /// [`crate::export::ExportSettings::subtitles`]) has to fix it up or drop
1434- /// it, or it names a different track afterwards.1450+ /// it, or it names a different track afterwards. The placements on the
1451+ /// lanes are *not* a caller's to fix: a [`SubClip`] names its track by
1452+ /// index into this very list, so every one past `idx` is walked down with
1453+ /// it here -- the reindex [`remove_source`](Project::remove_source) does
1454+ /// for its clips, for its reason.
1455+ ///
1456+ /// Refused, changing nothing, while anything placed on a subtitle lane
1457+ /// plays *this* row: the placement would have to be deleted or retargeted,
1458+ /// and either one is an edit nobody asked for. The refusal names the lane
1459+ /// and the frame it is at, up to three of them --
1460+ /// [`remove_source`](Project::remove_source) refuses the same way for the
1461+ /// same silent corruption.
1435 ///1462 ///
1436 /// corner-cut: not an undo step, for the reason the limiter is not -- the1463 /// corner-cut: not an undo step, for the reason the limiter is not -- the
1437 /// history snapshots hold the lane list and subtitles are not on it1464 /// history snapshots hold the lane list and subtitles are not on it
@@ -1439,15 +1466,51 @@ impl Project {
1439 /// on -- [`crate::PlaybackSession::import_subtitles`], the panel's own1466 /// on -- [`crate::PlaybackSession::import_subtitles`], the panel's own
1440 /// door, which reads the subtitles of a file and nothing else -- and not a1467 /// door, which reads the subtitles of a file and nothing else -- and not a
1441 /// ctrl+z; the upgrade path is the same one the limiter has: a history1468 /// ctrl+z; the upgrade path is the same one the limiter has: a history
1442- /// entry holding the project's own settings beside the lanes.1469+ /// entry holding the project's own settings beside the lanes. The history
1470+ /// is *cleared* rather than kept, as a source removal clears it: the
1471+ /// snapshots hold the indexes as they were before the reindex, so an undo
1472+ /// into one would put every placement past `idx` on the wrong track.
1443 pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> {1473 pub fn remove_subtitles(&mut self, idx: usize) -> crate::Result<()> {
1444 if idx >= self.subtitles.len() {1474 if idx >= self.subtitles.len() {
1445 return Err(format!("there is no subtitle track {idx} to remove").into());1475 return Err(format!("there is no subtitle track {idx} to remove").into());
1446 }1476 }
1477+ let placed: Vec<String> = handles(&self.lanes)
1478+ .into_iter()
1479+ .zip(&self.lanes)
1480+ .flat_map(|(lane, data)| {
1481+ data.subs
1482+ .iter()
1483+ .filter(move |s| s.track == idx)
1484+ .map(move |s| format!("{} at frame {}", lane.label(), s.start))
1485+ })
1486+ .take(3)
1487+ .collect();
1488+ if !placed.is_empty() {
1489+ return Err(format!(
1490+ "{} is on the timeline ({}): delete those clips first",
1491+ self.track_name(idx),
1492+ placed.join(", ")
1493+ )
1494+ .into());
1495+ }
1447 self.subtitles.remove(idx);1496 self.subtitles.remove(idx);
1497+ for s in self.lanes.iter_mut().flat_map(|l| &mut l.subs) {
1498+ if s.track > idx {
1499+ s.track -= 1;
1500+ }
1501+ }
1502+ self.history.clear();
1448 Ok(())1503 Ok(())
1449 }1504 }
1450 1505
1506+ /// What a refusal calls a palette row: the name the panel shows, and the
1507+ /// index alone for a row that is not there.
1508+ fn track_name(&self, track: usize) -> String {
1509+ self.subtitles
1510+ .get(track)
1511+ .map_or_else(|| format!("subtitle track {track}"), |t| t.label.clone())
1512+ }
1513+
1451 /// The subtitle lanes in display order -- what a front-end lays out and1514 /// The subtitle lanes in display order -- what a front-end lays out and
1452 /// what [`sub_lane_cues`](Project::sub_lane_cues) is asked of, the twin of1515 /// what [`sub_lane_cues`](Project::sub_lane_cues) is asked of, the twin of
1453 /// [`audio_lanes`](Project::audio_lanes).1516 /// [`audio_lanes`](Project::audio_lanes).
@@ -1469,6 +1532,13 @@ impl Project {
1469 /// twin, with one deliberate difference: it **refuses** an overlap instead1532 /// twin, with one deliberate difference: it **refuses** an overlap instead
1470 /// of overwriting what it lands on.1533 /// of overwriting what it lands on.
1471 ///1534 ///
1535+ /// `at` is where it lands and [`sub.start`](SubClip::start) is ignored,
1536+ /// exactly as [`place`](Project::place) ignores a [`Clip`]'s: what a drag
1537+ /// carries is a *window of a track* and where the hand let go, and the
1538+ /// second of those is the argument. So a caller builds the placement once
1539+ /// (from the palette row it dragged) and drops it at as many frames as it
1540+ /// likes without rewriting the field each time.
1541+ ///
1472 /// A picture placed over a picture hides it and the hidden one is still1542 /// A picture placed over a picture hides it and the hidden one is still
1473 /// there to drag back out; two captions in one frame are two lines nobody1543 /// there to drag back out; two captions in one frame are two lines nobody
1474 /// asked to stack, and the words that lost are gone from the screen with1544 /// asked to stack, and the words that lost are gone from the screen with
@@ -1548,7 +1618,9 @@ impl Project {
1548 ///1618 ///
1549 /// One undo step on success; nothing changed and no step on a refusal --1619 /// One undo step on success; nothing changed and no step on a refusal --
1550 /// an index that is not there, a lane that is not a subtitle lane or is not1620 /// an index that is not there, a lane that is not a subtitle lane or is not
1551- /// there, an overlap, and a drop that changes neither lane nor frame.1621+ /// there, and an overlap. A drop that changes neither lane nor frame is
1622+ /// `Ok` and no step: a hand that picked a caption up and put it back has
1623+ /// done nothing wrong, and a front-end showing every `Err` would say so.
1552 pub fn move_sub(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> crate::Result<()> {1624 pub fn move_sub(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> crate::Result<()> {
1553 let Some(sub) = self.sub_lane(from).get(idx).copied() else {1625 let Some(sub) = self.sub_lane(from).get(idx).copied() else {
1554 return Err(format!("there is no subtitle {idx} on {}", from.label()).into());1626 return Err(format!("there is no subtitle {idx} on {}", from.label()).into());
@@ -1564,7 +1636,10 @@ impl Project {
1564 return Err(format!("there is no {} to move onto", to.label()).into());1636 return Err(format!("there is no {} to move onto", to.label()).into());
1565 };1637 };
1566 if (here, start) == (dest, sub.start) {1638 if (here, start) == (dest, sub.start) {
1567- return Err("that subtitle is already there".into());1639+ // A pick-up-put-back: the drag ended where it began, which is not a
1640+ // mistake to name -- `Ok`, and no undo step, because nothing
1641+ // changed. A refusal here would toast a hand that did nothing.
1642+ return Ok(());
1568 }1643 }
1569 if start.checked_add(sub.frames).is_none() {1644 if start.checked_add(sub.frames).is_none() {
1570 return Err(1645 return Err(
@@ -1620,7 +1695,10 @@ impl Project {
1620 /// caller's table for, and which this one can simply read.1695 /// caller's table for, and which this one can simply read.
1621 ///1696 ///
1622 /// `Err`, with nothing changed and no undo step, for an index that is not1697 /// `Err`, with nothing changed and no undo step, for an index that is not
1623- /// there, an unusable `fps`, and an edge already where it was asked to go.1698+ /// there and an unusable `fps`. An edge that ends up where it already was
1699+ /// -- the hand did not move it, or the clamp stopped it at a wall it stood
1700+ /// against -- is `Ok` and no step, for [`move_sub`](Project::move_sub)'s
1701+ /// reason: a drag that stops at a wall is not a mistake to report.
1624 pub fn trim_sub(1702 pub fn trim_sub(
1625 &mut self,1703 &mut self,
1626 lane: Lane,1704 lane: Lane,
@@ -1644,10 +1722,11 @@ impl Project {
1644 Edge::End => sub.end(),1722 Edge::End => sub.end(),
1645 };1723 };
1646 if to == at {1724 if to == at {
1647- return Err(format!(1725+ // The edge is where it was asked to go -- either the hand did not
1648- "that edge is at frame {at} already -- it cannot go past frames {lo}..{hi}"1726+ // move it, or the clamp above stopped it at a wall it was already
1649- )1727+ // standing against. Both are a drag that changed nothing: `Ok`, no
1650- .into());1728+ // undo step, and no toast for a box that simply stopped moving.
1729+ return Ok(());
1651 }1730 }
1652 self.snapshot();1731 self.snapshot();
1653 let i = self.index(lane).expect("the subtitle was found on it");1732 let i = self.index(lane).expect("the subtitle was found on it");
crates/engine/tests/lanes.rsmodified+5 -1
@@ -508,7 +508,11 @@ fn a_subtitle_lane_is_a_peer_and_every_media_path_refuses_it() {
508 508
509 // The lane's own life cycle, which is the machinery it *does* use.509 // The lane's own life cycle, which is the machinery it *does* use.
510 let held = project.remove_lane(s1).expect_err("it still holds a caption");510 let held = project.remove_lane(s1).expect_err("it still holds a caption");
511- assert!(held.to_string().contains("still holds 1 subtitle(s)"), "{held}");511+ assert!(
512+ held.to_string().contains("at frame 0")
513+ && held.to_string().contains("drag them to another track"),
514+ "the refusal names the caption and the way out: {held}"
515+ );
512 assert!(project.lift_sub(s1, 0), "the caption comes off");516 assert!(project.lift_sub(s1, 0), "the caption comes off");
513 project.remove_lane(s1).expect("and then so does the lane");517 project.remove_lane(s1).expect("and then so does the lane");
514 assert_eq!(project.lanes(), [Lane::V1, Lane::A1], "the last one may go");518 assert_eq!(project.lanes(), [Lane::V1, Lane::A1], "the last one may go");
crates/engine/tests/subtitles.rsmodified+121 -4
@@ -11,7 +11,7 @@ use std::path::PathBuf;
11 11
12 use engine::project::{Edge, Lane, LaneKind, SubClip};12 use engine::project::{Edge, Lane, LaneKind, SubClip};
13 use engine::scratch::Scratch;13 use engine::scratch::Scratch;
14-use engine::subtitle::{self, Cue};14+use engine::subtitle::{self, Cue, SubtitleTrack};
15 15
16 fn asset(name: &str) -> PathBuf {16 fn asset(name: &str) -> PathBuf {
17 PathBuf::from(env!("CARGO_MANIFEST_DIR"))17 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() {
939 "both cues clipped to the window and shifted to the placement"939 "both cues clipped to the window and shifted to the placement"
940 );940 );
941 941
942- // A placement whose track has been taken off the palette shows nothing --942+ // The palette row cannot be taken off under the placement that plays it --
943- // never another track's words.943+ // the refusal names the lane and the frame it is at.
944- project.remove_subtitles(0).expect("the row comes off");944+ let why = project
945+ .remove_subtitles(0)
946+ .expect_err("a placed row does not come off")
947+ .to_string();
948+ assert!(
949+ why.contains("S1 at frame 0") && why.contains("delete those clips first"),
950+ "the refusal names where it is placed: {why}"
951+ );
952+
953+ // A placement whose track is gone all the same -- a load that put a shorter
954+ // palette back, which is the only door left -- shows nothing, never another
955+ // track's words.
956+ let project = project.with_subtitles(Vec::new());
945 assert!(957 assert!(
946 project.sub_lane_cues(lane, LANE_FPS).is_empty(),958 project.sub_lane_cues(lane, LANE_FPS).is_empty(),
947 "a placement with no track left shows nothing"959 "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() {
952 );964 );
953 }965 }
954 966
967+/// A palette row taken off while *another* row is placed: the bug a
968+/// placement-that-names-an-index invites, and the rule
969+/// [`engine::Project::remove_source`] already follows -- the rows below the one
970+/// that went move down, so every clip that named one has to move with them or
971+/// it plays somebody else's words.
972+#[test]
973+fn a_removed_palette_row_walks_the_placements_below_it_down() {
974+ let mut project = engine::Project::single(FILE, 150);
975+ let base = subtitle::open(&data("test_subs.srt"), None);
976+ // Three rows of one file: `add_subtitles` dedupes by (path, track), so
977+ // three track numbers are three rows without three files.
978+ for (n, label) in [(0u64, "eng"), (1, "fra - Signs"), (2, "spa")] {
979+ assert!(
980+ project.add_subtitles(&SubtitleTrack {
981+ track: Some(n),
982+ label: label.into(),
983+ ..base.clone()
984+ }),
985+ "row {label} goes on the palette"
986+ );
987+ }
988+ let lane = project.add_lane(LaneKind::Subtitle);
989+ project
990+ .place_sub(lane, 30, whole(1))
991+ .expect("the middle row is placed");
992+ assert_eq!(
993+ project.subtitles()[project.sub_lane(lane)[0].track].label,
994+ "fra - Signs"
995+ );
996+
997+ // The row above it comes off -- nothing plays it -- and the placement is
998+ // walked down with the palette, so it still says the same words.
999+ project.remove_subtitles(0).expect("an unplaced row comes off");
1000+ assert_eq!(
1001+ project.subtitles().iter().map(|t| &*t.label).collect::<Vec<_>>(),
1002+ ["fra - Signs", "spa"],
1003+ "the survivors keep their order"
1004+ );
1005+ assert_eq!(project.sub_lane(lane)[0].track, 0, "the placement moved down");
1006+ assert_eq!(
1007+ project.subtitles()[project.sub_lane(lane)[0].track].label,
1008+ "fra - Signs",
1009+ "and names the very track it named before"
1010+ );
1011+
1012+ // The history went with it: its snapshots hold the indexes as they were
1013+ // before the reindex, so an undo into one would put the placement back on a
1014+ // track that is no longer there.
1015+ assert!(
1016+ !project.undo(),
1017+ "no step survives a reindex the snapshots do not know about"
1018+ );
1019+
1020+ // ...and now that it *is* placed, the same row does not come off at all:
1021+ // the refusal names the lane, the frame, and the way out.
1022+ let why = project
1023+ .remove_subtitles(0)
1024+ .expect_err("a placed row stays")
1025+ .to_string();
1026+ assert!(
1027+ why.contains("fra - Signs") && why.contains("S1 at frame 30"),
1028+ "the refusal names the row and where it plays: {why}"
1029+ );
1030+ assert_eq!(project.subtitles().len(), 2, "and nothing came off");
1031+}
1032+
1033+/// A drag that changes nothing is not a mistake to report: a caption picked up
1034+/// and put back down, and an edge pulled past a wall it already stands against,
1035+/// both come back `Ok` and cost no undo step -- a front-end shows every `Err`
1036+/// as a notice, and neither of these two is worth one.
1037+#[test]
1038+fn a_drag_that_changes_nothing_is_ok_and_costs_no_step() {
1039+ let (mut project, lane) = with_subtitle_lane();
1040+ project
1041+ .place_sub(lane, 30, SubClip { frames: 60, ..whole(0) })
1042+ .expect("it goes down");
1043+ let placed = project.sub_lane(lane)[0];
1044+
1045+ project
1046+ .move_sub(lane, 0, lane, 30)
1047+ .expect("put back where it was picked up");
1048+ assert_eq!(project.sub_lane(lane)[0], placed, "and nothing moved");
1049+
1050+ let (_lo, hi) = project
1051+ .trim_sub_room(lane, 0, Edge::End, LANE_FPS)
1052+ .expect("the room the tail has");
1053+ project
1054+ .trim_sub(lane, 0, Edge::End, hi, LANE_FPS)
1055+ .expect("out to the wall");
1056+ let at_wall = project.sub_lane(lane)[0];
1057+ project
1058+ .trim_sub(lane, 0, Edge::End, hi + 5, LANE_FPS)
1059+ .expect("and past it, which is the wall again");
1060+ assert_eq!(project.sub_lane(lane)[0], at_wall, "the edge stayed put");
1061+
1062+ // Two edits happened, so two steps exist: the trim, then the placement.
1063+ assert!(project.undo(), "the trim walks back");
1064+ assert_eq!(project.sub_lane(lane)[0], placed);
1065+ assert!(project.undo(), "and the placement");
1066+ assert!(
1067+ project.sub_lane(lane).is_empty(),
1068+ "the two no-ops left no step of their own"
1069+ );
1070+}
1071+
955 /// The razor and the ripple, which is the whole reason a caption is a lane and1072 /// The razor and the ripple, which is the whole reason a caption is a lane and
956 /// not a setting: a cut through the picture cuts the words over it, and the1073 /// not a setting: a cut through the picture cuts the words over it, and the
957 /// windows follow by proportion -- no frame rate anywhere in that arithmetic.1074 /// windows follow by proportion -- no frame rate anywhere in that arithmetic.