From ba7de0f070291357f37480223c52cd8dadc727b8 Mon Sep 17 00:00:00 2001 From: David Mann Date: Tue, 17 Nov 2026 18:29:09 +0100 Subject: [PATCH] Window move: float rounding of a window's size is no OS resize The input hook releases a held press when an undecorated window's inner size changes under it (the OS resized it and keeps the release). It compared the sizes exactly. A window the app moves reports its size again in points from its pixels, and at a fractional scale the same size comes back a float step off (290.4 as 290.40002 at 125 %), so the hook released the press a few points into every move of a floating panel on Windows. A size now counts as changed only by more than 0.05 pt, far under one physical pixel at any scale and far over that rounding. Part of storytold/photocraft#15478. --- crates/ui-contract/src/runtime/windows.rs | 13 +++++++++- .../ui-contract/tests/it/runtime/windows.rs | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/crates/ui-contract/src/runtime/windows.rs b/crates/ui-contract/src/runtime/windows.rs index 8c3194e9b..a42121e9f 100644 --- a/crates/ui-contract/src/runtime/windows.rs +++ b/crates/ui-contract/src/runtime/windows.rs @@ -230,6 +230,16 @@ pub(crate) fn set_input(ctx: &Context) { }); } +/// The least change of an inner size, in points, that counts as a resize: far under one physical pixel at any scale +/// (a quarter point at 400 %), far over the float rounding of a size the platform reports again in points from its +/// pixels as the window moves (290.4 coming back as 290.40002 at 125 %). +const RESIZE_LEAST: f32 = 0.05; + +/// Whether a window's inner size changed from `before` to `now` by more than float rounding. +fn size_changed(before: egui::Vec2, now: egui::Vec2) -> bool { + (before.x - now.x).abs() > RESIZE_LEAST || (before.y - now.y).abs() > RESIZE_LEAST +} + /// H4, in the input hook. The OS takes the button release of a move or resize it runs, so the press stays down in /// egui and every later press is lost. Two cases end it with a release of the primary button, in any chrome: a move /// or resize [`act`] handed the OS, when the input brings no release of its own (where the OS sends one, a second @@ -258,7 +268,8 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> bool { input.events.push(egui::Event::ModifiersChanged(modifiers)); } let (released, handed_off) = PerViewport::::with(ctx, viewport, |record| { - let resized = record.resizable != Some(false) && matches!((record.last_inner_size, size), (Some(before), Some(now)) if before != now); + let resized = + record.resizable != Some(false) && matches!((record.last_inner_size, size), (Some(before), Some(now)) if size_changed(before, now)); record.last_inner_size = size.or(record.last_inner_size); let handed_off = record.handoff.is_some_and(|handoff| handoff.released.is_none() && handoff.pass < pass) && held != Some(true); if handed_off { diff --git a/crates/ui-contract/tests/it/runtime/windows.rs b/crates/ui-contract/tests/it/runtime/windows.rs index 09e3afc5d..42730a364 100644 --- a/crates/ui-contract/tests/it/runtime/windows.rs +++ b/crates/ui-contract/tests/it/runtime/windows.rs @@ -80,6 +80,32 @@ fn a_press_is_released_after_an_os_resize_of_an_undecorated_window() { } } +/// A window that moves reports its inner size again, in points from its physical pixels: at a fractional scale the +/// same size can come back a float step off (290.4 as 290.40002). That is no OS resize, so the press stays down: a +/// release there ended every move of a floating panel on Windows a few points into the drag. +#[trial] +fn a_press_survives_a_size_that_differs_only_by_float_rounding() { + let shell = ShellInfo::default() + .os(Os::Windows) + .caps(Caps::native(Os::Windows)) + .window(WindowInfo::new(WindowKind::Main).chrome(WindowChrome::Integrated)); + let windows = Windows::with(InstallSpec::new(shell, Arc::new(NoWindows))); + let down = Cell::new(false); + let sized = |width: f32| ViewportInfo { inner_rect: Some(Rect::from_min_size(Pos2::ZERO, Vec2::new(width, 600.0))), ..ViewportInfo::default() }; + let frame = |events: Vec| { + windows.frame(events, vec![], |ui| down.set(ui.ctx().input(|input| input.pointer.any_down()))); + }; + windows.root_info(sized(800.0)); + frame(vec![]); + frame(vec![moved(Pos2::new(20.0, 20.0))]); + frame(vec![press(Pos2::new(20.0, 20.0))]); + assert!(down.get(), "the press is down"); + windows.root_info(sized(800.0 + 800.0 * f32::EPSILON)); + frame(vec![]); + frame(vec![]); + assert!(down.get(), "a float step of size is no resize: the press stays down"); +} + /// One frame of `windows` with the `Modal` open in the child viewport. fn modal_frame(windows: &Windows) -> Out { windows.frame(vec![], vec![], |ui| {