Window move: an app-run move follows the system pointer

Where the app moves an OS window itself (a floating panel on Windows), each
pass placed the window from the pass's pointer event, relative to where the
window reports itself. The event can be an older sample than the window's
current place, since the window moves between passes, so the window landed
behind the pointer every pass and the release left it there: on Windows a
400 px drag moved a floating panel about 260 px, and at times only the
click distance.

Where the platform tells the system pointer, the move now places the
window from it (pointer - grab - inset), at the drag's start, on each pass
and once more at the release. Platforms without a system pointer keep the
event-based place. The new `windows::system_pointer` never falls back to
the drag's own noted pointer, which `desktop_pointer` does, so the move
cannot feed its own place back.

Part of storytold/photocraft#15478.
This commit is contained in:
David Mann
2026-11-17 18:13:33 +01:00
parent 2ceb1bd87e
commit 2695cc1ed0
4 changed files with 71 additions and 4 deletions
+20 -3
View File
@@ -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()
+1 -1
View File
@@ -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)]
@@ -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<Pos2> {
shared(ctx, |shared| shared.system_pointer)
}
pub(crate) fn note_moved_pointer(ctx: &Context, viewport: ViewportId, desktop: Option<Pos2>) {
PerViewport::<Record>::with(ctx, viewport, |record| record.moved_pointer = desktop);
}
@@ -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