diff --git a/crates/app/src/library_meta.rs b/crates/app/src/library_meta.rs index 4ffa497..e08cd8b 100644 --- a/crates/app/src/library_meta.rs +++ b/crates/app/src/library_meta.rs @@ -9,19 +9,26 @@ use crate::*; /// **a different file** -- the next one along -- over the range it was copied /// from. /// -/// The clip's own file gone means there is nothing to paste: `None`, and the -/// next paste says the clipboard is empty rather than putting some other take -/// down. Every index past it moves down by one, exactly as the lanes' clips do. -pub(crate) fn clipboard_after_remove(clip: Option, removed: usize) -> Option { - let mut clip = clip?; - match clip.source.cmp(&removed) { - std::cmp::Ordering::Equal => None, - std::cmp::Ordering::Greater => { - clip.source -= 1; - Some(clip) - } - std::cmp::Ordering::Less => Some(clip), - } +/// An entry whose own file is gone drops out of the clipboard rather than +/// pasting some other take down in its place; a set with even one such +/// member is trimmed, not emptied whole, the way a lane keeps every clip a +/// removal did not touch. Every index past `removed` moves down by one, +/// exactly as the lanes' clips do. +pub(crate) fn clipboard_after_remove( + clipboard: Vec<(Lane, Clip)>, + removed: usize, +) -> Vec<(Lane, Clip)> { + clipboard + .into_iter() + .filter_map(|(lane, mut clip)| match clip.source.cmp(&removed) { + std::cmp::Ordering::Equal => None, + std::cmp::Ordering::Greater => { + clip.source -= 1; + Some((lane, clip)) + } + std::cmp::Ordering::Less => Some((lane, clip)), + }) + .collect() } /// One library row: a file and one of its audio streams. Plain data, planned diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 52ed74d..24ddd5f 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -298,9 +298,13 @@ struct Player { /// reason (a VA-API init), and kept for the life of the process because the /// answer cannot change while we run. `None` is "not asked yet". hw_caps: Option, - /// The copied clip. Frame ranges only, so it survives the clip it was taken - /// from being deleted -- and it outlives the selection. - clipboard: Option, + /// The copied clip, or clips: frame ranges and the lane each was on, in + /// click order, so it survives the clip (or clips) it was taken from + /// being deleted -- and it outlives the selection. One entry for a plain + /// Copy, the whole ctrl-click set for one made over a selection; either + /// way [`Player::paste`] drops the set at the playhead keeping the gaps + /// and lanes between its members intact. + clipboard: Vec<(Lane, Clip)>, /// A drag that started on the ruler. Moves anywhere in the window scrub /// while it is set; the release commits the exact position. scrubbing: bool, @@ -932,7 +936,7 @@ fn main() { decoders: HashMap::new(), export_seat: None, hw_caps: None, - clipboard: None, + clipboard: Vec::new(), scrubbing: false, splits: load_stance_splits(), split_drag: None, diff --git a/crates/app/src/player/actions.rs b/crates/app/src/player/actions.rs index 6f76115..b3aecc6 100644 --- a/crates/app/src/player/actions.rs +++ b/crates/app/src/player/actions.rs @@ -354,7 +354,7 @@ impl Player { }), playhead: frame_at(session.now(), self.fps), timeline: true, - clipboard: self.clipboard.is_some(), + clipboard: !self.clipboard.is_empty(), subtitles: !session.subtitles().is_empty(), playable: !nothing_to_play(self.active_session()), exporting: self.exporting().is_some(), diff --git a/crates/app/src/player/library.rs b/crates/app/src/player/library.rs index 1061652..1652dd5 100644 --- a/crates/app/src/player/library.rs +++ b/crates/app/src/player/library.rs @@ -164,7 +164,7 @@ impl Player { // A copied clip names its source by *index*, and every index // past the one that went has just moved down: without this the // next paste puts some other file on the timeline. - self.clipboard = clipboard_after_remove(self.clipboard, idx); + self.clipboard = clipboard_after_remove(std::mem::take(&mut self.clipboard), idx); self.reset_after_reseek(); // The last row leaves a session naming no file: nothing to // play, nothing to save and nothing to show, which is the empty @@ -305,7 +305,7 @@ impl Player { self.image = None; // The drawn cue with it, and its tile for the same reason as above. self.sub_image = None; - self.clipboard = None; + self.clipboard.clear(); self.selected.clear(); self.selected_asset = None; // The subtitle rows go with the timeline they were on, and so does the @@ -1486,7 +1486,7 @@ impl Player { self.name = file_name(path).into(); // A copied clip names its source by index, which means a // different file -- or none -- in another project. - self.clipboard = None; + self.clipboard.clear(); self.selected.clear(); // A menu can be up while a project is dropped on the window -- // the scrim swallows clicks, never a drop -- and its index diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index 4a6f715..8e9f955 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -250,18 +250,26 @@ impl Player { cx.notify(); } - /// Copies the selected clip. Nothing on screen changes, so no notify. + /// Copies the selection: one entry for a plain click, the whole + /// ctrl-click set -- every pick, its own lane and its own frames -- for + /// one made over more than a clip. Out of the lane each was clicked in: + /// the audio half of a group is a different clip from the video one, and + /// copying the wrong lane's frames is a paste of the wrong thing. Nothing + /// on screen changes, so no notify. Empty rather than touched at all for + /// a selection naming nothing any more, so a stale Copy leaves the + /// clipboard exactly as a stale Paste would find it. pub(crate) fn copy_selected(&mut self) { - let session = self.session.as_ref(); - // Out of the lane it was clicked in: the audio half of a group is a - // different clip from the video one, and copying the wrong lane's - // frames is a paste of the wrong thing. - if let Some(clip) = self + let Some(session) = self.session.as_ref() else { + return; + }; + let picks: Vec<(Lane, Clip)> = self .selected - .anchor() - .and_then(|(lane, idx)| session?.lane_clips(lane).get(idx).copied()) - { - self.clipboard = Some(clip); + .picks() + .iter() + .filter_map(|&(lane, idx)| Some((lane, session.lane_clips(lane).get(idx).copied()?))) + .collect(); + if !picks.is_empty() { + self.clipboard = picks; } } @@ -288,17 +296,37 @@ impl Player { (picks, links) } - /// Drops the copied clip in at the playhead. The engine reseeks itself, so - /// like a delete this owes the flag reset -- and the selection, whose index - /// the insert has just moved. + /// Drops the copied clip -- or the whole copied set -- in at the + /// playhead. A single clipboard entry takes the door it always has + /// ([`PlaybackSession::paste_at`]): across `V1`/`A1`, splitting whatever + /// it lands inside of and rippling the room open, byte-identical to + /// before a set could be copied at all. More than one takes + /// [`PlaybackSession::paste_set_at`] instead: every member lands on the + /// lane it was copied off, at the same distance from the others it had on + /// the bed it was copied from, refused whole rather than opening room -- + /// see [`Project::paste_set`] for why a set-paste cannot ripple. The + /// engine reseeks itself either way, so like a delete this owes the flag + /// reset -- and the selection, whose index the insert has just moved. pub(crate) fn paste(&mut self, cx: &mut Context) { - let pasted = match (&mut self.session, self.clipboard) { - (Some(session), Some(clip)) => session.paste_at(session.now(), clip), + let pasted = match (&mut self.session, self.clipboard.as_slice()) { + (Some(session), [(_, clip)]) => session.paste_at(session.now(), *clip), + (Some(session), items) if !items.is_empty() => { + session.paste_set_at(session.now(), items) + } _ => false, }; if pasted { self.selected.clear(); self.reset_after_reseek(); + } else if self.clipboard.len() > 1 { + // The set-paste's own refusal, worded like a set-drag's + // ([`Player::move_clip`]) -- a single-clip paste stays silent on + // failure exactly as it always has. + self.notify_user( + "NOT PASTED — another clip already covers where one of the copied clips would \ + land" + .into(), + ); } cx.notify(); } diff --git a/crates/app/src/tests/editing.rs b/crates/app/src/tests/editing.rs index 21d4d07..efa6f97 100644 --- a/crates/app/src/tests/editing.rs +++ b/crates/app/src/tests/editing.rs @@ -1685,19 +1685,35 @@ fn a_copied_clip_is_renumbered_or_dropped_when_a_row_leaves_the_library() { }; // Copied from source 2, source 0 removed: the same file is source 1 now. assert_eq!( - clipboard_after_remove(Some(clip(2)), 0).map(|c| c.source), - Some(1), + clipboard_after_remove(vec![(Lane::V1, clip(2))], 0) + .into_iter() + .map(|(_, c)| c.source) + .collect::>(), + vec![1], "the clipboard follows its file down the list" ); // Copied from a source *before* the one that went: untouched. assert_eq!( - clipboard_after_remove(Some(clip(0)), 2).map(|c| c.source), - Some(0) + clipboard_after_remove(vec![(Lane::V1, clip(0))], 2) + .into_iter() + .map(|(_, c)| c.source) + .collect::>(), + vec![0] ); // Copied from the row that was just removed: there is nothing left to // paste, and pasting the next file along would be a lie. - assert!(clipboard_after_remove(Some(clip(1)), 1).is_none()); - assert!(clipboard_after_remove(None, 0).is_none()); + assert!(clipboard_after_remove(vec![(Lane::V1, clip(1))], 1).is_empty()); + assert!(clipboard_after_remove(Vec::new(), 0).is_empty()); + // A set with one member's file gone loses only that member -- the rest + // of the set survives to be pasted, gaps and lanes intact. + let trimmed = clipboard_after_remove( + vec![(Lane::V1, clip(1)), (Lane::A1, clip(2))], + 1, + ); + assert_eq!( + trimmed.into_iter().map(|(_, c)| c.source).collect::>(), + vec![1] + ); } /// The trim-a-clip path through the doors the edge drag uses: @@ -2009,6 +2025,124 @@ fn a_selection_drag_refuses_as_one_set_when_any_pick_cannot_land() { assert_eq!(session.lane_clips(v2)[1], blocker); } +/// A plain click's copy is one clipboard entry, and one entry still takes +/// [`PlaybackSession::paste_at`] -- the door it always has, unchanged by a +/// set ever being able to ride [`PlaybackSession::paste_set_at`] instead. +#[test] +fn a_single_pick_copy_paste_round_trips_unchanged() { + let mut session = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); + session.set_gain(0.0); + // `open` seeds V1 with the whole fixture as its own clip -- what a plain + // click would copy. + let copied = session.lane_clips(Lane::V1)[0]; + + let at = session.timeline_duration(); + assert!(session.paste_at(at, copied), "a free spot always takes it"); + let pasted = session.lane_clips(Lane::V1)[1]; + assert_eq!( + (pasted.source, pasted.in_frame, pasted.out_frame), + (copied.source, copied.in_frame, copied.out_frame), + "the pasted clip plays the same source frames the copy did" + ); + + assert!(session.undo(), "one step for the paste"); + assert_eq!(session.lane_clips(Lane::V1).len(), 1); +} + +/// [`PlaybackSession::paste_set_at`], the set-copy's own door +/// ([`Player::paste`]'s multi-entry path): three picks across two lanes, some +/// distance apart, keep that same distance and the same lane each landed on +/// -- the offset [`Project::paste_set`] moves every item by is the one from +/// the earliest pick to `at`, not a collapse onto `V1`/`A1`. +#[test] +fn a_three_pick_copy_paste_preserves_gaps_and_lane_assignment() { + 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(20.0, &path, 0, Some(v2)) + .expect("test_av.mp4 is on this timeline")); + let clip_c = session.lane_clips(v2)[0]; + + // Copied in click order, out of the lanes each was clicked in -- exactly + // what `Player::copy_selected` builds. + let items = [(Lane::V1, clip0), (Lane::V1, clip1), (v2, clip_c)]; + let at = session.timeline_duration(); + assert!(session.paste_set_at(at, &items), "a free spot takes the set"); + + let new0 = session.lane_clips(Lane::V1)[2]; + let new1 = session.lane_clips(Lane::V1)[3]; + let new_c = session.lane_clips(v2)[1]; + // The gaps between picks survive the trip untouched. + assert_eq!(new1.start - new0.start, clip1.start - clip0.start); + assert_eq!(new_c.start - new0.start, clip_c.start - clip0.start); + // And each still plays the source frames it was copied with, off the + // lane it was copied off of. + assert_eq!( + (new0.source, new0.in_frame, new0.out_frame), + (clip0.source, clip0.in_frame, clip0.out_frame) + ); + assert_eq!( + (new_c.source, new_c.in_frame, new_c.out_frame), + (clip_c.source, clip_c.in_frame, clip_c.out_frame) + ); + + assert!(session.undo(), "one step for the whole set-paste"); + assert_eq!(session.lane_clips(Lane::V1).len(), 2); + assert_eq!(session.lane_clips(v2).len(), 1); +} + +/// A set-paste never ripples the bed: a landing that would overlap so much +/// as one existing clip on its own lane refuses the *whole* set, exactly as +/// [`Project::paste_set`] documents, leaving the project as it was rather +/// than placing the picks that had room to spare. +#[test] +fn a_colliding_set_paste_refuses_whole() { + 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(20.0, &path, 0, Some(v2)) + .expect("test_av.mp4 is on this timeline")); + let clip_c = session.lane_clips(v2)[0]; + + let items = [(Lane::V1, clip0), (Lane::V1, clip1), (v2, clip_c)]; + // A blocker sat right where the V1 half of the set would land: pasting + // at the timeline's own start walks every item straight back onto the + // lanes it was copied off of, colliding with everything already there. + assert!(!session.paste_set_at(0.0, &items)); + + 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(Lane::V1).len(), 2); + assert_eq!(session.lane_clips(v2).len(), 1); +} + /// 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 diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index 84cba42..2c7ce84 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -2953,6 +2953,16 @@ impl PlaybackSession { self.edit(Dirty::Both, |p| p.paste(at, clip)) } + /// [`paste_at`](Self::paste_at)'s general form: [`Project::paste_set`]'s + /// door, clamped to the timeline's own end the same way -- a set let go + /// past the last frame lands there rather than carrying black in front of + /// it, exactly as a lone clip's paste does. + pub fn paste_set_at(&mut self, timeline_secs: f64, items: &[(Lane, Clip)]) -> bool { + let at = secs_to_frame(timeline_secs, self.meta.frame_rate) + .min(self.project.timeline_frames()); + self.edit(Dirty::Both, |p| p.paste_set(at, items)) + } + /// Places the whole of `path` played on its audio `stream` at /// `timeline_secs`, the way [`paste_at`](Self::paste_at) places a copy -- /// the door a library row goes through, and the only way a stream other diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index e03b625..31d946c 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -4204,6 +4204,70 @@ impl Project { true } + /// [`paste`](Project::paste)'s general form: every `(lane, clip)` in + /// `items` lands on the lane it names, its head moved by the offset every + /// other item moves by too -- `at` less the earliest `start` among them, + /// so the set's own gaps and lane assignment survive the trip from + /// clipboard to bed unchanged, rather than collapsing onto `V1`/`A1` the + /// way a lone clip's copy does. + /// + /// All-or-nothing, and **refused** rather than opening room or splitting + /// a neighbour the way [`paste`](Project::paste) does for one clip: + /// a set-paste never ripples the bed, so a landing that overlaps so much + /// as one existing clip on its own lane refuses the whole set, changing + /// nothing -- the paste's own [`move_selection`](Project::move_selection) + /// clamps instead of refusing, but a paste has no drag to clamp against, + /// and a landing moved off the frame asked for is not what a paste means. + /// `items` empty, naming a lane or a source that is not there, or a clip + /// with `out_frame <= in_frame`, refuses too, before anything is touched. + /// + /// Exactly one history snapshot for the whole set, so one + /// [`Project::undo`] takes it back. Changes the timeline->source mapping + /// on every lane an item lands on: the caller must reseek. + pub fn paste_set(&mut self, at: u32, items: &[(Lane, Clip)]) -> bool { + let Some(anchor) = items.iter().map(|(_, c)| c.start).min() else { + return false; + }; + // Resolved once, up front, off the lanes as they stand now: a lane or + // source that is not there, or a landing that would overlap a + // neighbour, refuses the whole set before any of it is written. + let mut landings: Vec<(usize, Clip)> = Vec::with_capacity(items.len()); + for &(lane, clip) in items { + let Some(li) = self.index(lane) else { + return false; + }; + if self.sources.get(clip.source).is_none() || clip.out_frame <= clip.in_frame { + return false; + } + let delta = i64::from(clip.start) - i64::from(anchor); + let Ok(start) = u32::try_from(i64::from(at) + delta) else { + return false; + }; + let Some(end) = start.checked_add(clip.frames()) else { + return false; + }; + if self.lanes[li] + .clips + .iter() + .any(|c| c.start < end && start < c.end()) + { + return false; + } + landings.push((li, Clip { start, ..clip })); + } + self.snapshot(); + for (li, mut clip) in landings { + clip.link = Some(self.new_link()); + clip.fade_in = 0; + clip.fade_out = 0; + clip.transition_out = 0; + let clips = &mut self.lanes[li].clips; + let idx = clips.partition_point(|c| c.start < clip.start); + clips.insert(idx, clip); + } + true + } + /// Lift the clip at `idx` out of `lane`, leaving a gap: black frames or /// silence, and nothing else moves. Refused only for an out-of-range index /// (which a lane that is not there always is) -- lifting the last placement