From 56a174652c2d27ee6f94d109d880a5dc6219b7cc Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 05:37:46 +0700 Subject: [PATCH] ui-contract: a drop lands where the button went up (PointerFrame::pick_pos), not where the pointer ends the frame --- crates/ui-contract/src/runtime/drag.rs | 5 ++-- crates/ui-contract/src/runtime/egui/input.rs | 12 +++++----- crates/ui-contract/src/runtime/pointer.rs | 8 ++++++- crates/ui-contract/tests/it/drag/mod.rs | 25 ++++++++++++++++++++ 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/crates/ui-contract/src/runtime/drag.rs b/crates/ui-contract/src/runtime/drag.rs index f8873da4b..d9faa15b7 100644 --- a/crates/ui-contract/src/runtime/drag.rs +++ b/crates/ui-contract/src/runtime/drag.rs @@ -495,11 +495,12 @@ pub fn cross_window_pointer(ctx: &Context, viewport: ViewportId) -> Option } /// The pointer of the running session in screen space, whatever its payload: the pointer `dragging_of` pairs with a -/// payload. `None` without a session or a pointer. +/// payload, where the button went up in the pass of its release (`PointerFrame::pick_pos`). `None` without a session or +/// a pointer. pub(crate) fn session_pointer(ctx: &Context) -> Option { let os = board(ctx, |board| board.session.as_ref().map(|session| session.os))?; let host = WindowHost::of(ctx); - if os && host.desktop_pointer().is_some() { host.pointer_in(pass::current_viewport(ctx)) } else { pointer::frame_raw(ctx).pos } + if os && host.desktop_pointer().is_some() { host.pointer_in(pass::current_viewport(ctx)) } else { pointer::frame_raw(ctx).pick_pos } } /// The running session's payload whose type is `payload` and the pointer in screen space. For files the OS drags, the diff --git a/crates/ui-contract/src/runtime/egui/input.rs b/crates/ui-contract/src/runtime/egui/input.rs index cc1d56138..19b82f75d 100644 --- a/crates/ui-contract/src/runtime/egui/input.rs +++ b/crates/ui-contract/src/runtime/egui/input.rs @@ -269,8 +269,8 @@ pub(crate) struct PointerRead { pub buttons: Buttons, /// The button that went up last in the pass, any button. pub release: Option, - /// The modifiers of the pass's last button release. - pub release_mods: Option, + /// Where the pass's last button release went up, and its modifiers. + pub released_at: Option<(Pos2, Mods)>, /// The last press of a button other than the primary one: the button and where it went down. pub other_press: Option<(PointerButton, Pos2)>, /// Whether the primary button went down in this pass. @@ -342,7 +342,7 @@ pub(crate) fn pointer_read(ctx: &Context) -> PointerRead { let mut touched = false; let mut touch_sample = None; let mut release = None; - let mut release_mods = None; + let mut released_at = None; let mut touches = Vec::new(); let mut other_press = None; let mut rotation = 0.0; @@ -368,7 +368,7 @@ pub(crate) fn pointer_read(ctx: &Context) -> PointerRead { } } else { release = Some(button); - release_mods = Some(Mods::from_egui(modifiers, os)); + released_at = Some((*pos, Mods::from_egui(modifiers, os))); } } Event::PointerMoved(pos) => { @@ -388,7 +388,7 @@ pub(crate) fn pointer_read(ctx: &Context) -> PointerRead { } } Event::PointerButton { pos, button: egui::PointerButton::Primary, pressed: false, modifiers } => { - release_mods = Some(Mods::from_egui(modifiers, os)); + released_at = Some((*pos, Mods::from_egui(modifiers, os))); if barrel_press { barrel_press = false; release = Some(PointerButton::Secondary); @@ -457,7 +457,7 @@ pub(crate) fn pointer_read(ctx: &Context) -> PointerRead { primary_down: input.pointer.primary_down() && !barrel_press, buttons, release, - release_mods, + released_at, touches, other_press, primary_pressed: input.pointer.primary_pressed() && !barrel_pressed, diff --git a/crates/ui-contract/src/runtime/pointer.rs b/crates/ui-contract/src/runtime/pointer.rs index 8022e2964..b95f7e9b9 100644 --- a/crates/ui-contract/src/runtime/pointer.rs +++ b/crates/ui-contract/src/runtime/pointer.rs @@ -50,6 +50,10 @@ pub struct PointerFrame { /// The modifiers of the pass's last button release, with the latched ones added; the held `mods` in a pass with no /// release and in a rerun pass. The one source of a pick's and a drop's modifiers. pub pick_mods: Mods, + /// Where the pass's last button release went up; the latest `pos` in a pass with no release and in a rerun pass. The + /// one source of a drop's position: a pointer that moves on or leaves the window after the release in the same + /// frame does not move the drop. + pub pick_pos: Option, /// The tap a per-viewport recognizer reports when the last finger lifts, in the frame's first pass only. pub multi_tap: Option, /// The pass's time in seconds. @@ -295,7 +299,8 @@ pub(crate) fn begin(ctx: &Context, hit: HitSizes, latched: craft_keymap::Mods) { press, release: read.release, mods, - pick_mods: read.release_mods.map_or(mods, |released| with_latched(released, latched)), + pick_mods: read.released_at.map_or(mods, |(_, released)| with_latched(released, latched)), + pick_pos: read.released_at.map_or(read.pos, |(at, _)| Some(at)), multi_tap: tap, time: read.time, kind: read.kind, @@ -448,6 +453,7 @@ pub(crate) fn frame_raw(ctx: &Context) -> PointerFrame { release: None, mods: Mods::NONE, pick_mods: Mods::NONE, + pick_pos: None, multi_tap: None, time: 0.0, kind: PointerKind::Mouse, diff --git a/crates/ui-contract/tests/it/drag/mod.rs b/crates/ui-contract/tests/it/drag/mod.rs index 7b4f8c7c3..1760c962a 100644 --- a/crates/ui-contract/tests/it/drag/mod.rs +++ b/crates/ui-contract/tests/it/drag/mod.rs @@ -162,6 +162,31 @@ fn a_named_drag_shows_its_name_inside_a_region_mark_and_nothing_without_a_mark() frames.frame(vec![release(SLOT.center())], &mut show); } +/// A drop lands where the button went up: the pointer moving on out of the target after the release, in the same +/// frame, does not take the drop away from it. +#[trial] +fn a_drop_lands_where_the_button_went_up_though_the_pointer_moves_on_in_its_frame() { + let seen = RefCell::new(Seen::default()); + let mut show = |ui: &mut Ui| { + Chip { common: Common::new(), seen: &seen, preview: DragPreview::None }.id_salt("chip").show_at(ui, CHIP); + Slot { common: Common::new(), seen: &seen, mark: DropMark::region(SLOT) }.id_salt("slot").show_at(ui, SLOT); + }; + let mut frames = Frames::new(); + let (from, to) = (CHIP.center(), SLOT.center()); + frames.frame(vec![moved(from)], &mut show); + frames.frame(vec![press(from)], &mut show); + for step in 1..=4 { + frames.frame(vec![moved(from + (to - from) * (step as f32 / 4.0))], &mut show); + } + frames.idle(1, &mut show); + let away = Pos2::new(120.0, 200.0); + assert!(!SLOT.contains(away)); + frames.frame(vec![release(to), moved(away)], &mut show); + frames.idle(2, &mut show); + let dropped: Vec = seen.borrow().dropped.iter().map(|(payload, _)| *payload).collect(); + assert_eq!(dropped, [7], "the slot under the release takes the drop"); +} + /// The bounds of the text shapes among `shapes`. fn texts(shapes: &[Shape]) -> Vec { shapes.iter().filter(|shape| matches!(shape, Shape::Text(_))).map(Shape::visual_bounding_rect).collect()