diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index f2323cc86..51805f6aa 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -233,9 +233,10 @@ impl<'b> BodyHooks<'b> { (_, ShowAt::Flow) => { let room = crate::layout::flow_room(ui); // A row that shrinks its children to fit gives a body among them a width: it measures and shows - // in that room. + // in that room. A body that fills a bounded row takes the rest of it instead. + let fills = self.sizing(props) != Sizing::Content && room.x.is_finite(); let cap = match Env::of(ui).shrink { - Some(crate::layout::Shrink::Apply(row)) => crate::layout::shrink_cap(ui.ctx(), row, id), + Some(crate::layout::Shrink::Apply(row)) if !fills => crate::layout::shrink_cap(ui.ctx(), row, id), _ => None, }; let room = Vec2::new(cap.map_or(room.x, |cap| room.x.min(cap)), props.room_height.unwrap_or(room.y)); diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index 943bdf0f1..eb238c477 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -442,6 +442,8 @@ pub(crate) trait Erased { fn in_flow(&self) -> bool; /// Whether the shape fills the width of the room it is shown in (a body not sized from its content). fn fills_width(&self) -> bool; + /// How much wider than the size it measured to the shape's content wants to be (`ShapeHooks::overflow`). + fn overflow(&self) -> f32; fn sized(&self, ui: &Ui, id: Id, size: Vec2) -> Vec2; fn arena(&self) -> bool; fn extent(&mut self, ui: &Ui, at: ShowAt, id: Id, scx: &StyleCx<'_>) -> Vec2; @@ -1121,8 +1123,8 @@ pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option crate::layout::shrink_cap(&ctx, row, id), _ => None, @@ -1219,6 +1221,12 @@ pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option crate::layout::report_least(&ctx, row, id, size.x), _ => {} } + // A body that fills the rest of a shrinking row needs at least its content: the row counts it at that width, so + // the units narrow to leave it, and it takes what they leave. + if let Some(crate::layout::Shrink::Count(row)) = env.shrink.filter(|_| fill_body) { + let content = measured.x + t.overflow(); + crate::layout::report_shrink(&ctx, row, id, size.x.min(content), content, Some(content)); + } // A component shown at a rect fills it: the rect is its size, and a leaf that measures nothing (a content // painter) still shows there. let inert = match at { diff --git a/crates/ui-contract/src/contract/run/typed.rs b/crates/ui-contract/src/contract/run/typed.rs index 563a19544..a796c0bf0 100644 --- a/crates/ui-contract/src/contract/run/typed.rs +++ b/crates/ui-contract/src/contract/run/typed.rs @@ -171,6 +171,10 @@ impl Erased for Typed<'_, '_, C> { self.hooks.fills_width(self.comp.get_ref()) } + fn overflow(&self) -> f32 { + self.hooks.overflow() + } + fn sized(&self, ui: &Ui, id: Id, size: Vec2) -> Vec2 { self.hooks.sized(self.comp.get_ref(), ui, id, size) } diff --git a/crates/ui-panels/tests/it/sections/footer_row.rs b/crates/ui-panels/tests/it/sections/footer_row.rs new file mode 100644 index 000000000..0b6fe94fa --- /dev/null +++ b/crates/ui-panels/tests/it/sections/footer_row.rs @@ -0,0 +1,42 @@ +//! A footer row: a row of view modes, a thumbnail slider and a footer bar narrows its slider to leave the bar its +//! actions. + +use craft_behavior::IconId; +use craft_ui::choice::Segmented; +use craft_ui::contract::{Bind, Entry, RowAlign, Show as _, ShowContainer as _}; +use craft_ui::layout_components::Row; +use craft_ui::sections::{FooterAction, FooterBar}; +use craft_ui::testing::{Fixture, trial}; +use craft_ui::tile_grid::{ThumbnailSlider, TileZoom}; +use egui::{Rect, Vec2}; +use std::cell::RefCell; +use std::rc::Rc; + +/// The slider's rect and the rects of the footer bar's actions, in a footer row `width` wide. +fn footer(width: f32) -> (Rect, Vec) { + let seen = Rc::new(RefCell::new((Rect::NOTHING, Vec::new()))); + let out = Rc::clone(&seen); + let mut fixture = Fixture::new(move |ui| { + let modes = [Entry::new(0u8, "List").icon(IconId::new("list")), Entry::new(1, "Icons").icon(IconId::new("grid"))]; + let mut zoom = 1.0; + Row::new().fill_width().align(RowAlign::End).show(ui, |ui, _| { + Segmented::new("View", Bind::given(0u8), &modes).show(ui); + let slider = ThumbnailSlider::new("Size", Bind::bound(&mut zoom), TileZoom::Ladder).show(ui).response.rect; + let actions = ["new_folder", "trash", "more"].map(|icon| FooterAction::new(IconId::new(icon), icon)); + let bar = FooterBar::actions(actions).show_footer(ui); + *out.borrow_mut() = (slider, bar.responses.iter().map(|response| response.rect).collect()); + }); + }); + fixture.room(Vec2::new(width, 60.0)).settle(); + seen.borrow().clone() +} + +#[trial] +fn a_footer_row_narrows_its_slider_to_leave_the_footer_bar_its_actions() { + let (wide, _) = footer(900.0); + let (slider, actions) = footer(300.0); + let last = actions.last().copied().unwrap_or(Rect::NOTHING); + assert!(last.max.x <= 300.0 + 0.5, "the last action ends at {} past a 300-point row", last.max.x); + assert!(slider.width() < wide.width() - 0.5, "the slider kept its width {} in a narrow row", slider.width()); + assert!(slider.max.x <= actions[0].min.x + 0.5, "the slider {slider:?} runs into the actions {actions:?}"); +} diff --git a/crates/ui-panels/tests/it/sections/mod.rs b/crates/ui-panels/tests/it/sections/mod.rs index c3b718731..10e4c8bb7 100644 --- a/crates/ui-panels/tests/it/sections/mod.rs +++ b/crates/ui-panels/tests/it/sections/mod.rs @@ -1,4 +1,5 @@ //! Reorderable sections. +mod footer_row; mod layout; mod reorderable; diff --git a/crates/ui-widgets/src/tile_grid/slider.rs b/crates/ui-widgets/src/tile_grid/slider.rs index 1076dd801..3b9427b8d 100644 --- a/crates/ui-widgets/src/tile_grid/slider.rs +++ b/crates/ui-widgets/src/tile_grid/slider.rs @@ -7,7 +7,7 @@ use crate::contract::{ }; use crate::runtime::env::Label; use crate::style::{ComponentStyle, StyleCx}; -use craft_behavior::{IconId, Metrics, Mixed, NavShape, PartKey, Proposal, Roving, Sense}; +use craft_behavior::{IconId, Metrics, Mixed, NavShape, PartKey, Roving, Sense}; use egui::{Rect, Vec2}; use std::ops::RangeInclusive; @@ -162,7 +162,7 @@ impl Component for ThumbnailSlider<'_> { fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { let button = cx.face(IconButton::new(IconId::new(SMALLER_ICON))); let shown = cx.value().copied().map_or(Mixed::Mixed, Mixed::Same); - let track = cx.child("slider", &mut self.track(shown, cx.label()), Proposal::Ideal); + let track = cx.child("slider", &mut self.track(shown, cx.label()), cx.proposal); let width = 2.0 * (button.x + cx.gap()) + track.x; Vec2::new(cx.room.map_or(width, |room| width.min(room)), button.y.max(track.y)) } diff --git a/crates/ui-widgets/src/value_drag/slider.rs b/crates/ui-widgets/src/value_drag/slider.rs index c5b01680d..ec09fe951 100644 --- a/crates/ui-widgets/src/value_drag/slider.rs +++ b/crates/ui-widgets/src/value_drag/slider.rs @@ -13,7 +13,7 @@ use crate::primitives::{PaintShape, StrokeWidth}; use crate::runtime::env::Label; use crate::runtime::focus::{ActivateKeys, KeepKeys, KeyUse}; use crate::style::{ComponentStyle, FocusLook, StyleColor, StyleCx, TextLook, TrackLook}; -use craft_behavior::{Flow, HitLevel, HitPriority, Metrics, PartKey, PressPhase, Sense}; +use craft_behavior::{Flow, HitLevel, HitPriority, Metrics, PartKey, PressPhase, Proposal, Sense}; use craft_theme::Stateful; use egui::{Pos2, Rect, Vec2}; use std::ops::RangeInclusive; @@ -166,7 +166,8 @@ impl Component for Slider<'_> { let accent = &colors.accent; let length = cx.tokens.size.length.track; SliderStyle { - metrics: Metrics::new().min_size(Vec2::new(length, control.height)), + // Where a row shrinks it, the track narrows to a short field's length. + metrics: Metrics::new().min_size(Vec2::new(length, control.height)).least_width(cx.tokens.size.length.short), focus: cx.focus_look(), mark_text: track::mark_text(cx), track: cx.track(), @@ -180,7 +181,8 @@ impl Component for Slider<'_> { fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 { self.marks.lay_out(&cx.style.mark_text, cx.style.space, |label| cx.env.resolve(label), |run| cx.layout(run).size()); - Vec2::new(cx.style.length, cx.icon() + self.marks.below(&cx.style.track)) + let along = if cx.proposal == Proposal::Min { 0.0 } else { cx.style.length }; + Vec2::new(along, cx.icon() + self.marks.below(&cx.style.track)) } fn sense(&self) -> Sense {