PresetBrowser footer: stays in the room under a long library, spans the footer under one rule, count on one line; room_height sizes a browser to its presets

This commit is contained in:
aiongg
2026-11-13 17:44:41 +00:00
parent 4e542896f7
commit 25bc6bda1c
4 changed files with 75 additions and 16 deletions
+26 -5
View File
@@ -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::<fn(&mut Ui)>).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<R>(self, ui: &mut Ui, leading: impl FnMut(&mut Ui) -> R) -> (FooterOutput, Option<R>) {
self.show_footer_with(ui, Some(leading))
}
fn show_footer_with<R>(mut self, ui: &mut Ui, mut leading: Option<impl FnMut(&mut Ui) -> R>) -> (FooterOutput, Option<R>) {
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)
}
}
@@ -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<u16>,
/// 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<f32>,
}
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<K: Clone + Eq + Hash + Send + Sync + 'static> PresetBrowser<'_, K> {
actions,
footer,
max_rows,
room_height,
} = self;
let env = Env::of(ui);
let words = env.words::<PresetWords>();
@@ -939,7 +945,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> 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()));
@@ -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<K: Clone + Send + Sync + 'static> 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::<ListDrag<K>>().map(|drag| drag.keys.clone()).unwrap_or_default();
if let Some(Slot::Custom(index)) = slots.get(drop.index) {
@@ -233,6 +230,6 @@ impl<K: Clone + Send + Sync + 'static> PresetFooter<'_, K> {
});
}
}
PresetFooterOutput { response: row.output, events, zoom: zoom_edit.flatten() }
PresetFooterOutput { response: footer.response, events, zoom: zoom_edit.flatten() }
}
}
@@ -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<f32>) -> (Rect, Option<Rect>) {
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::<u32>::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:?}");
}