forge
Explore
Log in

Don't you dare go Hollow

Drag moves the whole selection, all-or-nothing

Browse files at this commit

33ae7d9ffabeb8d24ee073faa0a2175fce1a57dfAhmet Kaan GümüşParent: 9e3e5bc
  • feat
    • drag moves the whole selection, all-or-nothing
Show full message
✨ feat(app): drag moves the whole selection, all-or-nothing

A dragged clip that is itself one of the selection's picks now carries
every other pick along by the same frame delta, in one undo step, and
the selection survives the move with its indices remapped rather than
cleared. One pick refused blocks the whole set -- nothing moves, one
notice. A clip dragged from outside the selection is unchanged.

Project::move_clip is now Project::move_selection with a selection of
one, so the existing single-clip path is byte-identical.
crates/app/src/player/timeline_edit.rsmodified+89 -5
@@ -310,6 +310,13 @@ impl Player {
310310 /// nothing is said about it. The engine reseeks, so all this owes is the
311311 /// flag reset -- and the selection, whose index was that lane's own and now
312312 /// names a different clip there.
313+ ///
314+ /// A dragged clip that is *itself* one of the selection's picks carries the
315+ /// whole selection with it: every other pick moves the same frame delta,
316+ /// all-or-nothing ([`PlaybackSession::move_selection_to`]), and the picks
317+ /// survive the move -- their indices remapped rather than cleared, since a
318+ /// pick that travels still names the clip it named before. A clip dragged
319+ /// from outside the selection moves alone, exactly as it always has.
313320 pub(crate) fn move_clip(
314321 &mut self,
315322 from: Lane,
@@ -330,10 +337,23 @@ impl Player {
330337 ) else {
331338 return;
332339 };
333- let moved = self
334- .session
335- .as_mut()
336- .is_some_and(|session| session.move_clip_to(from, idx, to, start));
340+ let set_move = self.selected.contains((from, idx)) && self.selected.len() > 1;
341+ let picks: Vec<(Lane, usize)> = self.selected.picks().to_vec();
342+ // Every pick's own start, read before the move touches anything -- the
343+ // only way to find a pick again afterwards, since a lane change or an
344+ // insert can move the index it was named by.
345+ let pre_starts: Vec<Option<u32>> = picks
346+ .iter()
347+ .map(|&(lane, i)| {
348+ self.session
349+ .as_ref()
350+ .and_then(|session| session.lane_clips(lane).get(i).map(|c| c.start))
351+ })
352+ .collect();
353+ let moved = self.session.as_mut().is_some_and(|session| match set_move {
354+ true => session.move_selection_to(&picks, from, idx, to, start),
355+ false => session.move_clip_to(from, idx, to, start),
356+ });
337357 let (kind, lanes) = match from.kind {
338358 LaneKind::Video => ("picture", "video"),
339359 LaneKind::Audio => ("sound", "audio"),
@@ -343,7 +363,18 @@ impl Player {
343363 };
344364 match moved {
345365 true => {
346- self.selected.clear();
366+ // Every pick named a `(lane, ...)` by index into that lane's
367+ // clips, sorted by start -- exactly what an insert or a lane
368+ // change reorders. Re-read by the frame each pick's clip now
369+ // starts at (recorded before the move moved anything), so the
370+ // selection survives naming the clips it named, not the slots
371+ // they used to sit in.
372+ if set_move {
373+ self.selected =
374+ self.remap_selection(&picks, &pre_starts, from, idx, to, was, start);
375+ } else {
376+ self.selected.clear();
377+ }
347378 self.reset_after_reseek();
348379 }
349380 // The three ways a drag is refused, told apart by what the
@@ -360,6 +391,15 @@ impl Player {
360391 // Picked up and put back down where it was: a click, and a click
361392 // says nothing.
362393 false if from == to && start == was => {}
394+ false if set_move => self.notify_user(
395+ format!(
396+ "NOT MOVED — {} clips selected, and another clip already covers where one of \
397+ them would land on {}",
398+ picks.len(),
399+ to.label()
400+ )
401+ .into(),
402+ ),
363403 false => self.notify_user(
364404 format!(
365405 "NOT MOVED — another clip already covers those frames on {}",
@@ -371,6 +411,50 @@ impl Player {
371411 cx.notify();
372412 }
373413
414+ /// What a selection's picks are called after a set-move that shifted every
415+ /// one of them by `landed - was` timeline frames (the delta the dragged
416+ /// clip's own head travelled, which every pick travelled too -- see
417+ /// [`Project::move_selection`]): the clip a pick named is found again by
418+ /// where it now starts, on the lane it stays on (or `to`, for the one
419+ /// that changed lane), rather than by the index it had, which a lane
420+ /// change or an insert can have moved. A pick whose clip cannot be found
421+ /// there any more (a bad index handed in) is dropped rather than guessed
422+ /// at.
423+ fn remap_selection(
424+ &self,
425+ picks: &[(Lane, usize)],
426+ pre_starts: &[Option<u32>],
427+ from: Lane,
428+ dragged: usize,
429+ to: Lane,
430+ was: u32,
431+ landed: u32,
432+ ) -> Selection {
433+ let mut out = Selection::new();
434+ let Some(session) = self.session.as_ref() else {
435+ return out;
436+ };
437+ let delta = i64::from(landed) - i64::from(was);
438+ for (&pick, &old_start) in picks.iter().zip(pre_starts) {
439+ let Some(old_start) = old_start else {
440+ continue;
441+ };
442+ let want = (i64::from(old_start) + delta) as u32;
443+ // Every pick but the dragged clip itself stays on the lane it was
444+ // already on; only the exact clip the hand let go of changed
445+ // lane, so only its own pick is found on `to` rather than `from`.
446+ let now_lane = if pick == (from, dragged) { to } else { pick.0 };
447+ if let Some(found) = session
448+ .lane_clips(now_lane)
449+ .iter()
450+ .position(|c| c.start == want)
451+ {
452+ out.add((now_lane, found));
453+ }
454+ }
455+ out
456+ }
457+
374458 /// The whole of a palette track as a placement: from its own first
375459 /// microsecond to its last cue, and as many timeline frames as that is worth
376460 /// at this rate. What a row dragged out of the Subtitles list carries, and
crates/app/src/tests/editing.rsmodified+135 -0
@@ -1897,6 +1897,141 @@ fn a_track_can_be_added_dropped_on_edited_and_taken_back() {
18971897 assert_eq!(format_refusal(&session, Format::Mp4), None);
18981898 }
18991899
1900+/// [`PlaybackSession::move_selection_to`], the door a selection drag uses
1901+/// ([`Player::move_clip`]'s set path): three picks across two lanes, one of
1902+/// them the dragged clip, all travel the *same* frame delta in one call --
1903+/// the same all-or-nothing clamp [`Project::move_room`] already gave a
1904+/// linked group, generalised to an arbitrary selection.
1905+#[test]
1906+fn a_selection_drag_moves_every_pick_by_the_same_delta() {
1907+ use engine::project::LaneKind;
1908+
1909+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1910+ session.set_gain(0.0);
1911+ let path = session.sources()[0].path.clone();
1912+ session.import(&asset("test_av2.mp4")).expect("av2 matches");
1913+ let second = session.sources()[1].path.clone();
1914+
1915+ // clip0 on V1 at the fixture's own start; clip1 on V1 ten seconds in,
1916+ // appended to the open bed and rippling nothing.
1917+ assert!(session
1918+ .place_stream_at(10.0, &second, 0, None)
1919+ .expect("av2 is on this timeline"));
1920+ let clip0 = session.lane_clips(Lane::V1)[0];
1921+ let clip1 = session.lane_clips(Lane::V1)[1];
1922+ assert_eq!((clip0.start, clip1.start), (0, 300));
1923+
1924+ // clipC on a second video row, well past both of the above.
1925+ let v2 = session.add_lane(LaneKind::Video);
1926+ assert!(session
1927+ .place_stream_at(20.0, &path, 0, Some(v2))
1928+ .expect("test_av.mp4 is on this timeline"));
1929+ let clip_c = session.lane_clips(v2)[0];
1930+ assert_eq!(clip_c.start, 600);
1931+
1932+ let picks = [(Lane::V1, 0), (Lane::V1, 1), (v2, 0)];
1933+ // Drag clip1 fifty frames along its own lane -- the other two picks own
1934+ // no neighbours of their own here, so nothing narrows the room and the
1935+ // full fifty-frame ask goes through for all three.
1936+ assert!(session.move_selection_to(&picks, Lane::V1, 1, Lane::V1, clip1.start + 50));
1937+ assert_eq!(session.lane_clips(Lane::V1)[0].start, clip0.start + 50);
1938+ assert_eq!(session.lane_clips(Lane::V1)[1].start, clip1.start + 50);
1939+ assert_eq!(session.lane_clips(v2)[0].start, clip_c.start + 50);
1940+ // Every pick still names the clip it named: found again by the frame it
1941+ // now starts at -- exactly the lookup `Player::remap_selection` makes
1942+ // once the move lands, which is what keeps a set-move's selection alive
1943+ // rather than clearing it.
1944+ assert_eq!(
1945+ session.lane_clips(Lane::V1)[0],
1946+ Clip { start: clip0.start + 50, ..clip0 }
1947+ );
1948+ assert_eq!(
1949+ session.lane_clips(v2)[0],
1950+ Clip { start: clip_c.start + 50, ..clip_c }
1951+ );
1952+
1953+ assert!(session.undo(), "one step for the whole set-move");
1954+ assert_eq!(session.lane_clips(Lane::V1)[0].start, clip0.start);
1955+ assert_eq!(session.lane_clips(Lane::V1)[1].start, clip1.start);
1956+ assert_eq!(session.lane_clips(v2)[0].start, clip_c.start);
1957+}
1958+
1959+/// One pick with nowhere to go refuses the whole set: the walk onto another
1960+/// clip's own span that already refuses a lone drag
1961+/// (`move_clip_keeps_the_frames_and_refuses_the_rest`, in this crate's own
1962+/// engine tests) refuses a selection drag exactly the same way, and every
1963+/// *other* pick -- which had all the room in the world -- stays exactly
1964+/// where it was rather than moving on its own.
1965+#[test]
1966+fn a_selection_drag_refuses_as_one_set_when_any_pick_cannot_land() {
1967+ use engine::project::LaneKind;
1968+
1969+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
1970+ session.set_gain(0.0);
1971+ let path = session.sources()[0].path.clone();
1972+ session.import(&asset("test_av2.mp4")).expect("av2 matches");
1973+ let second = session.sources()[1].path.clone();
1974+
1975+ assert!(session
1976+ .place_stream_at(10.0, &second, 0, None)
1977+ .expect("av2 is on this timeline"));
1978+ let clip0 = session.lane_clips(Lane::V1)[0];
1979+ let clip1 = session.lane_clips(Lane::V1)[1];
1980+
1981+ let v2 = session.add_lane(LaneKind::Video);
1982+ assert!(session
1983+ .place_stream_at(0.0, &path, 0, Some(v2))
1984+ .expect("test_av.mp4 is on this timeline"));
1985+ let clip_c = session.lane_clips(v2)[0];
1986+ // A clip clip1 is not picked, blocking the road on V2 the drag is about
1987+ // to try: clip1 dragged onto V2 lands its head inside this one.
1988+ assert!(session
1989+ .place_stream_at(10.0, &second, 0, Some(v2))
1990+ .expect("av2 is on this timeline"));
1991+ let blocker = session.lane_clips(v2)[1];
1992+ assert_eq!(blocker.start, 300);
1993+
1994+ let picks = [(Lane::V1, 0), (Lane::V1, 1), (v2, 0)];
1995+ // Dragged onto V2 at a frame inside the blocker: refused outright, the
1996+ // way a lone drag onto another clip already is.
1997+ assert!(!session.move_selection_to(
1998+ &picks,
1999+ Lane::V1,
2000+ 1,
2001+ v2,
2002+ blocker.start + 10
2003+ ));
2004+ // Nothing moved -- not clip1 clamped short of the blocker, and not the
2005+ // two picks that had all the room to spare.
2006+ assert_eq!(session.lane_clips(Lane::V1)[0], clip0);
2007+ assert_eq!(session.lane_clips(Lane::V1)[1], clip1);
2008+ assert_eq!(session.lane_clips(v2)[0], clip_c);
2009+ assert_eq!(session.lane_clips(v2)[1], blocker);
2010+}
2011+
2012+/// The refactor `move_clip` -> `move_selection` owes: a clip dragged from
2013+/// outside the selection (or with nothing else selected) moves exactly as it
2014+/// always has -- `move_clip_to`'s own door and `move_selection_to`'s
2015+/// selection-of-one both walk the same code, so they cannot drift apart.
2016+#[test]
2017+fn a_lone_clip_drag_is_unchanged_by_the_selection_path() {
2018+ use engine::project::LaneKind;
2019+
2020+ let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture");
2021+ session.set_gain(0.0);
2022+ let v2 = session.add_lane(LaneKind::Video);
2023+
2024+ assert!(session.move_clip_to(Lane::V1, 0, v2, 0), "V1 -> V2");
2025+ assert!(session.lane_clips(Lane::V1).is_empty());
2026+ assert_eq!(session.lane_clips(v2).len(), 1);
2027+
2028+ // The other kind of lane is refused exactly as before.
2029+ assert!(!session.move_clip_to(v2, 0, Lane::A1, 0));
2030+ // A drop back where it was picked up is a click, still refused.
2031+ let clip = session.lane_clips(v2)[0];
2032+ assert!(!session.move_clip_to(v2, 0, v2, clip.start));
2033+}
2034+
19002035 /// A caption grouped with a clip on an *added* lane closes its lane's hole
19012036 /// too, not just the first pair's: the same law commit `cdc53a6` gave `V1`
19022037 /// (`whole_take` -> `Project::delete_members`) has to hold from every anchor
crates/engine/src/playback.rsmodified+16 -0
@@ -2374,6 +2374,22 @@ impl PlaybackSession {
23742374 self.edit(Dirty::Both, |p| p.move_clip(from, idx, to, start))
23752375 }
23762376
2377+ /// [`move_clip_to`](Self::move_clip_to)'s general form: every one of
2378+ /// `picks` travels the same frame delta as the dragged clip at `idx` of
2379+ /// `from`, all-or-nothing, one undo step for the lot -- what a hand drags
2380+ /// on a selection rather than a lone clip. See
2381+ /// [`Project::move_selection`] for the clamp that makes it all-or-nothing.
2382+ pub fn move_selection_to(
2383+ &mut self,
2384+ picks: &[(Lane, usize)],
2385+ from: Lane,
2386+ idx: usize,
2387+ to: Lane,
2388+ start: u32,
2389+ ) -> bool {
2390+ self.edit(Dirty::Both, |p| p.move_selection(picks, from, idx, to, start))
2391+ }
2392+
23772393 /// Drags one end of that clip to timeline frame `to`, changing how much of
23782394 /// its source it plays and nothing else on the lane -- see [`Project::trim`]
23792395 /// for the walls it is clamped by, which this fills the source lengths in
crates/engine/src/project.rsmodified+55 -7
@@ -3591,23 +3591,71 @@ impl Project {
35913591 /// is on, and a drop that changes neither lane nor frame, which is a clip
35923592 /// picked up and put back down, i.e. a click.
35933593 pub fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> bool {
3594+ self.move_selection(&[(from, idx)], from, idx, to, start)
3595+ }
3596+
3597+ /// [`move_clip`](Self::move_clip)'s general form: every pick in `picks`
3598+ /// (each with its own link group, exactly as [`group_of`](Self::group_of)
3599+ /// reads it) travels the same distance as the clip at `idx` of `from`,
3600+ /// which alone changes lane, onto `to`. One clamp across the whole set
3601+ /// ([`move_room`](Self::move_room)) is what makes this all-or-nothing: a
3602+ /// neighbour that boxes in *any* member narrows the delta every member
3603+ /// moves by, and a member with no room at all (`lo > hi`) refuses the
3604+ /// whole set rather than moving the rest and leaving that one behind.
3605+ /// `move_clip` is this with a selection of one -- the dragged clip's own
3606+ /// group and nothing else -- so a drag on a clip outside the selection is
3607+ /// byte-identical to before this existed.
3608+ pub fn move_selection(
3609+ &mut self,
3610+ picks: &[(Lane, usize)],
3611+ from: Lane,
3612+ idx: usize,
3613+ to: Lane,
3614+ start: u32,
3615+ ) -> bool {
35943616 let (Some(dest), Some(clip)) = (self.index(to), self.lane(from).get(idx).copied()) else {
35953617 return false;
35963618 };
35973619 if from.kind != to.kind {
35983620 return false;
35993621 }
3600- let members = self.group_of(from, idx).expect("the clip was found");
36013622 let held = self.index(from).expect("the clip was found on it");
3602- // The two halves of one group would land on one span of one lane, which
3603- // is the one thing a link may never mean. Refused rather than clamped:
3604- // the partner moves the same distance, so there is no room for it
3605- // anywhere on that lane.
3623+ // Every pick's own group, unioned: the whole selection moves as one,
3624+ // and a pick's link partners (not themselves picked) come along with
3625+ // it exactly as a lone drag's do.
3626+ let mut members = Members::default();
3627+ for &(lane, i) in picks {
3628+ let Some(group) = self.group_of(lane, i) else {
3629+ continue;
3630+ };
3631+ for m in group.clips {
3632+ if !members.clips.contains(&m) {
3633+ members.clips.push(m);
3634+ }
3635+ }
3636+ for m in group.subs {
3637+ if !members.subs.contains(&m) {
3638+ members.subs.push(m);
3639+ }
3640+ }
3641+ }
3642+ if !members.clips.contains(&(held, idx)) {
3643+ members.clips.push((held, idx));
3644+ }
3645+ // The two halves of *the dragged clip's own* group would land on one
3646+ // span of one lane, which is the one thing a link may never mean --
3647+ // checked against that group alone, not the whole moving set: an
3648+ // unlinked fellow pick already sitting on `dest` is no conflict at
3649+ // all, since it travels by the same delta and `move_room` below
3650+ // leaves it the room to. Refused rather than clamped: the partner
3651+ // moves the same distance, so there is no room for it anywhere on
3652+ // that lane.
3653+ let own_group = self.group_of(from, idx).expect("the clip was found");
36063654 if dest != held
3607- && members
3655+ && own_group
36083656 .clips
36093657 .iter()
3610- .chain(&members.subs)
3658+ .chain(&own_group.subs)
36113659 .any(|&(l, _)| l == dest)
36123660 {
36133661 return false;