diff --git a/crates/ui-panels/src/sections/footer.rs b/crates/ui-panels/src/sections/footer.rs index dc3f8d3d9..fc78cdc29 100644 --- a/crates/ui-panels/src/sections/footer.rs +++ b/crates/ui-panels/src/sections/footer.rs @@ -591,19 +591,39 @@ impl FooterBar { } /// Shows the bar with the actions or buttons it was made with and reports what happened to them. - pub fn show_footer(mut self, ui: &mut Ui) -> FooterOutput { + pub fn show_footer(self, ui: &mut Ui) -> FooterOutput { + self.show_footer_with(ui, None::).0 + } + + /// Shows a bar of actions with the host's `leading` controls at its start edge, before the actions placed there by + /// [`at_start`](FooterBar::at_start), under the same rule: a preset panel's size slider. Returns what `leading` + /// returned on its last run. + pub fn show_footer_leading(self, ui: &mut Ui, leading: impl FnMut(&mut Ui) -> R) -> (FooterOutput, Option) { + self.show_footer_with(ui, Some(leading)) + } + + fn show_footer_with(mut self, ui: &mut Ui, mut leading: Option R>) -> (FooterOutput, Option) { let content = std::mem::take(&mut self.content); let (enter, column, start) = (self.enter, self.column, self.start); + if leading.is_some() { + self.props = self.props.layout(BodyLayout::row().align(RowAlign::SpaceBetween)); + } + let mut led = None; let shown = self.show(ui, |ui, _| match &content { Content::Body => Shown::default(), Content::Actions(actions) => { let mut shown = Shown::default(); let start = start.min(actions.len()); let from_end = || (start..actions.len()).rev(); - if start == 0 { + if start == 0 && leading.is_none() { show_actions(ui, actions, from_end(), &mut shown); } else { - Row::new().show(ui, |ui, _| show_actions(ui, actions, 0..start, &mut shown)); + Row::new().show(ui, |ui, _| { + if let Some(leading) = leading.as_mut() { + led = Some(leading(ui)); + } + show_actions(ui, actions, 0..start, &mut shown); + }); Row::new().show(ui, |ui, _| show_actions(ui, actions, from_end(), &mut shown)); } shown.responses.sort_by_key(|(at, _)| *at); @@ -664,14 +684,15 @@ impl FooterBar { }); let inner = shown.inner.unwrap_or_default(); let mods = if inner.picked.is_some() { crate::runtime::pointer::frame(ui.ctx()).mods } else { craft_keymap::Mods::NONE }; - FooterOutput { + let output = FooterOutput { response: shown.output, picked: inner.picked, dropped: inner.dropped, drop_hint: inner.drop_hint, responses: inner.responses.into_iter().map(|(_, response)| response).collect(), mods, - } + }; + (output, led) } } diff --git a/crates/ui-patterns/src/patterns/presets/browser.rs b/crates/ui-patterns/src/patterns/presets/browser.rs index 774f1d035..c7596a8e3 100644 --- a/crates/ui-patterns/src/patterns/presets/browser.rs +++ b/crates/ui-patterns/src/patterns/presets/browser.rs @@ -246,6 +246,10 @@ pub struct PresetBrowser<'a, K: Clone + Eq + Hash + Send + Sync + 'static> { /// body that scrolls (a dialog page) keeps its own scroll this way. #[prop] max_rows: Option, + /// The browser takes the presets' height, up to `room_height` (then they scroll), instead of the available + /// height: for a host that sizes to its content, such as a floating panel. + #[prop] + room_height: Option, } impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> PresetBrowser<'a, K> { @@ -275,6 +279,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> PresetBrowser<'a, K> { actions: Vec::new(), footer: None, max_rows: None, + room_height: None, } } @@ -554,6 +559,7 @@ impl PresetBrowser<'_, K> { actions, footer, max_rows, + room_height, } = self; let env = Env::of(ui); let words = env.words::(); @@ -939,7 +945,7 @@ impl PresetBrowser<'_, K> { commit(cx, &state, rows, &name); }; - let stack = Stack::new().fill(); + let stack = room_height.map_or_else(|| Stack::new().fill(), |height| Stack::new().fill_width().room_height(height)); let stack = if has_footer { stack.footer(footer_region) } else { stack }; let shown = stack.show(ui, body); let (response, hovered, mut events) = shown.inner.unwrap_or_else(|| (shown.output.clone(), None, Vec::new())); diff --git a/crates/ui-patterns/src/patterns/presets/footer.rs b/crates/ui-patterns/src/patterns/presets/footer.rs index 31e845fe4..d22815d91 100644 --- a/crates/ui-patterns/src/patterns/presets/footer.rs +++ b/crates/ui-patterns/src/patterns/presets/footer.rs @@ -1,10 +1,8 @@ //! `PresetFooter`: the actions, the thumbnail size slider and the count of a preset panel's footer. use super::words::PresetWords; -use crate::contract::{Bind, EditResponse, ListDrag, NoValue, Outcome, Show as _, ShowContainer as _}; -use crate::display::Text; -use crate::layout::RowAlign; -use crate::layout_components::Row; +use crate::contract::{Bind, EditResponse, ListDrag, NoValue, Outcome, Show as _}; +use crate::display::{Elide, Text}; use crate::runtime::env::Env; use crate::sections::{FooterAction, FooterBar}; use crate::tile_grid::{ThumbnailSlider, TileZoom}; @@ -206,19 +204,18 @@ impl PresetFooter<'_, K> { slots.push(Slot::Custom(index)); } - let row = Row::new().fill().align(RowAlign::End).show(ui, |ui, _| { + let (footer, zoom_edit) = FooterBar::actions(shown).show_footer_leading(ui, |ui| { if let Some(add) = &leading { add(ui); } let edit = slider.then(|| ThumbnailSlider::new(words.thumb_size.clone(), zoom.reborrowed(), range).show(ui)); if let Some(n) = count { - Text::new(words.count.text(n as u64, &env.format().integer(n as u64))).show(ui); + Text::new(words.count.text(n as u64, &env.format().integer(n as u64))).elide(Elide::End).show(ui); } - (edit, FooterBar::actions(shown.clone()).show_footer(ui)) + edit }); - let (zoom_edit, footer) = row.inner.unzip(); let mut events = Vec::new(); - if let Some(footer) = footer { + { if let Some(drop) = &footer.dropped { let keys = drop.payload::>().map(|drag| drag.keys.clone()).unwrap_or_default(); if let Some(Slot::Custom(index)) = slots.get(drop.index) { @@ -233,6 +230,6 @@ impl PresetFooter<'_, K> { }); } } - PresetFooterOutput { response: row.output, events, zoom: zoom_edit.flatten() } + PresetFooterOutput { response: footer.response, events, zoom: zoom_edit.flatten() } } } diff --git a/crates/ui-patterns/tests/it/presets/browser.rs b/crates/ui-patterns/tests/it/presets/browser.rs index 27835baf6..e81424bb7 100644 --- a/crates/ui-patterns/tests/it/presets/browser.rs +++ b/crates/ui-patterns/tests/it/presets/browser.rs @@ -59,3 +59,38 @@ fn a_preset_released_over_the_zone_reports_drop_at_and_nothing_else_does() { assert_eq!(marked, expected.is_some(), "{row} over {at:?} shows the zone's mark only where it drops"); } } + +/// A browser over `presets` presets in a 400×300 room with a footer, `room_height` set when given, and where its New +/// Group button and its last preset are. +fn footer_rig(presets: u32, room_height: Option) -> (Rect, Option) { + const ROOM: Rect = Rect { min: Pos2::ZERO, max: Pos2::new(400.0, 300.0) }; + let mut rows = vec![PresetRow::group(GROUP, "Group", 0)]; + rows.extend((1..=presets).map(|key| PresetRow::item(key, format!("Preset {key}"), 1))); + let mut state = PresetState::::new(); + let mut fixture = Fixture::new(move |ui| { + ui.scope_builder(UiBuilder::new().max_rect(ROOM), |ui| { + let mut browser = PresetBrowser::new("Patterns", Bind::bound(&mut state), &rows) + .view_lock(PresetView::List) + .footer(craft_ui::patterns::presets::PresetFooterOptions::new().new_group(true)); + if let Some(height) = room_height { + browser = browser.room_height(height); + } + let _ = browser.show(ui); + }); + }); + fixture.room(vec2(480.0, 320.0)).settle(); + let new_group = fixture.by_label("New Group").expect("the footer's New Group button shows").rect; + let last = fixture.by_label(&format!("Preset {presets}")).map(|target| target.rect); + (new_group, last) +} + +#[trial] +fn the_footer_stays_in_the_room_and_a_room_height_sizes_the_browser_to_its_presets() { + let (footer, _) = footer_rig(40, None); + assert!(footer.bottom() <= 300.0, "a long library scrolls above the footer: {footer:?}"); + let (footer, last) = footer_rig(2, Some(200.0)); + let last = last.expect("both presets show"); + assert!(footer.top() >= last.bottom() && footer.top() - last.bottom() < 40.0, "the footer follows two presets: {footer:?} {last:?}"); + let (footer, _) = footer_rig(40, Some(200.0)); + assert!(footer.bottom() <= 240.0, "a long library scrolls within the room height: {footer:?}"); +}