diff --git a/crates/ui-contract/src/contract/body/window.rs b/crates/ui-contract/src/contract/body/window.rs index ffe88ce169..99ae7c20de 100644 --- a/crates/ui-contract/src/contract/body/window.rs +++ b/crates/ui-contract/src/contract/body/window.rs @@ -592,7 +592,11 @@ impl BodyHooks<'_> { PressPhase::DragStart { start, pos, .. } if !tracked => { moving.grab = start.origin.to_vec2(); self.reports.push(change(outer, GesturePhase::Begin)); - moving.at = outer.translate(pos - start.origin); + // As every later step: from the system pointer where the platform tells it (see below). + moving.at = match windows::system_pointer(&ctx) { + Some(pointer) => Rect::from_min_size(pointer - moving.grab - inset, outer.size()), + None => outer.translate(pos - start.origin), + }; windows::act(&ctx, viewport, WindowAction::SetOuterPosition(moving.at.min)); self.reports.push(change(moving.at, GesturePhase::Update)); } @@ -612,6 +616,13 @@ impl BodyHooks<'_> { if ended == GesturePhase::Cancel && !moving.tracked { windows::act(&ctx, viewport, WindowAction::SetOuterPosition(moving.start.min)); moving.at = moving.start; + } else if moving.begun + && !moving.tracked + && let Some(to) = windows::system_pointer(&ctx).map(|pointer| pointer - moving.grab - inset).filter(|to| *to != moving.at.min) + { + // The release lands the window where the pointer let go, past the last pass that moved it. + windows::act(&ctx, viewport, WindowAction::SetOuterPosition(to)); + moving.at = Rect::from_min_size(to, moving.at.size()); } if moving.begun { self.reports.push(change(moving.at, ended)); @@ -624,9 +635,15 @@ impl BodyHooks<'_> { // AppKit sends no pointer events while it moves the window: the next pass reads the system pointer again. crate::runtime::egui::repaint::request(&ctx); host.desktop_pointer().map(|pointer| pointer - moving.grab - inset) + } else if let Some(pointer) = windows::system_pointer(&ctx) { + // The system pointer is in desktop points and does not depend on where the window is, so the window lands + // with the pressed point under it whatever moves the platform has applied by now. A pointer event is + // relative to the window as it was when the system sent it; the window moves between passes, so a place + // derived from an event lags further each pass and the window falls behind the pointer. + Some(pointer - moving.grab - inset) } else { - // A pointer move of this pass is relative to where the window was when the system sent it, which is where - // the window reports itself now; a position from an earlier pass was measured against an older place. + // A platform that tells no system pointer: a pointer move of this pass is relative to where the window was + // when the system sent it, taken as where the window reports itself now. let moved = pointer::pass_pointer(&ctx) .events .iter() diff --git a/crates/ui-contract/src/runtime/windows.rs b/crates/ui-contract/src/runtime/windows.rs index 9a1d907447..8c3194e9bb 100644 --- a/crates/ui-contract/src/runtime/windows.rs +++ b/crates/ui-contract/src/runtime/windows.rs @@ -34,7 +34,7 @@ pub use hit::{CaptionRegions, HitSink, ResizeEdge, WindowHit}; pub(crate) use hit::{hit_sink_installed, note_exclusive}; use hit::{publish_regions, replay_caption_events}; pub use host::{KeyEquivalent, WindowFacts, WindowHost}; -pub(crate) use host::{clear_source_pointer, note_moved_pointer, note_source_pointer}; +pub(crate) use host::{clear_source_pointer, note_moved_pointer, note_source_pointer, system_pointer}; #[doc(hidden)] pub use native::keeps_app_drag; #[cfg(windows)] diff --git a/crates/ui-contract/src/runtime/windows/host.rs b/crates/ui-contract/src/runtime/windows/host.rs index 5ce81ebda5..98f6c80f63 100644 --- a/crates/ui-contract/src/runtime/windows/host.rs +++ b/crates/ui-contract/src/runtime/windows/host.rs @@ -409,6 +409,13 @@ pub(crate) fn note_source_pointer(ctx: &Context, viewport: ViewportId, local: Op /// Records the desktop pointer of a move the app runs of `viewport`'s window: the pressed point of the window where the /// move last put it. `None` once the move ends. +/// The system's pointer in desktop points as the pass began, `None` where the platform does not tell it. Unlike +/// [`WindowHost::desktop_pointer`] it never falls back to a drag's own pointer, so a window moved after it does not +/// feed its own place back into the move. +pub(crate) fn system_pointer(ctx: &Context) -> Option { + shared(ctx, |shared| shared.system_pointer) +} + pub(crate) fn note_moved_pointer(ctx: &Context, viewport: ViewportId, desktop: Option) { PerViewport::::with(ctx, viewport, |record| record.moved_pointer = desktop); } diff --git a/crates/ui-patterns/tests/it/docking/native.rs b/crates/ui-patterns/tests/it/docking/native.rs index 9d97a05ed5..56bd6a2dc8 100644 --- a/crates/ui-patterns/tests/it/docking/native.rs +++ b/crates/ui-patterns/tests/it/docking/native.rs @@ -311,6 +311,49 @@ fn dragging_a_native_palette_by_its_title_row_docks_it_where_the_app_moves_windo assert!(!docked, "a cancelled move docks nothing"); } +/// Where the app moves windows itself and the platform tells the system pointer (Windows): the window follows the +/// system pointer, not the pass's pointer event, which can be an older sample than the window's current place (the +/// window moves between passes). Measured from such an event the window lands a step behind the pointer each pass and +/// stays behind after the release; from the system pointer the pressed point stays under the pointer, also at the +/// release that comes a move after the last pass. +#[trial] +fn a_native_palette_the_app_moves_follows_the_system_pointer_not_a_stale_event() { + let system = Arc::new(FakeSystem::new()); + let mut dock = NativeDock::on(Os::Linux, Arc::clone(&system)); + let mut window = FLOAT_AT; + let mut desktop = FLOAT_AT.min + ROW_AT.to_vec2(); + system.set_pointer(Some([desktop.x, desktop.y])); + for events in [vec![moved(ROW_AT)], vec![press(ROW_AT)]] { + window = Rect::from_min_size(*outer_positions(&dock.frame(events).1).last().unwrap_or(&window.min), window.size()); + } + // Away from the main window, so the release docks nothing. + let step = Vec2::new(40.0, 25.0); + let mut moves = 0; + for _ in 0..4 { + let older = desktop; + desktop += step; + system.set_pointer(Some([desktop.x, desktop.y])); + // The pass's event is the previous sample, relative to where the window is now. The first one is still the + // pressed point, so the drag starts a pass later. + let event = moved(older - window.min.to_vec2()); + if let Some(to) = outer_positions(&dock.frame(vec![event]).1).last() { + window = Rect::from_min_size(*to, window.size()); + moves += 1; + assert_eq!(window.min + ROW_AT.to_vec2(), desktop, "the pressed point stays under the system pointer"); + } + } + assert_eq!(moves, 3, "the window follows every pass once the drag has started"); + // The pointer moves once more before the release reaches the app. + desktop += step; + system.set_pointer(Some([desktop.x, desktop.y])); + for events in [vec![release(desktop - window.min.to_vec2())], vec![], vec![]] { + if let Some(to) = outer_positions(&dock.frame(events).1).last() { + window = Rect::from_min_size(*to, window.size()); + } + } + assert_eq!(window.min + ROW_AT.to_vec2(), desktop, "the window lands where the pointer let go"); +} + /// Where the OS moves windows and the app reads the system pointer and button meanwhile (macOS): the title row's press /// hands the move to the OS in its own pass, the system pointer over the main window's group marks it (Cmd keeps the /// drop floating), and the window docks when the system tells the button is up, not on the first pass after the