diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 4b70e25020..5926062046 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -1421,9 +1421,11 @@ impl BodyHooks<'_> { Pos2::new(if rtl { shows.max.x } else { shows.max.x - inset.x }, shows.max.y - inset.y), ); let (width, height) = (if scroll.horizontal_on() { big } else { seen.width() }, if scroll.vertical_on() { big } else { seen.height() }); - // The content moves by whole steps of egui's UI rounding, as egui places every widget rect: a settled run moved - // with the content then replays where a run would be (`lazy::Place::shift_to`). - let (x, y) = ((off.x as f32).round_ui(), (off.y as f32).round_ui()); + // The content moves by whole physical pixels on steps of egui's UI rounding, the grids widget rects and paint + // are rounded to: a settled run moved with the content then replays where a run would be + // (`lazy::keeps_placement`). + let ppp = ctx.pixels_per_point(); + let (x, y) = ((off.x as f32).round_to_pixels(ppp).round_ui(), (off.y as f32).round_to_pixels(ppp).round_ui()); let x = if scroll.horizontal_on() { x } else { 0.0 }; let min_x = if rtl { seen.max.x - width + x } else { seen.min.x - x }; let max = Rect::from_min_size(Pos2::new(min_x, seen.min.y - y), Vec2::new(width, height)); diff --git a/crates/ui-contract/src/primitives/sensing.rs b/crates/ui-contract/src/primitives/sensing.rs index d874890439..6a65597981 100644 --- a/crates/ui-contract/src/primitives/sensing.rs +++ b/crates/ui-contract/src/primitives/sensing.rs @@ -472,8 +472,8 @@ fn register_root_again(ctx: &Context, arena: ArenaId, registration: RootRegistra fn journal_join(ctx: &Context, arena: ArenaId, layer: Layer, clip: Rect, joined: &[MemberDecl]) { crate::runtime::replay::journal( ctx, - || (arena, layer, clip, joined.to_vec()), - |ctx, (arena, layer, clip, joined)| join_again(ctx, *arena, *layer, *clip, joined), + || (arena, layer, crate::runtime::Clip::new(clip), joined.to_vec()), + |ctx, (arena, layer, clip, joined)| join_again(ctx, *arena, *layer, clip.rect(), joined), ); } @@ -506,7 +506,11 @@ fn join_member(state: &mut ArenaState, mut decl: MemberDecl, layer: Layer, targe /// so far, as `place_over_widgets` does, and records their shapes on `layer` through `clip`; journaled, so a replay /// places them above the members joined in the replaying pass. fn place_over(ctx: &Context, (arena, id, member, layer, clip): (ArenaId, Id, bool, Layer, Rect)) { - crate::runtime::replay::journal(ctx, || (arena, id, member, layer, clip), |ctx, &args| place_over(ctx, args)); + crate::runtime::replay::journal( + ctx, + || (arena, id, member, layer, crate::runtime::Clip::new(clip)), + |ctx, &(arena, id, member, layer, clip)| place_over(ctx, (arena, id, member, layer, clip.rect())), + ); let read = PassRead::of(ctx); let shapes = with_arena_in(ctx, read.viewport, arena, read.pass_nr, |state| { rotate(state, read.pass_nr); diff --git a/crates/ui-contract/src/runtime/lazy.rs b/crates/ui-contract/src/runtime/lazy.rs index 711eb72324..8a126f7a42 100644 --- a/crates/ui-contract/src/runtime/lazy.rs +++ b/crates/ui-contract/src/runtime/lazy.rs @@ -192,8 +192,8 @@ struct Moved { impl Place { /// How a run at this place that took `rect` replays at `now`, or `None` when it cannot. The room may have moved; its /// size, the layer, layout, scale, font atlas and environment are the ones the run had. The record moves by the - /// room's move when that is a whole step of egui's UI rounding (a run rounds its rects to it, so a moved record is - /// what a run there gives) and, for a move, the record holds nothing a move cannot place (`movable`: no paint callback) on an + /// room's move when that keeps where a run places content (`keeps_placement`, so a moved record is what a run there + /// gives) and, for a move, the record holds nothing a move cannot place (`movable`: no paint callback) on an /// untransformed layer (`transformed`: egui's AccessKit bounds are layer space only there). Its clip rects follow /// the clip when either: /// - the clip moved with the room (`with_clip`): every rect and clip moves exactly, whatever the clip cut; @@ -210,7 +210,7 @@ impl Place { && now.pixels_per_point == self.pixels_per_point && now.atlas == self.atlas && now.env == self.env; - let placeable = offset == Vec2::ZERO || (offset == offset.round_ui() && movable && !transformed); + let placeable = offset == Vec2::ZERO || (keeps_placement(offset, now.pixels_per_point) && movable && !transformed); if !same || !placeable { return None; } @@ -220,6 +220,13 @@ 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) +} + /// The last measuring run of a measure-only child: content a pass measures and never shows, as a page `Pages` holds /// out of view. #[derive(Clone)] diff --git a/crates/ui-contract/src/runtime/replay.rs b/crates/ui-contract/src/runtime/replay.rs index 33a4e931f3..9d55dfca73 100644 --- a/crates/ui-contract/src/runtime/replay.rs +++ b/crates/ui-contract/src/runtime/replay.rs @@ -34,7 +34,6 @@ pub(crate) struct Entry(Arc); trait Noted: Send + Sync { fn call(&self, ctx: &Context, shift: &RecordShift); /// The entry a replay at the place `shift` names calls (`call`). - #[cfg(feature = "testing")] fn shifted(&self, shift: &RecordShift) -> Entry; fn as_any(&self) -> &dyn Any; fn same(&self, other: &dyn Noted) -> bool; @@ -51,7 +50,6 @@ where A: PartialEq + fmt::Debug + Shift + Send + Sync + 'static, F: Fn(&Context, &A) + Copy + Send + Sync + 'static, { - #[cfg(feature = "testing")] fn shifted(&self, shift: &RecordShift) -> Entry { Entry(Arc::new(Note { args: self.args.shifted(shift), again: self.again })) } @@ -309,12 +307,12 @@ fn min_wake(a: Option, b: Option) -> Option { pub(crate) fn replay(ctx: &Context, record: &Record, shift: &RecordShift) { ctx.replay_pass_records(&record.egui, shift); focus::replay(ctx, &record.focus); - // The records open around the replay hold its journal as it was: an entry's call notes nothing, whether it is a - // service's own registration call or not. + // The records open around the replay hold its journal at the place it replays at, as a run there would have noted + // it: an entry's call notes nothing, whether it is a service's own registration call or not. let noting = recording(ctx); if noting { pass_state::with::(ctx, |recorder| { - recorder.journal.extend(record.journal.iter().cloned()); + recorder.journal.extend(record.journal.iter().map(|entry| if shift.is_none() { entry.clone() } else { entry.0.shifted(shift) })); recorder.replaying = true; }); } diff --git a/crates/ui-widgets/tests/it/body/memo_audit.rs b/crates/ui-widgets/tests/it/body/memo_audit.rs index 652a2f27fa..912108dbcd 100644 --- a/crates/ui-widgets/tests/it/body/memo_audit.rs +++ b/crates/ui-widgets/tests/it/body/memo_audit.rs @@ -5,7 +5,7 @@ use craft_ui::buttons::Button; use craft_ui::contract::{ContainerProps, Lazy, MemoProps, MemoView, Props as _, Scroll, Show as _, ShowContainer as _}; use craft_ui::testing::layout::in_region; -use craft_ui::testing::pointer::moved; +use craft_ui::testing::pointer::{moved, wheel}; use craft_ui::testing::{ Frames, MemoRun, audit_replays, in_audit_run, last_frame_memo_runs, last_pass_memo_runs, last_pass_runs, last_pass_text_layouts, trial, }; @@ -235,3 +235,97 @@ fn a_view_whose_body_measured_in_its_run_replays_as_a_run_in_a_pass_that_does_no let ran: Vec<_> = last_frame_memo_runs(&frames.ctx).into_iter().filter(|(_, _, run)| *run == MemoRun::Ran).collect(); assert!(ran.is_empty(), "the view settles: {ran:?}"); } + +/// A card: one pressable member of the arena around it. +#[derive(MemoProps)] +struct Card { + at: usize, +} + +fn card(props: &mut Card, ui: &mut Ui) { + Button::new(format!("Card {}", props.at)).show(ui); +} + +/// Where the pointer rests to scroll the cards: over their body, beside every card. +const BESIDE: Pos2 = Pos2::new(140.0, 120.0); + +/// Cards in a scrolling arena body, under a lead of `lead` points. +fn cards(lead: f32) -> impl FnMut(&mut Ui) { + move |ui: &mut Ui| { + in_region(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(160.0, 200.0)), |ui| { + Slab::new(ContainerProps::new().scroll(Scroll::vertical()).arena(true)).id_salt("cards").show(ui, |ui, _| { + ui.add_space(lead); + for at in 0..12 { + MemoView::new(("card", at), Card { at }, card).show(ui); + } + }); + }); + } +} + +/// The cards that replayed in the last frame. +fn replayed(frames: &Frames) -> usize { + last_frame_memo_runs(&frames.ctx).into_iter().filter(|(name, _, run)| name.contains("Card") && *run == MemoRun::Replayed).count() +} + +/// A wheel step of a fraction of a pixel moves the content by whole pixels: the cards it moves replay, and each replay +/// paints where a run there paints (paint snaps to the pixel grid, so a record moved by a fraction would not). +#[trial] +fn cards_a_fractional_wheel_step_moves_replay_where_they_would_run() { + let mut frames = Frames::new(); + audit_replays(&frames.ctx, true); + let mut show = cards(40.0); + frames.frame(vec![moved(BESIDE)], &mut show); + frames.idle(SETTLE, &mut show); + frames.frame(vec![wheel(-10.4)], &mut show); + assert!(replayed(&frames) > 0, "the moved cards replayed"); + frames.idle(SETTLE, &mut show); +} + +/// A card scrolled partly out of the view replays through the view's clip: the members it joins its arena with are +/// cut by the clip as a run there cuts them. +#[trial] +fn a_card_scrolled_partly_out_of_view_replays_through_the_views_clip() { + let mut frames = Frames::new(); + audit_replays(&frames.ctx, true); + let mut show = cards(0.0); + frames.frame(vec![moved(BESIDE)], &mut show); + frames.idle(SETTLE, &mut show); + frames.frame(vec![wheel(-10.0)], &mut show); + assert!(replayed(&frames) > 0, "the moved cards replayed"); + frames.idle(SETTLE, &mut show); +} + +/// A view that holds a card below a gap of its own. +#[derive(MemoProps)] +struct Holder { + gap: f32, +} + +fn holder(props: &mut Holder, ui: &mut Ui) { + ui.add_space(props.gap); + MemoView::new("held", Card { at: 0 }, card).show(ui); +} + +/// An inner view that replays moved inside the outer view's run is in the outer record where it replayed: the outer +/// view's later replay joins the card's member at the card's new place. +#[trial] +fn a_view_replayed_moved_inside_a_run_is_kept_at_its_new_place() { + let mut frames = Frames::new(); + audit_replays(&frames.ctx, true); + let gap = Cell::new(0.0); + let mut show = |ui: &mut Ui| { + in_region(ui, Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(160.0, 200.0)), |ui| { + Slab::new(ContainerProps::new().arena(true)).id_salt("holder").show(ui, |ui, _| { + MemoView::new("holder", Holder { gap: gap.get() }, holder).show(ui); + }); + }); + }; + frames.idle(SETTLE, &mut show); + gap.set(15.0); + frames.frame(vec![], &mut show); + let runs = last_frame_memo_runs(&frames.ctx); + assert!(runs.iter().any(|(name, _, run)| name.contains("Holder") && *run == MemoRun::Ran), "the holder ran: {runs:?}"); + assert!(runs.iter().any(|(name, _, run)| name.contains("Card") && *run == MemoRun::Replayed), "the card replayed in it: {runs:?}"); + frames.idle(SETTLE, &mut show); +}