forge
Explore
Log in

Don't you dare go Hollow

Drag drop commits the frame the ghost last showed, snaps to 0 within 10px

Browse files at this commit

1f68773215df7392060b8a448b779b6f18b4a4cbAhmet Kaan GümüşParent: cafb588
  • fix
    • drag drop commits the frame the ghost last showed, snaps to 0 within 10px
Show full message
🐛 fix(app): drag drop commits the frame the ghost last showed, snaps to 0 within 10px

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.rsmodified+8 -0
@@ -272,6 +272,14 @@ pub(crate) fn swallow(_: &MouseDownEvent, _: &mut Window, cx: &mut App) {
272272 /// takes must still be able to leave a gap of a few frames there.
273273 pub(crate) const SNAP_PX: f64 = 5.;
274274
275+/// The same magnet, wider, for a drag's own *release* -- a library row or a
276+/// clip let go of, rather than an edge nudged one frame at a time. The user's
277+/// own complaint ("to drop at 0.0 you must be exactly at 0.0") is a 5px
278+/// window read at zoom: past a couple of minutes on screen a pixel is worth
279+/// several frames, and 5px of aim is not forgiving enough to feel like a
280+/// magnet at all. Twice [`SNAP_PX`], [`Scale::drop_snap_frames`].
281+pub(crate) const DROP_SNAP_PX: f64 = 10.;
282+
275283 /// The scrollbar's thumb never narrows past this, however long the timeline:
276284 /// a thumb a pixel wide is a thumb no hand can hold. A quarter of the ruler's
277285 /// own seek strip, which is the idiom it sits under.
crates/app/src/main.rsmodified+8 -0
@@ -393,6 +393,13 @@ struct Player {
393393 /// the clip under the hand: every other pick draws its own shadow too, at
394394 /// the same delta the anchor's is landing at ([`Player::preview_ghost`]).
395395 ghost: Vec<Ghost>,
396+ /// The window `x` the last live `on_drag_move` sample over a bed used to
397+ /// draw [`Player::ghost`] -- read back by that drag's `on_drop` instead of
398+ /// a fresh `window.mouse_position()`. gpui's drop callback carries no
399+ /// event of its own to read a position off, and a second, later poll of
400+ /// the pointer is not guaranteed to answer what the ghost just showed
401+ /// (DESIGN: "drop == shadow, by construction"). `None` between drags.
402+ drag_x: Option<Pixels>,
396403 /// The slot a track header being dragged is about to drop into, or `None`
397404 /// while the pointer is over no lane: the line drawn between two headers,
398405 /// for the reason [`Player::ghost`] draws a shadow -- where a gesture lands
@@ -945,6 +952,7 @@ fn main() {
945952 sub_lane: None,
946953 snap_cue: None,
947954 ghost: Vec::new(),
955+ drag_x: None,
948956 lane_drop: None,
949957 last_scrub: Instant::now(),
950958 last_target: 0,
crates/app/src/player/timeline_edit.rsmodified+11 -3
@@ -753,7 +753,7 @@ impl Player {
753753 self.grab,
754754 sub.frames,
755755 self.snap,
756- self.snap_frames(),
756+ self.drop_snap_frames(),
757757 &marks,
758758 )
759759 }
@@ -826,7 +826,7 @@ impl Player {
826826 self.grab,
827827 clip.frames(),
828828 self.snap,
829- self.snap_frames(),
829+ self.drop_snap_frames(),
830830 &marks,
831831 ))
832832 }
@@ -843,7 +843,7 @@ impl Player {
843843 0,
844844 0,
845845 self.snap,
846- self.snap_frames(),
846+ self.drop_snap_frames(),
847847 &marks,
848848 )
849849 }
@@ -1153,6 +1153,14 @@ impl Player {
11531153 self.scale.snap_frames(self.fps)
11541154 }
11551155
1156+ /// [`Player::snap_frames`]'s twin for a drag's own release
1157+ /// ([`DROP_SNAP_PX`]): every `place_frame`/`drop_frame`/`sub_drop_frame`
1158+ /// asks this instead, so a dropped clip is aimed with the wider magnet
1159+ /// while an edge trim keeps the finer one.
1160+ pub(crate) fn drop_snap_frames(&self) -> u32 {
1161+ self.scale.drop_snap_frames(self.fps)
1162+ }
1163+
11561164 /// Opens the clip menu on the box under the pointer, from the right button
11571165 /// wherever it was pressed on that box -- its middle or one of its edge
11581166 /// strips, which cover the middle's own listener. Selecting first is part of
crates/app/src/timeline_math.rsmodified+39 -0
@@ -686,3 +686,42 @@ pub(crate) fn frac_down(y: Pixels, bounds: Bounds<Pixels>) -> f32 {
686686 }
687687 ((y - bounds.top()) / bounds.size.height).clamp(0., 1.)
688688 }
689+
690+#[cfg(test)]
691+mod drop_landing_tests {
692+ use super::*;
693+
694+ /// A release a few pixels' worth of frames right of an empty timeline's
695+ /// head still lands on frame 0: [`snap_marks`] always offers 0
696+ /// (`marks.push(0)`, above) even with no clips on the timeline yet, so
697+ /// the "you must be exactly at 0.0" complaint is the tolerance, not a
698+ /// missing mark.
699+ #[test]
700+ fn a_pull_near_the_head_of_an_empty_timeline_snaps_to_frame_0() {
701+ let marks = snap_marks(&[&[]], None, None, 0);
702+ assert!(marks.contains(&0), "an empty timeline still offers frame 0");
703+ assert_eq!(snapped(4, 0, 10, &marks), 0);
704+ // Outside the tolerance, the raw ask stands: this is a magnet, not a
705+ // clamp to 0.
706+ assert_eq!(snapped(11, 0, 10, &marks), 11);
707+ }
708+
709+ /// A clip already on the lane offers both of its own edges as marks
710+ /// ([`snap_marks`]'s `flat_map(|clip| [clip.start, clip.end()])`), and a
711+ /// release close to either -- within the drop tolerance -- lands on it
712+ /// rather than the raw pixel.
713+ #[test]
714+ fn a_pull_near_a_clip_edge_snaps_onto_it() {
715+ let marks = [0, 100, 200];
716+ assert_eq!(snapped(205, 0, 10, &marks), 200);
717+ }
718+
719+ /// The playhead is a mark too, so a clip let go near it lines up with
720+ /// where the timeline is parked.
721+ #[test]
722+ fn a_pull_near_the_playhead_snaps_onto_it() {
723+ let marks = snap_marks(&[&[]], None, None, 500);
724+ assert!(marks.contains(&500));
725+ assert_eq!(snapped(493, 0, 10, &marks), 500);
726+ }
727+}
crates/app/src/ui/bench_stance.rsmodified+29 -6
@@ -1029,11 +1029,27 @@ fn lane_row(
10291029 .min_w(px(0.))
10301030 .h_full()
10311031 .bg(rgb(DARK_CANVAS()))
1032- .border_l_1()
1033- .border_color(rgba(DARK_SEAM()))
1032+ // The seam is drawn as an absolute overlay rather than a
1033+ // layout `border_l_1`: a border eats a pixel out of the
1034+ // content box it decorates, so frame 0 -- drawn by every
1035+ // ghost and clip at `left(px(0.))` relative to *this* box --
1036+ // would sit one pixel right of what `Player::frame_under`
1037+ // computes through the ruler's own (unbordered) bounds
1038+ // (bench-ruler, below). One shared origin, so a drop lands
1039+ // exactly on the frame its ghost showed.
1040+ .child(
1041+ div()
1042+ .absolute()
1043+ .left_0()
1044+ .top_0()
1045+ .w(px(1.))
1046+ .h_full()
1047+ .bg(rgba(DARK_SEAM())),
1048+ )
10341049 .drag_over::<AssetDrag>(|d, _, _, _| d.bg(rgb(DARK_RAISED())))
10351050 .on_drop(cx.listener(move |this, drag: &AssetDrag, window, cx| {
1036- let at = this.place_frame(window.mouse_position().x).0;
1051+ let x = this.drag_x.take().unwrap_or_else(|| window.mouse_position().x);
1052+ let at = this.place_frame(x).0;
10371053 this.insert_source(&drag.0.clone(), drag.1, Some(lane), Some(at), cx)
10381054 }))
10391055 // Right-click on empty bed space: the "Close Gap" door. A
@@ -1065,6 +1081,7 @@ fn lane_row(
10651081 return;
10661082 }
10671083 let path = event.drag(cx).0.clone();
1084+ this.drag_x = Some(event.event.position.x);
10681085 this.preview_ghost_asset(&path, lane, event.event.position.x, cx);
10691086 }),
10701087 )
@@ -1073,7 +1090,8 @@ fn lane_row(
10731090 let Some(idx) = this.dragged(drag) else {
10741091 return;
10751092 };
1076- this.move_clip(drag.lane, idx, lane, window.mouse_position().x, cx)
1093+ let x = this.drag_x.take().unwrap_or_else(|| window.mouse_position().x);
1094+ this.move_clip(drag.lane, idx, lane, x, cx)
10771095 }))
10781096 .on_drag_move(
10791097 cx.listener(move |this, event: &DragMoveEvent<ClipDrag>, _, cx| {
@@ -1081,6 +1099,7 @@ fn lane_row(
10811099 return;
10821100 }
10831101 let drag = *event.drag(cx);
1102+ this.drag_x = Some(event.event.position.x);
10841103 this.preview_ghost(&drag, lane, event.event.position.x, cx);
10851104 }),
10861105 )
@@ -1089,7 +1108,8 @@ fn lane_row(
10891108 // the same call `ui/timeline.rs`'s own bed makes).
10901109 .drag_over::<SubDrag>(|d, _, _, _| d.bg(rgb(DARK_RAISED())))
10911110 .on_drop(cx.listener(move |this, drag: &SubDrag, window, cx| {
1092- this.move_sub(drag, lane, window.mouse_position().x, cx);
1111+ let x = this.drag_x.take().unwrap_or_else(|| window.mouse_position().x);
1112+ this.move_sub(drag, lane, x, cx);
10931113 }))
10941114 .on_drag_move(
10951115 cx.listener(move |this, event: &DragMoveEvent<SubDrag>, _, cx| {
@@ -1097,12 +1117,14 @@ fn lane_row(
10971117 return;
10981118 }
10991119 let drag = *event.drag(cx);
1120+ this.drag_x = Some(event.event.position.x);
11001121 this.preview_ghost_sub(&drag, lane, event.event.position.x, cx);
11011122 }),
11021123 )
11031124 .drag_over::<SubPick>(|d, _, _, _| d.bg(rgb(DARK_RAISED())))
11041125 .on_drop(cx.listener(move |this, drag: &SubPick, window, cx| {
1105- this.place_sub(drag.0, lane, window.mouse_position().x, cx);
1126+ let x = this.drag_x.take().unwrap_or_else(|| window.mouse_position().x);
1127+ this.place_sub(drag.0, lane, x, cx);
11061128 }))
11071129 .on_drag_move(
11081130 cx.listener(move |this, event: &DragMoveEvent<SubPick>, _, cx| {
@@ -1110,6 +1132,7 @@ fn lane_row(
11101132 return;
11111133 }
11121134 let track = event.drag(cx).0;
1135+ this.drag_x = Some(event.event.position.x);
11131136 this.preview_ghost_pick(track, lane, event.event.position.x, cx);
11141137 }),
11151138 )
crates/app/src/viewport.rsmodified+12 -0
@@ -79,6 +79,18 @@ impl Scale {
7979 0
8080 }
8181 }
82+
83+ /// [`DROP_SNAP_PX`] in timeline frames, [`Scale::snap_frames`]'s twin for a
84+ /// drag's *release* rather than a trim: a dropped clip is aimed with a
85+ /// coarser hand than an edge nudged one frame at a time, and a pixel-exact
86+ /// release is what the user asked never to need.
87+ pub(crate) fn drop_snap_frames(self, fps: f64) -> u32 {
88+ if self.pps > 0. {
89+ (DROP_SNAP_PX / self.pps * fps) as u32
90+ } else {
91+ 0
92+ }
93+ }
8294 }
8395
8496 /// A [`Scale`] against the bed it is drawn on and the timeline it is drawn