From 64d6ffd08b35cf77f9be5f83a7892ab7445edb8a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 15:35:15 -0800 Subject: [PATCH] ui-contract: a Row given its room at its center or end is placed from its content's width A Row body that places its block at its center or end reads the width its content took in the pass's measuring run. A body given its room skipped the body size comparison, so content that changed width kept the old placement and could run past the room. Such a body now compares its body size again, and the frame runs a second pass when the width changes. --- crates/ui-contract/src/contract/body/hooks.rs | 7 +++- .../ui-widgets/tests/it/layout/fixed_body.rs | 42 +++++++++++++++++-- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index fce3150d40..0a50385e94 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -52,8 +52,11 @@ impl ShapeHooks for BodyHooks<'_> { fn regions(&self, id: Id) -> Vec<(Id, Vec2)> { // A body that takes its room (`takes_room`) and does not scroll takes it whatever its content takes: a change in // what its content took leaves the container's size as it was, so it does not run the pass again. A share of a - // scrolling view takes at least its content, so its body is still compared. - let body_fixed = self.takes_room && scroll_of(&self.props).is_none(); + // scrolling view takes at least its content, so its body is still compared. A `Row` that places its block at its + // center or end places it from the width its content took (`run_row`), so its body is still compared too. + let places_from_content = + matches!(self.props.layout, layout::BodyLayout::Row { align: layout::RowAlign::Center | layout::RowAlign::End, .. }); + let body_fixed = self.takes_room && scroll_of(&self.props).is_none() && !places_from_content; [ ("body", self.body_size.filter(|_| !body_fixed)), ("header", self.header_size), diff --git a/crates/ui-widgets/tests/it/layout/fixed_body.rs b/crates/ui-widgets/tests/it/layout/fixed_body.rs index 3097234c08..0d01048438 100644 --- a/crates/ui-widgets/tests/it/layout/fixed_body.rs +++ b/crates/ui-widgets/tests/it/layout/fixed_body.rs @@ -1,10 +1,11 @@ //! A body given both axes takes its room whatever its content takes: when what its content takes changes (a canvas //! whose overlay grows a line under the pointer), the frame runs once and the body keeps its rect. A body that fills a -//! share of a scrolling view takes at least its content, so a change in its content still moves what follows it. +//! share of a scrolling view takes at least its content, so a change in its content still moves what follows it. A +//! `Row` given its room that places its block at its center or end places it from the width its content takes now. use craft_ui::buttons::Button; -use craft_ui::contract::{Props as _, Scroll, Show as _, ShowContainer as _, Sizing}; -use craft_ui::layout_components::Stack; +use craft_ui::contract::{Props as _, RowAlign, Scroll, Show as _, ShowContainer as _, Sizing}; +use craft_ui::layout_components::{Row, Stack}; use craft_ui::testing::{Fixture, trial}; use egui::{Pos2, Rect, Vec2}; use std::cell::Cell; @@ -46,6 +47,41 @@ fn a_change_in_the_content_of_a_body_given_its_room_runs_the_frame_once() { } } +#[trial] +fn a_row_given_its_room_places_its_block_from_the_content_it_now_takes() { + for align in [RowAlign::Center, RowAlign::End] { + let width = Rc::new(Cell::new(20.0_f32)); + let shown = Rc::new(Cell::new([Rect::NOTHING; 2])); + let (asked, out) = (Rc::clone(&width), Rc::clone(&shown)); + let mut fixture = Fixture::new(move |ui| { + Stack::new().fill().show_at(ui, ui.available_rect_before_wrap(), |ui, _| { + let room = Rect::from_min_size(ui.cursor().min, Vec2::new(200.0, 120.0)); + let mut block = Rect::NOTHING; + let body = Row::new().sizing(Sizing::Fill).align(align).show_at(ui, room, |ui, _| { + block = ui.allocate_exact_size(Vec2::new(asked.get(), 10.0), egui::Sense::hover()).0; + }); + if !ui.is_sizing_pass() { + out.set([body.output.rect, block]); + } + }); + }); + fixture.room(Vec2::new(300.0, 300.0)).settle(); + fixture.idle(3); + for (step, next) in [180.0, 20.0, 180.0].into_iter().enumerate() { + width.set(next); + fixture.idle(1); + let [body, block] = shown.get(); + let spare = body.width() - next; + let lead = if align == RowAlign::Center { spare / 2.0 } else { spare }; + assert!( + (block.left() - (body.left() + lead)).abs() <= SNAP && block.right() <= body.right() + SNAP, + "{align:?} step {step}: the block of {next} sits {lead} into {body:?}, not at {block:?}" + ); + fixture.idle(2); + } + } +} + #[trial] fn a_change_in_the_content_of_a_share_of_a_scrolling_view_moves_what_follows_it() { let height = Rc::new(Cell::new(10.0_f32));