diff --git a/crates/ui-contract/src/contract/body/inherit.rs b/crates/ui-contract/src/contract/body/inherit.rs index 0892d21ce6..046a57e76d 100644 --- a/crates/ui-contract/src/contract/body/inherit.rs +++ b/crates/ui-contract/src/contract/body/inherit.rs @@ -50,6 +50,11 @@ impl BodyHooks<'_> { real: bool, ) { let (id, ctx) = (cx.core.id, &cx.core.ctx); + // A layer's content starts from its opener's scope with the layer-local fields fresh, in a measure under the + // opener's env as in the run under the layer's. + if matches!(props.mount, Mount::Layer(_) | Mount::Viewport { .. } | Mount::FullWindow) { + scoped.start_layer(); + } // The container's own `enabled(false)` greys everything inside it, as a scope would. scoped.enabled &= cx.core.resolved.enabled; if let Some(window) = window { diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 3315413628..98bbf6528e 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -645,7 +645,8 @@ pub enum Gap { /// Set by a bar for its body: its fields put `space.gap.item` between a caption and the control, a vertical divider /// takes a box wider than its line, a select fits its widest entry up to a field length, a rule shows only with the -/// member after it, and a row too long for its room narrows its last units first. +/// member after it, and a row too long for its room narrows its last units first. A layer or pane opened from the bar +/// starts without it. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[doc(hidden)] pub struct InBar(pub bool); diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 5fcb68fd1a..a0423d9aed 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -293,6 +293,28 @@ impl Scoped { !self.content.0 && !self.content_column } + /// Starts a layer from its opener's scope: the inherited group stays, the layer-local fields start fresh, and the + /// layer is no line of a bar it was opened from. A layer's content measures and runs in this. + pub(crate) fn start_layer(&mut self) { + self.start_room(); + self.measure = None; + self.arena = None; + self.roving = None; + self.transform = None; + self.scroll_owner = None; + self.wheel = WheelScope::Plain; + self.unbounded = (false, false); + self.lazy = None; + self.pane_edge = None; + self.glyphs = GlyphRole::Control; + self.item = None; + self.row = None; + self.item_facts = None; + self.ink = Ink::default(); + self.surface = SurfaceKind::Background; + self.inherited_mut().values.set(crate::contract::InBar(false)); + } + /// Ends the layout room of the enclosing body: its flow grid and content column. pub(crate) fn start_room(&mut self) { self.grid = None; @@ -474,24 +496,7 @@ impl Env { /// The inherited group only; layer-local fields start fresh where the capture is installed. pub fn capture(&self) -> EnvCapture { - EnvCapture(self.derive(|s| { - s.start_room(); - s.measure = None; - s.arena = None; - s.roving = None; - s.transform = None; - s.scroll_owner = None; - s.wheel = WheelScope::Plain; - s.unbounded = (false, false); - s.lazy = None; - s.pane_edge = None; - s.glyphs = GlyphRole::Control; - s.item = None; - s.row = None; - s.item_facts = None; - s.ink = Ink::default(); - s.surface = SurfaceKind::Background; - })) + EnvCapture(self.derive(Scoped::start_layer)) } /// What component code sees of this env. @@ -517,6 +522,8 @@ impl Env { scoped.start_room(); scoped.lazy = None; scoped.measure = None; + // A pane opened from a bar is not the bar's line. + scoped.inherited_mut().values.set(crate::contract::InBar(false)); }) } diff --git a/crates/ui-widgets/tests/it/bars/bar.rs b/crates/ui-widgets/tests/it/bars/bar.rs index dd68da63ef..602c9e7e87 100644 --- a/crates/ui-widgets/tests/it/bars/bar.rs +++ b/crates/ui-widgets/tests/it/bars/bar.rs @@ -44,13 +44,13 @@ fn bar(width: f32, buttons: usize) -> (Rect, Rect) { #[trial] fn a_bar_too_narrow_for_its_members_elides_a_select_before_it_scrolls() { let (wide, _) = bar(900.0, 1); - let (select, button) = bar(240.0, 1); + let (select, button) = bar(200.0, 1); assert!(select.width() < wide.width() - SLACK, "the select kept its width {} in a narrow bar", select.width()); - assert!(button.max.x <= 240.0 + SLACK, "the last member ends at {} past the bar's end", button.max.x); + assert!(button.max.x <= 200.0 + SLACK, "the last member ends at {} past the bar's end", button.max.x); // Buttons do not shrink: when even the select's least leaves no room for them, the select keeps its width (at most // the bar's room) and the rest scrolls. - let (crowded, button) = bar(240.0, 6); - assert!(button.max.x > 240.0, "six buttons fit a 240-point bar: {button:?}"); + let (crowded, button) = bar(200.0, 6); + assert!(button.max.x > 200.0, "six buttons fit a 200-point bar: {button:?}"); assert!(crowded.width() > select.width() + SLACK, "the select narrowed to {} in a bar that scrolls anyway", crowded.width()); } @@ -87,9 +87,9 @@ fn captioned(width: f32, buttons: usize) -> Captioned { fn a_captioned_select_in_a_narrow_bar_narrows_with_its_caption_beside_it_before_the_bar_scrolls() { let wide = captioned(900.0, 1); assert!(!wide.probed, "a bar its members fit asked a body for its least width"); - let narrow = captioned(300.0, 1); + let narrow = captioned(260.0, 1); assert!(narrow.select.width() < wide.select.width() - SLACK, "the select kept its width {} in a narrow bar", narrow.select.width()); - assert!(narrow.last.max.x <= 300.0 + SLACK, "the last member ends at {} past the bar's end", narrow.last.max.x); + assert!(narrow.last.max.x <= 260.0 + SLACK, "the last member ends at {} past the bar's end", narrow.last.max.x); assert!( narrow.caption.max.x <= narrow.select.min.x + SLACK && narrow.caption.center().y > narrow.select.min.y, "the caption {:?} is not beside the select {:?}", @@ -97,8 +97,8 @@ fn a_captioned_select_in_a_narrow_bar_narrows_with_its_caption_beside_it_before_ narrow.select ); // Once the field is at its least, the rest scrolls. - let crowded = captioned(300.0, 6); - assert!(crowded.last.max.x > 300.0, "six buttons fit a 300-point bar: {:?}", crowded.last); + let crowded = captioned(260.0, 6); + assert!(crowded.last.max.x > 260.0, "six buttons fit a 260-point bar: {:?}", crowded.last); } /// The rects of an up button, a path, a search field and a count, in that order, in a tool bar `width` wide. diff --git a/crates/ui-widgets/tests/it/bars/overflow.rs b/crates/ui-widgets/tests/it/bars/overflow.rs index bcd0b16e37..a0614dbaa1 100644 --- a/crates/ui-widgets/tests/it/bars/overflow.rs +++ b/crates/ui-widgets/tests/it/bars/overflow.rs @@ -12,6 +12,7 @@ use craft_ui::contract::{Bind, Entry, Lazy, Props as _, Show as _, ShowContainer use craft_ui::display::{Divider, Text}; use craft_ui::form::Field; use craft_ui::pickers::Select; +use craft_ui::popover::Popover; use craft_ui::runtime::commands::EditCommands; use craft_ui::testing::layout::in_region; use craft_ui::testing::{Fixture, Frames, trial}; @@ -157,7 +158,7 @@ const SHORT: &[&str] = &["Normal", "Fixed"]; const LONG: &[&str] = &["Normal", "A select entry far longer than any field is wide, so the box elides it"]; /// The width of a small select of `entries`, in an options bar or in a plain column. -fn select_width(entries: &[&str], in_bar: bool) -> f32 { +fn select_width(entries: &'static [&'static str], in_bar: bool) -> f32 { let entries: Vec> = (0u8..).zip(entries).map(|(at, text)| Entry::new(at, *text)).collect(); let mut fixture = Fixture::new(move |ui| { let select = |ui: &mut Ui| { @@ -182,13 +183,32 @@ fn a_select_in_a_bar_fits_its_widest_entry_up_to_the_field_length() { assert!(long_plain > long_bar + SLACK, "outside a bar the long entry sets the width: {long_plain}, in a bar {long_bar}"); } -/// A bar `width` wide holding a button, a rule and a wider button; `rects` gets the two buttons' rects. +/// A layer opened from a bar is not the bar's line: a select in a popover a bar's button opens keeps the field length. +#[trial] +fn a_select_in_a_popover_opened_from_a_bar_keeps_the_field_length() { + let mut fixture = Fixture::new(|ui| { + let entries = [Entry::new(0u8, SHORT[0]), Entry::new(1, SHORT[1])]; + Bar::new("Options", BarKind::Options).show(ui, |ui, _| { + let opener = Button::new("More").show(ui).response; + Popover::new("More options", Bind::given(true)).opened_by(&opener).show(ui, |ui, _| { + Select::new("Inside", Bind::given(0u8), &entries).size(ControlSize::Sm).show(ui); + }); + }); + }); + fixture.room(Vec2::new(900.0, 300.0)).settle(); + let inside = fixture.by_label("Inside").expect("the select in the popover").rect.width(); + let plain = select_width(SHORT, false); + assert!((inside - plain).abs() <= SLACK, "the select in the popover is {inside} wide, a select outside any bar {plain}"); +} + +/// A bar `width` wide holding an icon button, a rule and a second icon button; `rects` gets the two buttons' rects. +/// Icon buttons keep their width, so nothing in the line narrows to make room. fn rule_bar(ui: &mut Ui, width: f32, rects: &std::cell::Cell<(Rect, Rect)>) { in_region(ui, Rect::from_min_size(Pos2::ZERO, Vec2::new(width, 80.0)), |ui| { Bar::new("Options", BarKind::Options).show(ui, |ui, _| { - let first = Button::new("First").show(ui).response.rect; + let first = IconButton::new(IconId::new("plus")).label("First").show(ui).rect; Divider::vertical().show(ui); - let second = Button::new("A second, wider button").show(ui).response.rect; + let second = IconButton::new(IconId::new("minus")).label("Second").show(ui).rect; rects.set((first, second)); }); }); @@ -209,30 +229,25 @@ fn rule_bar_shapes(width: f32) -> (Vec, (Rect, Rect)) { (shapes, rects.get()) } -/// A width at which the bar shows the second button only in part: it starts inside the bar and ends past it. -fn cutting_width() -> f32 { - (180..=520) - .step_by(11) - .map(|width| width as f32) - .find(|&width| { - let (_, (_, second)) = rule_bar_shapes(width); - second.min.x + 2.0 < width - 20.0 && second.max.x > width && second.width() < width - 40.0 - }) - .expect("no width put the second button across the bar's end") +/// A width at which the bar's view ends inside the second button of `rects`, as a wide bar lays it out. +fn cutting_width((_, second): (Rect, Rect)) -> f32 { + (second.min.x + second.max.x) / 2.0 } #[trial] fn a_rule_before_a_member_the_bar_hides_is_not_drawn() { let (shown, rects) = rule_bar_shapes(900.0); assert!(draws_rule(&shown, rects), "with both buttons whole the rule between them is drawn"); - let (cut, rects) = rule_bar_shapes(cutting_width()); + let (cut, (_, hidden)) = rule_bar_shapes(cutting_width(rects)); + assert!(!hidden.is_positive(), "the second button shows at {hidden:?} in a bar that ends inside it"); assert!(!draws_rule(&cut, rects), "the rule before the hidden button is drawn"); } /// A settled region holding a bar whose rule was taken back replays the bar without the rule. #[trial] fn a_settled_bar_replays_the_rule_it_took_back() { - let width = cutting_width(); + let (_, wide) = rule_bar_shapes(900.0); + let width = cutting_width(wide); let runs = std::cell::Cell::new(0); let rects = std::cell::Cell::new((Rect::NOTHING, Rect::NOTHING)); let scene = |ui: &mut Ui| { @@ -249,6 +264,6 @@ fn a_settled_bar_replays_the_rule_it_took_back() { runs.set(0); let shapes = frames.frame_shapes(scene); assert_eq!(runs.get(), 0, "settled frame {frame} ran the region"); - assert!(!draws_rule(&shapes, rects.get()), "settled frame {frame} replays the rule the run took back"); + assert!(!draws_rule(&shapes, wide), "settled frame {frame} replays the rule the run took back"); } }