mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 11:38:08 +00:00
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.
This commit is contained in:
@@ -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::<Record>::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 {
|
||||
|
||||
@@ -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<Event>| {
|
||||
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| {
|
||||
|
||||
Reference in New Issue
Block a user