diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index af0e9bba3..553a82c4d 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -194,7 +194,7 @@ fn run_shrinking(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) -> if !measure::active(&ctx) { return child::block(ui, None, layout, |ui| sweep(ui, Shrink::Apply(id), &mut body)).0; } - let room = flow_room(ui).x; + let room = shrink_room(ui); let first = crate::contract::keeping_ids(&ctx, || { let mut first = ui.new_child(egui::UiBuilder::new().layout(layout)); pass_state::with::(&ctx, |reports| reports.0.insert(id, Vec::new())); @@ -220,6 +220,17 @@ fn run_shrinking(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) -> } } +/// The width the leaves of a row in `ui` shrink to fit: the row's flow room, except in a body that scrolls along x, +/// where it is what the view shows from the row's start. There the leaves narrow to their least before the body +/// scrolls, and the body scrolls only when even their least does not fit. +fn shrink_room(ui: &Ui) -> f32 { + let env = Env::of(ui); + match env.lazy.filter(|view| env.unbounded.0 && view.axes.0) { + Some(view) => (view.rect.width() - view.start_of(ui.available_rect_before_wrap()).x).max(0.0), + None => flow_room(ui).x, + } +} + /// Runs `body` as the content of the `Stack` (along y) or unwrapped `Row` (along x) body `id` in `ui`, the block /// it is placed in, when the body is given that axis. /// diff --git a/crates/ui-widgets/tests/it/bars/bar.rs b/crates/ui-widgets/tests/it/bars/bar.rs new file mode 100644 index 000000000..921c77637 --- /dev/null +++ b/crates/ui-widgets/tests/it/bars/bar.rs @@ -0,0 +1,42 @@ +//! `Bar`: a body too wide for the bar shrinks its members before it scrolls. + +use craft_ui::bars::{Bar, BarKind}; +use craft_ui::buttons::Button; +use craft_ui::contract::{Bind, Entry, Show as _, ShowContainer as _}; +use craft_ui::pickers::Select; +use craft_ui::testing::{Fixture, trial}; +use egui::{Rect, Vec2}; +use std::cell::Cell; +use std::rc::Rc; + +const SLACK: f32 = 0.5; + +/// The rects of a select and of the last of `buttons` buttons after it, in an options bar `width` wide. +fn bar(width: f32, buttons: usize) -> (Rect, Rect) { + let seen = Rc::new(Cell::new((Rect::NOTHING, Rect::NOTHING))); + let out = Rc::clone(&seen); + let mut fixture = Fixture::new(move |ui| { + let entries = [Entry::new(0u8, "Current and Below, No Adjustments"), Entry::new(1, "All Layers")]; + Bar::new("Options", BarKind::Options).show(ui, |ui, _| { + let select = Select::new("Sample", Bind::given(1u8), &entries).show(ui).response.rect; + let mut last = Rect::NOTHING; + for at in 0..buttons { + last = Button::new(format!("Button {at}")).show(ui).rect; + } + out.set((select, last)); + }); + }); + fixture.room(Vec2::new(width, 80.0)).settle(); + seen.get() +} + +#[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); + 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); + // Buttons do not shrink: once the select is at its least, the rest scrolls. + let (_, button) = bar(240.0, 6); + assert!(button.max.x > 240.0, "six buttons fit a 240-point bar: {button:?}"); +} diff --git a/crates/ui-widgets/tests/it/bars/mod.rs b/crates/ui-widgets/tests/it/bars/mod.rs index a3217611b..259a34ee7 100644 --- a/crates/ui-widgets/tests/it/bars/mod.rs +++ b/crates/ui-widgets/tests/it/bars/mod.rs @@ -1,4 +1,5 @@ //! `Bar` and `BarItems`. +mod bar; mod breadcrumb; mod constraint_fields;