From 6b634df42e47fdb1e02e22346428d2bed62cd499 Mon Sep 17 00:00:00 2001 From: aiongg Date: Fri, 20 Nov 2026 05:38:29 +0000 Subject: [PATCH] craft-ui: a scrolling body's real run decides its scroll from the content this pass measured The measuring run records the content's extent, and the real run takes it at step 5: whether the content overflows (the wheel, the bars, the strips) and how far it scrolls. A bar too short for its members takes the wheel from its first frame. --- crates/ui-contract/src/contract/body.rs | 3 ++- crates/ui-contract/src/contract/body/hooks.rs | 13 +++++++--- crates/ui-widgets/tests/it/bars/overflow.rs | 26 +++++++++++++++++++ 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/crates/ui-contract/src/contract/body.rs b/crates/ui-contract/src/contract/body.rs index ab1d798f2..439f4ba52 100644 --- a/crates/ui-contract/src/contract/body.rs +++ b/crates/ui-contract/src/contract/body.rs @@ -103,7 +103,8 @@ pub(super) struct BodyHooks<'b> { scroll_bound: bool, moved: bool, last: BodyView, - /// The content's extent from the last run that laid it out, kept with the offset it bounds (`ScrollState`). + /// The content's extent. Step 4 and the measuring run read the last pass's, kept with the offset it bounds + /// (`ScrollState`); from step 5 the real run takes what this pass's measuring run found. last_extent: (f32, f32), /// The height of a scroll strip (`ScrollIndicator::Strips`). strip: f32, diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index ca6ac1c80..f276a0480 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -344,12 +344,13 @@ impl ShapeHooks for BodyHooks<'_> { let mut stage = MemoryStage::open_peek(&ctx, id); let mut fresh = self.measuring_hooks(fading); crate::contract::run::measured_extent(comp, &mut fresh, at, &mut stage, style, shown); - // The strips show in the pass whose content first overflows: the measuring run's content decides them. - if let Some(extent) = fresh.extent.filter(|_| scroll_of(&props).is_some_and(|scroll| scroll.has_strips())) { - self.last_extent = (extent.x, extent.y); - } } let took = |key: Id| layout_memory::measured(&ctx, key).map(|measured| measured.took); + // The real run decides the scroll from the content this pass measured: whether it overflows (the wheel, the + // bars, the strips) and how far it scrolls. + if let Some(extent) = took(sub(id, "extent")).filter(|_| scroll_of(&props).is_some()) { + self.last_extent = (extent.x, extent.y); + } self.measured_body = took(id); self.measured_header = props.header.and_then(|_| took(sub(id, "header"))); self.measured_footer = props.footer.as_ref().and_then(|_| took(sub(id, "footer"))).unwrap_or(Vec2::ZERO); @@ -824,6 +825,10 @@ impl BodyHooks<'_> { if let Some(content) = content { record(sub(id, "content"), content); } + // What a scrolling body's content took, for the real run's scroll (`pass_extent`). + if let Some(extent) = self.extent.filter(|_| scroll_of(&props).is_some()) { + record(sub(id, "extent"), extent); + } // The rows of a content-sized body reach past the columns of the group around by as much more as the // container does: a box's padding, or a row's members after its labelled one. The group measures its extent // from these, at its widest columns. diff --git a/crates/ui-widgets/tests/it/bars/overflow.rs b/crates/ui-widgets/tests/it/bars/overflow.rs index aff54feab..6320639b0 100644 --- a/crates/ui-widgets/tests/it/bars/overflow.rs +++ b/crates/ui-widgets/tests/it/bars/overflow.rs @@ -15,6 +15,7 @@ use craft_ui::pickers::Select; use craft_ui::primitives::TextRun; use craft_ui::runtime::commands::EditCommands; use craft_ui::testing::layout::in_region; +use craft_ui::testing::pointer; use craft_ui::testing::{Fixture, Frames, MemoRun, last_frame_memo_runs, trial}; use craft_ui::value_drag::Slider; use egui::{Pos2, Rect, Shape, Ui, Vec2}; @@ -226,6 +227,31 @@ fn a_settled_bar_replays_the_rule_it_took_back() { } } +/// A bar too short for its members knows it overflows from the content it measured in its first frame: a wheel turned +/// over it in the next frame scrolls the button it hides into view. +#[trial] +fn a_bar_too_short_for_its_members_takes_the_wheel_from_its_first_frame() { + let (_, wide) = rule_bar_shapes(900.0); + let width = cutting_width(wide); + let rects = Cell::new((Rect::NOTHING, Rect::NOTHING)); + let scene = |ui: &mut Ui| rule_bar(ui, width, &rects); + let mut frames = Frames::new(); + frames.frame(vec![pointer::moved(Pos2::new(width / 2.0, 40.0))], scene); + let hidden = rects.get().1; + assert!(!hidden.is_positive(), "the first frame shows the second button at {hidden:?} in a bar that ends inside it"); + let wheel = egui::Event::MouseWheel { + unit: egui::MouseWheelUnit::Point, + delta: Vec2::new(-40.0, 0.0), + phase: egui::TouchPhase::Move, + modifiers: egui::Modifiers::NONE, + source: egui::MouseWheelSource::Wheel, + }; + frames.frame(vec![wheel], scene); + frames.idle(2, scene); + let shown = rects.get().1; + assert!(shown.is_positive(), "the wheel in the second frame left the second button out of the bar"); +} + /// A bar `width` wide holding an icon button, a rule, a labelled checkbox, a rule and a second icon button; `rects` /// gets the first button's and the checkbox's rects. fn labelled_rule_bar(ui: &mut Ui, width: f32, rects: &std::cell::Cell<(Rect, Rect)>) {