diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 84b027d..20b8c27 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -250,8 +250,9 @@ struct AssetDrag(PathBuf, usize); /// A clip already on the timeline being dragged: the lane it is on and its index /// there, which is how every other edit names a clip. Unlike an [`AssetDrag`] -/// nothing is inserted -- the same clip changes lane and keeps its frames -- so -/// where along the bed it is let go says nothing here either. +/// nothing is inserted -- the same clip changes lane and keeps the frames it +/// plays -- but where along the bed it is let go is exactly where it lands, less +/// the offset the hand grabbed it at ([`Player::grab`]). #[derive(Clone, Copy)] struct ClipDrag { lane: Lane, @@ -283,6 +284,12 @@ struct Trim { /// still the body. const EDGE_W: f32 = 6.; +/// How close to an edge a dragged clip has to be let go for it to land *on* it: +/// the snap every timeline has, in pixels rather than frames so that it feels +/// the same at every zoom. Narrower than [`EDGE_W`] -- a hand aiming between two +/// takes must still be able to leave a gap of a few frames there. +const SNAP_PX: f64 = 5.; + /// An open clip menu: which clip it was opened on, where it hangs, and whether /// it has been turned over to show what that clip *is* instead of what can be /// done to it. The lane and index are the ones the same click selected, so @@ -647,6 +654,13 @@ struct Player { /// `scrubbing`'s reason: a 6 px strip is not where the pointer stays. See /// [`Trim`]. trim: Option, + /// How far into the clip the last press on a box landed, in timeline + /// frames: what a drag lets go of is the *point that was grabbed*, so the + /// head lands that much in front of the pointer and the clip does not jump + /// under the hand. Recorded at the press because gpui hands the drop only + /// the value being dragged, and stale between drags, which costs nothing -- + /// no drag starts without a press on the box it moves. + grab: u32, last_scrub: Instant, last_target: u32, /// The running export. While it owns the UI the editor is read-only. @@ -2050,19 +2064,29 @@ impl Player { cx.notify(); } - /// A clip dragged off one lane and let go over another: it changes track and - /// keeps the frames it covers, as one undo step. Dropped back on its own - /// lane it is not an edit at all, so 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. - fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, cx: &mut Context) { - if self.exporting().is_some() || from == to { + /// A clip let go at window `x` over lane `to`: it lands with its head where + /// the hand is carrying it ([`Player::drop_frame`]), on the track it was + /// dropped on, taking its whole take with it -- one undo step for the + /// gesture. Dropped back where it was picked up it is not an edit at all, so + /// 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. + fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, x: Pixels, cx: &mut Context) { + if self.exporting().is_some() { return; } + let (Some(start), Some(was)) = ( + self.drop_frame(from, idx, to, x), + self.session + .as_ref() + .and_then(|session| session.lane_clips(from).get(idx).map(|c| c.start)), + ) else { + return; + }; let moved = self .session .as_mut() - .is_some_and(|session| session.move_clip_to_lane(from, idx, to)); + .is_some_and(|session| session.move_clip_to(from, idx, to, start)); let (kind, lanes) = match from.kind { LaneKind::Video => ("picture", "video"), LaneKind::Audio => ("sound", "audio"), @@ -2072,9 +2096,10 @@ impl Player { self.selected = None; self.reset_after_reseek(); } - // The two ways a drag is refused, told apart by the one thing the - // front-end already knows: a lane's kind. Everything else that - // could refuse (a clip that is not there) cannot be dragged. + // The three ways a drag is refused, told apart by what the + // front-end already knows: a lane's kind, and where the clip was. + // Everything else that could refuse (a clip that is not there) + // cannot be dragged. false if from.kind != to.kind => { self.notice = Some( format!( @@ -2084,6 +2109,9 @@ impl Player { .into(), ) } + // Picked up and put back down where it was: a click, and a click + // says nothing. + false if from == to && start == was => {} false => { self.notice = Some( format!( @@ -2097,6 +2125,55 @@ impl Player { cx.notify(); } + /// Where a clip let go at window `x` over lane `to` wants its head: the + /// frame under the pointer, less however far into the box the hand grabbed + /// it (so the clip does not jump under the pointer), pulled onto a + /// neighbouring edge when it lands within [`SNAP_PX`] of one. `None` when + /// there is no such clip to move. The engine has the last word on where it + /// may actually go -- this is the ask, not the answer. + /// + /// ponytail: the bed ends where the timeline does, so a clip cannot be + /// dragged *past* the last frame -- the pointer has no pixel out there to + /// name and [`View::time_at`] clamps to the duration. Room to the right is + /// made by pulling a tail out first ([`Player::drawn_duration`] already + /// lengthens the bed for exactly that gesture). The upgrade is to draw the + /// bed past the end while a clip drag is in flight, the way a trim already + /// does: `drawn_duration` would take the dragged clip's length the same way + /// it takes a trim's reach. + fn drop_frame(&self, from: Lane, idx: usize, to: Lane, x: Pixels) -> Option { + let session = self.session.as_ref()?; + let clip = session.lane_clips(from).get(idx).copied()?; + let raw = self.frame_under(x).saturating_sub(self.grab); + // The edges worth landing on: both ends of every clip already on the + // lane it is being let go over, the playhead, and the head of the + // timeline. Never the group's own boxes -- a clip does not snap to + // where it already is. + let mut marks: Vec = session + .lane_clips(to) + .iter() + .enumerate() + .filter(|&(i, c)| { + (to, i) != (from, idx) && !(clip.link.is_some() && c.link == clip.link) + }) + .flat_map(|(_, c)| [c.start, c.end()]) + .collect(); + marks.push(frame_at(session.now(), self.fps)); + marks.push(0); + Some(snapped(raw, clip.frames(), self.snap_frames(), &marks)) + } + + /// [`SNAP_PX`] in timeline frames at the zoom the bed is drawn at: a snap is + /// a distance on screen, so zoomed right in it is worth less than a frame + /// (no snap at all, which is what a hand placing single frames wants) and + /// zoomed out to the whole timeline it is worth many. + fn snap_frames(&self) -> u32 { + let bed = f64::from(f32::from(self.ruler.get().size.width)); + if bed <= 0. { + return 0; + } + (SNAP_PX / bed * self.view.span(self.drawn_duration()) * self.fps) as u32 + } + /// Opens the clip menu on the box under the pointer, from the right button /// wherever it was pressed on that box -- its middle or one of its edge /// strips, which cover the middle's own listener. Selecting first is part of @@ -2151,20 +2228,30 @@ impl Player { /// against the same duration the boxes are drawn to, so the edge tracks the /// pointer exactly. fn trim_to(&mut self, x: Pixels, cx: &mut Context) { - let (duration, fps) = (self.drawn_duration(), self.fps); - let at = self - .view - .time_at(frac_along(x, self.ruler.get()), duration); + let at = self.frame_under(x); let (Some(trim), Some(session)) = (&mut self.trim, &self.session) else { return; }; let Some((lo, hi)) = session.trim_room(trim.lane, trim.idx, trim.edge) else { return; }; - trim.to = frame_at(at, fps).clamp(lo, hi); + trim.to = at.clamp(lo, hi); cx.notify(); } + /// The timeline frame a pointer at window x is on: along the same bed the + /// ruler is measured on, through the same [`View`] every box is drawn + /// through, so a zoomed-in panel answers with the frame under the pointer + /// and not with the one that would have been there unzoomed. The one + /// question a trim, a grab and a drop all ask. + fn frame_under(&self, x: Pixels) -> u32 { + let duration = self.drawn_duration(); + frame_at( + self.view.time_at(frac_along(x, self.ruler.get()), duration), + self.fps, + ) + } + /// The release: the whole drag reaches the engine as one edit, so it is one /// undo step. The selection survives it -- a trim inserts and removes /// nothing, so every index a lane had still names the clip it named. @@ -2264,16 +2351,18 @@ impl Player { /// The one way a library row reaches the timeline: the Add button and a row /// dragged onto a lane both come here, so there is a single answer to what - /// "add this source" does. The whole source goes in at the playhead as one - /// grouped take -- the same insert a paste makes, so everything after it - /// moves along rather than being painted over. Reseeks like every other - /// edit, and drops the timeline's selection with it: the insert has just - /// moved the indices it pointed at. + /// "add this source" does. The whole source goes in as one grouped take at + /// `at` -- the frame the pointer let it go on, or the playhead for the + /// button, which names no place. It is the same insert a paste makes, so + /// everything after it moves along rather than being painted over. Reseeks + /// like every other edit, and drops the timeline's selection with it: the + /// insert has just moved the indices it pointed at. fn insert_source( &mut self, path: &Path, stream: usize, onto: Option, + at: Option, cx: &mut Context, ) { if self.exporting().is_some() { @@ -2308,8 +2397,16 @@ impl Player { // in: a row that has never been on a lane is placeable at its full // length, which is the whole point of an import that only fills the // library. + let fps = self.fps; let placed = match &mut self.session { - Some(session) => session.place_stream_at(session.now(), path, stream, onto), + // Seconds, because that is what the engine's own door takes: the + // frame the pointer named goes back through the same rate every box + // on the bed is drawn at, so it lands on the frame it was let go on + // rather than a neighbouring one. + Some(session) => { + let at = at.map_or_else(|| session.now(), |frame| f64::from(frame) / fps); + session.place_stream_at(at, path, stream, onto) + } None => Ok(false), }; match placed { @@ -2459,7 +2556,7 @@ impl Player { } RowItem::Add => { self.library_menu = None; - self.insert_source(&menu.path, menu.stream, None, cx); + self.insert_source(&menu.path, menu.stream, None, None, cx); } RowItem::Remove => { self.library_menu = None; @@ -3585,7 +3682,7 @@ impl Player { // row that says where it came from has to say which rate that // was. (None, Some(meta)) => format!( - "{} — {}x{} @ {:.2} fps · drag onto a lane, or Add at playhead", + "{} — {}x{} @ {:.2} fps · drag it where you want it, or Add at playhead", row.path.display(), meta.width, meta.height, @@ -3769,7 +3866,7 @@ impl Player { "Add at playhead", match self.selected_asset { Some(_) => "inserts the picked file at the playhead".to_string(), - None => "click a file above first — or drag one onto a lane".to_string(), + None => "click a file above first — or drag one where you want it".to_string(), }, can_add( self.selected_asset.as_ref(), @@ -3780,7 +3877,7 @@ impl Player { if let Some((path, stream)) = this.selected_asset.clone() { // No lane: the button means "wherever this belongs", // which for a file with no picture is the audio lane. - this.insert_source(&path, stream, None, cx); + this.insert_source(&path, stream, None, None, cx); } }), )) @@ -5810,7 +5907,7 @@ impl Player { let (sel, sel_link) = (self.selected, self.selected_link()); let audio = lane.kind == LaneKind::Audio; let tip: SharedString = format!( - "Select (or {} under the playhead, {}/{} along the lane) — drag an end to trim, {} removes the take, {} leaves a gap, {} rejoins a cut", + "Select (or {} under the playhead, {}/{} along the lane) — drag it to move it, an end to trim, {} removes the take, {} leaves a gap, {} rejoins a cut", self.keymap.display(ActionId::Select), self.keymap.display(ActionId::SelectPrev), self.keymap.display(ActionId::SelectNext), @@ -5877,18 +5974,23 @@ impl Player { .bg(rgb(LETTERBOX)) .overflow_hidden() // A library row let go over a lane is the same insert the - // Add button makes, through the same call -- at the - // playhead, not at the pointer: clips here are placed end - // to end, so where along the bed it landed says nothing. - .on_drop(cx.listener(move |this, drag: &AssetDrag, _, cx| { - this.insert_source(&drag.0.clone(), drag.1, Some(lane), cx) + // Add button makes, through the same call -- but where the + // pointer let it go, not at the playhead: a hand that + // carried a file to a place on the bed named that place. + // gpui hands a drop no event, so the pointer is read off + // the window, which took it from the release that fired + // this (gpui window.rs:3602). + .on_drop(cx.listener(move |this, drag: &AssetDrag, window, cx| { + let at = this.frame_under(window.mouse_position().x); + this.insert_source(&drag.0.clone(), drag.1, Some(lane), Some(at), cx) })) .drag_over::(|s, _, _, _| s.bg(rgb(HOVER_DIM))) - // A clip let go over another lane changes track and keeps - // the frames it covers -- the pointer's position along the - // bed says nothing here either, for the reason above. - .on_drop(cx.listener(move |this, drag: &ClipDrag, _, cx| { - this.move_clip(drag.lane, drag.idx, lane, cx) + // ...and a clip let go over a lane lands the same way: on + // the track it was dropped on, at the frame it was carried + // to -- its own included, which is the drag that moves a + // take along its track. + .on_drop(cx.listener(move |this, drag: &ClipDrag, window, cx| { + this.move_clip(drag.lane, drag.idx, lane, window.mouse_position().x, cx) })) .drag_over::(|s, _, _, _| s.bg(rgb(HOVER_DIM))) .children(clips.iter().enumerate().map(|(i, clip)| { @@ -5933,6 +6035,10 @@ impl Player { // says what is moving. let ghost: SharedString = label.clone().unwrap_or_else(|| lane.label()).into(); + // Its head in frames, for the press below: the `start` + // above is the same moment in seconds, which is what + // the box is *drawn* from. + let head = clip.start; div() // Named per lane: two rows numbering their clips // from zero would hand gpui the same id twice. @@ -5960,16 +6066,23 @@ impl Player { .cursor_pointer() .hover(|s| s.border_color(rgb(ACCENT))) .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into()) - // Dragged onto another lane it *moves* there; the - // click that starts the drag still selects, so - // picking a clip up and putting it back down where - // it was is exactly a click. + // Dragged, it *moves*: to the frame it was let go on + // and to the lane it was let go over. The click that + // starts the drag still selects, so picking a clip + // up and putting it back down where it was is + // exactly a click. .on_drag(ClipDrag { lane, idx: i }, move |_, _, _, cx| { cx.new(|_| Tip(ghost.clone())) }) .on_mouse_down( MouseButton::Left, - cx.listener(move |this, _: &MouseDownEvent, _, cx| { + cx.listener(move |this, event: &MouseDownEvent, _, cx| { + // Where in the box the hand took hold of it, + // for the drag this press may become: the + // clip has to move with the pointer rather + // than jump its head under it. + this.grab = + this.frame_under(event.position.x).saturating_sub(head); this.select((lane, i), cx); }), ) @@ -7511,6 +7624,28 @@ fn frac_along(x: Pixels, bounds: Bounds) -> f32 { ((x - bounds.left()) / bounds.size.width).clamp(0., 1.) } +/// The frame a dropped clip's head lands on: `raw`, unless one of `marks` is +/// within `tol` frames of where its head -- or its tail, `len` frames along -- +/// would come to rest, in which case that edge wins. The snap every timeline +/// has: clips meet exactly instead of a frame apart, and the hand does not have +/// to aim. The nearest mark wins, and a head landing on one beats a tail at the +/// same distance -- what was dragged is the head. +fn snapped(raw: u32, len: u32, tol: u32, marks: &[u32]) -> u32 { + let mut best: Option<(u32, u32)> = None; + for &mark in marks { + // Head on the mark, then tail on it -- a clip dragged up against the + // take in front of it snaps by whichever end reaches first. + for start in [Some(mark), mark.checked_sub(len)] { + let Some(start) = start else { continue }; + let d = start.abs_diff(raw); + if d <= tol && best.is_none_or(|(near, _)| d < near) { + best = Some((d, start)); + } + } + } + best.map_or(raw, |(_, start)| start) +} + /// Where down an element a pointer sits, 0..1 from the top: the vertical twin /// of [`frac_along`], for the equalizer, whose gain axis is the y one. An /// element that was never painted reads as its middle -- flat, the one answer @@ -7848,8 +7983,8 @@ mod tests { eq_y, export_path, export_settings, format_key, format_line, format_refusal, frac_along, frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked, menu_at, normalise, nothing_to_play, panel_h, project_path, push_digit, retarget, - scrub_due, show_label, silence_rate, source_tint, span_partner, speed_at, timecode, - unseen_paths, unseen_sources, whole_take, width_frac, window_title, + scrub_due, show_label, silence_rate, snapped, source_tint, span_partner, speed_at, + timecode, unseen_paths, unseen_sources, whole_take, width_frac, window_title, }; use super::{ LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows, @@ -8669,7 +8804,7 @@ mod tests { let v2 = session.add_lane(LaneKind::Video); assert_eq!(session.video_clip_at(0.0), Some((Lane::V1, 0))); - assert!(session.move_clip_to_lane(Lane::V1, 0, v2), "V1 -> V2"); + assert!(session.move_clip_to(Lane::V1, 0, v2, 0), "V1 -> V2"); assert!(session.lane_clips(Lane::V1).is_empty(), "it left V1"); assert_eq!(session.lane_clips(v2).len(), 1, "and landed on V2"); assert_eq!( @@ -8682,11 +8817,11 @@ mod tests { // Dropped on a lane of the other kind it is refused and nothing moves -- // the notice the front-end shows for it says which kind of lane to use. // (The other refusal, landing on another clip, is the engine's own test - // `move_to_lane_keeps_the_frames_and_refuses_the_rest`.) - assert!(!session.move_clip_to_lane(v2, 0, Lane::A1), "picture on A1"); + // `move_clip_keeps_the_frames_and_refuses_the_rest`.) + assert!(!session.move_clip_to(v2, 0, Lane::A1, 0), "picture on A1"); assert_eq!(session.lane_clips(v2).len(), 1, "and it stayed on V2"); assert!( - session.move_clip_to_lane(v2, 0, Lane::V1), + session.move_clip_to(v2, 0, Lane::V1, 0), "dragged back down" ); @@ -8886,6 +9021,70 @@ mod tests { assert_eq!(frac_down(px(50.), Bounds::default()), 0.5); } + /// What a drop reads: the frame under the pointer, through the same view + /// the boxes are drawn through. Zoomed in, the same pixel is a different + /// frame -- which is the whole reason `Player::frame_under` goes through + /// [`View`] rather than through the duration alone. + #[test] + fn a_drop_reads_the_frame_under_the_pointer_at_every_zoom() { + // A 200 px bed inset by the panel's padding, 10 seconds at 30 fps. + let bed: Bounds = Bounds { + origin: point(px(12.), px(400.)), + size: size(px(200.), px(6.)), + }; + let (duration, fps) = (10., 30.); + // The frame a pointer at window `x` names, exactly as `frame_under` + // composes it. + let under = + |view: View, x: f32| frame_at(view.time_at(frac_along(px(x), bed), duration), fps); + + // The whole timeline across the bed: halfway along is frame 150. + let fit = View::default(); + assert_eq!(under(fit, 12.), 0); + assert_eq!(under(fit, 112.), 150); + assert_eq!(under(fit, 212.), 300); + + // Four times in, starting at second 5: the same middle pixel is now the + // frame in the middle of seconds 5..7.5, and the left edge is not 0. + let zoomed = View { + zoom: 4., + start: 5., + }; + assert_eq!(under(zoomed, 12.), 150); + assert_eq!(under(zoomed, 112.), 187); + assert_eq!(under(zoomed, 212.), 225); + // ...and a drop past either end of the bed lands on the timeline, never + // outside it: `time_at` clamps. + assert_eq!(under(zoomed, 0.), 150); + assert_eq!(under(fit, 9999.), 300); + } + + /// The snap: a clip let go a few frames off a neighbour's edge lands *on* + /// it, by whichever of its own ends is nearer, and a clip let go in open bed + /// stays exactly where the hand left it. + #[test] + fn a_dropped_clip_snaps_to_the_edges_worth_landing_on() { + // A neighbour covering [100, 160) and the playhead at 300. + let marks = [100, 160, 300, 0]; + // Head a frame short of the neighbour's tail: laid end to end with it. + assert_eq!(snapped(158, 40, 4, &marks), 160); + // Tail a frame into its head: pulled back so the two meet exactly. + assert_eq!(snapped(62, 40, 4, &marks), 60); + // The playhead is an edge like any other. + assert_eq!(snapped(298, 40, 4, &marks), 300); + // Outside the tolerance nothing moves -- a gap the hand meant to leave + // is a gap. + assert_eq!(snapped(150, 40, 4, &marks), 150); + // No tolerance at all (zoomed right in, where a few pixels are worth + // less than a frame) is no snap: single frames are placed by hand. + assert_eq!(snapped(158, 40, 0, &marks), 158); + // The nearer edge wins when two are in reach. + assert_eq!(snapped(101, 40, 8, &marks), 100); + // ...and a mark closer to the head than `len` cannot pull the clip to a + // negative start. + assert_eq!(snapped(2, 40, 4, &marks), 0); + } + #[test] fn timecode_counts_frames_inside_the_second() { assert_eq!(timecode(0., 30.), "00:00:00:00"); @@ -9030,7 +9229,11 @@ mod tests { // The fit: what `start_frac`/`width_frac` used to answer, exactly. let fit = View::default(); for t in [0., 5., 12.5, 20.] { - assert_eq!(fit.frac_at(t, duration), (t / duration) as f32, "fit at {t}"); + assert_eq!( + fit.frac_at(t, duration), + (t / duration) as f32, + "fit at {t}" + ); } assert_eq!(fit.width_frac(5., duration), width_frac(5., duration)); for view in [ @@ -9149,7 +9352,11 @@ mod tests { assert_eq!(view.span(duration), 5.); // Inside: untouched, whichever part of the slice it is in. for at in [5., 7.5, 10.] { - assert_eq!(view.following(at, duration, fps), view, "{at} is on the bed"); + assert_eq!( + view.following(at, duration, fps), + view, + "{at} is on the bed" + ); } // Past the right edge, as playback does it: the head comes back on the // bed, and the zoom is not changed by the scroll. @@ -10153,7 +10360,10 @@ mod tests { while session.try_frame().is_some() {} std::thread::sleep(Duration::from_millis(10)); } - assert!(session.is_eos(), "an empty timeline is done before it starts"); + assert!( + session.is_eos(), + "an empty timeline is done before it starts" + ); // So the press is refused rather than sent down the restart branch -- // and with no session at all it is the same refusal. @@ -10374,6 +10584,7 @@ fn main() { clipboard: None, scrubbing: false, trim: None, + grab: 0, last_scrub: Instant::now(), last_target: 0, export: None, diff --git a/crates/engine/src/playback.rs b/crates/engine/src/playback.rs index c16245b..54f45a5 100644 --- a/crates/engine/src/playback.rs +++ b/crates/engine/src/playback.rs @@ -1075,14 +1075,20 @@ impl PlaybackSession { self.edit(|p| p.lift(lane, idx)) } - /// Moves that clip onto another lane of the same kind, keeping the frames it - /// covers -- the drag between tracks. One undo step, and it reseeks like - /// every other edit: which lane a clip sits on is what the compositor's - /// topmost-lane-wins rule reads, so the frame on screen is recomposed at - /// once. `false` for a bad index, for a move across kinds and for one that - /// would land on another clip; nothing changes. - pub fn move_clip_to_lane(&mut self, from: Lane, idx: usize, to: Lane) -> bool { - self.edit(|p| p.move_to_lane(from, idx, to)) + /// Moves that clip onto lane `to` with its head at timeline frame `start` -- + /// the drag that carries a take along its track or onto another one, and its + /// whole group with it. See [`Project::move_clip`] for the walls `start` is + /// clamped to and the ways a drop is refused. One undo step, and it reseeks + /// like every other edit: which lane a clip sits on is what the compositor's + /// topmost-lane-wins rule reads, and where it starts is what the playhead + /// reads, so the frame on screen is recomposed at once. + /// + /// A *frame*, where the rest of this type takes seconds, for + /// [`trim_clip`](Self::trim_clip)'s reason: the drag has already put the + /// pointer on a frame, and converting it back through seconds would be a + /// rounding step between the box let go of and the box committed. + pub fn move_clip_to(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> bool { + self.edit(|p| p.move_clip(from, idx, to, start)) } /// Drags one end of that clip to timeline frame `to`, changing how much of diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 5ae102b..e87dd41 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -1724,42 +1724,106 @@ impl Project { true } - /// Move the clip at `idx` of `from` onto `to`, keeping the timeline frames - /// it covers: the drag that rearranges takes across tracks. One snapshot, so - /// one [`Project::undo`] puts it back where it was. + /// Move the clip at `idx` of `from` onto `to` with its head at timeline + /// frame `start`: the drag that rearranges takes, along a track and across + /// them alike. One snapshot, so one [`Project::undo`] puts it back where it + /// was. `start` at the clip's own is the pure lane change. /// - /// Its group id travels with it, and that is not a desync: a link means - /// "these cover one span on however many lanes" and names no lane at all - /// (see [`links_are_consistent`]), so a picture moved from `V1` to `V2` is - /// still the same take as the sound under it. The span never changes here -- - /// only which lane draws it. + /// The whole group travels the same distance, each half on the lane it + /// already sits on -- a link means "these cover one span on however many + /// lanes" ([`links_are_consistent`]), so a picture that slid away from its + /// sound would be a group no save could load. Only the dragged half changes + /// lane, and that is not a desync: a link names no lane at all. /// - /// Refused, changing nothing, for a lane that is not there, an index that is - /// not there, a move onto the lane it is already on, a move across *kinds* - /// (a picture cannot play on an audio lane, and the save it wrote would not - /// open again), and a landing that would touch another clip -- a move that - /// overwrote what it landed on would destroy a take the pointer never named, - /// which is what [`place`](Project::place) may do and a drag may not. - pub fn move_to_lane(&mut self, from: Lane, idx: usize, to: Lane) -> bool { - if from.kind != to.kind || from == to || self.index(to).is_none() { + /// `start` is **clamped**, never refused, to the room the group has: no + /// member crosses a neighbour on the lane it lands on, exactly as + /// [`trim`](Project::trim) stops an edge at the clip in front of it. A hand + /// dragging past a neighbour means "as far as it goes", and butting up + /// against the take next door is how clips are laid end to end. The + /// tightest member's wall wins, so a sound half boxed in between two others + /// holds its picture still. + /// + /// Refused, changing nothing and costing no undo step, for a lane that is + /// not there, an index that is not there, a move across *kinds* (a picture + /// cannot play on an audio lane, and the save it wrote would not open + /// again), a head let go *inside* another clip or into a gap too narrow to + /// hold this one -- a move that overwrote what it landed on would destroy a + /// take the pointer never named, which is what [`place`](Project::place) + /// may do and a drag may not -- a half let go onto the lane its own partner + /// 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 { + 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 Some(clip) = self.lane(from).get(idx).copied() else { + 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. + if dest != held && members.iter().any(|&(l, _)| l == dest) { return false; - }; - if self - .lane(to) - .iter() - .any(|c| c.start < clip.end() && clip.start < c.end()) - { + } + let want = i64::from(start) - i64::from(clip.start); + // How far the group may travel, in timeline frames and signed: every + // member narrows it to the gap it is landing in, and what is left is + // what the pointer is clamped to. + let (mut lo, mut hi) = (i64::MIN, i64::MAX); + for &(l, i) in &members { + let c = self.lanes[l].clips[i]; + let land = if (l, i) == (held, idx) { dest } else { l }; + // Which gap this member's walls are read off: its own place on the + // lane it stays on, and the frame the pointer named on the lane it + // is let go over. + let at = if land == l { c.start } else { start }; + let (mut wall_lo, mut wall_hi) = (0, u32::MAX); + for (j, other) in self.lanes[land].clips.iter().enumerate() { + if members.contains(&(land, j)) { + continue; + } + if other.start <= at && at < other.end() { + return false; + } + match other.end() <= at { + true => wall_lo = wall_lo.max(other.end()), + false => wall_hi = wall_hi.min(other.start), + } + } + if wall_hi - wall_lo < c.frames() { + return false; + } + lo = lo.max(i64::from(wall_lo) - i64::from(c.start)); + hi = hi.min(i64::from(wall_hi - c.frames()) - i64::from(c.start)); + } + if lo > hi { + return false; + } + let delta = want.clamp(lo, hi); + if delta == 0 && dest == held { return false; } self.snapshot(); - self.lane_mut(from).expect("checked above").remove(idx); - let clips = self.lane_mut(to).expect("checked above"); - let at = clips.partition_point(|c| c.start < clip.start); - clips.insert(at, clip); - debug_assert!(sorted_disjoint(clips)); + for &(l, i) in &members { + let c = &mut self.lanes[l].clips[i]; + // In range by the walls above, which are `u32` frames throughout. + c.start = (i64::from(c.start) + delta) as u32; + } + if dest != held { + let clip = self.lanes[held].clips.remove(idx); + let clips = &mut self.lanes[dest].clips; + let at = clips.partition_point(|c| c.start < clip.start); + clips.insert(at, clip); + } + debug_assert!(sorted_disjoint(&self.lanes[dest].clips)); + debug_assert!( + members + .iter() + .all(|&(l, _)| sorted_disjoint(&self.lanes[l].clips)) + ); true } @@ -2100,7 +2164,12 @@ impl Project { // Nothing reaches past `at`: there is nothing to cut and nothing to // slide back, and a delete that changes nothing must not cost an undo // step (see [`snapshot`](Project::snapshot)). - if !self.lanes.iter().flat_map(|l| &l.clips).any(|c| c.end() > at) { + if !self + .lanes + .iter() + .flat_map(|l| &l.clips) + .any(|c| c.end() > at) + { return false; } self.snapshot(); @@ -3452,7 +3521,11 @@ mod tests { // A gap on one lane leaves the other's clip selectable there. assert!(p.lift(Lane::A1, 0)); assert_eq!(p.lane_clip_at(Lane::A1, 0), None, "the gap holds nothing"); - assert_eq!(p.lane_clip_at(Lane::A1, 3), Some(0), "indices moved with it"); + assert_eq!( + p.lane_clip_at(Lane::A1, 3), + Some(0), + "indices moved with it" + ); assert_eq!(p.lane_clip_at(Lane::V1, 0), Some(0)); // A lane that is not there is not a panic. assert_eq!(p.lane_clip_at(Lane::new(LaneKind::Video, 1), 0), None); @@ -3499,16 +3572,17 @@ mod tests { assert!(!p.place(Lane::V1, 0, clip(0, 7, 7, 0)), "empty clip"); } - /// The drag between tracks: the clip keeps its frames, one undo takes it - /// back, and every way it can be refused leaves the project untouched. + /// The drag between tracks: let go at the frames it already covers the clip + /// keeps them, one undo takes it back, and every way it can be refused + /// leaves the project untouched. #[test] - fn move_to_lane_keeps_the_frames_and_refuses_the_rest() { + fn move_clip_keeps_the_frames_and_refuses_the_rest() { let v2 = Lane::new(LaneKind::Video, 1); let mut p = three(); assert_eq!(p.add_lane(LaneKind::Video), v2); let before = shape(&p); - assert!(p.move_to_lane(Lane::V1, 1, v2), "V1's middle clip moves up"); + assert!(p.move_clip(Lane::V1, 1, v2, 3), "V1's middle clip moves up"); assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(5, 5, 9, 0)]); assert_eq!(shape(&p)[2], vec![clip(3, 3, 5, 0)], "same frames on V2"); assert_eq!(shape(&p)[1], before[1], "the audio lane is untouched"); @@ -3517,17 +3591,21 @@ mod tests { assert!(p.undo()); assert_eq!(shape(&p), before); - // A lane that is not there, the lane it is already on, an index that is - // not there, and a move across kinds: all refused, nothing changed. + // A lane that is not there, the lane and frame it is already at, an + // index that is not there, and a move across kinds: all refused, + // nothing changed. let history = p.history.len(); - for (from, idx, to) in [ - (Lane::V1, 1, Lane::new(LaneKind::Video, 7)), - (Lane::V1, 1, Lane::V1), - (Lane::V1, 9, v2), - (Lane::V1, 1, Lane::A1), - (Lane::A1, 1, v2), + for (from, idx, to, start) in [ + (Lane::V1, 1, Lane::new(LaneKind::Video, 7), 3), + (Lane::V1, 1, Lane::V1, 3), + (Lane::V1, 9, v2, 3), + (Lane::V1, 1, Lane::A1, 3), + (Lane::A1, 1, v2, 3), ] { - assert!(!p.move_to_lane(from, idx, to), "{from:?} {idx} -> {to:?}"); + assert!( + !p.move_clip(from, idx, to, start), + "{from:?} {idx} -> {to:?} at {start}" + ); } assert_eq!(shape(&p), before); assert_eq!(p.history.len(), history, "a refusal snapshots nothing"); @@ -3535,12 +3613,87 @@ mod tests { // Landing on another clip is refused rather than overwriting it: the // pointer named the lane, never the take already sitting there. assert!(p.place(v2, 3, clip(0, 100, 101, 0)), "V2 holds [3,4)"); - assert!(!p.move_to_lane(Lane::V1, 1, v2), "[3,5) would land on it"); - assert!(p.move_to_lane(Lane::V1, 0, v2), "[0,3) merely abuts it"); + assert!(!p.move_clip(Lane::V1, 1, v2, 3), "let go inside it"); + assert!(p.move_clip(Lane::V1, 0, v2, 0), "[0,3) merely abuts it"); assert_eq!(shape(&p)[0], vec![clip(3, 3, 5, 0), clip(5, 5, 9, 0)]); assert_eq!(shape(&p)[2], vec![clip(0, 0, 3, 0), clip(3, 100, 101, 0)]); } + /// The drag *along* a track, which is the one every other editor has: the + /// clip lands on the frame the pointer let it go at, and stops dead against + /// the neighbour rather than overwriting it. + #[test] + fn a_clip_slides_along_its_own_lane_and_butts_against_its_neighbour() { + let sources = vec![Source::new(FILE, 0)]; + let lanes = vec![ + (LaneKind::Video, vec![clip(0, 0, 3, 0), clip(10, 3, 6, 0)]), + (LaneKind::Audio, vec![clip(0, 0, 3, 0)]), + ]; + let mut p = Project::from_parts(sources, lanes, vec![], vec![]).expect("valid parts"); + let before = shape(&p); + + // Into the gap, exactly where it was let go -- nothing else moves. + assert!(p.move_clip(Lane::V1, 1, Lane::V1, 5)); + assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(5, 3, 6, 0)]); + assert_eq!(shape(&p)[1], before[1], "the audio lane is untouched"); + // Dragged past the clip in front of it: clamped to its wall, laid end + // to end with it, never over it. + assert!(p.move_clip(Lane::V1, 1, Lane::V1, 0)); + assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 6, 0)]); + // One undo per drag, and both of them together are the state it started + // in -- the frames each clip plays never changed. + assert!(p.undo() && p.undo()); + assert_eq!(shape(&p), before); + + // A drop that changes nothing is a click, not an edit. + let history = p.history.len(); + assert!(!p.move_clip(Lane::V1, 1, Lane::V1, 10)); + assert_eq!(p.history.len(), history, "a refusal snapshots nothing"); + } + + /// A drag carries the whole take: the sound half travels exactly as far as + /// the picture does, and the tighter of their two walls stops both. + #[test] + fn a_dragged_take_carries_its_sound_and_stops_at_the_tighter_wall() { + let mut p = three(); + assert!( + p.lift(Lane::V1, 2) && p.lift(Lane::A1, 2), + "room to the right" + ); + let link = p.lane(Lane::V1)[1].link.expect("a split hands out ids"); + assert_eq!(p.lane(Lane::A1)[1].link, Some(link), "both halves grouped"); + // Something in the sound's way, and nothing at all in the picture's. + assert!(p.place(Lane::A1, 10, clip(0, 0, 2, 0)), "A1 holds [10,12)"); + let before = shape(&p); + + assert!(p.move_clip(Lane::V1, 1, Lane::V1, 30), "dragged far right"); + assert_eq!(p.lane(Lane::V1)[1].start, 8, "stopped where the sound did"); + assert_eq!(p.lane(Lane::A1)[1].start, 8, "and the sound came with it"); + links_are_consistent(&p.lanes).expect("one id per lane, one span"); + assert!(p.undo()); + assert_eq!(shape(&p), before, "one undo for the whole take"); + } + + /// The drag that does both at once -- another lane *and* another frame -- + /// which is what a pointer let go over a lane always names. + #[test] + fn a_clip_dragged_to_another_lane_lands_on_the_pointers_frame() { + let v2 = Lane::new(LaneKind::Video, 1); + let mut p = three(); + assert_eq!(p.add_lane(LaneKind::Video), v2); + + assert!(p.move_clip(Lane::V1, 2, v2, 20), "[5,9) -> V2 at 20"); + assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 5, 0)]); + assert_eq!(shape(&p)[2], vec![clip(20, 5, 9, 0)], "at the pointer"); + assert_eq!( + p.lane(Lane::A1)[2].start, + 20, + "its sound travelled the same distance" + ); + links_are_consistent(&p.lanes).expect("one id per lane, one span"); + assert_eq!(p.timeline_frames(), 24, "a move is not an insert"); + } + /// How long `FILE` is, which is what a trim's tail is allowed to reach: /// [`three`] cuts up all nine of its frames. const SRC: &[u32] = &[9]; @@ -3563,8 +3716,15 @@ mod tests { // ...and back out, no further than the clip behind it. assert!(p.trim(Lane::V1, 1, Edge::End, 8, SRC)); - assert_eq!(shape(&p)[0][1], clip(3, 3, 5, 0), "stopped at the neighbour"); - assert!(!p.trim(Lane::V1, 1, Edge::End, 8, SRC), "already at the wall"); + assert_eq!( + shape(&p)[0][1], + clip(3, 3, 5, 0), + "stopped at the neighbour" + ); + assert!( + !p.trim(Lane::V1, 1, Edge::End, 8, SRC), + "already at the wall" + ); // One frame of clip always survives, however far back the pointer went. assert!(p.trim(Lane::V1, 1, Edge::End, 0, SRC)); @@ -3588,7 +3748,11 @@ mod tests { fn trimming_the_head_moves_the_in_point() { let mut p = three(); assert!(p.trim(Lane::V1, 0, Edge::Start, 2, SRC)); - assert_eq!(shape(&p)[0][0], clip(2, 2, 3, 0), "two frames off the front"); + assert_eq!( + shape(&p)[0][0], + clip(2, 2, 3, 0), + "two frames off the front" + ); assert_eq!(p.map(Lane::V1, 2), Some((0, 2)), "frame 2 still plays 2"); assert_eq!(p.map(Lane::V1, 1), None, "and the front is a gap"); assert!(p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "back out again"); @@ -3625,7 +3789,11 @@ mod tests { ); assert!(!p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "already there"); assert!(p.trim(Lane::V1, 0, Edge::Start, 2, SRC)); - assert_eq!(shape(&p)[0], vec![clip(2, 7, 9, 0)], "the in-point followed"); + assert_eq!( + shape(&p)[0], + vec![clip(2, 7, 9, 0)], + "the in-point followed" + ); assert!(p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "and back out"); assert_eq!(shape(&p)[0], vec![clip(0, 5, 9, 0)]); } @@ -3665,7 +3833,11 @@ mod tests { ); // Past the cap is clamped, exactly as the tail is -- not refused. assert!(p.trim(Lane::V1, 0, Edge::Start, 0, CAP)); - assert_eq!(shape(&p)[0][0], clip(60, 0, 60, 0), "a cap's worth, no more"); + assert_eq!( + shape(&p)[0][0], + clip(60, 0, 60, 0), + "a cap's worth, no more" + ); // ...and the clip in front is the nearer wall of the two: no head trim // may open an overlap. assert!(p.trim(Lane::V1, 0, Edge::Start, 90, CAP)); @@ -3692,7 +3864,10 @@ mod tests { assert!(p.trim(Lane::V1, 2, Edge::Start, 7, SRC)); assert_eq!(shape(&p)[0][2], clip(7, 7, 9, 0)); assert_eq!(shape(&p)[1][2], clip(7, 7, 9, 0), "the sound followed"); - assert!(p.trim(Lane::A1, 2, Edge::End, 8, SRC), "either half drags it"); + assert!( + p.trim(Lane::A1, 2, Edge::End, 8, SRC), + "either half drags it" + ); assert_eq!(shape(&p)[0][2], clip(7, 7, 8, 0), "and the picture follows"); links_are_consistent(&p.lanes).expect("one id per lane, one span"); @@ -3727,13 +3902,13 @@ mod tests { /// A moved half stays in its group: a link names a span, not a lane, so the /// picture on `V2` is still the same take as the sound under it on `A1`. #[test] - fn move_to_lane_keeps_the_group() { + fn move_clip_keeps_the_group() { let v2 = Lane::new(LaneKind::Video, 1); let mut p = three(); p.add_lane(LaneKind::Video); let link = p.lane(Lane::V1)[1].link.expect("a split hands out ids"); assert_eq!(p.lane(Lane::A1)[1].link, Some(link), "both halves grouped"); - assert!(p.move_to_lane(Lane::V1, 1, v2)); + assert!(p.move_clip(Lane::V1, 1, v2, 3)); assert_eq!(p.lane(v2)[0].link, Some(link), "the id travelled with it"); links_are_consistent(&p.lanes).expect("one id per lane, one span"); assert_eq!( @@ -4878,7 +5053,12 @@ mod tests { } let lane = if next(2) == 0 { Lane::V1 } else { Lane::A1 }; let idx = next(4) as usize; - let _ = match next(15) { + let _ = match next(16) { + // A clip dragged along its own lane to wherever the pointer + // let it go: the op that moves a placement without changing + // what it plays, so what it may not do is land on top of the + // neighbour it was dragged at. + 15 => p.move_clip(lane, idx, lane, frame), // ...and a second `place`, so a punch lands inside a clip // far more often than one op in fourteen would put it there. 14 => p.place(lane, frame, copied), @@ -5298,7 +5478,12 @@ mod tests { .unwrap_or(&clip(0, 0, 5, 0)); let lane = lanes[next(3) as usize]; let idx = next(4) as usize; - let _ = match next(13) { + let _ = match next(14) { + // The drag, in the mix: a clip dragged along its own lane + // and onto another one, at a frame the pointer picked -- + // which is where the group rule and the sorted+disjoint one + // meet, since every half travels the same distance. + 13 => p.move_clip(lane, idx, lanes[next(3) as usize], frame), 10 => p.set_eq(lane, idx, Some(band_at(next(4)))), 11 => p.set_eq(lane, idx, None), // A rate, which on many lanes is the group rule as well: