Drag drop commits the frame the ghost last showed, snaps to 0 within 10px
Browse files at this commit1f68773215df7392060b8a448b779b6f18b4a4cbParent: cafb588- fix
- drag drop commits the frame the ghost last showed, snaps to 0 within 10px
6 changed files +107 -9
- crates/app/src/interact.rs +8 -0
- crates/app/src/main.rs +8 -0
- crates/app/src/player/timeline_edit.rs +11 -3
- crates/app/src/timeline_math.rs +39 -0
- crates/app/src/ui/bench_stance.rs +29 -6
- crates/app/src/viewport.rs +12 -0
crates/app/src/interact.rsmodified+8 -0
| @@ -272,6 +272,14 @@ pub(crate) fn swallow(_: &MouseDownEvent, _: &mut Window, cx: &mut App) { | |||
| 272 | /// takes must still be able to leave a gap of a few frames there. | 272 | /// takes must still be able to leave a gap of a few frames there. |
| 273 | pub(crate) const SNAP_PX: f64 = 5.; | 273 | pub(crate) const SNAP_PX: f64 = 5.; |
| 274 | 274 | ||
| 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 | + | ||
| 275 | /// The scrollbar's thumb never narrows past this, however long the timeline: | 283 | /// The scrollbar's thumb never narrows past this, however long the timeline: |
| 276 | /// a thumb a pixel wide is a thumb no hand can hold. A quarter of the ruler's | 284 | /// a thumb a pixel wide is a thumb no hand can hold. A quarter of the ruler's |
| 277 | /// own seek strip, which is the idiom it sits under. | 285 | /// own seek strip, which is the idiom it sits under. |
crates/app/src/main.rsmodified+8 -0
| @@ -393,6 +393,13 @@ struct Player { | |||
| 393 | /// the clip under the hand: every other pick draws its own shadow too, at | 393 | /// the clip under the hand: every other pick draws its own shadow too, at |
| 394 | /// the same delta the anchor's is landing at ([`Player::preview_ghost`]). | 394 | /// the same delta the anchor's is landing at ([`Player::preview_ghost`]). |
| 395 | ghost: Vec<Ghost>, | 395 | 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>, | ||
| 396 | /// The slot a track header being dragged is about to drop into, or `None` | 403 | /// The slot a track header being dragged is about to drop into, or `None` |
| 397 | /// while the pointer is over no lane: the line drawn between two headers, | 404 | /// while the pointer is over no lane: the line drawn between two headers, |
| 398 | /// for the reason [`Player::ghost`] draws a shadow -- where a gesture lands | 405 | /// for the reason [`Player::ghost`] draws a shadow -- where a gesture lands |
| @@ -945,6 +952,7 @@ fn main() { | |||
| 945 | sub_lane: None, | 952 | sub_lane: None, |
| 946 | snap_cue: None, | 953 | snap_cue: None, |
| 947 | ghost: Vec::new(), | 954 | ghost: Vec::new(), |
| 955 | + drag_x: None, | ||
| 948 | lane_drop: None, | 956 | lane_drop: None, |
| 949 | last_scrub: Instant::now(), | 957 | last_scrub: Instant::now(), |
| 950 | last_target: 0, | 958 | last_target: 0, |
crates/app/src/player/timeline_edit.rsmodified+11 -3
| @@ -753,7 +753,7 @@ impl Player { | |||
| 753 | self.grab, | 753 | self.grab, |
| 754 | sub.frames, | 754 | sub.frames, |
| 755 | self.snap, | 755 | self.snap, |
| 756 | - self.snap_frames(), | 756 | + self.drop_snap_frames(), |
| 757 | &marks, | 757 | &marks, |
| 758 | ) | 758 | ) |
| 759 | } | 759 | } |
| @@ -826,7 +826,7 @@ impl Player { | |||
| 826 | self.grab, | 826 | self.grab, |
| 827 | clip.frames(), | 827 | clip.frames(), |
| 828 | self.snap, | 828 | self.snap, |
| 829 | - self.snap_frames(), | 829 | + self.drop_snap_frames(), |
| 830 | &marks, | 830 | &marks, |
| 831 | )) | 831 | )) |
| 832 | } | 832 | } |
| @@ -843,7 +843,7 @@ impl Player { | |||
| 843 | 0, | 843 | 0, |
| 844 | 0, | 844 | 0, |
| 845 | self.snap, | 845 | self.snap, |
| 846 | - self.snap_frames(), | 846 | + self.drop_snap_frames(), |
| 847 | &marks, | 847 | &marks, |
| 848 | ) | 848 | ) |
| 849 | } | 849 | } |
| @@ -1153,6 +1153,14 @@ impl Player { | |||
| 1153 | self.scale.snap_frames(self.fps) | 1153 | self.scale.snap_frames(self.fps) |
| 1154 | } | 1154 | } |
| 1155 | 1155 | ||
| 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 | + | ||
| 1156 | /// Opens the clip menu on the box under the pointer, from the right button | 1164 | /// Opens the clip menu on the box under the pointer, from the right button |
| 1157 | /// wherever it was pressed on that box -- its middle or one of its edge | 1165 | /// wherever it was pressed on that box -- its middle or one of its edge |
| 1158 | /// strips, which cover the middle's own listener. Selecting first is part of | 1166 | /// 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 { | |||
| 686 | } | 686 | } |
| 687 | ((y - bounds.top()) / bounds.size.height).clamp(0., 1.) | 687 | ((y - bounds.top()) / bounds.size.height).clamp(0., 1.) |
| 688 | } | 688 | } |
| 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( | |||
| 1029 | .min_w(px(0.)) | 1029 | .min_w(px(0.)) |
| 1030 | .h_full() | 1030 | .h_full() |
| 1031 | .bg(rgb(DARK_CANVAS())) | 1031 | .bg(rgb(DARK_CANVAS())) |
| 1032 | - .border_l_1() | 1032 | + // The seam is drawn as an absolute overlay rather than a |
| 1033 | - .border_color(rgba(DARK_SEAM())) | 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 | + ) | ||
| 1034 | .drag_over::<AssetDrag>(|d, _, _, _| d.bg(rgb(DARK_RAISED()))) | 1049 | .drag_over::<AssetDrag>(|d, _, _, _| d.bg(rgb(DARK_RAISED()))) |
| 1035 | .on_drop(cx.listener(move |this, drag: &AssetDrag, window, cx| { | 1050 | .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; | ||
| 1037 | this.insert_source(&drag.0.clone(), drag.1, Some(lane), Some(at), cx) | 1053 | this.insert_source(&drag.0.clone(), drag.1, Some(lane), Some(at), cx) |
| 1038 | })) | 1054 | })) |
| 1039 | // Right-click on empty bed space: the "Close Gap" door. A | 1055 | // Right-click on empty bed space: the "Close Gap" door. A |
| @@ -1065,6 +1081,7 @@ fn lane_row( | |||
| 1065 | return; | 1081 | return; |
| 1066 | } | 1082 | } |
| 1067 | let path = event.drag(cx).0.clone(); | 1083 | let path = event.drag(cx).0.clone(); |
| 1084 | + this.drag_x = Some(event.event.position.x); | ||
| 1068 | this.preview_ghost_asset(&path, lane, event.event.position.x, cx); | 1085 | this.preview_ghost_asset(&path, lane, event.event.position.x, cx); |
| 1069 | }), | 1086 | }), |
| 1070 | ) | 1087 | ) |
| @@ -1073,7 +1090,8 @@ fn lane_row( | |||
| 1073 | let Some(idx) = this.dragged(drag) else { | 1090 | let Some(idx) = this.dragged(drag) else { |
| 1074 | return; | 1091 | return; |
| 1075 | }; | 1092 | }; |
| 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) | ||
| 1077 | })) | 1095 | })) |
| 1078 | .on_drag_move( | 1096 | .on_drag_move( |
| 1079 | cx.listener(move |this, event: &DragMoveEvent<ClipDrag>, _, cx| { | 1097 | cx.listener(move |this, event: &DragMoveEvent<ClipDrag>, _, cx| { |
| @@ -1081,6 +1099,7 @@ fn lane_row( | |||
| 1081 | return; | 1099 | return; |
| 1082 | } | 1100 | } |
| 1083 | let drag = *event.drag(cx); | 1101 | let drag = *event.drag(cx); |
| 1102 | + this.drag_x = Some(event.event.position.x); | ||
| 1084 | this.preview_ghost(&drag, lane, event.event.position.x, cx); | 1103 | this.preview_ghost(&drag, lane, event.event.position.x, cx); |
| 1085 | }), | 1104 | }), |
| 1086 | ) | 1105 | ) |
| @@ -1089,7 +1108,8 @@ fn lane_row( | |||
| 1089 | // the same call `ui/timeline.rs`'s own bed makes). | 1108 | // the same call `ui/timeline.rs`'s own bed makes). |
| 1090 | .drag_over::<SubDrag>(|d, _, _, _| d.bg(rgb(DARK_RAISED()))) | 1109 | .drag_over::<SubDrag>(|d, _, _, _| d.bg(rgb(DARK_RAISED()))) |
| 1091 | .on_drop(cx.listener(move |this, drag: &SubDrag, window, cx| { | 1110 | .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); | ||
| 1093 | })) | 1113 | })) |
| 1094 | .on_drag_move( | 1114 | .on_drag_move( |
| 1095 | cx.listener(move |this, event: &DragMoveEvent<SubDrag>, _, cx| { | 1115 | cx.listener(move |this, event: &DragMoveEvent<SubDrag>, _, cx| { |
| @@ -1097,12 +1117,14 @@ fn lane_row( | |||
| 1097 | return; | 1117 | return; |
| 1098 | } | 1118 | } |
| 1099 | let drag = *event.drag(cx); | 1119 | let drag = *event.drag(cx); |
| 1120 | + this.drag_x = Some(event.event.position.x); | ||
| 1100 | this.preview_ghost_sub(&drag, lane, event.event.position.x, cx); | 1121 | this.preview_ghost_sub(&drag, lane, event.event.position.x, cx); |
| 1101 | }), | 1122 | }), |
| 1102 | ) | 1123 | ) |
| 1103 | .drag_over::<SubPick>(|d, _, _, _| d.bg(rgb(DARK_RAISED()))) | 1124 | .drag_over::<SubPick>(|d, _, _, _| d.bg(rgb(DARK_RAISED()))) |
| 1104 | .on_drop(cx.listener(move |this, drag: &SubPick, window, cx| { | 1125 | .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); | ||
| 1106 | })) | 1128 | })) |
| 1107 | .on_drag_move( | 1129 | .on_drag_move( |
| 1108 | cx.listener(move |this, event: &DragMoveEvent<SubPick>, _, cx| { | 1130 | cx.listener(move |this, event: &DragMoveEvent<SubPick>, _, cx| { |
| @@ -1110,6 +1132,7 @@ fn lane_row( | |||
| 1110 | return; | 1132 | return; |
| 1111 | } | 1133 | } |
| 1112 | let track = event.drag(cx).0; | 1134 | let track = event.drag(cx).0; |
| 1135 | + this.drag_x = Some(event.event.position.x); | ||
| 1113 | this.preview_ghost_pick(track, lane, event.event.position.x, cx); | 1136 | this.preview_ghost_pick(track, lane, event.event.position.x, cx); |
| 1114 | }), | 1137 | }), |
| 1115 | ) | 1138 | ) |
crates/app/src/viewport.rsmodified+12 -0
| @@ -79,6 +79,18 @@ impl Scale { | |||
| 79 | 0 | 79 | 0 |
| 80 | } | 80 | } |
| 81 | } | 81 | } |
| 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 | + } | ||
| 82 | } | 94 | } |
| 83 | 95 | ||
| 84 | /// A [`Scale`] against the bed it is drawn on and the timeline it is drawn | 96 | /// A [`Scale`] against the bed it is drawn on and the timeline it is drawn |