diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index a92e7820ed..eabfe86f2b 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -308,6 +308,7 @@ impl ShapeHooks for BodyHooks<'_> { let env = Env::of(ui); self.given = self.given_axes(props, &env, at); self.fixed_height = self.fixed_height(props, at); + self.least_width = self.least_width_of(props, &env, scx.tokens); self.passes = self.passes_width(props, &env, at); self.held = self.held(props, &env); self.across = self.held && matches!(env.column, Some(ColumnSweep::Column(..))); @@ -1904,7 +1905,6 @@ impl BodyHooks<'_> { let start = layout::flow_start(at.ui.ctx(), at.env); self.after_rule = start == Some(true); self.rules = RuleBands::of(&props, at.env, crate::runtime::egui::scale::pixels_per_point(at.ui.ctx()), start); - self.least_width = least_of(&at.env.tokens, props.least_width); self.room = match at.width { Some(width) => Vec2::new(width, props.room_height.unwrap_or(f32::INFINITY)), None => self.mount_room(&props, at.ui, id, at.at, at.scx.tokens), @@ -1912,6 +1912,7 @@ impl BodyHooks<'_> { self.given = self.given_axes(&props, at.env, at.at); self.given.1 |= self.given_room(&props, at.env, at.at, at.ui.ctx(), id); self.fixed_height = self.fixed_height(&props, at.at); + self.least_width = self.least_width_of(&props, at.env, at.scx.tokens); self.passes = self.passes_width(&props, at.env, at.at); self.held = self.held(&props, at.env); self.across = self.held && matches!(at.env.column, Some(ColumnSweep::Column(..))); @@ -2028,12 +2029,10 @@ impl BodyHooks<'_> { /// The extent a measuring run reports from what its regions `took`. fn measured_took(&mut self, took: Vec2, pad: Vec2, vertical: bool) -> Vec2 { - // A layer hung from an opener or a rect is at least as wide as its overlay asks for, in the measuring run too, - // so the rows of a content-sized list inside it are placed at that width. - let took = Vec2::new(overlay_of(&self.props).map_or(took.x, |overlay| took.x.max(crate::contract::overlay::anchor_floor(overlay))), took.y); // The least width holds in the measure a parent places it by, as in its own extent (`wanted_width`): a row // measuring a side menu among its children gave it only its content's width, so the menu shrank to its widest - // row's name. + // row's name. A layer's anchor floor is part of it, so the rows of a content-sized list inside are placed at + // that width. let took = Vec2::new(took.x.max(self.least_width + pad.x), took.y); self.overflow = (took.x - self.room.x).max(0.0); // A body wider than the room overflows it, or scrolls along it; either way the container keeps to the room. diff --git a/crates/ui-contract/src/contract/body/inherit.rs b/crates/ui-contract/src/contract/body/inherit.rs index c16d15959c..ab692c88dd 100644 --- a/crates/ui-contract/src/contract/body/inherit.rs +++ b/crates/ui-contract/src/contract/body/inherit.rs @@ -55,10 +55,8 @@ impl BodyHooks<'_> { // opener's env as in the run under the layer's. if matches!(props.mount, Mount::Layer(_) | Mount::Viewport { .. } | Mount::FullWindow) { scoped.start_layer(); - let floor = super::placement::overlay_of(props).map_or(0.0, crate::contract::overlay::anchor_floor); - // The anchor floors the layer's outer width; its regions have less room after the layer's padding. - let horizontal_padding = self.pads(props, &scoped.tokens).x; - scoped.inherited_mut().values.set(crate::contract::overlay::LayerFloor((floor - 2.0 * horizontal_padding).max(0.0))); + let floor = self.layer_floor(props, &scoped.tokens); + scoped.inherited_mut().values.set(crate::contract::overlay::LayerFloor(floor)); } // The container's own `enabled(false)` greys everything inside it, as a scope would. scoped.enabled &= core.resolved.enabled; diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index fb634c8430..6d51cd0bd8 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -2,7 +2,7 @@ use super::BodyHooks; use super::placement::overlay_of; -use super::regions::{RegionSizes, end_full_height, footer_in_body, footer_in_end}; +use super::regions::{RegionSizes, end_full_height, footer_in_body, footer_in_end, least_of}; use super::scroll::scroll_of; use crate::contract::run::ShowAt; use crate::contract::{ContainerProps, Mount, PadEdges, PaneEdge, ShellPane, Sizing}; @@ -279,6 +279,20 @@ impl<'b> BodyHooks<'b> { } } + /// The least width of the regions: `ContainerProps::least_width`, and on a layer hung from an opener or a rect the + /// width its overlay asks for (`anchor_floor`) less the padding, so the layer is as wide whether a run measures it + /// or places it, whatever its content took (an empty result too). + pub(super) fn least_width_of(&self, props: &ContainerProps, env: &Env, tokens: &UiTokens) -> f32 { + least_of(&env.tokens, props.least_width).max(self.layer_floor(props, tokens)) + } + + /// The anchor floor of a layer's outer width (`anchor_floor`), as the room it leaves its regions inside the padding; + /// 0 outside a layer. + pub(super) fn layer_floor(&self, props: &ContainerProps, tokens: &UiTokens) -> f32 { + let floor = overlay_of(props).map_or(0.0, crate::contract::overlay::anchor_floor); + (floor - 2.0 * self.pads(props, tokens).x).max(0.0) + } + /// The width the regions ask for in a content-sized container: what they took together in this pass's measuring run. pub(super) fn wanted_width(&self, props: &ContainerProps) -> Option { self.measured_body.map(|body| { diff --git a/crates/ui-widgets/tests/it/popover/popup.rs b/crates/ui-widgets/tests/it/popover/popup.rs index d6909a1f23..205dd1113c 100644 --- a/crates/ui-widgets/tests/it/popover/popup.rs +++ b/crates/ui-widgets/tests/it/popover/popup.rs @@ -4,7 +4,7 @@ use crate::overlay::kit::{Leaf, Opened, Opener, Seen}; use craft_behavior::{DisplayOrder, ItemSource}; use craft_ui::buttons::Button; use craft_ui::contract::{ - Bind, CollectionEvent, Overlay, Props as _, Role, Scroll, Show as _, ShowCollection as _, ShowContainer as _, Trigger, clicked, + Bind, CollectionEvent, Overlay, Props as _, Role, Scroll, Show as _, ShowCollection as _, ShowContainer as _, Sizing, Trigger, clicked, }; use craft_ui::display::Text; use craft_ui::layout_components::Stack; @@ -194,6 +194,62 @@ fn a_popup_list_with_short_rows_fits_its_wide_opener() { assert!(row.left() >= popup.left() && row.right() <= popup.right(), "the row stays inside the popup: {row:?} {popup:?}"); } +/// The first `0` of the trial rows: a search result. +struct Found(Rc>); + +impl DisplayOrder for Found { + fn len(&self) -> usize { + self.0.get() + } + + fn key(&self, at: usize) -> usize { + at + } + + fn text(&self, at: usize) -> Option> { + Some(std::borrow::Cow::Owned(row(at))) + } +} + +impl ItemSource for Found {} + +/// A popup list whose result empties stays as wide as its opener, and when rows come back they get that width and +/// show: neither the empty pass's size nor its measurement carries over. +#[trial] +fn a_popup_list_whose_result_empties_and_fills_again_shows_its_rows_across_the_opener() { + let mut open = true; + let found = Rc::new(Cell::new(3)); + let shown = Rc::clone(&found); + let anchor = Rect::from_min_size(Pos2::new(100.0, 100.0), Vec2::new(240.0, 30.0)); + let mut fixture = Fixture::new(move |ui| { + let opener = Button::new("Open").show_at(ui, anchor).response; + PopupList::new("Popup", Bind::bound(&mut open)).opened_by(&opener).show(ui, |ui, _| { + let count = shown.get(); + List::::options("Choices").empty("No matches").sizing(Sizing::FillWidth).rows_stamp(count as u64).show_items( + ui, + &Found(Rc::clone(&shown)), + &mut |at, _| ListRow::new(row(*at)).option(), + ); + }); + }); + fixture.room(Vec2::new(600.0, 400.0)).settle(); + let opener = fixture.by_label("Open").expect("the opener").rect; + let before = fixture.by_label(&row(0)).expect("a row before the result empties").rect; + found.set(0); + fixture.settle(); + assert!(fixture.by_role(Role::LIST_BOX_OPTION).is_empty(), "an empty result shows no rows"); + let popup = fixture.by_label("Popup").expect("the popup stays open").rect; + assert!(popup.width() >= opener.width() - 1.0, "an empty popup is at least as wide as its opener: {popup:?} {opener:?}"); + found.set(3); + fixture.settle(); + let popup = fixture.by_label("Popup").expect("the popup").rect; + let after = fixture.by_label(&row(0)).expect("the rows show again").rect; + assert!(after.width() >= before.width() - 1.0, "the row is as wide as before the result emptied: {after:?} {before:?}"); + assert!(after.left() >= popup.left() && after.right() <= popup.right(), "the row stays inside the popup: {after:?} {popup:?}"); + let last = fixture.by_label(&row(2)).expect("the last row shows").rect; + assert!(popup.contains_rect(last), "the popup grows back around its rows: {last:?} {popup:?}"); +} + /// A document field's options start at its edge, so the selection fills the field's width. #[trial] fn a_document_popup_list_aligns_its_rows_with_the_opener() {