forge
Explore
Log in

Don't you dare go Hollow

🔀 merge(T7): clips drag anywhere, media drops where the mouse lands

Browse files at this commit

e9589fba3106522e0039269d6580329a378ad2cdAhmet Kaan GümüşParent: 972805336c8b93
crates/app/src/main.rsmodified+264 -53
@@ -251,8 +251,9 @@ struct AssetDrag(PathBuf, usize);
251 251
252 /// A clip already on the timeline being dragged: the lane it is on and its index252 /// A clip already on the timeline being dragged: the lane it is on and its index
253 /// there, which is how every other edit names a clip. Unlike an [`AssetDrag`]253 /// there, which is how every other edit names a clip. Unlike an [`AssetDrag`]
254-/// nothing is inserted -- the same clip changes lane and keeps its frames -- so254+/// nothing is inserted -- the same clip changes lane and keeps the frames it
255-/// where along the bed it is let go says nothing here either.255+/// plays -- but where along the bed it is let go is exactly where it lands, less
256+/// the offset the hand grabbed it at ([`Player::grab`]).
256 #[derive(Clone, Copy)]257 #[derive(Clone, Copy)]
257 struct ClipDrag {258 struct ClipDrag {
258 lane: Lane,259 lane: Lane,
@@ -284,6 +285,12 @@ struct Trim {
284 /// still the body.285 /// still the body.
285 const EDGE_W: f32 = 6.;286 const EDGE_W: f32 = 6.;
286 287
288+/// How close to an edge a dragged clip has to be let go for it to land *on* it:
289+/// the snap every timeline has, in pixels rather than frames so that it feels
290+/// the same at every zoom. Narrower than [`EDGE_W`] -- a hand aiming between two
291+/// takes must still be able to leave a gap of a few frames there.
292+const SNAP_PX: f64 = 5.;
293+
287 /// An open clip menu: which clip it was opened on, where it hangs, and whether294 /// An open clip menu: which clip it was opened on, where it hangs, and whether
288 /// it has been turned over to show what that clip *is* instead of what can be295 /// it has been turned over to show what that clip *is* instead of what can be
289 /// done to it. The lane and index are the ones the same click selected, so296 /// done to it. The lane and index are the ones the same click selected, so
@@ -661,6 +668,13 @@ struct Player {
661 /// `scrubbing`'s reason: a 6 px strip is not where the pointer stays. See668 /// `scrubbing`'s reason: a 6 px strip is not where the pointer stays. See
662 /// [`Trim`].669 /// [`Trim`].
663 trim: Option<Trim>,670 trim: Option<Trim>,
671+ /// How far into the clip the last press on a box landed, in timeline
672+ /// frames: what a drag lets go of is the *point that was grabbed*, so the
673+ /// head lands that much in front of the pointer and the clip does not jump
674+ /// under the hand. Recorded at the press because gpui hands the drop only
675+ /// the value being dragged, and stale between drags, which costs nothing --
676+ /// no drag starts without a press on the box it moves.
677+ grab: u32,
664 last_scrub: Instant,678 last_scrub: Instant,
665 last_target: u32,679 last_target: u32,
666 /// The running export. While it owns the UI the editor is read-only.680 /// The running export. While it owns the UI the editor is read-only.
@@ -2131,19 +2145,29 @@ impl Player {
2131 cx.notify();2145 cx.notify();
2132 }2146 }
2133 2147
2134- /// A clip dragged off one lane and let go over another: it changes track and2148+ /// A clip let go at window `x` over lane `to`: it lands with its head where
2135- /// keeps the frames it covers, as one undo step. Dropped back on its own2149+ /// the hand is carrying it ([`Player::drop_frame`]), on the track it was
2136- /// lane it is not an edit at all, so nothing is said about it. The engine2150+ /// dropped on, taking its whole take with it -- one undo step for the
2137- /// reseeks, so all this owes is the flag reset -- and the selection, whose2151+ /// gesture. Dropped back where it was picked up it is not an edit at all, so
2138- /// index was that lane's own and now names a different clip there.2152+ /// nothing is said about it. The engine reseeks, so all this owes is the
2139- fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, cx: &mut Context<Self>) {2153+ /// flag reset -- and the selection, whose index was that lane's own and now
2140- if self.exporting().is_some() || from == to {2154+ /// names a different clip there.
2155+ fn move_clip(&mut self, from: Lane, idx: usize, to: Lane, x: Pixels, cx: &mut Context<Self>) {
2156+ if self.exporting().is_some() {
2141 return;2157 return;
2142 }2158 }
2159+ let (Some(start), Some(was)) = (
2160+ self.drop_frame(from, idx, to, x),
2161+ self.session
2162+ .as_ref()
2163+ .and_then(|session| session.lane_clips(from).get(idx).map(|c| c.start)),
2164+ ) else {
2165+ return;
2166+ };
2143 let moved = self2167 let moved = self
2144 .session2168 .session
2145 .as_mut()2169 .as_mut()
2146- .is_some_and(|session| session.move_clip_to_lane(from, idx, to));2170+ .is_some_and(|session| session.move_clip_to(from, idx, to, start));
2147 let (kind, lanes) = match from.kind {2171 let (kind, lanes) = match from.kind {
2148 LaneKind::Video => ("picture", "video"),2172 LaneKind::Video => ("picture", "video"),
2149 LaneKind::Audio => ("sound", "audio"),2173 LaneKind::Audio => ("sound", "audio"),
@@ -2153,9 +2177,10 @@ impl Player {
2153 self.selected = None;2177 self.selected = None;
2154 self.reset_after_reseek();2178 self.reset_after_reseek();
2155 }2179 }
2156- // The two ways a drag is refused, told apart by the one thing the2180+ // The three ways a drag is refused, told apart by what the
2157- // front-end already knows: a lane's kind. Everything else that2181+ // front-end already knows: a lane's kind, and where the clip was.
2158- // could refuse (a clip that is not there) cannot be dragged.2182+ // Everything else that could refuse (a clip that is not there)
2183+ // cannot be dragged.
2159 false if from.kind != to.kind => {2184 false if from.kind != to.kind => {
2160 self.notice = Some(2185 self.notice = Some(
2161 format!(2186 format!(
@@ -2165,6 +2190,9 @@ impl Player {
2165 .into(),2190 .into(),
2166 )2191 )
2167 }2192 }
2193+ // Picked up and put back down where it was: a click, and a click
2194+ // says nothing.
2195+ false if from == to && start == was => {}
2168 false => {2196 false => {
2169 self.notice = Some(2197 self.notice = Some(
2170 format!(2198 format!(
@@ -2178,6 +2206,55 @@ impl Player {
2178 cx.notify();2206 cx.notify();
2179 }2207 }
2180 2208
2209+ /// Where a clip let go at window `x` over lane `to` wants its head: the
2210+ /// frame under the pointer, less however far into the box the hand grabbed
2211+ /// it (so the clip does not jump under the pointer), pulled onto a
2212+ /// neighbouring edge when it lands within [`SNAP_PX`] of one. `None` when
2213+ /// there is no such clip to move. The engine has the last word on where it
2214+ /// may actually go -- this is the ask, not the answer.
2215+ ///
2216+ /// ponytail: the bed ends where the timeline does, so a clip cannot be
2217+ /// dragged *past* the last frame -- the pointer has no pixel out there to
2218+ /// name and [`View::time_at`] clamps to the duration. Room to the right is
2219+ /// made by pulling a tail out first ([`Player::drawn_duration`] already
2220+ /// lengthens the bed for exactly that gesture). The upgrade is to draw the
2221+ /// bed past the end while a clip drag is in flight, the way a trim already
2222+ /// does: `drawn_duration` would take the dragged clip's length the same way
2223+ /// it takes a trim's reach.
2224+ fn drop_frame(&self, from: Lane, idx: usize, to: Lane, x: Pixels) -> Option<u32> {
2225+ let session = self.session.as_ref()?;
2226+ let clip = session.lane_clips(from).get(idx).copied()?;
2227+ let raw = self.frame_under(x).saturating_sub(self.grab);
2228+ // The edges worth landing on: both ends of every clip already on the
2229+ // lane it is being let go over, the playhead, and the head of the
2230+ // timeline. Never the group's own boxes -- a clip does not snap to
2231+ // where it already is.
2232+ let mut marks: Vec<u32> = session
2233+ .lane_clips(to)
2234+ .iter()
2235+ .enumerate()
2236+ .filter(|&(i, c)| {
2237+ (to, i) != (from, idx) && !(clip.link.is_some() && c.link == clip.link)
2238+ })
2239+ .flat_map(|(_, c)| [c.start, c.end()])
2240+ .collect();
2241+ marks.push(frame_at(session.now(), self.fps));
2242+ marks.push(0);
2243+ Some(snapped(raw, clip.frames(), self.snap_frames(), &marks))
2244+ }
2245+
2246+ /// [`SNAP_PX`] in timeline frames at the zoom the bed is drawn at: a snap is
2247+ /// a distance on screen, so zoomed right in it is worth less than a frame
2248+ /// (no snap at all, which is what a hand placing single frames wants) and
2249+ /// zoomed out to the whole timeline it is worth many.
2250+ fn snap_frames(&self) -> u32 {
2251+ let bed = f64::from(f32::from(self.ruler.get().size.width));
2252+ if bed <= 0. {
2253+ return 0;
2254+ }
2255+ (SNAP_PX / bed * self.view.span(self.drawn_duration()) * self.fps) as u32
2256+ }
2257+
2181 /// Opens the clip menu on the box under the pointer, from the right button2258 /// Opens the clip menu on the box under the pointer, from the right button
2182 /// wherever it was pressed on that box -- its middle or one of its edge2259 /// wherever it was pressed on that box -- its middle or one of its edge
2183 /// strips, which cover the middle's own listener. Selecting first is part of2260 /// strips, which cover the middle's own listener. Selecting first is part of
@@ -2232,20 +2309,30 @@ impl Player {
2232 /// against the same duration the boxes are drawn to, so the edge tracks the2309 /// against the same duration the boxes are drawn to, so the edge tracks the
2233 /// pointer exactly.2310 /// pointer exactly.
2234 fn trim_to(&mut self, x: Pixels, cx: &mut Context<Self>) {2311 fn trim_to(&mut self, x: Pixels, cx: &mut Context<Self>) {
2235- let (duration, fps) = (self.drawn_duration(), self.fps);2312+ let at = self.frame_under(x);
2236- let at = self
2237- .view
2238- .time_at(frac_along(x, self.ruler.get()), duration);
2239 let (Some(trim), Some(session)) = (&mut self.trim, &self.session) else {2313 let (Some(trim), Some(session)) = (&mut self.trim, &self.session) else {
2240 return;2314 return;
2241 };2315 };
2242 let Some((lo, hi)) = session.trim_room(trim.lane, trim.idx, trim.edge) else {2316 let Some((lo, hi)) = session.trim_room(trim.lane, trim.idx, trim.edge) else {
2243 return;2317 return;
2244 };2318 };
2245- trim.to = frame_at(at, fps).clamp(lo, hi);2319+ trim.to = at.clamp(lo, hi);
2246 cx.notify();2320 cx.notify();
2247 }2321 }
2248 2322
2323+ /// The timeline frame a pointer at window x is on: along the same bed the
2324+ /// ruler is measured on, through the same [`View`] every box is drawn
2325+ /// through, so a zoomed-in panel answers with the frame under the pointer
2326+ /// and not with the one that would have been there unzoomed. The one
2327+ /// question a trim, a grab and a drop all ask.
2328+ fn frame_under(&self, x: Pixels) -> u32 {
2329+ let duration = self.drawn_duration();
2330+ frame_at(
2331+ self.view.time_at(frac_along(x, self.ruler.get()), duration),
2332+ self.fps,
2333+ )
2334+ }
2335+
2249 /// The release: the whole drag reaches the engine as one edit, so it is one2336 /// The release: the whole drag reaches the engine as one edit, so it is one
2250 /// undo step. The selection survives it -- a trim inserts and removes2337 /// undo step. The selection survives it -- a trim inserts and removes
2251 /// nothing, so every index a lane had still names the clip it named.2338 /// nothing, so every index a lane had still names the clip it named.
@@ -2345,16 +2432,18 @@ impl Player {
2345 2432
2346 /// The one way a library row reaches the timeline: the Add button and a row2433 /// The one way a library row reaches the timeline: the Add button and a row
2347 /// dragged onto a lane both come here, so there is a single answer to what2434 /// dragged onto a lane both come here, so there is a single answer to what
2348- /// "add this source" does. The whole source goes in at the playhead as one2435+ /// "add this source" does. The whole source goes in as one grouped take at
2349- /// grouped take -- the same insert a paste makes, so everything after it2436+ /// `at` -- the frame the pointer let it go on, or the playhead for the
2350- /// moves along rather than being painted over. Reseeks like every other2437+ /// button, which names no place. It is the same insert a paste makes, so
2351- /// edit, and drops the timeline's selection with it: the insert has just2438+ /// everything after it moves along rather than being painted over. Reseeks
2352- /// moved the indices it pointed at.2439+ /// like every other edit, and drops the timeline's selection with it: the
2440+ /// insert has just moved the indices it pointed at.
2353 fn insert_source(2441 fn insert_source(
2354 &mut self,2442 &mut self,
2355 path: &Path,2443 path: &Path,
2356 stream: usize,2444 stream: usize,
2357 onto: Option<Lane>,2445 onto: Option<Lane>,
2446+ at: Option<u32>,
2358 cx: &mut Context<Self>,2447 cx: &mut Context<Self>,
2359 ) {2448 ) {
2360 if self.exporting().is_some() {2449 if self.exporting().is_some() {
@@ -2389,8 +2478,16 @@ impl Player {
2389 // in: a row that has never been on a lane is placeable at its full2478 // in: a row that has never been on a lane is placeable at its full
2390 // length, which is the whole point of an import that only fills the2479 // length, which is the whole point of an import that only fills the
2391 // library.2480 // library.
2481+ let fps = self.fps;
2392 let placed = match &mut self.session {2482 let placed = match &mut self.session {
2393- Some(session) => session.place_stream_at(session.now(), path, stream, onto),2483+ // Seconds, because that is what the engine's own door takes: the
2484+ // frame the pointer named goes back through the same rate every box
2485+ // on the bed is drawn at, so it lands on the frame it was let go on
2486+ // rather than a neighbouring one.
2487+ Some(session) => {
2488+ let at = at.map_or_else(|| session.now(), |frame| f64::from(frame) / fps);
2489+ session.place_stream_at(at, path, stream, onto)
2490+ }
2394 None => Ok(false),2491 None => Ok(false),
2395 };2492 };
2396 match placed {2493 match placed {
@@ -2540,7 +2637,7 @@ impl Player {
2540 }2637 }
2541 RowItem::Add => {2638 RowItem::Add => {
2542 self.library_menu = None;2639 self.library_menu = None;
2543- self.insert_source(&menu.path, menu.stream, None, cx);2640+ self.insert_source(&menu.path, menu.stream, None, None, cx);
2544 }2641 }
2545 RowItem::Remove => {2642 RowItem::Remove => {
2546 self.library_menu = None;2643 self.library_menu = None;
@@ -3673,7 +3770,7 @@ impl Player {
3673 // row that says where it came from has to say which rate that3770 // row that says where it came from has to say which rate that
3674 // was.3771 // was.
3675 (None, Some(meta)) => format!(3772 (None, Some(meta)) => format!(
3676- "{} — {}x{} @ {:.2} fps · drag onto a lane, or Add at playhead",3773+ "{} — {}x{} @ {:.2} fps · drag it where you want it, or Add at playhead",
3677 row.path.display(),3774 row.path.display(),
3678 meta.width,3775 meta.width,
3679 meta.height,3776 meta.height,
@@ -3857,7 +3954,7 @@ impl Player {
3857 "Add at playhead",3954 "Add at playhead",
3858 match self.selected_asset {3955 match self.selected_asset {
3859 Some(_) => "inserts the picked file at the playhead".to_string(),3956 Some(_) => "inserts the picked file at the playhead".to_string(),
3860- None => "click a file above first — or drag one onto a lane".to_string(),3957+ None => "click a file above first — or drag one where you want it".to_string(),
3861 },3958 },
3862 can_add(3959 can_add(
3863 self.selected_asset.as_ref(),3960 self.selected_asset.as_ref(),
@@ -3868,7 +3965,7 @@ impl Player {
3868 if let Some((path, stream)) = this.selected_asset.clone() {3965 if let Some((path, stream)) = this.selected_asset.clone() {
3869 // No lane: the button means "wherever this belongs",3966 // No lane: the button means "wherever this belongs",
3870 // which for a file with no picture is the audio lane.3967 // which for a file with no picture is the audio lane.
3871- this.insert_source(&path, stream, None, cx);3968+ this.insert_source(&path, stream, None, None, cx);
3872 }3969 }
3873 }),3970 }),
3874 ))3971 ))
@@ -5941,7 +6038,7 @@ impl Player {
5941 let (sel, sel_link) = (self.selected, self.selected_link());6038 let (sel, sel_link) = (self.selected, self.selected_link());
5942 let audio = lane.kind == LaneKind::Audio;6039 let audio = lane.kind == LaneKind::Audio;
5943 let tip: SharedString = format!(6040 let tip: SharedString = format!(
5944- "Select (or {} under the playhead, {}/{} along the lane) — drag an end to trim, {} removes the take, {} leaves a gap, {} rejoins a cut",6041+ "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",
5945 self.keymap.display(ActionId::Select),6042 self.keymap.display(ActionId::Select),
5946 self.keymap.display(ActionId::SelectPrev),6043 self.keymap.display(ActionId::SelectPrev),
5947 self.keymap.display(ActionId::SelectNext),6044 self.keymap.display(ActionId::SelectNext),
@@ -6008,18 +6105,23 @@ impl Player {
6008 .bg(rgb(LETTERBOX))6105 .bg(rgb(LETTERBOX))
6009 .overflow_hidden()6106 .overflow_hidden()
6010 // A library row let go over a lane is the same insert the6107 // A library row let go over a lane is the same insert the
6011- // Add button makes, through the same call -- at the6108+ // Add button makes, through the same call -- but where the
6012- // playhead, not at the pointer: clips here are placed end6109+ // pointer let it go, not at the playhead: a hand that
6013- // to end, so where along the bed it landed says nothing.6110+ // carried a file to a place on the bed named that place.
6014- .on_drop(cx.listener(move |this, drag: &AssetDrag, _, cx| {6111+ // gpui hands a drop no event, so the pointer is read off
6015- this.insert_source(&drag.0.clone(), drag.1, Some(lane), cx)6112+ // the window, which took it from the release that fired
6113+ // this (gpui window.rs:3602).
6114+ .on_drop(cx.listener(move |this, drag: &AssetDrag, window, cx| {
6115+ let at = this.frame_under(window.mouse_position().x);
6116+ this.insert_source(&drag.0.clone(), drag.1, Some(lane), Some(at), cx)
6016 }))6117 }))
6017 .drag_over::<AssetDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM)))6118 .drag_over::<AssetDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM)))
6018- // A clip let go over another lane changes track and keeps6119+ // ...and a clip let go over a lane lands the same way: on
6019- // the frames it covers -- the pointer's position along the6120+ // the track it was dropped on, at the frame it was carried
6020- // bed says nothing here either, for the reason above.6121+ // to -- its own included, which is the drag that moves a
6021- .on_drop(cx.listener(move |this, drag: &ClipDrag, _, cx| {6122+ // take along its track.
6022- this.move_clip(drag.lane, drag.idx, lane, cx)6123+ .on_drop(cx.listener(move |this, drag: &ClipDrag, window, cx| {
6124+ this.move_clip(drag.lane, drag.idx, lane, window.mouse_position().x, cx)
6023 }))6125 }))
6024 .drag_over::<ClipDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM)))6126 .drag_over::<ClipDrag>(|s, _, _, _| s.bg(rgb(HOVER_DIM)))
6025 .children(clips.iter().enumerate().map(|(i, clip)| {6127 .children(clips.iter().enumerate().map(|(i, clip)| {
@@ -6064,6 +6166,10 @@ impl Player {
6064 // says what is moving.6166 // says what is moving.
6065 let ghost: SharedString =6167 let ghost: SharedString =
6066 label.clone().unwrap_or_else(|| lane.label()).into();6168 label.clone().unwrap_or_else(|| lane.label()).into();
6169+ // Its head in frames, for the press below: the `start`
6170+ // above is the same moment in seconds, which is what
6171+ // the box is *drawn* from.
6172+ let head = clip.start;
6067 div()6173 div()
6068 // Named per lane: two rows numbering their clips6174 // Named per lane: two rows numbering their clips
6069 // from zero would hand gpui the same id twice.6175 // from zero would hand gpui the same id twice.
@@ -6091,16 +6197,23 @@ impl Player {
6091 .cursor_pointer()6197 .cursor_pointer()
6092 .hover(|s| s.border_color(rgb(ACCENT)))6198 .hover(|s| s.border_color(rgb(ACCENT)))
6093 .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into())6199 .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into())
6094- // Dragged onto another lane it *moves* there; the6200+ // Dragged, it *moves*: to the frame it was let go on
6095- // click that starts the drag still selects, so6201+ // and to the lane it was let go over. The click that
6096- // picking a clip up and putting it back down where6202+ // starts the drag still selects, so picking a clip
6097- // it was is exactly a click.6203+ // up and putting it back down where it was is
6204+ // exactly a click.
6098 .on_drag(ClipDrag { lane, idx: i }, move |_, _, _, cx| {6205 .on_drag(ClipDrag { lane, idx: i }, move |_, _, _, cx| {
6099 cx.new(|_| Tip(ghost.clone()))6206 cx.new(|_| Tip(ghost.clone()))
6100 })6207 })
6101 .on_mouse_down(6208 .on_mouse_down(
6102 MouseButton::Left,6209 MouseButton::Left,
6103- cx.listener(move |this, _: &MouseDownEvent, _, cx| {6210+ cx.listener(move |this, event: &MouseDownEvent, _, cx| {
6211+ // Where in the box the hand took hold of it,
6212+ // for the drag this press may become: the
6213+ // clip has to move with the pointer rather
6214+ // than jump its head under it.
6215+ this.grab =
6216+ this.frame_under(event.position.x).saturating_sub(head);
6104 this.select((lane, i), cx);6217 this.select((lane, i), cx);
6105 }),6218 }),
6106 )6219 )
@@ -7682,6 +7795,28 @@ fn frac_along(x: Pixels, bounds: Bounds<Pixels>) -> f32 {
7682 ((x - bounds.left()) / bounds.size.width).clamp(0., 1.)7795 ((x - bounds.left()) / bounds.size.width).clamp(0., 1.)
7683 }7796 }
7684 7797
7798+/// The frame a dropped clip's head lands on: `raw`, unless one of `marks` is
7799+/// within `tol` frames of where its head -- or its tail, `len` frames along --
7800+/// would come to rest, in which case that edge wins. The snap every timeline
7801+/// has: clips meet exactly instead of a frame apart, and the hand does not have
7802+/// to aim. The nearest mark wins, and a head landing on one beats a tail at the
7803+/// same distance -- what was dragged is the head.
7804+fn snapped(raw: u32, len: u32, tol: u32, marks: &[u32]) -> u32 {
7805+ let mut best: Option<(u32, u32)> = None;
7806+ for &mark in marks {
7807+ // Head on the mark, then tail on it -- a clip dragged up against the
7808+ // take in front of it snaps by whichever end reaches first.
7809+ for start in [Some(mark), mark.checked_sub(len)] {
7810+ let Some(start) = start else { continue };
7811+ let d = start.abs_diff(raw);
7812+ if d <= tol && best.is_none_or(|(near, _)| d < near) {
7813+ best = Some((d, start));
7814+ }
7815+ }
7816+ }
7817+ best.map_or(raw, |(_, start)| start)
7818+}
7819+
7685 /// Where down an element a pointer sits, 0..1 from the top: the vertical twin7820 /// Where down an element a pointer sits, 0..1 from the top: the vertical twin
7686 /// of [`frac_along`], for the equalizer, whose gain axis is the y one. An7821 /// of [`frac_along`], for the equalizer, whose gain axis is the y one. An
7687 /// element that was never painted reads as its middle -- flat, the one answer7822 /// element that was never painted reads as its middle -- flat, the one answer
@@ -8019,8 +8154,8 @@ mod tests {
8019 eq_y, export_path, export_settings, format_key, format_line, format_refusal, frac_along,8154 eq_y, export_path, export_settings, format_key, format_line, format_refusal, frac_along,
8020 frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked,8155 frac_down, frame_at, histogram, is_bare_modifier, is_project, keymap, lanes_h, marked,
8021 menu_at, normalise, nothing_to_play, panel_h, project_path, push_digit, retarget,8156 menu_at, normalise, nothing_to_play, panel_h, project_path, push_digit, retarget,
8022- scrub_due, show_label, silence_rate, source_tint, span_partner, speed_at, timecode,8157+ scrub_due, show_label, silence_rate, snapped, source_tint, span_partner, speed_at,
8023- unseen_paths, unseen_sources, whole_take, width_frac, window_title,8158+ timecode, unseen_paths, unseen_sources, whole_take, width_frac, window_title,
8024 };8159 };
8025 use super::{8160 use super::{
8026 LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows,8161 LaneKind, Repeat, View, ZOOM_MIN_FRAMES, ZOOM_STEP, file_name, file_uri, library_rows,
@@ -8858,7 +8993,7 @@ mod tests {
8858 let v2 = session.add_lane(LaneKind::Video);8993 let v2 = session.add_lane(LaneKind::Video);
8859 assert_eq!(session.video_clip_at(0.0), Some((Lane::V1, 0)));8994 assert_eq!(session.video_clip_at(0.0), Some((Lane::V1, 0)));
8860 8995
8861- assert!(session.move_clip_to_lane(Lane::V1, 0, v2), "V1 -> V2");8996+ assert!(session.move_clip_to(Lane::V1, 0, v2, 0), "V1 -> V2");
8862 assert!(session.lane_clips(Lane::V1).is_empty(), "it left V1");8997 assert!(session.lane_clips(Lane::V1).is_empty(), "it left V1");
8863 assert_eq!(session.lane_clips(v2).len(), 1, "and landed on V2");8998 assert_eq!(session.lane_clips(v2).len(), 1, "and landed on V2");
8864 assert_eq!(8999 assert_eq!(
@@ -8871,11 +9006,11 @@ mod tests {
8871 // Dropped on a lane of the other kind it is refused and nothing moves --9006 // Dropped on a lane of the other kind it is refused and nothing moves --
8872 // the notice the front-end shows for it says which kind of lane to use.9007 // the notice the front-end shows for it says which kind of lane to use.
8873 // (The other refusal, landing on another clip, is the engine's own test9008 // (The other refusal, landing on another clip, is the engine's own test
8874- // `move_to_lane_keeps_the_frames_and_refuses_the_rest`.)9009+ // `move_clip_keeps_the_frames_and_refuses_the_rest`.)
8875- assert!(!session.move_clip_to_lane(v2, 0, Lane::A1), "picture on A1");9010+ assert!(!session.move_clip_to(v2, 0, Lane::A1, 0), "picture on A1");
8876 assert_eq!(session.lane_clips(v2).len(), 1, "and it stayed on V2");9011 assert_eq!(session.lane_clips(v2).len(), 1, "and it stayed on V2");
8877 assert!(9012 assert!(
8878- session.move_clip_to_lane(v2, 0, Lane::V1),9013+ session.move_clip_to(v2, 0, Lane::V1, 0),
8879 "dragged back down"9014 "dragged back down"
8880 );9015 );
8881 9016
@@ -9075,6 +9210,70 @@ mod tests {
9075 assert_eq!(frac_down(px(50.), Bounds::default()), 0.5);9210 assert_eq!(frac_down(px(50.), Bounds::default()), 0.5);
9076 }9211 }
9077 9212
9213+ /// What a drop reads: the frame under the pointer, through the same view
9214+ /// the boxes are drawn through. Zoomed in, the same pixel is a different
9215+ /// frame -- which is the whole reason `Player::frame_under` goes through
9216+ /// [`View`] rather than through the duration alone.
9217+ #[test]
9218+ fn a_drop_reads_the_frame_under_the_pointer_at_every_zoom() {
9219+ // A 200 px bed inset by the panel's padding, 10 seconds at 30 fps.
9220+ let bed: Bounds<Pixels> = Bounds {
9221+ origin: point(px(12.), px(400.)),
9222+ size: size(px(200.), px(6.)),
9223+ };
9224+ let (duration, fps) = (10., 30.);
9225+ // The frame a pointer at window `x` names, exactly as `frame_under`
9226+ // composes it.
9227+ let under =
9228+ |view: View, x: f32| frame_at(view.time_at(frac_along(px(x), bed), duration), fps);
9229+
9230+ // The whole timeline across the bed: halfway along is frame 150.
9231+ let fit = View::default();
9232+ assert_eq!(under(fit, 12.), 0);
9233+ assert_eq!(under(fit, 112.), 150);
9234+ assert_eq!(under(fit, 212.), 300);
9235+
9236+ // Four times in, starting at second 5: the same middle pixel is now the
9237+ // frame in the middle of seconds 5..7.5, and the left edge is not 0.
9238+ let zoomed = View {
9239+ zoom: 4.,
9240+ start: 5.,
9241+ };
9242+ assert_eq!(under(zoomed, 12.), 150);
9243+ assert_eq!(under(zoomed, 112.), 187);
9244+ assert_eq!(under(zoomed, 212.), 225);
9245+ // ...and a drop past either end of the bed lands on the timeline, never
9246+ // outside it: `time_at` clamps.
9247+ assert_eq!(under(zoomed, 0.), 150);
9248+ assert_eq!(under(fit, 9999.), 300);
9249+ }
9250+
9251+ /// The snap: a clip let go a few frames off a neighbour's edge lands *on*
9252+ /// it, by whichever of its own ends is nearer, and a clip let go in open bed
9253+ /// stays exactly where the hand left it.
9254+ #[test]
9255+ fn a_dropped_clip_snaps_to_the_edges_worth_landing_on() {
9256+ // A neighbour covering [100, 160) and the playhead at 300.
9257+ let marks = [100, 160, 300, 0];
9258+ // Head a frame short of the neighbour's tail: laid end to end with it.
9259+ assert_eq!(snapped(158, 40, 4, &marks), 160);
9260+ // Tail a frame into its head: pulled back so the two meet exactly.
9261+ assert_eq!(snapped(62, 40, 4, &marks), 60);
9262+ // The playhead is an edge like any other.
9263+ assert_eq!(snapped(298, 40, 4, &marks), 300);
9264+ // Outside the tolerance nothing moves -- a gap the hand meant to leave
9265+ // is a gap.
9266+ assert_eq!(snapped(150, 40, 4, &marks), 150);
9267+ // No tolerance at all (zoomed right in, where a few pixels are worth
9268+ // less than a frame) is no snap: single frames are placed by hand.
9269+ assert_eq!(snapped(158, 40, 0, &marks), 158);
9270+ // The nearer edge wins when two are in reach.
9271+ assert_eq!(snapped(101, 40, 8, &marks), 100);
9272+ // ...and a mark closer to the head than `len` cannot pull the clip to a
9273+ // negative start.
9274+ assert_eq!(snapped(2, 40, 4, &marks), 0);
9275+ }
9276+
9078 #[test]9277 #[test]
9079 fn timecode_counts_frames_inside_the_second() {9278 fn timecode_counts_frames_inside_the_second() {
9080 assert_eq!(timecode(0., 30.), "00:00:00:00");9279 assert_eq!(timecode(0., 30.), "00:00:00:00");
@@ -9219,7 +9418,11 @@ mod tests {
9219 // The fit: what `start_frac`/`width_frac` used to answer, exactly.9418 // The fit: what `start_frac`/`width_frac` used to answer, exactly.
9220 let fit = View::default();9419 let fit = View::default();
9221 for t in [0., 5., 12.5, 20.] {9420 for t in [0., 5., 12.5, 20.] {
9222- assert_eq!(fit.frac_at(t, duration), (t / duration) as f32, "fit at {t}");9421+ assert_eq!(
9422+ fit.frac_at(t, duration),
9423+ (t / duration) as f32,
9424+ "fit at {t}"
9425+ );
9223 }9426 }
9224 assert_eq!(fit.width_frac(5., duration), width_frac(5., duration));9427 assert_eq!(fit.width_frac(5., duration), width_frac(5., duration));
9225 for view in [9428 for view in [
@@ -9338,7 +9541,11 @@ mod tests {
9338 assert_eq!(view.span(duration), 5.);9541 assert_eq!(view.span(duration), 5.);
9339 // Inside: untouched, whichever part of the slice it is in.9542 // Inside: untouched, whichever part of the slice it is in.
9340 for at in [5., 7.5, 10.] {9543 for at in [5., 7.5, 10.] {
9341- assert_eq!(view.following(at, duration, fps), view, "{at} is on the bed");9544+ assert_eq!(
9545+ view.following(at, duration, fps),
9546+ view,
9547+ "{at} is on the bed"
9548+ );
9342 }9549 }
9343 // Past the right edge, as playback does it: the head comes back on the9550 // Past the right edge, as playback does it: the head comes back on the
9344 // bed, and the zoom is not changed by the scroll.9551 // bed, and the zoom is not changed by the scroll.
@@ -10344,7 +10551,10 @@ mod tests {
10344 while session.try_frame().is_some() {}10551 while session.try_frame().is_some() {}
10345 std::thread::sleep(Duration::from_millis(10));10552 std::thread::sleep(Duration::from_millis(10));
10346 }10553 }
10347- assert!(session.is_eos(), "an empty timeline is done before it starts");10554+ assert!(
10555+ session.is_eos(),
10556+ "an empty timeline is done before it starts"
10557+ );
10348 10558
10349 // So the press is refused rather than sent down the restart branch --10559 // So the press is refused rather than sent down the restart branch --
10350 // and with no session at all it is the same refusal.10560 // and with no session at all it is the same refusal.
@@ -10567,6 +10777,7 @@ fn main() {
10567 clipboard: None,10777 clipboard: None,
10568 scrubbing: false,10778 scrubbing: false,
10569 trim: None,10779 trim: None,
10780+ grab: 0,
10570 last_scrub: Instant::now(),10781 last_scrub: Instant::now(),
10571 last_target: 0,10782 last_target: 0,
10572 export: None,10783 export: None,
crates/engine/src/playback.rsmodified+14 -8
@@ -1103,14 +1103,20 @@ impl PlaybackSession {
1103 self.edit(|p| p.lift(lane, idx))1103 self.edit(|p| p.lift(lane, idx))
1104 }1104 }
1105 1105
1106- /// Moves that clip onto another lane of the same kind, keeping the frames it1106+ /// Moves that clip onto lane `to` with its head at timeline frame `start` --
1107- /// covers -- the drag between tracks. One undo step, and it reseeks like1107+ /// the drag that carries a take along its track or onto another one, and its
1108- /// every other edit: which lane a clip sits on is what the compositor's1108+ /// whole group with it. See [`Project::move_clip`] for the walls `start` is
1109- /// topmost-lane-wins rule reads, so the frame on screen is recomposed at1109+ /// clamped to and the ways a drop is refused. One undo step, and it reseeks
1110- /// once. `false` for a bad index, for a move across kinds and for one that1110+ /// like every other edit: which lane a clip sits on is what the compositor's
1111- /// would land on another clip; nothing changes.1111+ /// topmost-lane-wins rule reads, and where it starts is what the playhead
1112- pub fn move_clip_to_lane(&mut self, from: Lane, idx: usize, to: Lane) -> bool {1112+ /// reads, so the frame on screen is recomposed at once.
1113- self.edit(|p| p.move_to_lane(from, idx, to))1113+ ///
1114+ /// A *frame*, where the rest of this type takes seconds, for
1115+ /// [`trim_clip`](Self::trim_clip)'s reason: the drag has already put the
1116+ /// pointer on a frame, and converting it back through seconds would be a
1117+ /// rounding step between the box let go of and the box committed.
1118+ pub fn move_clip_to(&mut self, from: Lane, idx: usize, to: Lane, start: u32) -> bool {
1119+ self.edit(|p| p.move_clip(from, idx, to, start))
1114 }1120 }
1115 1121
1116 /// Drags one end of that clip to timeline frame `to`, changing how much of1122 /// 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: