From 9c03eb613c2fbd4ee7eef3e12575c8ab57e37097 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 20 Nov 2026 15:20:43 +0000 Subject: [PATCH] Memoized views: a move keeps placement only on one side of zero Paint snaps to physical pixels and widget rects to the UI grid with f32::round, which takes a half step away from zero: a coordinate on a half pixel moved by a whole pixel across zero snaps one pixel off the move (389.5 rounds to 390, -21.5 to -22). keeps_placement takes the moved area and needs it to keep its side of zero along each axis, so a view moved across the window's edge runs. The gallery's glide to a selected card replayed a card one pixel off its run under the audit. --- crates/ui-contract/src/runtime/lazy.rs | 20 +++++++++++----- crates/ui-widgets/tests/it/body/memo_audit.rs | 23 +++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/crates/ui-contract/src/runtime/lazy.rs b/crates/ui-contract/src/runtime/lazy.rs index 0313b24d2..9ee1ade7b 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -212,7 +212,8 @@ impl Place { && now.pixels_per_point == self.pixels_per_point && now.atlas == self.atlas && now.env == self.env; - let placeable = offset == Vec2::ZERO || (keeps_placement(offset, now.pixels_per_point) && movable && !transformed); + let placeable = + offset == Vec2::ZERO || (keeps_placement(rect.union(self.room), offset, now.pixels_per_point) && movable && !transformed); if !same || !placeable { return None; } @@ -222,11 +223,18 @@ impl Place { } } -/// Whether content moved by `offset` lies where a run at the new place puts it: the move is a whole step of egui's UI -/// rounding, which every widget rect is rounded to, and a whole physical pixel, which paint snaps to (`Surface`, text). -/// A move between them leaves a rounded rect off the grid a run there rounds it to. -pub(crate) fn keeps_placement(offset: Vec2, pixels_per_point: f32) -> bool { - offset == offset.round_ui() && offset == offset.round_to_pixels(pixels_per_point) +/// Whether content in `area` moved by `offset` lies where a run at the new place puts it: the move is a whole step of +/// egui's UI rounding, which every widget rect is rounded to, and a whole physical pixel, which paint snaps to +/// (`Surface`, text). A move between them leaves a rounded rect off the grid a run there rounds it to. Both roundings +/// take a half step away from zero (`f32::round`), so a coordinate on a half step rounds to the same place after the +/// move only on the same side of zero: the area keeps its side of zero along each axis. +pub(crate) fn keeps_placement(area: Rect, offset: Vec2, pixels_per_point: f32) -> bool { + let moved = area.translate(offset); + let one_side = |(min, max): (f32, f32), (to_min, to_max): (f32, f32)| (min >= 0.0 && to_min >= 0.0) || (max <= 0.0 && to_max <= 0.0); + offset == offset.round_ui() + && offset == offset.round_to_pixels(pixels_per_point) + && one_side((area.min.x, area.max.x), (moved.min.x, moved.max.x)) + && one_side((area.min.y, area.max.y), (moved.min.y, moved.max.y)) } /// The last measuring run of a measure-only child: content a pass measures and never shows, as a page `Pages` holds diff --git a/crates/ui-widgets/tests/it/body/memo_audit.rs b/crates/ui-widgets/tests/it/body/memo_audit.rs index 9474c4c58..2183e20e5 100644 --- a/crates/ui-widgets/tests/it/body/memo_audit.rs +++ b/crates/ui-widgets/tests/it/body/memo_audit.rs @@ -377,6 +377,29 @@ fn a_card_holding_a_scrolling_body_replays_moved_with_its_rows_places() { frames.idle(SETTLE, &mut show); } +/// A card on half points moved by whole pixels replays while it keeps its side of zero, and runs when the move takes it +/// across: paint rounds a half pixel away from zero, so across zero the card snaps one pixel off the move. The audit +/// finds every replay equal to a run. +#[trial] +fn a_card_on_half_points_replays_moved_on_its_side_of_zero_and_runs_across_it() { + let mut frames = Frames::new(); + audit_replays(&frames.ctx, true); + let top = Cell::new(30.5); + let mut show = |ui: &mut Ui| { + in_region(ui, Rect::from_min_size(Pos2::new(10.0, top.get()), Vec2::new(100.0, 30.0)), |ui| { + MemoView::new("card", Card { at: 0 }, card).show(ui); + }); + }; + frames.idle(SETTLE, &mut show); + top.set(10.5); + frames.frame(vec![], &mut show); + assert_eq!(replayed(&frames), 1, "a move that keeps the card below zero replays it"); + top.set(-9.5); + frames.frame(vec![], &mut show); + assert_eq!(replayed(&frames), 0, "a move across zero runs it"); + frames.idle(SETTLE, &mut show); +} + /// A view that holds a card in a room of its own, `gap` points down. #[derive(MemoProps)] struct Holder {