forge
Explore
Log in

Don't you dare go Hollow

A clip goes where you drop it, along its track or across them

Browse files at this commit

cdb0c4be33e0a45072c4d2795af456c789fca36aAhmet Kaan GümüşParent: d648c77
  • feat
    • a clip goes where you drop it, along its track or across them
Show full message
✨ feat(timeline): a clip goes where you drop it, along its track or across them

A clip dragged along its own lane now lands on the frame the pointer let it go
on, and a library row dropped on a lane goes in under the pointer instead of at
the playhead.

Project::move_to_lane becomes Project::move_clip(from, idx, to, start): the
whole group travels the same distance, each half on its own lane, and start is
clamped to the gap every member lands in -- a clip butts up against the take
next door instead of overwriting it, and the tightest member's wall wins. A drop
that changes neither lane nor frame is a click and costs no undo step.

The front-end reads the frame under the pointer through the same View the boxes
are drawn through, so it lands where it was let go at any zoom, less the offset
the hand grabbed the box at, and snaps to a neighbour edge or the playhead
within a few pixels.
crates/app/src/main.rsmodified+264 -53
@@ -250,8 +250,9 @@ struct AssetDrag(PathBuf, usize);
250 250
251 /// A clip already on the timeline being dragged: the lane it is on and its index251 /// A clip already on the timeline being dragged: the lane it is on and its index
252 /// there, which is how every other edit names a clip. Unlike an [`AssetDrag`]252 /// there, which is how every other edit names a clip. Unlike an [`AssetDrag`]
253-/// nothing is inserted -- the same clip changes lane and keeps its frames -- so253+/// nothing is inserted -- the same clip changes lane and keeps the frames it
254-/// where along the bed it is let go says nothing here either.254+/// plays -- but where along the bed it is let go is exactly where it lands, less
255+/// the offset the hand grabbed it at ([`Player::grab`]).
255 #[derive(Clone, Copy)]256 #[derive(Clone, Copy)]
256 struct ClipDrag {257 struct ClipDrag {
257 lane: Lane,258 lane: Lane,
@@ -283,6 +284,12 @@ struct Trim {
283 /// still the body.284 /// still the body.
284 const EDGE_W: f32 = 6.;285 const EDGE_W: f32 = 6.;
285 286
287+/// How close to an edge a dragged clip has to be let go for it to land *on* it:
288+/// the snap every timeline has, in pixels rather than frames so that it feels
289+/// the same at every zoom. Narrower than [`EDGE_W`] -- a hand aiming between two
290+/// takes must still be able to leave a gap of a few frames there.
291+const SNAP_PX: f64 = 5.;
292+
286 /// An open clip menu: which clip it was opened on, where it hangs, and whether293 /// An open clip menu: which clip it was opened on, where it hangs, and whether
287 /// it has been turned over to show what that clip *is* instead of what can be294 /// it has been turned over to show what that clip *is* instead of what can be
288 /// done to it. The lane and index are the ones the same click selected, so295 /// done to it. The lane and index are the ones the same click selected, so
@@ -647,6 +654,13 @@ struct Player {
647 /// `scrubbing`'s reason: a 6 px strip is not where the pointer stays. See654 /// `scrubbing`'s reason: a 6 px strip is not where the pointer stays. See
648 /// [`Trim`].655 /// [`Trim`].
649 trim: Option<Trim>,656 trim: Option<Trim>,
657+ /// How far into the clip the last press on a box landed, in timeline
658+ /// frames: what a drag lets go of is the *point that was grabbed*, so the
659+ /// head lands that much in front of the pointer and the clip does not jump
660+ /// under the hand. Recorded at the press because gpui hands the drop only
661+ /// the value being dragged, and stale between drags, which costs nothing --
662+ /// no drag starts without a press on the box it moves.
663+ grab: u32,
650 last_scrub: Instant,664 last_scrub: Instant,
651 last_target: u32,665 last_target: u32,
652 /// The running export. While it owns the UI the editor is read-only.666 /// The running export. While it owns the UI the editor is read-only.
@@ -2050,19 +2064,29 @@ impl Player {
2050 cx.notify();2064 cx.notify();
2051 }2065 }
2052 2066
2053- /// A clip dragged off one lane and let go over another: it changes track and2067+ /// A clip let go at window `x` over lane `to`: it lands with its head where
2054- /// keeps the frames it covers, as one undo step. Dropped back on its own2068+ /// the hand is carrying it ([`Player::drop_frame`]), on the track it was
2055- /// lane it is not an edit at all, so nothing is said about it. The engine2069+ /// dropped on, taking its whole take with it -- one undo step for the
2056- /// reseeks, so all this owes is the flag reset -- and the selection, whose2070+ /// gesture. Dropped back where it was picked up it is not an edit at all, so
2057- /// index was that lane's own and now names a different clip there.2071+ /// nothing is said about it. The engine reseeks, so all this owes is the
2058- fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, cx: &mut Context<Self>) {2072+ /// flag reset -- and the selection, whose index was that lane's own and now
2059- if self.exporting().is_some() || from == to {2073+ /// names a different clip there.
2074+ fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, x: Pixels, cx: &mut Context<Self>) {
2075+ if self.exporting().is_some() {
2060 return;2076 return;
2061 }2077 }
2078+ let (Some(start), Some(was)) = (
2079+ self.drop_frame(from, idx, to, x),
2080+ self.session
2081+ .as_ref()
2082+ .and_then(|session| session.lane_clips(from).get(idx).map(|c| c.start)),
2083+ ) else {
2084+ return;
2085+ };
2062 let moved = self2086 let moved = self
2063 .session2087 .session
2064 .as_mut()2088 .as_mut()
2065- .is_some_and(|session| session.move_clip_to_lane(from, idx, to));2089+ .is_some_and(|session| session.move_clip_to(from, idx, to, start));
2066 let (kind, lanes) = match from.kind {2090 let (kind, lanes) = match from.kind {
2067 LaneKind::Video => ("picture", "video"),2091 LaneKind::Video => ("picture", "video"),
2068 LaneKind::Audio => ("sound", "audio"),2092 LaneKind::Audio => ("sound", "audio"),
@@ -2072,9 +2096,10 @@ impl Player {
2072 self.selected = None;2096 self.selected = None;
2073 self.reset_after_reseek();2097 self.reset_after_reseek();
2074 }2098 }
2075- // The two ways a drag is refused, told apart by the one thing the2099+ // The three ways a drag is refused, told apart by what the
2076- // front-end already knows: a lane's kind. Everything else that2100+ // front-end already knows: a lane's kind, and where the clip was.
2077- // could refuse (a clip that is not there) cannot be dragged.2101+ // Everything else that could refuse (a clip that is not there)
2102+ // cannot be dragged.
2078 false if from.kind != to.kind => {2103 false if from.kind != to.kind => {
2079 self.notice = Some(2104 self.notice = Some(
2080 format!(2105 format!(
@@ -2084,6 +2109,9 @@ impl Player {
2084 .into(),2109 .into(),
2085 )2110 )
2086 }2111 }
2112+ // Picked up and put back down where it was: a click, and a click
2113+ // says nothing.
2114+ false if from == to && start == was => {}
2087 false => {2115 false => {
2088 self.notice = Some(2116 self.notice = Some(
2089 format!(2117 format!(
@@ -2097,6 +2125,55 @@ impl Player {
2097 cx.notify();2125 cx.notify();
2098 }2126 }
2099 2127
2128+ /// Where a clip let go at window `x` over lane `to` wants its head: the
2129+ /// frame under the pointer, less however far into the box the hand grabbed
2130+ /// it (so the clip does not jump under the pointer), pulled onto a
2131+ /// neighbouring edge when it lands within [`SNAP_PX`] of one. `None` when
2132+ /// there is no such clip to move. The engine has the last word on where it
2133+ /// may actually go -- this is the ask, not the answer.
2134+ ///
2135+ /// ponytail: the bed ends where the timeline does, so a clip cannot be
2136+ /// dragged *past* the last frame -- the pointer has no pixel out there to
2137+ /// name and [`View::time_at`] clamps to the duration. Room to the right is
2138+ /// made by pulling a tail out first ([`Player::drawn_duration`] already
2139+ /// lengthens the bed for exactly that gesture). The upgrade is to draw the
2140+ /// bed past the end while a clip drag is in flight, the way a trim already
2141+ /// does: `drawn_duration` would take the dragged clip's length the same way
2142+ /// it takes a trim's reach.
2143+ fn drop_frame(&self, from: Lane, idx: usize, to: Lane, x: Pixels) -> Option<u32> {
2144+ let session = self.session.as_ref()?;
2145+ let clip = session.lane_clips(from).get(idx).copied()?;
2146+ let raw = self.frame_under(x).saturating_sub(self.grab);
2147+ // The edges worth landing on: both ends of every clip already on the
2148+ // lane it is being let go over, the playhead, and the head of the
2149+ // timeline. Never the group's own boxes -- a clip does not snap to
2150+ // where it already is.
2151+ let mut marks: Vec<u32> = session
2152+ .lane_clips(to)
2153+ .iter()
2154+ .enumerate()
2155+ .filter(|&(i, c)| {
2156+ (to, i) != (from, idx) && !(clip.link.is_some() && c.link == clip.link)
2157+ })
2158+ .flat_map(|(_, c)| [c.start, c.end()])
2159+ .collect();
2160+ marks.push(frame_at(session.now(), self.fps));
2161+ marks.push(0);
2162+ Some(snapped(raw, clip.frames(), self.snap_frames(), &marks))
2163+ }
2164+
2165+ /// [`SNAP_PX`] in timeline frames at the zoom the bed is drawn at: a snap is
2166+ /// a distance on screen, so zoomed right in it is worth less than a frame
2167+ /// (no snap at all, which is what a hand placing single frames wants) and
2168+ /// zoomed out to the whole timeline it is worth many.
2169+ fn snap_frames(&self) -> u32 {
2170+ let bed = f64::from(f32::from(self.ruler.get().size.width));
2171+ if bed <= 0. {
2172+ return 0;
2173+ }
2174+ (SNAP_PX / bed * self.view.span(self.drawn_duration()) * self.fps) as u32
2175+ }
2176+
2100 /// Opens the clip menu on the box under the pointer, from the right button2177 /// Opens the clip menu on the box under the pointer, from the right button
2101 /// wherever it was pressed on that box -- its middle or one of its edge2178 /// wherever it was pressed on that box -- its middle or one of its edge
2102 /// strips, which cover the middle's own listener. Selecting first is part of2179 /// strips, which cover the middle's own listener. Selecting first is part of
@@ -2151,20 +2228,30 @@ impl Player {
2151 /// against the same duration the boxes are drawn to, so the edge tracks the2228 /// against the same duration the boxes are drawn to, so the edge tracks the
2152 /// pointer exactly.2229 /// pointer exactly.
2153 fn trim_to(&mut self, x: Pixels, cx: &mut Context<Self>) {2230 fn trim_to(&mut self, x: Pixels, cx: &mut Context<Self>) {
2154- let (duration, fps) = (self.drawn_duration(), self.fps);2231+ let at = self.frame_under(x);
2155- let at = self
2156- .view
2157- .time_at(frac_along(x, self.ruler.get()), duration);
2158 let (Some(trim), Some(session)) = (&mut self.trim, &self.session) else {2232 let (Some(trim), Some(session)) = (&mut self.trim, &self.session) else {
2159 return;2233 return;
2160 };2234 };
2161 let Some((lo, hi)) = session.trim_room(trim.lane, trim.idx, trim.edge) else {2235 let Some((lo, hi)) = session.trim_room(trim.lane, trim.idx, trim.edge) else {
2162 return;2236 return;
2163 };2237 };
2164- trim.to = frame_at(at, fps).clamp(lo, hi);2238+ trim.to = at.clamp(lo, hi);
2165 cx.notify();2239 cx.notify();
2166 }2240 }
2167 2241
2242+ /// The timeline frame a pointer at window x is on: along the same bed the
2243+ /// ruler is measured on, through the same [`View`] every box is drawn
2244+ /// through, so a zoomed-in panel answers with the frame under the pointer
2245+ /// and not with the one that would have been there unzoomed. The one
2246+ /// question a trim, a grab and a drop all ask.
2247+ fn frame_under(&self, x: Pixels) -> u32 {
2248+ let duration = self.drawn_duration();
2249+ frame_at(
2250+ self.view.time_at(frac_along(x, self.ruler.get()), duration),
2251+ self.fps,
2252+ )
2253+ }
2254+
2168 /// The release: the whole drag reaches the engine as one edit, so it is one2255 /// The release: the whole drag reaches the engine as one edit, so it is one
2169 /// undo step. The selection survives it -- a trim inserts and removes2256 /// undo step. The selection survives it -- a trim inserts and removes
2170 /// nothing, so every index a lane had still names the clip it named.2257 /// nothing, so every index a lane had still names the clip it named.
@@ -2264,16 +2351,18 @@ impl Player {
2264 2351
2265 /// The one way a library row reaches the timeline: the Add button and a row2352 /// The one way a library row reaches the timeline: the Add button and a row
2266 /// dragged onto a lane both come here, so there is a single answer to what2353 /// dragged onto a lane both come here, so there is a single answer to what
2267- /// "add this source" does. The whole source goes in at the playhead as one2354+ /// "add this source" does. The whole source goes in as one grouped take at
2268- /// grouped take -- the same insert a paste makes, so everything after it2355+ /// `at` -- the frame the pointer let it go on, or the playhead for the
2269- /// moves along rather than being painted over. Reseeks like every other2356+ /// button, which names no place. It is the same insert a paste makes, so
2270- /// edit, and drops the timeline's selection with it: the insert has just2357+ /// everything after it moves along rather than being painted over. Reseeks
2271- /// moved the indices it pointed at.2358+ /// like every other edit, and drops the timeline's selection with it: the
2359+ /// insert has just moved the indices it pointed at.
2272 fn insert_source(2360 fn insert_source(
2273 &mut self,2361 &mut self,
2274 path: &Path,2362 path: &Path,
2275 stream: usize,2363 stream: usize,
2276 onto: Option<Lane>,2364 onto: Option<Lane>,
2365+ at: Option<u32>,
2277 cx: &mut Context<Self>,2366 cx: &mut Context<Self>,
2278 ) {2367 ) {
2279 if self.exporting().is_some() {2368 if self.exporting().is_some() {
@@ -2308,8 +2397,16 @@ impl Player {
2308 // in: a row that has never been on a lane is placeable at its full2397 // in: a row that has never been on a lane is placeable at its full
2309 // length, which is the whole point of an import that only fills the2398 // length, which is the whole point of an import that only fills the
2310 // library.2399 // library.
2400+ let fps = self.fps;
2311 let placed = match &mut self.session {2401 let placed = match &mut self.session {
2312- Some(session) => session.place_stream_at(session.now(), path, stream, onto),2402+ // Seconds, because that is what the engine's own door takes: the
2403+ // frame the pointer named goes back through the same rate every box
2404+ // on the bed is drawn at, so it lands on the frame it was let go on
2405+ // rather than a neighbouring one.
2406+ Some(session) => {
2407+ let at = at.map_or_else(|| session.now(), |frame| f64::from(frame) / fps);
2408+ session.place_stream_at(at, path, stream, onto)
2409+ }
2313 None => Ok(false),2410 None => Ok(false),
2314 };2411 };
2315 match placed {2412 match placed {
@@ -2459,7 +2556,7 @@ impl Player {
2459 }2556 }
2460 RowItem::Add => {2557 RowItem::Add => {
2461 self.library_menu = None;2558 self.library_menu = None;
2462- self.insert_source(&menu.path, menu.stream, None, cx);2559+ self.insert_source(&menu.path, menu.stream, None, None, cx);
2463 }2560 }
2464 RowItem::Remove => {2561 RowItem::Remove => {
2465 self.library_menu = None;2562 self.library_menu = None;
@@ -3585,7 +3682,7 @@ impl Player {
3585 // row that says where it came from has to say which rate that3682 // row that says where it came from has to say which rate that
3586 // was.3683 // was.
3587 (None, Some(meta)) => format!(3684 (None, Some(meta)) => format!(
3588- "{} — {}x{} @ {:.2} fps · drag onto a lane, or Add at playhead",3685+ "{} — {}x{} @ {:.2} fps · drag it where you want it, or Add at playhead",
3589 row.path.display(),3686 row.path.display(),
3590 meta.width,3687 meta.width,
3591 meta.height,3688 meta.height,
@@ -3769,7 +3866,7 @@ impl Player {
3769 "Add at playhead",3866 "Add at playhead",
3770 match self.selected_asset {3867 match self.selected_asset {
3771 Some(_) => "inserts the picked file at the playhead".to_string(),3868 Some(_) => "inserts the picked file at the playhead".to_string(),
3772- None => "click a file above first — or drag one onto a lane".to_string(),3869+ None => "click a file above first — or drag one where you want it".to_string(),
3773 },3870 },
3774 can_add(3871 can_add(
3775 self.selected_asset.as_ref(),3872 self.selected_asset.as_ref(),
@@ -3780,7 +3877,7 @@ impl Player {
3780 if let Some((path, stream)) = this.selected_asset.clone() {3877 if let Some((path, stream)) = this.selected_asset.clone() {
3781 // No lane: the button means "wherever this belongs",3878 // No lane: the button means "wherever this belongs",
3782 // which for a file with no picture is the audio lane.3879 // which for a file with no picture is the audio lane.
3783- this.insert_source(&path, stream, None, cx);3880+ this.insert_source(&path, stream, None, None, cx);
3784 }3881 }
3785 }),3882 }),
3786 ))3883 ))
@@ -5810,7 +5907,7 @@ impl Player {
5810 let (sel, sel_link) = (self.selected, self.selected_link());5907 let (sel, sel_link) = (self.selected, self.selected_link());
5811 let audio = lane.kind == LaneKind::Audio;5908 let audio = lane.kind == LaneKind::Audio;
5812 let tip: SharedString = format!(5909 let tip: SharedString = format!(
5813- "Select (or {} under the playhead, {}/{} along the lane) — drag an end to trim, {} removes the take, {} leaves a gap, {} rejoins a cut",5910+ "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",
5814 self.keymap.display(ActionId::Select),5911 self.keymap.display(ActionId::Select),
5815 self.keymap.display(ActionId::SelectPrev),5912 self.keymap.display(ActionId::SelectPrev),
5816 self.keymap.display(ActionId::SelectNext),5913 self.keymap.display(ActionId::SelectNext),
@@ -5877,18 +5974,23 @@ impl Player {
5877 .bg(rgb(LETTERBOX))5974 .bg(rgb(LETTERBOX))
5878 .overflow_hidden()5975 .overflow_hidden()
5879 // A library row let go over a lane is the same insert the5976 // A library row let go over a lane is the same insert the
5880- // Add button makes, through the same call -- at the5977+ // Add button makes, through the same call -- but where the
5881- // playhead, not at the pointer: clips here are placed end5978+ // pointer let it go, not at the playhead: a hand that
5882- // to end, so where along the bed it landed says nothing.5979+ // carried a file to a place on the bed named that place.
5883- .on_drop(cx.listener(move |this, drag: &AssetDrag, _, cx| {5980+ // gpui hands a drop no event, so the pointer is read off
5884- this.insert_source(&drag.0.clone(), drag.1, Some(lane), cx)5981+ // the window, which took it from the release that fired
5982+ // this (gpui window.rs:3602).
5983+ .on_drop(cx.listener(move |this, drag: &AssetDrag, window, cx| {
5984+ let at = this.frame_under(window.mouse_position().x);
5985+ this.insert_source(&drag.0.clone(), drag.1, Some(lane), Some(at), cx)
5885 }))5986 }))
5886 .drag_over::<AssetDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM)))5987 .drag_over::<AssetDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM)))
5887- // A clip let go over another lane changes track and keeps5988+ // ...and a clip let go over a lane lands the same way: on
5888- // the frames it covers -- the pointer's position along the5989+ // the track it was dropped on, at the frame it was carried
5889- // bed says nothing here either, for the reason above.5990+ // to -- its own included, which is the drag that moves a
5890- .on_drop(cx.listener(move |this, drag: &ClipDrag, _, cx| {5991+ // take along its track.
5891- this.move_clip(drag.lane, drag.idx, lane, cx)5992+ .on_drop(cx.listener(move |this, drag: &ClipDrag, window, cx| {
5993+ this.move_clip(drag.lane, drag.idx, lane, window.mouse_position().x, cx)
5892 }))5994 }))
5893 .drag_over::<ClipDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM)))5995 .drag_over::<ClipDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM)))
5894 .children(clips.iter().enumerate().map(|(i, clip)| {5996 .children(clips.iter().enumerate().map(|(i, clip)| {
@@ -5933,6 +6035,10 @@ impl Player {
5933 // says what is moving.6035 // says what is moving.
5934 let ghost: SharedString =6036 let ghost: SharedString =
5935 label.clone().unwrap_or_else(|| lane.label()).into();6037 label.clone().unwrap_or_else(|| lane.label()).into();
6038+ // Its head in frames, for the press below: the `start`
6039+ // above is the same moment in seconds, which is what
6040+ // the box is *drawn* from.
6041+ let head = clip.start;
5936 div()6042 div()
5937 // Named per lane: two rows numbering their clips6043 // Named per lane: two rows numbering their clips
5938 // from zero would hand gpui the same id twice.6044 // from zero would hand gpui the same id twice.
@@ -5960,16 +6066,23 @@ impl Player {
5960 .cursor_pointer()6066 .cursor_pointer()
5961 .hover(|s| s.border_color(rgb(ACCENT)))6067 .hover(|s| s.border_color(rgb(ACCENT)))
5962 .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into())6068 .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into())
5963- // Dragged onto another lane it *moves* there; the6069+ // Dragged, it *moves*: to the frame it was let go on
5964- // click that starts the drag still selects, so6070+ // and to the lane it was let go over. The click that
5965- // picking a clip up and putting it back down where6071+ // starts the drag still selects, so picking a clip
5966- // it was is exactly a click.6072+ // up and putting it back down where it was is
6073+ // exactly a click.
5967 .on_drag(ClipDrag { lane, idx: i }, move |_, _, _, cx| {6074 .on_drag(ClipDrag { lane, idx: i }, move |_, _, _, cx| {
5968 cx.new(|_| Tip(ghost.clone()))6075 cx.new(|_| Tip(ghost.clone()))
5969 })6076 })
5970 .on_mouse_down(6077 .on_mouse_down(
5971 MouseButton::Left,6078 MouseButton::Left,
5972- cx.listener(move |this, _: &MouseDownEvent, _, cx| {6079+ cx.listener(move |this, event: &MouseDownEvent, _, cx| {
6080+ // Where in the box the hand took hold of it,
6081+ // for the drag this press may become: the
6082+ // clip has to move with the pointer rather
6083+ // than jump its head under it.
6084+ this.grab =
6085+ this.frame_under(event.position.x).saturating_sub(head);
5973 this.select((lane, i), cx);6086 this.select((lane, i), cx);
5974 }),6087 }),
5975 )6088 )
@@ -7511,6 +7624,28 @@ fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 {
7511 ((x - bounds.left()) / bounds.size.width).clamp(0., 1.)7624 ((x - bounds.left()) / bounds.size.width).clamp(0., 1.)
7512 }7625 }
7513 7626
7627+/// The frame a dropped clip's head lands on: `raw`, unless one of `marks` is
7628+/// within `tol` frames of where its head -- or its tail, `len` frames along --
7629+/// would come to rest, in which case that edge wins. The snap every timeline
7630+/// has: clips meet exactly instead of a frame apart, and the hand does not have
7631+/// to aim. The nearest mark wins, and a head landing on one beats a tail at the
7632+/// same distance -- what was dragged is the head.
7633+fn snapped(raw: u32, len: u32, tol: u32, marks: &[u32]) -> u32 {
7634+ let mut best: Option<(u32, u32)> = None;
7635+ for &mark in marks {
7636+ // Head on the mark, then tail on it -- a clip dragged up against the
7637+ // take in front of it snaps by whichever end reaches first.
7638+ for start in [Some(mark), mark.checked_sub(len)] {
7639+ let Some(start) = start else { continue };
7640+ let d = start.abs_diff(raw);
7641+ if d <= tol && best.is_none_or(|(near, _)| d < near) {
7642+ best = Some((d, start));
7643+ }
7644+ }
7645+ }
7646+ best.map_or(raw, |(_, start)| start)
7647+}
7648+
7514 /// Where down an element a pointer sits, 0..1 from the top: the vertical twin7649 /// Where down an element a pointer sits, 0..1 from the top: the vertical twin
7515 /// of [`frac_along`], for the equalizer, whose gain axis is the y one. An7650 /// of [`frac_along`], for the equalizer, whose gain axis is the y one. An
7516 /// element that was never painted reads as its middle -- flat, the one answer7651 /// element that was never painted reads as its middle -- flat, the one answer
@@ -7848,8 +7983,8 @@ mod tests {
7848 eq_y, export_path, export_settings, format_key, format_line, format_refusal, frac_along,7983 eq_y, export_path, export_settings, format_key, format_line, format_refusal, frac_along,
7849 frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked,7984 frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked,
7850 menu_at, normalise, nothing_to_play, panel_h, project_path, push_digit, retarget,7985 menu_at, normalise, nothing_to_play, panel_h, project_path, push_digit, retarget,
7851- scrub_due, show_label, silence_rate, source_tint, span_partner, speed_at, timecode,7986+ scrub_due, show_label, silence_rate, snapped, source_tint, span_partner, speed_at,
7852- unseen_paths, unseen_sources, whole_take, width_frac, window_title,7987+ timecode, unseen_paths, unseen_sources, whole_take, width_frac, window_title,
7853 };7988 };
7854 use super::{7989 use super::{
7855 LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows,7990 LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows,
@@ -8669,7 +8804,7 @@ mod tests {
8669 let v2 = session.add_lane(LaneKind::Video);8804 let v2 = session.add_lane(LaneKind::Video);
8670 assert_eq!(session.video_clip_at(0.0), Some((Lane::V1, 0)));8805 assert_eq!(session.video_clip_at(0.0), Some((Lane::V1, 0)));
8671 8806
8672- assert!(session.move_clip_to_lane(Lane::V1, 0, v2), "V1 -> V2");8807+ assert!(session.move_clip_to(Lane::V1, 0, v2, 0), "V1 -> V2");
8673 assert!(session.lane_clips(Lane::V1).is_empty(), "it left V1");8808 assert!(session.lane_clips(Lane::V1).is_empty(), "it left V1");
8674 assert_eq!(session.lane_clips(v2).len(), 1, "and landed on V2");8809 assert_eq!(session.lane_clips(v2).len(), 1, "and landed on V2");
8675 assert_eq!(8810 assert_eq!(
@@ -8682,11 +8817,11 @@ mod tests {
8682 // Dropped on a lane of the other kind it is refused and nothing moves --8817 // Dropped on a lane of the other kind it is refused and nothing moves --
8683 // the notice the front-end shows for it says which kind of lane to use.8818 // the notice the front-end shows for it says which kind of lane to use.
8684 // (The other refusal, landing on another clip, is the engine's own test8819 // (The other refusal, landing on another clip, is the engine's own test
8685- // `move_to_lane_keeps_the_frames_and_refuses_the_rest`.)8820+ // `move_clip_keeps_the_frames_and_refuses_the_rest`.)
8686- assert!(!session.move_clip_to_lane(v2, 0, Lane::A1), "picture on A1");8821+ assert!(!session.move_clip_to(v2, 0, Lane::A1, 0), "picture on A1");
8687 assert_eq!(session.lane_clips(v2).len(), 1, "and it stayed on V2");8822 assert_eq!(session.lane_clips(v2).len(), 1, "and it stayed on V2");
8688 assert!(8823 assert!(
8689- session.move_clip_to_lane(v2, 0, Lane::V1),8824+ session.move_clip_to(v2, 0, Lane::V1, 0),
8690 "dragged back down"8825 "dragged back down"
8691 );8826 );
8692 8827
@@ -8886,6 +9021,70 @@ mod tests {
8886 assert_eq!(frac_down(px(50.), Bounds::default()), 0.5);9021 assert_eq!(frac_down(px(50.), Bounds::default()), 0.5);
8887 }9022 }
8888 9023
9024+ /// What a drop reads: the frame under the pointer, through the same view
9025+ /// the boxes are drawn through. Zoomed in, the same pixel is a different
9026+ /// frame -- which is the whole reason `Player::frame_under` goes through
9027+ /// [`View`] rather than through the duration alone.
9028+ #[test]
9029+ fn a_drop_reads_the_frame_under_the_pointer_at_every_zoom() {
9030+ // A 200 px bed inset by the panel's padding, 10 seconds at 30 fps.
9031+ let bed: Bounds<Pixels> = Bounds {
9032+ origin: point(px(12.), px(400.)),
9033+ size: size(px(200.), px(6.)),
9034+ };
9035+ let (duration, fps) = (10., 30.);
9036+ // The frame a pointer at window `x` names, exactly as `frame_under`
9037+ // composes it.
9038+ let under =
9039+ |view: View, x: f32| frame_at(view.time_at(frac_along(px(x), bed), duration), fps);
9040+
9041+ // The whole timeline across the bed: halfway along is frame 150.
9042+ let fit = View::default();
9043+ assert_eq!(under(fit, 12.), 0);
9044+ assert_eq!(under(fit, 112.), 150);
9045+ assert_eq!(under(fit, 212.), 300);
9046+
9047+ // Four times in, starting at second 5: the same middle pixel is now the
9048+ // frame in the middle of seconds 5..7.5, and the left edge is not 0.
9049+ let zoomed = View {
9050+ zoom: 4.,
9051+ start: 5.,
9052+ };
9053+ assert_eq!(under(zoomed, 12.), 150);
9054+ assert_eq!(under(zoomed, 112.), 187);
9055+ assert_eq!(under(zoomed, 212.), 225);
9056+ // ...and a drop past either end of the bed lands on the timeline, never
9057+ // outside it: `time_at` clamps.
9058+ assert_eq!(under(zoomed, 0.), 150);
9059+ assert_eq!(under(fit, 9999.), 300);
9060+ }
9061+
9062+ /// The snap: a clip let go a few frames off a neighbour's edge lands *on*
9063+ /// it, by whichever of its own ends is nearer, and a clip let go in open bed
9064+ /// stays exactly where the hand left it.
9065+ #[test]
9066+ fn a_dropped_clip_snaps_to_the_edges_worth_landing_on() {
9067+ // A neighbour covering [100, 160) and the playhead at 300.
9068+ let marks = [100, 160, 300, 0];
9069+ // Head a frame short of the neighbour's tail: laid end to end with it.
9070+ assert_eq!(snapped(158, 40, 4, &marks), 160);
9071+ // Tail a frame into its head: pulled back so the two meet exactly.
9072+ assert_eq!(snapped(62, 40, 4, &marks), 60);
9073+ // The playhead is an edge like any other.
9074+ assert_eq!(snapped(298, 40, 4, &marks), 300);
9075+ // Outside the tolerance nothing moves -- a gap the hand meant to leave
9076+ // is a gap.
9077+ assert_eq!(snapped(150, 40, 4, &marks), 150);
9078+ // No tolerance at all (zoomed right in, where a few pixels are worth
9079+ // less than a frame) is no snap: single frames are placed by hand.
9080+ assert_eq!(snapped(158, 40, 0, &marks), 158);
9081+ // The nearer edge wins when two are in reach.
9082+ assert_eq!(snapped(101, 40, 8, &marks), 100);
9083+ // ...and a mark closer to the head than `len` cannot pull the clip to a
9084+ // negative start.
9085+ assert_eq!(snapped(2, 40, 4, &marks), 0);
9086+ }
9087+
8889 #[test]9088 #[test]
8890 fn timecode_counts_frames_inside_the_second() {9089 fn timecode_counts_frames_inside_the_second() {
8891 assert_eq!(timecode(0., 30.), "00:00:00:00");9090 assert_eq!(timecode(0., 30.), "00:00:00:00");
@@ -9030,7 +9229,11 @@ mod tests {
9030 // The fit: what `start_frac`/`width_frac` used to answer, exactly.9229 // The fit: what `start_frac`/`width_frac` used to answer, exactly.
9031 let fit = View::default();9230 let fit = View::default();
9032 for t in [0., 5., 12.5, 20.] {9231 for t in [0., 5., 12.5, 20.] {
9033- assert_eq!(fit.frac_at(t, duration), (t / duration) as f32, "fit at {t}");9232+ assert_eq!(
9233+ fit.frac_at(t, duration),
9234+ (t / duration) as f32,
9235+ "fit at {t}"
9236+ );
9034 }9237 }
9035 assert_eq!(fit.width_frac(5., duration), width_frac(5., duration));9238 assert_eq!(fit.width_frac(5., duration), width_frac(5., duration));
9036 for view in [9239 for view in [
@@ -9149,7 +9352,11 @@ mod tests {
9149 assert_eq!(view.span(duration), 5.);9352 assert_eq!(view.span(duration), 5.);
9150 // Inside: untouched, whichever part of the slice it is in.9353 // Inside: untouched, whichever part of the slice it is in.
9151 for at in [5., 7.5, 10.] {9354 for at in [5., 7.5, 10.] {
9152- assert_eq!(view.following(at, duration, fps), view, "{at} is on the bed");9355+ assert_eq!(
9356+ view.following(at, duration, fps),
9357+ view,
9358+ "{at} is on the bed"
9359+ );
9153 }9360 }
9154 // Past the right edge, as playback does it: the head comes back on the9361 // Past the right edge, as playback does it: the head comes back on the
9155 // bed, and the zoom is not changed by the scroll.9362 // bed, and the zoom is not changed by the scroll.
@@ -10153,7 +10360,10 @@ mod tests {
10153 while session.try_frame().is_some() {}10360 while session.try_frame().is_some() {}
10154 std::thread::sleep(Duration::from_millis(10));10361 std::thread::sleep(Duration::from_millis(10));
10155 }10362 }
10156- assert!(session.is_eos(), "an empty timeline is done before it starts");10363+ assert!(
10364+ session.is_eos(),
10365+ "an empty timeline is done before it starts"
10366+ );
10157 10367
10158 // So the press is refused rather than sent down the restart branch --10368 // So the press is refused rather than sent down the restart branch --
10159 // and with no session at all it is the same refusal.10369 // and with no session at all it is the same refusal.
@@ -10374,6 +10584,7 @@ fn main() {
10374 clipboard: None,10584 clipboard: None,
10375 scrubbing: false,10585 scrubbing: false,
10376 trim: None,10586 trim: None,
10587+ grab: 0,
10377 last_scrub: Instant::now(),10588 last_scrub: Instant::now(),
10378 last_target: 0,10589 last_target: 0,
10379 export: None,10590 export: None,
crates/engine/src/playback.rsmodified+14 -8
@@ -1075,14 +1075,20 @@ impl PlaybackSession {
1075 self.edit(|p| p.lift(lane, idx))1075 self.edit(|p| p.lift(lane, idx))
1076 }1076 }
1077 1077
1078- /// Moves that clip onto another lane of the same kind, keeping the frames it1078+ /// Moves that clip onto lane `to` with its head at timeline frame `start` --
1079- /// covers -- the drag between tracks. One undo step, and it reseeks like1079+ /// the drag that carries a take along its track or onto another one, and its
1080- /// every other edit: which lane a clip sits on is what the compositor's1080+ /// whole group with it. See [`Project::move_clip`] for the walls `start` is
1081- /// topmost-lane-wins rule reads, so the frame on screen is recomposed at1081+ /// clamped to and the ways a drop is refused. One undo step, and it reseeks
1082- /// once. `false` for a bad index, for a move across kinds and for one that1082+ /// like every other edit: which lane a clip sits on is what the compositor's
1083- /// would land on another clip; nothing changes.1083+ /// topmost-lane-wins rule reads, and where it starts is what the playhead
1084- pub fn move_clip_to_lane(&mut self, from: Lane, idx: usize, to: Lane) -> bool {1084+ /// reads, so the frame on screen is recomposed at once.
1085- self.edit(|p| p.move_to_lane(from, idx, to))1085+ ///
1086+ /// A *frame*, where the rest of this type takes seconds, for
1087+ /// [`trim_clip`](Self::trim_clip)'s reason: the drag has already put the
1088+ /// pointer on a frame, and converting it back through seconds would be a
1089+ /// rounding step between the box let go of and the box committed.
1090+ pub fn move_clip_to(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> bool {
1091+ self.edit(|p| p.move_clip(from, idx, to, start))
1086 }1092 }
1087 1093
1088 /// Drags one end of that clip to timeline frame `to`, changing how much of1094 /// Drags one end of that clip to timeline frame `to`, changing how much of
crates/engine/src/project.rsmodified+240 -55
@@ -1724,42 +1724,106 @@ impl Project {
1724 true1724 true
1725 }1725 }
1726 1726
1727- /// Move the clip at `idx` of `from` onto `to`, keeping the timeline frames1727+ /// Move the clip at `idx` of `from` onto `to` with its head at timeline
1728- /// it covers: the drag that rearranges takes across tracks. One snapshot, so1728+ /// frame `start`: the drag that rearranges takes, along a track and across
1729- /// one [`Project::undo`] puts it back where it was.1729+ /// them alike. One snapshot, so one [`Project::undo`] puts it back where it
1730+ /// was. `start` at the clip's own is the pure lane change.
1730 ///1731 ///
1731- /// Its group id travels with it, and that is not a desync: a link means1732+ /// The whole group travels the same distance, each half on the lane it
1732- /// "these cover one span on however many lanes" and names no lane at all1733+ /// already sits on -- a link means "these cover one span on however many
1733- /// (see [`links_are_consistent`]), so a picture moved from `V1` to `V2` is1734+ /// lanes" ([`links_are_consistent`]), so a picture that slid away from its
1734- /// still the same take as the sound under it. The span never changes here --1735+ /// sound would be a group no save could load. Only the dragged half changes
1735- /// only which lane draws it.1736+ /// lane, and that is not a desync: a link names no lane at all.
1736 ///1737 ///
1737- /// Refused, changing nothing, for a lane that is not there, an index that is1738+ /// `start` is **clamped**, never refused, to the room the group has: no
1738- /// not there, a move onto the lane it is already on, a move across *kinds*1739+ /// member crosses a neighbour on the lane it lands on, exactly as
1739- /// (a picture cannot play on an audio lane, and the save it wrote would not1740+ /// [`trim`](Project::trim) stops an edge at the clip in front of it. A hand
1740- /// open again), and a landing that would touch another clip -- a move that1741+ /// dragging past a neighbour means "as far as it goes", and butting up
1741- /// overwrote what it landed on would destroy a take the pointer never named,1742+ /// against the take next door is how clips are laid end to end. The
1742- /// which is what [`place`](Project::place) may do and a drag may not.1743+ /// tightest member's wall wins, so a sound half boxed in between two others
1743- pub fn move_to_lane(&mut self, from: Lane, idx: usize, to: Lane) -> bool {1744+ /// holds its picture still.
1744- if from.kind != to.kind || from == to || self.index(to).is_none() {1745+ ///
1746+ /// Refused, changing nothing and costing no undo step, for a lane that is
1747+ /// not there, an index that is not there, a move across *kinds* (a picture
1748+ /// cannot play on an audio lane, and the save it wrote would not open
1749+ /// again), a head let go *inside* another clip or into a gap too narrow to
1750+ /// hold this one -- a move that overwrote what it landed on would destroy a
1751+ /// take the pointer never named, which is what [`place`](Project::place)
1752+ /// may do and a drag may not -- a half let go onto the lane its own partner
1753+ /// is on, and a drop that changes neither lane nor frame, which is a clip
1754+ /// picked up and put back down, i.e. a click.
1755+ pub fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> bool {
1756+ let (Some(dest), Some(clip)) = (self.index(to), self.lane(from).get(idx).copied()) else {
1757+ return false;
1758+ };
1759+ if from.kind != to.kind {
1745 return false;1760 return false;
1746 }1761 }
1747- let Some(clip) = self.lane(from).get(idx).copied() else {1762+ let members = self.group_of(from, idx).expect("the clip was found");
1763+ let held = self.index(from).expect("the clip was found on it");
1764+ // The two halves of one group would land on one span of one lane, which
1765+ // is the one thing a link may never mean. Refused rather than clamped:
1766+ // the partner moves the same distance, so there is no room for it
1767+ // anywhere on that lane.
1768+ if dest != held && members.iter().any(|&(l, _)| l == dest) {
1748 return false;1769 return false;
1749- };1770+ }
1750- if self1771+ let want = i64::from(start) - i64::from(clip.start);
1751- .lane(to)1772+ // How far the group may travel, in timeline frames and signed: every
1752- .iter()1773+ // member narrows it to the gap it is landing in, and what is left is
1753- .any(|c| c.start < clip.end() && clip.start < c.end())1774+ // what the pointer is clamped to.
1754- {1775+ let (mut lo, mut hi) = (i64::MIN, i64::MAX);
1776+ for &(l, i) in &members {
1777+ let c = self.lanes[l].clips[i];
1778+ let land = if (l, i) == (held, idx) { dest } else { l };
1779+ // Which gap this member's walls are read off: its own place on the
1780+ // lane it stays on, and the frame the pointer named on the lane it
1781+ // is let go over.
1782+ let at = if land == l { c.start } else { start };
1783+ let (mut wall_lo, mut wall_hi) = (0, u32::MAX);
1784+ for (j, other) in self.lanes[land].clips.iter().enumerate() {
1785+ if members.contains(&(land, j)) {
1786+ continue;
1787+ }
1788+ if other.start <= at && at < other.end() {
1789+ return false;
1790+ }
1791+ match other.end() <= at {
1792+ true => wall_lo = wall_lo.max(other.end()),
1793+ false => wall_hi = wall_hi.min(other.start),
1794+ }
1795+ }
1796+ if wall_hi - wall_lo < c.frames() {
1797+ return false;
1798+ }
1799+ lo = lo.max(i64::from(wall_lo) - i64::from(c.start));
1800+ hi = hi.min(i64::from(wall_hi - c.frames()) - i64::from(c.start));
1801+ }
1802+ if lo > hi {
1803+ return false;
1804+ }
1805+ let delta = want.clamp(lo, hi);
1806+ if delta == 0 && dest == held {
1755 return false;1807 return false;
1756 }1808 }
1757 self.snapshot();1809 self.snapshot();
1758- self.lane_mut(from).expect("checked above").remove(idx);1810+ for &(l, i) in &members {
1759- let clips = self.lane_mut(to).expect("checked above");1811+ let c = &mut self.lanes[l].clips[i];
1760- let at = clips.partition_point(|c| c.start < clip.start);1812+ // In range by the walls above, which are `u32` frames throughout.
1761- clips.insert(at, clip);1813+ c.start = (i64::from(c.start) + delta) as u32;
1762- debug_assert!(sorted_disjoint(clips));1814+ }
1815+ if dest != held {
1816+ let clip = self.lanes[held].clips.remove(idx);
1817+ let clips = &mut self.lanes[dest].clips;
1818+ let at = clips.partition_point(|c| c.start < clip.start);
1819+ clips.insert(at, clip);
1820+ }
1821+ debug_assert!(sorted_disjoint(&self.lanes[dest].clips));
1822+ debug_assert!(
1823+ members
1824+ .iter()
1825+ .all(|&(l, _)| sorted_disjoint(&self.lanes[l].clips))
1826+ );
1763 true1827 true
1764 }1828 }
1765 1829
@@ -2100,7 +2164,12 @@ impl Project {
2100 // Nothing reaches past `at`: there is nothing to cut and nothing to2164 // Nothing reaches past `at`: there is nothing to cut and nothing to
2101 // slide back, and a delete that changes nothing must not cost an undo2165 // slide back, and a delete that changes nothing must not cost an undo
2102 // step (see [`snapshot`](Project::snapshot)).2166 // step (see [`snapshot`](Project::snapshot)).
2103- if !self.lanes.iter().flat_map(|l| &l.clips).any(|c| c.end() > at) {2167+ if !self
2168+ .lanes
2169+ .iter()
2170+ .flat_map(|l| &l.clips)
2171+ .any(|c| c.end() > at)
2172+ {
2104 return false;2173 return false;
2105 }2174 }
2106 self.snapshot();2175 self.snapshot();
@@ -3452,7 +3521,11 @@ mod tests {
3452 // A gap on one lane leaves the other's clip selectable there.3521 // A gap on one lane leaves the other's clip selectable there.
3453 assert!(p.lift(Lane::A1, 0));3522 assert!(p.lift(Lane::A1, 0));
3454 assert_eq!(p.lane_clip_at(Lane::A1, 0), None, "the gap holds nothing");3523 assert_eq!(p.lane_clip_at(Lane::A1, 0), None, "the gap holds nothing");
3455- assert_eq!(p.lane_clip_at(Lane::A1, 3), Some(0), "indices moved with it");3524+ assert_eq!(
3525+ p.lane_clip_at(Lane::A1, 3),
3526+ Some(0),
3527+ "indices moved with it"
3528+ );
3456 assert_eq!(p.lane_clip_at(Lane::V1, 0), Some(0));3529 assert_eq!(p.lane_clip_at(Lane::V1, 0), Some(0));
3457 // A lane that is not there is not a panic.3530 // A lane that is not there is not a panic.
3458 assert_eq!(p.lane_clip_at(Lane::new(LaneKind::Video, 1), 0), None);3531 assert_eq!(p.lane_clip_at(Lane::new(LaneKind::Video, 1), 0), None);
@@ -3499,16 +3572,17 @@ mod tests {
3499 assert!(!p.place(Lane::V1, 0, clip(0, 7, 7, 0)), "empty clip");3572 assert!(!p.place(Lane::V1, 0, clip(0, 7, 7, 0)), "empty clip");
3500 }3573 }
3501 3574
3502- /// The drag between tracks: the clip keeps its frames, one undo takes it3575+ /// The drag between tracks: let go at the frames it already covers the clip
3503- /// back, and every way it can be refused leaves the project untouched.3576+ /// keeps them, one undo takes it back, and every way it can be refused
3577+ /// leaves the project untouched.
3504 #[test]3578 #[test]
3505- fn move_to_lane_keeps_the_frames_and_refuses_the_rest() {3579+ fn move_clip_keeps_the_frames_and_refuses_the_rest() {
3506 let v2 = Lane::new(LaneKind::Video, 1);3580 let v2 = Lane::new(LaneKind::Video, 1);
3507 let mut p = three();3581 let mut p = three();
3508 assert_eq!(p.add_lane(LaneKind::Video), v2);3582 assert_eq!(p.add_lane(LaneKind::Video), v2);
3509 let before = shape(&p);3583 let before = shape(&p);
3510 3584
3511- assert!(p.move_to_lane(Lane::V1, 1, v2), "V1's middle clip moves up");3585+ assert!(p.move_clip(Lane::V1, 1, v2, 3), "V1's middle clip moves up");
3512 assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(5, 5, 9, 0)]);3586 assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(5, 5, 9, 0)]);
3513 assert_eq!(shape(&p)[2], vec![clip(3, 3, 5, 0)], "same frames on V2");3587 assert_eq!(shape(&p)[2], vec![clip(3, 3, 5, 0)], "same frames on V2");
3514 assert_eq!(shape(&p)[1], before[1], "the audio lane is untouched");3588 assert_eq!(shape(&p)[1], before[1], "the audio lane is untouched");
@@ -3517,17 +3591,21 @@ mod tests {
3517 assert!(p.undo());3591 assert!(p.undo());
3518 assert_eq!(shape(&p), before);3592 assert_eq!(shape(&p), before);
3519 3593
3520- // A lane that is not there, the lane it is already on, an index that is3594+ // A lane that is not there, the lane and frame it is already at, an
3521- // not there, and a move across kinds: all refused, nothing changed.3595+ // index that is not there, and a move across kinds: all refused,
3596+ // nothing changed.
3522 let history = p.history.len();3597 let history = p.history.len();
3523- for (from, idx, to) in [3598+ for (from, idx, to, start) in [
3524- (Lane::V1, 1, Lane::new(LaneKind::Video, 7)),3599+ (Lane::V1, 1, Lane::new(LaneKind::Video, 7), 3),
3525- (Lane::V1, 1, Lane::V1),3600+ (Lane::V1, 1, Lane::V1, 3),
3526- (Lane::V1, 9, v2),3601+ (Lane::V1, 9, v2, 3),
3527- (Lane::V1, 1, Lane::A1),3602+ (Lane::V1, 1, Lane::A1, 3),
3528- (Lane::A1, 1, v2),3603+ (Lane::A1, 1, v2, 3),
3529 ] {3604 ] {
3530- assert!(!p.move_to_lane(from, idx, to), "{from:?} {idx} -> {to:?}");3605+ assert!(
3606+ !p.move_clip(from, idx, to, start),
3607+ "{from:?} {idx} -> {to:?} at {start}"
3608+ );
3531 }3609 }
3532 assert_eq!(shape(&p), before);3610 assert_eq!(shape(&p), before);
3533 assert_eq!(p.history.len(), history, "a refusal snapshots nothing");3611 assert_eq!(p.history.len(), history, "a refusal snapshots nothing");
@@ -3535,12 +3613,87 @@ mod tests {
3535 // Landing on another clip is refused rather than overwriting it: the3613 // Landing on another clip is refused rather than overwriting it: the
3536 // pointer named the lane, never the take already sitting there.3614 // pointer named the lane, never the take already sitting there.
3537 assert!(p.place(v2, 3, clip(0, 100, 101, 0)), "V2 holds [3,4)");3615 assert!(p.place(v2, 3, clip(0, 100, 101, 0)), "V2 holds [3,4)");
3538- assert!(!p.move_to_lane(Lane::V1, 1, v2), "[3,5) would land on it");3616+ assert!(!p.move_clip(Lane::V1, 1, v2, 3), "let go inside it");
3539- assert!(p.move_to_lane(Lane::V1, 0, v2), "[0,3) merely abuts it");3617+ assert!(p.move_clip(Lane::V1, 0, v2, 0), "[0,3) merely abuts it");
3540 assert_eq!(shape(&p)[0], vec![clip(3, 3, 5, 0), clip(5, 5, 9, 0)]);3618 assert_eq!(shape(&p)[0], vec![clip(3, 3, 5, 0), clip(5, 5, 9, 0)]);
3541 assert_eq!(shape(&p)[2], vec![clip(0, 0, 3, 0), clip(3, 100, 101, 0)]);3619 assert_eq!(shape(&p)[2], vec![clip(0, 0, 3, 0), clip(3, 100, 101, 0)]);
3542 }3620 }
3543 3621
3622+ /// The drag *along* a track, which is the one every other editor has: the
3623+ /// clip lands on the frame the pointer let it go at, and stops dead against
3624+ /// the neighbour rather than overwriting it.
3625+ #[test]
3626+ fn a_clip_slides_along_its_own_lane_and_butts_against_its_neighbour() {
3627+ let sources = vec![Source::new(FILE, 0)];
3628+ let lanes = vec![
3629+ (LaneKind::Video, vec![clip(0, 0, 3, 0), clip(10, 3, 6, 0)]),
3630+ (LaneKind::Audio, vec![clip(0, 0, 3, 0)]),
3631+ ];
3632+ let mut p = Project::from_parts(sources, lanes, vec![], vec![]).expect("valid parts");
3633+ let before = shape(&p);
3634+
3635+ // Into the gap, exactly where it was let go -- nothing else moves.
3636+ assert!(p.move_clip(Lane::V1, 1, Lane::V1, 5));
3637+ assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(5, 3, 6, 0)]);
3638+ assert_eq!(shape(&p)[1], before[1], "the audio lane is untouched");
3639+ // Dragged past the clip in front of it: clamped to its wall, laid end
3640+ // to end with it, never over it.
3641+ assert!(p.move_clip(Lane::V1, 1, Lane::V1, 0));
3642+ assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 6, 0)]);
3643+ // One undo per drag, and both of them together are the state it started
3644+ // in -- the frames each clip plays never changed.
3645+ assert!(p.undo() && p.undo());
3646+ assert_eq!(shape(&p), before);
3647+
3648+ // A drop that changes nothing is a click, not an edit.
3649+ let history = p.history.len();
3650+ assert!(!p.move_clip(Lane::V1, 1, Lane::V1, 10));
3651+ assert_eq!(p.history.len(), history, "a refusal snapshots nothing");
3652+ }
3653+
3654+ /// A drag carries the whole take: the sound half travels exactly as far as
3655+ /// the picture does, and the tighter of their two walls stops both.
3656+ #[test]
3657+ fn a_dragged_take_carries_its_sound_and_stops_at_the_tighter_wall() {
3658+ let mut p = three();
3659+ assert!(
3660+ p.lift(Lane::V1, 2) && p.lift(Lane::A1, 2),
3661+ "room to the right"
3662+ );
3663+ let link = p.lane(Lane::V1)[1].link.expect("a split hands out ids");
3664+ assert_eq!(p.lane(Lane::A1)[1].link, Some(link), "both halves grouped");
3665+ // Something in the sound's way, and nothing at all in the picture's.
3666+ assert!(p.place(Lane::A1, 10, clip(0, 0, 2, 0)), "A1 holds [10,12)");
3667+ let before = shape(&p);
3668+
3669+ assert!(p.move_clip(Lane::V1, 1, Lane::V1, 30), "dragged far right");
3670+ assert_eq!(p.lane(Lane::V1)[1].start, 8, "stopped where the sound did");
3671+ assert_eq!(p.lane(Lane::A1)[1].start, 8, "and the sound came with it");
3672+ links_are_consistent(&p.lanes).expect("one id per lane, one span");
3673+ assert!(p.undo());
3674+ assert_eq!(shape(&p), before, "one undo for the whole take");
3675+ }
3676+
3677+ /// The drag that does both at once -- another lane *and* another frame --
3678+ /// which is what a pointer let go over a lane always names.
3679+ #[test]
3680+ fn a_clip_dragged_to_another_lane_lands_on_the_pointers_frame() {
3681+ let v2 = Lane::new(LaneKind::Video, 1);
3682+ let mut p = three();
3683+ assert_eq!(p.add_lane(LaneKind::Video), v2);
3684+
3685+ assert!(p.move_clip(Lane::V1, 2, v2, 20), "[5,9) -> V2 at 20");
3686+ assert_eq!(shape(&p)[0], vec![clip(0, 0, 3, 0), clip(3, 3, 5, 0)]);
3687+ assert_eq!(shape(&p)[2], vec![clip(20, 5, 9, 0)], "at the pointer");
3688+ assert_eq!(
3689+ p.lane(Lane::A1)[2].start,
3690+ 20,
3691+ "its sound travelled the same distance"
3692+ );
3693+ links_are_consistent(&p.lanes).expect("one id per lane, one span");
3694+ assert_eq!(p.timeline_frames(), 24, "a move is not an insert");
3695+ }
3696+
3544 /// How long `FILE` is, which is what a trim's tail is allowed to reach:3697 /// How long `FILE` is, which is what a trim's tail is allowed to reach:
3545 /// [`three`] cuts up all nine of its frames.3698 /// [`three`] cuts up all nine of its frames.
3546 const SRC: &[u32] = &[9];3699 const SRC: &[u32] = &[9];
@@ -3563,8 +3716,15 @@ mod tests {
3563 3716
3564 // ...and back out, no further than the clip behind it.3717 // ...and back out, no further than the clip behind it.
3565 assert!(p.trim(Lane::V1, 1, Edge::End, 8, SRC));3718 assert!(p.trim(Lane::V1, 1, Edge::End, 8, SRC));
3566- assert_eq!(shape(&p)[0][1], clip(3, 3, 5, 0), "stopped at the neighbour");3719+ assert_eq!(
3567- assert!(!p.trim(Lane::V1, 1, Edge::End, 8, SRC), "already at the wall");3720+ shape(&p)[0][1],
3721+ clip(3, 3, 5, 0),
3722+ "stopped at the neighbour"
3723+ );
3724+ assert!(
3725+ !p.trim(Lane::V1, 1, Edge::End, 8, SRC),
3726+ "already at the wall"
3727+ );
3568 3728
3569 // One frame of clip always survives, however far back the pointer went.3729 // One frame of clip always survives, however far back the pointer went.
3570 assert!(p.trim(Lane::V1, 1, Edge::End, 0, SRC));3730 assert!(p.trim(Lane::V1, 1, Edge::End, 0, SRC));
@@ -3588,7 +3748,11 @@ mod tests {
3588 fn trimming_the_head_moves_the_in_point() {3748 fn trimming_the_head_moves_the_in_point() {
3589 let mut p = three();3749 let mut p = three();
3590 assert!(p.trim(Lane::V1, 0, Edge::Start, 2, SRC));3750 assert!(p.trim(Lane::V1, 0, Edge::Start, 2, SRC));
3591- assert_eq!(shape(&p)[0][0], clip(2, 2, 3, 0), "two frames off the front");3751+ assert_eq!(
3752+ shape(&p)[0][0],
3753+ clip(2, 2, 3, 0),
3754+ "two frames off the front"
3755+ );
3592 assert_eq!(p.map(Lane::V1, 2), Some((0, 2)), "frame 2 still plays 2");3756 assert_eq!(p.map(Lane::V1, 2), Some((0, 2)), "frame 2 still plays 2");
3593 assert_eq!(p.map(Lane::V1, 1), None, "and the front is a gap");3757 assert_eq!(p.map(Lane::V1, 1), None, "and the front is a gap");
3594 assert!(p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "back out again");3758 assert!(p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "back out again");
@@ -3625,7 +3789,11 @@ mod tests {
3625 );3789 );
3626 assert!(!p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "already there");3790 assert!(!p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "already there");
3627 assert!(p.trim(Lane::V1, 0, Edge::Start, 2, SRC));3791 assert!(p.trim(Lane::V1, 0, Edge::Start, 2, SRC));
3628- assert_eq!(shape(&p)[0], vec![clip(2, 7, 9, 0)], "the in-point followed");3792+ assert_eq!(
3793+ shape(&p)[0],
3794+ vec![clip(2, 7, 9, 0)],
3795+ "the in-point followed"
3796+ );
3629 assert!(p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "and back out");3797 assert!(p.trim(Lane::V1, 0, Edge::Start, 0, SRC), "and back out");
3630 assert_eq!(shape(&p)[0], vec![clip(0, 5, 9, 0)]);3798 assert_eq!(shape(&p)[0], vec![clip(0, 5, 9, 0)]);
3631 }3799 }
@@ -3665,7 +3833,11 @@ mod tests {
3665 );3833 );
3666 // Past the cap is clamped, exactly as the tail is -- not refused.3834 // Past the cap is clamped, exactly as the tail is -- not refused.
3667 assert!(p.trim(Lane::V1, 0, Edge::Start, 0, CAP));3835 assert!(p.trim(Lane::V1, 0, Edge::Start, 0, CAP));
3668- assert_eq!(shape(&p)[0][0], clip(60, 0, 60, 0), "a cap's worth, no more");3836+ assert_eq!(
3837+ shape(&p)[0][0],
3838+ clip(60, 0, 60, 0),
3839+ "a cap's worth, no more"
3840+ );
3669 // ...and the clip in front is the nearer wall of the two: no head trim3841 // ...and the clip in front is the nearer wall of the two: no head trim
3670 // may open an overlap.3842 // may open an overlap.
3671 assert!(p.trim(Lane::V1, 0, Edge::Start, 90, CAP));3843 assert!(p.trim(Lane::V1, 0, Edge::Start, 90, CAP));
@@ -3692,7 +3864,10 @@ mod tests {
3692 assert!(p.trim(Lane::V1, 2, Edge::Start, 7, SRC));3864 assert!(p.trim(Lane::V1, 2, Edge::Start, 7, SRC));
3693 assert_eq!(shape(&p)[0][2], clip(7, 7, 9, 0));3865 assert_eq!(shape(&p)[0][2], clip(7, 7, 9, 0));
3694 assert_eq!(shape(&p)[1][2], clip(7, 7, 9, 0), "the sound followed");3866 assert_eq!(shape(&p)[1][2], clip(7, 7, 9, 0), "the sound followed");
3695- assert!(p.trim(Lane::A1, 2, Edge::End, 8, SRC), "either half drags it");3867+ assert!(
3868+ p.trim(Lane::A1, 2, Edge::End, 8, SRC),
3869+ "either half drags it"
3870+ );
3696 assert_eq!(shape(&p)[0][2], clip(7, 7, 8, 0), "and the picture follows");3871 assert_eq!(shape(&p)[0][2], clip(7, 7, 8, 0), "and the picture follows");
3697 links_are_consistent(&p.lanes).expect("one id per lane, one span");3872 links_are_consistent(&p.lanes).expect("one id per lane, one span");
3698 3873
@@ -3727,13 +3902,13 @@ mod tests {
3727 /// A moved half stays in its group: a link names a span, not a lane, so the3902 /// A moved half stays in its group: a link names a span, not a lane, so the
3728 /// picture on `V2` is still the same take as the sound under it on `A1`.3903 /// picture on `V2` is still the same take as the sound under it on `A1`.
3729 #[test]3904 #[test]
3730- fn move_to_lane_keeps_the_group() {3905+ fn move_clip_keeps_the_group() {
3731 let v2 = Lane::new(LaneKind::Video, 1);3906 let v2 = Lane::new(LaneKind::Video, 1);
3732 let mut p = three();3907 let mut p = three();
3733 p.add_lane(LaneKind::Video);3908 p.add_lane(LaneKind::Video);
3734 let link = p.lane(Lane::V1)[1].link.expect("a split hands out ids");3909 let link = p.lane(Lane::V1)[1].link.expect("a split hands out ids");
3735 assert_eq!(p.lane(Lane::A1)[1].link, Some(link), "both halves grouped");3910 assert_eq!(p.lane(Lane::A1)[1].link, Some(link), "both halves grouped");
3736- assert!(p.move_to_lane(Lane::V1, 1, v2));3911+ assert!(p.move_clip(Lane::V1, 1, v2, 3));
3737 assert_eq!(p.lane(v2)[0].link, Some(link), "the id travelled with it");3912 assert_eq!(p.lane(v2)[0].link, Some(link), "the id travelled with it");
3738 links_are_consistent(&p.lanes).expect("one id per lane, one span");3913 links_are_consistent(&p.lanes).expect("one id per lane, one span");
3739 assert_eq!(3914 assert_eq!(
@@ -4878,7 +5053,12 @@ mod tests {
4878 }5053 }
4879 let lane = if next(2) == 0 { Lane::V1 } else { Lane::A1 };5054 let lane = if next(2) == 0 { Lane::V1 } else { Lane::A1 };
4880 let idx = next(4) as usize;5055 let idx = next(4) as usize;
4881- let _ = match next(15) {5056+ let _ = match next(16) {
5057+ // A clip dragged along its own lane to wherever the pointer
5058+ // let it go: the op that moves a placement without changing
5059+ // what it plays, so what it may not do is land on top of the
5060+ // neighbour it was dragged at.
5061+ 15 => p.move_clip(lane, idx, lane, frame),
4882 // ...and a second `place`, so a punch lands inside a clip5062 // ...and a second `place`, so a punch lands inside a clip
4883 // far more often than one op in fourteen would put it there.5063 // far more often than one op in fourteen would put it there.
4884 14 => p.place(lane, frame, copied),5064 14 => p.place(lane, frame, copied),
@@ -5298,7 +5478,12 @@ mod tests {
5298 .unwrap_or(&clip(0, 0, 5, 0));5478 .unwrap_or(&clip(0, 0, 5, 0));
5299 let lane = lanes[next(3) as usize];5479 let lane = lanes[next(3) as usize];
5300 let idx = next(4) as usize;5480 let idx = next(4) as usize;
5301- let _ = match next(13) {5481+ let _ = match next(14) {
5482+ // The drag, in the mix: a clip dragged along its own lane
5483+ // and onto another one, at a frame the pointer picked --
5484+ // which is where the group rule and the sorted+disjoint one
5485+ // meet, since every half travels the same distance.
5486+ 13 => p.move_clip(lane, idx, lanes[next(3) as usize], frame),
5302 10 => p.set_eq(lane, idx, Some(band_at(next(4)))),5487 10 => p.set_eq(lane, idx, Some(band_at(next(4)))),
5303 11 => p.set_eq(lane, idx, None),5488 11 => p.set_eq(lane, idx, None),
5304 // A rate, which on many lanes is the group rule as well:5489 // A rate, which on many lanes is the group rule as well: