From 1f68773215df7392060b8a448b779b6f18b4a4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 27 Aug 2026 06:44:38 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(app):=20drag=20drop=20commit?= =?UTF-8?q?s=20the=20frame=20the=20ghost=20last=20showed,=20snaps=20to=200?= =?UTF-8?q?=20within=2010px?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit One x source for a drag: on_drag_move now stashes the pointer x that drew the ghost (Player::drag_x), and on_drop reads it back instead of a fresh window.mouse_position() -- gpui's drop callback carries no position of its own, so a second poll of the pointer was never guaranteed to answer what the ghost just showed. A wider DROP_SNAP_PX (10px, vs the 5px trim-nudge SNAP_PX) gives a release room to land on a mark; frame 0 was already always in snap_marks, even on an empty timeline, so the "must be pixel-exact to land at 0.0" complaint was tolerance, not a missing mark. The lane row's frame-0 seam moved from a layout border (which eats a pixel out of its own content box) to an absolute overlay, so the ghost, clips, and Player::frame_under all agree on one origin. Adds drop_landing_tests covering pull-to-0 on an empty timeline, pull onto a clip edge, and pull onto the playhead. --- crates/app/src/interact.rs | 8 ++++++ crates/app/src/main.rs | 8 ++++++ crates/app/src/player/timeline_edit.rs | 14 +++++++-- crates/app/src/timeline_math.rs | 39 ++++++++++++++++++++++++++ crates/app/src/ui/bench_stance.rs | 35 +++++++++++++++++++---- crates/app/src/viewport.rs | 12 ++++++++ 6 files changed, 107 insertions(+), 9 deletions(-) diff --git a/crates/app/src/interact.rs b/crates/app/src/interact.rs index 5c5b7f6..32fa24a 100644 --- a/crates/app/src/interact.rs +++ b/crates/app/src/interact.rs @@ -272,6 +272,14 @@ pub(crate) fn swallow(_: &MouseDownEvent, _: &mut Window, cx: &mut App) { /// takes must still be able to leave a gap of a few frames there. pub(crate) const SNAP_PX: f64 = 5.; +/// The same magnet, wider, for a drag's own *release* -- a library row or a +/// clip let go of, rather than an edge nudged one frame at a time. The user's +/// own complaint ("to drop at 0.0 you must be exactly at 0.0") is a 5px +/// window read at zoom: past a couple of minutes on screen a pixel is worth +/// several frames, and 5px of aim is not forgiving enough to feel like a +/// magnet at all. Twice [`SNAP_PX`], [`Scale::drop_snap_frames`]. +pub(crate) const DROP_SNAP_PX: f64 = 10.; + /// The scrollbar's thumb never narrows past this, however long the timeline: /// a thumb a pixel wide is a thumb no hand can hold. A quarter of the ruler's /// own seek strip, which is the idiom it sits under. diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index 6d2da0f..c96667e 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -393,6 +393,13 @@ struct Player { /// the clip under the hand: every other pick draws its own shadow too, at /// the same delta the anchor's is landing at ([`Player::preview_ghost`]). ghost: Vec, + /// The window `x` the last live `on_drag_move` sample over a bed used to + /// draw [`Player::ghost`] -- read back by that drag's `on_drop` instead of + /// a fresh `window.mouse_position()`. gpui's drop callback carries no + /// event of its own to read a position off, and a second, later poll of + /// the pointer is not guaranteed to answer what the ghost just showed + /// (DESIGN: "drop == shadow, by construction"). `None` between drags. + drag_x: Option, /// The slot a track header being dragged is about to drop into, or `None` /// while the pointer is over no lane: the line drawn between two headers, /// for the reason [`Player::ghost`] draws a shadow -- where a gesture lands @@ -945,6 +952,7 @@ fn main() { sub_lane: None, snap_cue: None, ghost: Vec::new(), + drag_x: None, lane_drop: None, last_scrub: Instant::now(), last_target: 0, diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index 308d666..aaae627 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -753,7 +753,7 @@ impl Player { self.grab, sub.frames, self.snap, - self.snap_frames(), + self.drop_snap_frames(), &marks, ) } @@ -826,7 +826,7 @@ impl Player { self.grab, clip.frames(), self.snap, - self.snap_frames(), + self.drop_snap_frames(), &marks, )) } @@ -843,7 +843,7 @@ impl Player { 0, 0, self.snap, - self.snap_frames(), + self.drop_snap_frames(), &marks, ) } @@ -1153,6 +1153,14 @@ impl Player { self.scale.snap_frames(self.fps) } + /// [`Player::snap_frames`]'s twin for a drag's own release + /// ([`DROP_SNAP_PX`]): every `place_frame`/`drop_frame`/`sub_drop_frame` + /// asks this instead, so a dropped clip is aimed with the wider magnet + /// while an edge trim keeps the finer one. + pub(crate) fn drop_snap_frames(&self) -> u32 { + self.scale.drop_snap_frames(self.fps) + } + /// Opens the clip menu on the box under the pointer, from the right button /// wherever it was pressed on that box -- its middle or one of its edge /// strips, which cover the middle's own listener. Selecting first is part of diff --git a/crates/app/src/timeline_math.rs b/crates/app/src/timeline_math.rs index 612dadb..7091c2b 100644 --- a/crates/app/src/timeline_math.rs +++ b/crates/app/src/timeline_math.rs @@ -686,3 +686,42 @@ pub(crate) fn frac_down(y: Pixels, bounds: Bounds) -> f32 { } ((y - bounds.top()) / bounds.size.height).clamp(0., 1.) } + +#[cfg(test)] +mod drop_landing_tests { + use super::*; + + /// A release a few pixels' worth of frames right of an empty timeline's + /// head still lands on frame 0: [`snap_marks`] always offers 0 + /// (`marks.push(0)`, above) even with no clips on the timeline yet, so + /// the "you must be exactly at 0.0" complaint is the tolerance, not a + /// missing mark. + #[test] + fn a_pull_near_the_head_of_an_empty_timeline_snaps_to_frame_0() { + let marks = snap_marks(&[&[]], None, None, 0); + assert!(marks.contains(&0), "an empty timeline still offers frame 0"); + assert_eq!(snapped(4, 0, 10, &marks), 0); + // Outside the tolerance, the raw ask stands: this is a magnet, not a + // clamp to 0. + assert_eq!(snapped(11, 0, 10, &marks), 11); + } + + /// A clip already on the lane offers both of its own edges as marks + /// ([`snap_marks`]'s `flat_map(|clip| [clip.start, clip.end()])`), and a + /// release close to either -- within the drop tolerance -- lands on it + /// rather than the raw pixel. + #[test] + fn a_pull_near_a_clip_edge_snaps_onto_it() { + let marks = [0, 100, 200]; + assert_eq!(snapped(205, 0, 10, &marks), 200); + } + + /// The playhead is a mark too, so a clip let go near it lines up with + /// where the timeline is parked. + #[test] + fn a_pull_near_the_playhead_snaps_onto_it() { + let marks = snap_marks(&[&[]], None, None, 500); + assert!(marks.contains(&500)); + assert_eq!(snapped(493, 0, 10, &marks), 500); + } +} diff --git a/crates/app/src/ui/bench_stance.rs b/crates/app/src/ui/bench_stance.rs index 6d90124..aae494e 100644 --- a/crates/app/src/ui/bench_stance.rs +++ b/crates/app/src/ui/bench_stance.rs @@ -1029,11 +1029,27 @@ fn lane_row( .min_w(px(0.)) .h_full() .bg(rgb(DARK_CANVAS())) - .border_l_1() - .border_color(rgba(DARK_SEAM())) + // The seam is drawn as an absolute overlay rather than a + // layout `border_l_1`: a border eats a pixel out of the + // content box it decorates, so frame 0 -- drawn by every + // ghost and clip at `left(px(0.))` relative to *this* box -- + // would sit one pixel right of what `Player::frame_under` + // computes through the ruler's own (unbordered) bounds + // (bench-ruler, below). One shared origin, so a drop lands + // exactly on the frame its ghost showed. + .child( + div() + .absolute() + .left_0() + .top_0() + .w(px(1.)) + .h_full() + .bg(rgba(DARK_SEAM())), + ) .drag_over::(|d, _, _, _| d.bg(rgb(DARK_RAISED()))) .on_drop(cx.listener(move |this, drag: &AssetDrag, window, cx| { - let at = this.place_frame(window.mouse_position().x).0; + let x = this.drag_x.take().unwrap_or_else(|| window.mouse_position().x); + let at = this.place_frame(x).0; this.insert_source(&drag.0.clone(), drag.1, Some(lane), Some(at), cx) })) // Right-click on empty bed space: the "Close Gap" door. A @@ -1065,6 +1081,7 @@ fn lane_row( return; } let path = event.drag(cx).0.clone(); + this.drag_x = Some(event.event.position.x); this.preview_ghost_asset(&path, lane, event.event.position.x, cx); }), ) @@ -1073,7 +1090,8 @@ fn lane_row( let Some(idx) = this.dragged(drag) else { return; }; - this.move_clip(drag.lane, idx, lane, window.mouse_position().x, cx) + let x = this.drag_x.take().unwrap_or_else(|| window.mouse_position().x); + this.move_clip(drag.lane, idx, lane, x, cx) })) .on_drag_move( cx.listener(move |this, event: &DragMoveEvent, _, cx| { @@ -1081,6 +1099,7 @@ fn lane_row( return; } let drag = *event.drag(cx); + this.drag_x = Some(event.event.position.x); this.preview_ghost(&drag, lane, event.event.position.x, cx); }), ) @@ -1089,7 +1108,8 @@ fn lane_row( // the same call `ui/timeline.rs`'s own bed makes). .drag_over::(|d, _, _, _| d.bg(rgb(DARK_RAISED()))) .on_drop(cx.listener(move |this, drag: &SubDrag, window, cx| { - this.move_sub(drag, lane, window.mouse_position().x, cx); + let x = this.drag_x.take().unwrap_or_else(|| window.mouse_position().x); + this.move_sub(drag, lane, x, cx); })) .on_drag_move( cx.listener(move |this, event: &DragMoveEvent, _, cx| { @@ -1097,12 +1117,14 @@ fn lane_row( return; } let drag = *event.drag(cx); + this.drag_x = Some(event.event.position.x); this.preview_ghost_sub(&drag, lane, event.event.position.x, cx); }), ) .drag_over::(|d, _, _, _| d.bg(rgb(DARK_RAISED()))) .on_drop(cx.listener(move |this, drag: &SubPick, window, cx| { - this.place_sub(drag.0, lane, window.mouse_position().x, cx); + let x = this.drag_x.take().unwrap_or_else(|| window.mouse_position().x); + this.place_sub(drag.0, lane, x, cx); })) .on_drag_move( cx.listener(move |this, event: &DragMoveEvent, _, cx| { @@ -1110,6 +1132,7 @@ fn lane_row( return; } let track = event.drag(cx).0; + this.drag_x = Some(event.event.position.x); this.preview_ghost_pick(track, lane, event.event.position.x, cx); }), ) diff --git a/crates/app/src/viewport.rs b/crates/app/src/viewport.rs index db7b5e9..6531f49 100644 --- a/crates/app/src/viewport.rs +++ b/crates/app/src/viewport.rs @@ -79,6 +79,18 @@ impl Scale { 0 } } + + /// [`DROP_SNAP_PX`] in timeline frames, [`Scale::snap_frames`]'s twin for a + /// drag's *release* rather than a trim: a dropped clip is aimed with a + /// coarser hand than an edge nudged one frame at a time, and a pixel-exact + /// release is what the user asked never to need. + pub(crate) fn drop_snap_frames(self, fps: f64) -> u32 { + if self.pps > 0. { + (DROP_SNAP_PX / self.pps * fps) as u32 + } else { + 0 + } + } } /// A [`Scale`] against the bed it is drawn on and the timeline it is drawn