diff --git a/crates/ui-contract/src/contract/body/inherit.rs b/crates/ui-contract/src/contract/body/inherit.rs index c1a1002cb..c0c96ac72 100644 --- a/crates/ui-contract/src/contract/body/inherit.rs +++ b/crates/ui-contract/src/contract/body/inherit.rs @@ -54,7 +54,9 @@ impl BodyHooks<'_> { 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); - scoped.inherited_mut().values.set(crate::contract::overlay::LayerFloor(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))); } // The container's own `enabled(false)` greys everything inside it, as a scope would. scoped.enabled &= cx.core.resolved.enabled; diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index c5d682cb9..2bca49614 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -242,8 +242,7 @@ impl<'b> BodyHooks<'b> { pub(super) fn wanted_width(&self, props: &ContainerProps) -> Option { self.measured_body.map(|body| { let (header, end) = (self.measured_header.unwrap_or(Vec2::ZERO), self.measured_end.unwrap_or(Vec2::ZERO)); - let wanted = self.regions_size(props, body, header, self.measured_footer, end).x.max(self.least_width); - overlay_of(props).map_or(wanted, |overlay| wanted.max(crate::contract::overlay::anchor_floor(overlay))) + self.regions_size(props, body, header, self.measured_footer, end).x.max(self.least_width) }) } diff --git a/crates/ui-widgets/tests/it/popover/popup.rs b/crates/ui-widgets/tests/it/popover/popup.rs index 0743b4206..8e27ccdd1 100644 --- a/crates/ui-widgets/tests/it/popover/popup.rs +++ b/crates/ui-widgets/tests/it/popover/popup.rs @@ -104,6 +104,26 @@ impl DisplayOrder for Rows { impl ItemSource for Rows {} +/// A short options list fills its opener's width without padding making the popup wider than the opener. +#[trial] +fn a_popup_list_with_short_rows_fits_its_wide_opener() { + let mut open = true; + 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, _| { + List::::options("Choices").show_items(ui, &Rows, &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 popup = fixture.by_label("Popup").expect("the popup").rect; + let row = fixture.by_label(&row(0)).expect("a row").rect; + assert!(popup.width() >= opener.width() - 1.0, "the popup is at least as wide as its opener: {popup:?} {opener:?}"); + assert!(popup.width() <= opener.width() + 1.0, "padding does not widen the popup: {popup:?} {opener:?}"); + assert!(row.left() >= popup.left() && row.right() <= popup.right(), "the row stays inside the popup: {row:?} {popup:?}"); +} + /// A scrolling page taller than the window: an "Open" button that opens a popup list of `ROWS` rows while `open`, then /// `PAGE` buttons. The rows the list activates go to `activated`. fn page(open: Rc>, activated: Rc>>) -> Fixture {