diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index 61693d9..7240e16 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -310,6 +310,13 @@ impl Player { /// nothing is said about it. The engine reseeks, so all this owes is the /// flag reset -- and the selection, whose index was that lane's own and now /// names a different clip there. + /// + /// A dragged clip that is *itself* one of the selection's picks carries the + /// whole selection with it: every other pick moves the same frame delta, + /// all-or-nothing ([`PlaybackSession::move_selection_to`]), and the picks + /// survive the move -- their indices remapped rather than cleared, since a + /// pick that travels still names the clip it named before. A clip dragged + /// from outside the selection moves alone, exactly as it always has. pub(crate) fn move_clip( &mut self, from: Lane, @@ -330,10 +337,23 @@ impl Player { ) else { return; }; - let moved = self - .session - .as_mut() - .is_some_and(|session| session.move_clip_to(from, idx, to, start)); + let set_move = self.selected.contains((from, idx)) && self.selected.len() > 1; + let picks: Vec<(Lane, usize)> = self.selected.picks().to_vec(); + // Every pick's own start, read before the move touches anything -- the + // only way to find a pick again afterwards, since a lane change or an + // insert can move the index it was named by. + let pre_starts: Vec> = picks + .iter() + .map(|&(lane, i)| { + self.session + .as_ref() + .and_then(|session| session.lane_clips(lane).get(i).map(|c| c.start)) + }) + .collect(); + let moved = self.session.as_mut().is_some_and(|session| match set_move { + true => session.move_selection_to(&picks, from, idx, to, start), + false => session.move_clip_to(from, idx, to, start), + }); let (kind, lanes) = match from.kind { LaneKind::Video => ("picture", "video"), LaneKind::Audio => ("sound", "audio"), @@ -343,7 +363,18 @@ impl Player { }; match moved { true => { - self.selected.clear(); + // Every pick named a `(lane, ...)` by index into that lane's + // clips, sorted by start -- exactly what an insert or a lane + // change reorders. Re-read by the frame each pick's clip now + // starts at (recorded before the move moved anything), so the + // selection survives naming the clips it named, not the slots + // they used to sit in. + if set_move { + self.selected = + self.remap_selection(&picks, &pre_starts, from, idx, to, was, start); + } else { + self.selected.clear(); + } self.reset_after_reseek(); } // The three ways a drag is refused, told apart by what the @@ -360,6 +391,15 @@ impl Player { // Picked up and put back down where it was: a click, and a click // says nothing. false if from == to && start == was => {} + false if set_move => self.notify_user( + format!( + "NOT MOVED — {} clips selected, and another clip already covers where one of \ + them would land on {}", + picks.len(), + to.label() + ) + .into(), + ), false => self.notify_user( format!( "NOT MOVED — another clip already covers those frames on {}", @@ -371,6 +411,50 @@ impl Player { cx.notify(); } + /// What a selection's picks are called after a set-move that shifted every + /// one of them by `landed - was` timeline frames (the delta the dragged + /// clip's own head travelled, which every pick travelled too -- see + /// [`Project::move_selection`]): the clip a pick named is found again by + /// where it now starts, on the lane it stays on (or `to`, for the one + /// that changed lane), rather than by the index it had, which a lane + /// change or an insert can have moved. A pick whose clip cannot be found + /// there any more (a bad index handed in) is dropped rather than guessed + /// at. + fn remap_selection( + &self, + picks: &[(Lane, usize)], + pre_starts: &[Option], + from: Lane, + dragged: usize, + to: Lane, + was: u32, + landed: u32, + ) -> Selection { + let mut out = Selection::new(); + let Some(session) = self.session.as_ref() else { + return out; + }; + let delta = i64::from(landed) - i64::from(was); + for (&pick, &old_start) in picks.iter().zip(pre_starts) { + let Some(old_start) = old_start else { + continue; + }; + let want = (i64::from(old_start) + delta) as u32; + // Every pick but the dragged clip itself stays on the lane it was + // already on; only the exact clip the hand let go of changed + // lane, so only its own pick is found on `to` rather than `from`. + let now_lane = if pick == (from, dragged) { to } else { pick.0 }; + if let Some(found) = session + .lane_clips(now_lane) + .iter() + .position(|c| c.start == want) + { + out.add((now_lane, found)); + } + } + out + } + /// The whole of a palette track as a placement: from its own first /// microsecond to its last cue, and as many timeline frames as that is worth /// at this rate. What a row dragged out of the Subtitles list carries, and diff --git a/crates/app/src/tests/editing.rs b/crates/app/src/tests/editing.rs index b8a8763..21d4d07 100644 --- a/crates/app/src/tests/editing.rs +++ b/crates/app/src/tests/editing.rs @@ -1897,6 +1897,141 @@ fn a_track_can_be_added_dropped_on_edited_and_taken_back() { assert_eq!(format_refusal(&session, Format::Mp4), None); } +/// [`PlaybackSession::move_selection_to`], the door a selection drag uses +/// ([`Player::move_clip`]'s set path): three picks across two lanes, one of +/// them the dragged clip, all travel the *same* frame delta in one call -- +/// the same all-or-nothing clamp [`Project::move_room`] already gave a +/// linked group, generalised to an arbitrary selection. +#[test] +fn a_selection_drag_moves_every_pick_by_the_same_delta() { + use engine::project::LaneKind; + + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + session.set_gain(0.0); + let path = session.sources()[0].path.clone(); + session.import(&asset("test_av2.mp4")).expect("av2 matches"); + let second = session.sources()[1].path.clone(); + + // clip0 on V1 at the fixture's own start; clip1 on V1 ten seconds in, + // appended to the open bed and rippling nothing. + assert!(session + .place_stream_at(10.0, &second, 0, None) + .expect("av2 is on this timeline")); + let clip0 = session.lane_clips(Lane::V1)[0]; + let clip1 = session.lane_clips(Lane::V1)[1]; + assert_eq!((clip0.start, clip1.start), (0, 300)); + + // clipC on a second video row, well past both of the above. + let v2 = session.add_lane(LaneKind::Video); + assert!(session + .place_stream_at(20.0, &path, 0, Some(v2)) + .expect("test_av.mp4 is on this timeline")); + let clip_c = session.lane_clips(v2)[0]; + assert_eq!(clip_c.start, 600); + + let picks = [(Lane::V1, 0), (Lane::V1, 1), (v2, 0)]; + // Drag clip1 fifty frames along its own lane -- the other two picks own + // no neighbours of their own here, so nothing narrows the room and the + // full fifty-frame ask goes through for all three. + assert!(session.move_selection_to(&picks, Lane::V1, 1, Lane::V1, clip1.start + 50)); + assert_eq!(session.lane_clips(Lane::V1)[0].start, clip0.start + 50); + assert_eq!(session.lane_clips(Lane::V1)[1].start, clip1.start + 50); + assert_eq!(session.lane_clips(v2)[0].start, clip_c.start + 50); + // Every pick still names the clip it named: found again by the frame it + // now starts at -- exactly the lookup `Player::remap_selection` makes + // once the move lands, which is what keeps a set-move's selection alive + // rather than clearing it. + assert_eq!( + session.lane_clips(Lane::V1)[0], + Clip { start: clip0.start + 50, ..clip0 } + ); + assert_eq!( + session.lane_clips(v2)[0], + Clip { start: clip_c.start + 50, ..clip_c } + ); + + assert!(session.undo(), "one step for the whole set-move"); + assert_eq!(session.lane_clips(Lane::V1)[0].start, clip0.start); + assert_eq!(session.lane_clips(Lane::V1)[1].start, clip1.start); + assert_eq!(session.lane_clips(v2)[0].start, clip_c.start); +} + +/// One pick with nowhere to go refuses the whole set: the walk onto another +/// clip's own span that already refuses a lone drag +/// (`move_clip_keeps_the_frames_and_refuses_the_rest`, in this crate's own +/// engine tests) refuses a selection drag exactly the same way, and every +/// *other* pick -- which had all the room in the world -- stays exactly +/// where it was rather than moving on its own. +#[test] +fn a_selection_drag_refuses_as_one_set_when_any_pick_cannot_land() { + use engine::project::LaneKind; + + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + session.set_gain(0.0); + let path = session.sources()[0].path.clone(); + session.import(&asset("test_av2.mp4")).expect("av2 matches"); + let second = session.sources()[1].path.clone(); + + assert!(session + .place_stream_at(10.0, &second, 0, None) + .expect("av2 is on this timeline")); + let clip0 = session.lane_clips(Lane::V1)[0]; + let clip1 = session.lane_clips(Lane::V1)[1]; + + let v2 = session.add_lane(LaneKind::Video); + assert!(session + .place_stream_at(0.0, &path, 0, Some(v2)) + .expect("test_av.mp4 is on this timeline")); + let clip_c = session.lane_clips(v2)[0]; + // A clip clip1 is not picked, blocking the road on V2 the drag is about + // to try: clip1 dragged onto V2 lands its head inside this one. + assert!(session + .place_stream_at(10.0, &second, 0, Some(v2)) + .expect("av2 is on this timeline")); + let blocker = session.lane_clips(v2)[1]; + assert_eq!(blocker.start, 300); + + let picks = [(Lane::V1, 0), (Lane::V1, 1), (v2, 0)]; + // Dragged onto V2 at a frame inside the blocker: refused outright, the + // way a lone drag onto another clip already is. + assert!(!session.move_selection_to( + &picks, + Lane::V1, + 1, + v2, + blocker.start + 10 + )); + // Nothing moved -- not clip1 clamped short of the blocker, and not the + // two picks that had all the room to spare. + assert_eq!(session.lane_clips(Lane::V1)[0], clip0); + assert_eq!(session.lane_clips(Lane::V1)[1], clip1); + assert_eq!(session.lane_clips(v2)[0], clip_c); + assert_eq!(session.lane_clips(v2)[1], blocker); +} + +/// The refactor `move_clip` -> `move_selection` owes: a clip dragged from +/// outside the selection (or with nothing else selected) moves exactly as it +/// always has -- `move_clip_to`'s own door and `move_selection_to`'s +/// selection-of-one both walk the same code, so they cannot drift apart. +#[test] +fn a_lone_clip_drag_is_unchanged_by_the_selection_path() { + use engine::project::LaneKind; + + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + session.set_gain(0.0); + let v2 = session.add_lane(LaneKind::Video); + + assert!(session.move_clip_to(Lane::V1, 0, v2, 0), "V1 -> V2"); + assert!(session.lane_clips(Lane::V1).is_empty()); + assert_eq!(session.lane_clips(v2).len(), 1); + + // The other kind of lane is refused exactly as before. + assert!(!session.move_clip_to(v2, 0, Lane::A1, 0)); + // A drop back where it was picked up is a click, still refused. + let clip = session.lane_clips(v2)[0]; + assert!(!session.move_clip_to(v2, 0, v2, clip.start)); +} + /// A caption grouped with a clip on an *added* lane closes its lane's hole /// too, not just the first pair's: the same law commit `cdc53a6` gave `V1` /// (`whole_take` -> `Project::delete_members`) has to hold from every anchor diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index bf9ecc1..84cba42 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -2374,6 +2374,22 @@ impl PlaybackSession { self.edit(Dirty::Both, |p| p.move_clip(from, idx, to, start)) } + /// [`move_clip_to`](Self::move_clip_to)'s general form: every one of + /// `picks` travels the same frame delta as the dragged clip at `idx` of + /// `from`, all-or-nothing, one undo step for the lot -- what a hand drags + /// on a selection rather than a lone clip. See + /// [`Project::move_selection`] for the clamp that makes it all-or-nothing. + pub fn move_selection_to( + &mut self, + picks: &[(Lane, usize)], + from: Lane, + idx: usize, + to: Lane, + start: u32, + ) -> bool { + self.edit(Dirty::Both, |p| p.move_selection(picks, from, idx, to, start)) + } + /// Drags one end of that clip to timeline frame `to`, changing how much of /// its source it plays and nothing else on the lane -- see [`Project::trim`] /// for the walls it is clamped by, which this fills the source lengths in diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 43643a4..e03b625 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -3591,23 +3591,71 @@ impl Project { /// is on, and a drop that changes neither lane nor frame, which is a clip /// picked up and put back down, i.e. a click. pub fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> bool { + self.move_selection(&[(from, idx)], from, idx, to, start) + } + + /// [`move_clip`](Self::move_clip)'s general form: every pick in `picks` + /// (each with its own link group, exactly as [`group_of`](Self::group_of) + /// reads it) travels the same distance as the clip at `idx` of `from`, + /// which alone changes lane, onto `to`. One clamp across the whole set + /// ([`move_room`](Self::move_room)) is what makes this all-or-nothing: a + /// neighbour that boxes in *any* member narrows the delta every member + /// moves by, and a member with no room at all (`lo > hi`) refuses the + /// whole set rather than moving the rest and leaving that one behind. + /// `move_clip` is this with a selection of one -- the dragged clip's own + /// group and nothing else -- so a drag on a clip outside the selection is + /// byte-identical to before this existed. + pub fn move_selection( + &mut self, + picks: &[(Lane, usize)], + from: Lane, + idx: usize, + to: Lane, + start: u32, + ) -> bool { let (Some(dest), Some(clip)) = (self.index(to), self.lane(from).get(idx).copied()) else { return false; }; if from.kind != to.kind { return false; } - let members = self.group_of(from, idx).expect("the clip was found"); let held = self.index(from).expect("the clip was found on it"); - // The two halves of one group would land on one span of one lane, which - // is the one thing a link may never mean. Refused rather than clamped: - // the partner moves the same distance, so there is no room for it - // anywhere on that lane. + // Every pick's own group, unioned: the whole selection moves as one, + // and a pick's link partners (not themselves picked) come along with + // it exactly as a lone drag's do. + let mut members = Members::default(); + for &(lane, i) in picks { + let Some(group) = self.group_of(lane, i) else { + continue; + }; + for m in group.clips { + if !members.clips.contains(&m) { + members.clips.push(m); + } + } + for m in group.subs { + if !members.subs.contains(&m) { + members.subs.push(m); + } + } + } + if !members.clips.contains(&(held, idx)) { + members.clips.push((held, idx)); + } + // The two halves of *the dragged clip's own* group would land on one + // span of one lane, which is the one thing a link may never mean -- + // checked against that group alone, not the whole moving set: an + // unlinked fellow pick already sitting on `dest` is no conflict at + // all, since it travels by the same delta and `move_room` below + // leaves it the room to. Refused rather than clamped: the partner + // moves the same distance, so there is no room for it anywhere on + // that lane. + let own_group = self.group_of(from, idx).expect("the clip was found"); if dest != held - && members + && own_group .clips .iter() - .chain(&members.subs) + .chain(&own_group.subs) .any(|&(l, _)| l == dest) { return false;