mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:24:24 +00:00
Collections take a row range: min_rows and max_rows bound their height, and a capped one scrolls its rows; MissingFonts, TabStops, TextStyles and FieldList set theirs
This commit is contained in:
@@ -235,7 +235,7 @@ impl StackDialog for MissingFontsDialog {
|
||||
}
|
||||
});
|
||||
Section::new(words.used_fonts.clone()).initially_open(false).remember(DOCUMENT_FONTS_OPEN).show(ui, |ui, _| {
|
||||
List::<usize>::new(words.font.clone()).select(SelectMode::Single).sizing(Sizing::Content).show_items(
|
||||
List::<usize>::new(words.font.clone()).select(SelectMode::Single).sizing(Sizing::Content).max_rows(6).show_items(
|
||||
ui,
|
||||
&Listed(&listed),
|
||||
&mut |key, _| ListRow::new(listed[*key].clone()),
|
||||
|
||||
@@ -488,18 +488,15 @@ impl Model<'_> {
|
||||
let source = Checklist { fields: self.fields, ticked: self.fields.iter().map(|field| self.ticked(&field.name)).collect() };
|
||||
let columns = vec![ColumnSpec::new(CHECK, Label::Empty, ColumnWidth::Controls(1.0)).kind(ColumnKind::Toggle(ToggleColumn::sweepable()))];
|
||||
let separator = self.words.samples_separator.as_str();
|
||||
let out = List::<String>::new(self.words.choose.clone()).natural_height(true).columns(columns).header(false).show_items(
|
||||
ui,
|
||||
&source,
|
||||
&mut |key, icx| {
|
||||
let out =
|
||||
List::<String>::new(self.words.choose.clone()).max_rows(8).columns(columns).header(false).show_items(ui, &source, &mut |key, icx| {
|
||||
let state = ToggleState::of(source.ticked[icx.at]);
|
||||
let row = ListRow::new(key.clone()).cells(icx.cells()).toggle(ToggleCell::check(CHECK, state, "{row}"));
|
||||
match self.fields.get(icx.at).filter(|field| !field.samples.is_empty()) {
|
||||
Some(field) => row.tooltip(field.samples.join(separator)),
|
||||
None => row,
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
let mut seen = HashSet::new();
|
||||
for event in out.events {
|
||||
let CollectionEvent::Toggle(edit) = event else { continue };
|
||||
@@ -527,6 +524,7 @@ impl Model<'_> {
|
||||
let out = List::<Slot>::new(name.clone())
|
||||
.id(ids[at])
|
||||
.natural_height(true)
|
||||
.min_rows(2)
|
||||
.empty(self.words.drop_here.clone())
|
||||
.reorder(ReorderRule::new())
|
||||
.deletable(true)
|
||||
|
||||
@@ -378,7 +378,7 @@ impl<'a> TextStylesPanel<'a> {
|
||||
.activate(ActivateOn::Click)
|
||||
.allow_empty(true)
|
||||
.deletable(true)
|
||||
.natural_height(true)
|
||||
.max_rows(8)
|
||||
.can_drag(move |_| highlighting)
|
||||
.drag_payload(|drag: &ListDrag<u64>| Some(drag.clone()))
|
||||
.selection(selection.reborrowed())
|
||||
|
||||
@@ -315,7 +315,8 @@ impl TabStopsDialog {
|
||||
let stops = self.stops.clone();
|
||||
let out = Table::<usize>::new(words.title.clone(), columns.clone())
|
||||
.select(SelectMode::Single)
|
||||
.natural_height(true)
|
||||
.min_rows(1)
|
||||
.max_rows(6)
|
||||
.selection(Bind::given(selection))
|
||||
.show_items(ui, &Stops { stops: &stops, texts: &texts }, &mut |key, icx| {
|
||||
let stop = &stops[*key];
|
||||
|
||||
@@ -190,6 +190,12 @@ pub struct CollectionProps {
|
||||
/// the lead scrolls the page to keep the row in view. For short collections only; a long one keeps its own
|
||||
/// scroll and is shown outside a scrolling body (16.3).
|
||||
pub natural_height: bool,
|
||||
/// The least number of rows the collection is tall: rows it does not have count the control height, so an empty
|
||||
/// collection stays a target of its size.
|
||||
pub min_rows: u16,
|
||||
/// The most rows the collection shows: past them it is as tall as its first `max_rows` rows and scrolls them. It
|
||||
/// owns that scroll inside a scrolling body too, since its height is bounded and the wheel passes on at its ends.
|
||||
pub max_rows: Option<u16>,
|
||||
/// The axis the rows overflow along. A collection owns the scroll along it, so it sits only in a scrolling body
|
||||
/// that does not scroll along it too: a strip of tabs (`Axis::X`) sits in a page that scrolls vertically.
|
||||
pub overflow: Axis,
|
||||
@@ -273,6 +279,8 @@ impl CollectionProps {
|
||||
drag_payload: None,
|
||||
drag_count: None,
|
||||
natural_height: false,
|
||||
min_rows: 0,
|
||||
max_rows: None,
|
||||
overflow: Axis::Y,
|
||||
pages: false,
|
||||
current: None,
|
||||
@@ -477,6 +485,20 @@ impl CollectionProps {
|
||||
self
|
||||
}
|
||||
|
||||
/// The collection is at least `rows` rows tall (see [`CollectionProps::min_rows`]).
|
||||
#[must_use]
|
||||
pub const fn min_rows(mut self, rows: u16) -> Self {
|
||||
self.min_rows = rows;
|
||||
self
|
||||
}
|
||||
|
||||
/// The collection shows at most `rows` rows and scrolls the rest (see [`CollectionProps::max_rows`]).
|
||||
#[must_use]
|
||||
pub const fn max_rows(mut self, rows: u16) -> Self {
|
||||
self.max_rows = Some(rows);
|
||||
self
|
||||
}
|
||||
|
||||
/// The axis the rows overflow along (see [`CollectionProps::overflow`]).
|
||||
#[must_use]
|
||||
pub const fn overflow(mut self, axis: Axis) -> Self {
|
||||
|
||||
@@ -143,6 +143,8 @@ pub(super) struct ItemsHooks<'s, 'r, C: Collection> {
|
||||
/// run, the width in a measuring run (the real run shows a collection that fills across the column it is held in).
|
||||
from_content: (bool, bool),
|
||||
scrolls: bool,
|
||||
/// The collection shows at most `max_rows` rows: its height is bounded by its rows, whatever its sizing.
|
||||
capped: bool,
|
||||
/// The axis the rows overflow along.
|
||||
overflow: Axis,
|
||||
selection: C::Selection,
|
||||
@@ -205,6 +207,7 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> {
|
||||
room: Vec2::ZERO,
|
||||
from_content: (false, false),
|
||||
scrolls: true,
|
||||
capped: false,
|
||||
overflow: Axis::Y,
|
||||
selection: C::Selection::default(),
|
||||
selection_at_read: C::Selection::default(),
|
||||
|
||||
@@ -76,11 +76,13 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
// second owner.
|
||||
let overflow = comp.items().overflow;
|
||||
let shared = env.scroll_owner.is_some() && if overflow == Axis::X { env.unbounded.0 } else { env.unbounded.1 };
|
||||
self.capped = comp.items().max_rows.is_some();
|
||||
debug_assert!(
|
||||
!shared || natural,
|
||||
"a collection owns its scroll: show it outside a body that scrolls along its rows, or give it the natural height (16.3)"
|
||||
!shared || natural || self.capped,
|
||||
"a collection owns its scroll: show it outside a body that scrolls along its rows, cap its rows, or give it the natural height (16.3)"
|
||||
);
|
||||
self.scrolls = !natural && !shared;
|
||||
// A capped collection is bounded by its rows, so it scrolls them inside a scrolling body too.
|
||||
self.scrolls = self.capped || (!natural && !shared);
|
||||
self.overflow = overflow;
|
||||
self.selection = match comp.selection() {
|
||||
Some(bind) => match bind.get() {
|
||||
@@ -209,10 +211,15 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
}
|
||||
let content = layout.content_size();
|
||||
self.content = Vec2::new(content.x as f32, content.y as f32);
|
||||
let props = comp.items();
|
||||
let row = scx.control().height;
|
||||
let floor = rows_height(&layout, self.source.len(), props.min_rows, row);
|
||||
let cap = props.max_rows.map(|rows| rows_height(&layout, self.source.len(), rows, row).max(floor));
|
||||
self.columns = layout.column_count() as u32;
|
||||
self.layout = Some(Arc::new(layout));
|
||||
let empty = if self.source.is_empty() { comp.empty_height(width, ui, scx) } else { 0.0 };
|
||||
let height = self.content.y.max(empty) + self.header.height;
|
||||
let rows = self.content.y.max(empty).max(floor);
|
||||
let height = cap.map_or(rows, |cap| rows.min(cap)) + self.header.height;
|
||||
if !(self.scrolls && room.y.is_finite()) {
|
||||
// Under a body sized from its content, a `Fill` collection offers its least width here too: the body
|
||||
// has no room of its own to hand it, and the layout above was made at the enclosing room.
|
||||
@@ -235,7 +242,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
} else {
|
||||
self.widest_in(comp, ui, id, room).min(room.x)
|
||||
};
|
||||
Vec2::new(wide, if y { height.min(room.y) } else { 0.0 })
|
||||
Vec2::new(wide, if y || self.capped { height.min(room.y) } else { 0.0 })
|
||||
}
|
||||
Sizing::Content | Sizing::FillWidth => Vec2::new(if self.content_sized(comp) { self.content.x } else { 0.0 }, height.min(room.y)),
|
||||
}
|
||||
@@ -248,7 +255,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
let width_from_content = self.from_content.0 && sizing == Sizing::Fill;
|
||||
let width = if self.room.x.is_finite() && !self.content_sized(comp) && !width_from_content { self.room.x.max(size.x) } else { size.x };
|
||||
let height = match (self.scrolls && self.room.y.is_finite(), sizing) {
|
||||
(true, Sizing::Fill) if !self.from_content.1 => self.room.y,
|
||||
(true, Sizing::Fill) if !self.from_content.1 && !self.capped => self.room.y,
|
||||
(true, Sizing::Fill) => size.y.min(self.room.y),
|
||||
(true, Sizing::Content | Sizing::FillWidth) => size.y.min(self.room.y),
|
||||
(false, _) => size.y,
|
||||
@@ -831,3 +838,13 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The height of the first `rows` rows of `layout` over `len` items: the rows it lays out, and the control height `row`
|
||||
/// for each one it does not have.
|
||||
fn rows_height(layout: &impl ItemLayout, len: usize, rows: u16, row: f32) -> f32 {
|
||||
let rows = usize::from(rows);
|
||||
let laid = rows.min(len);
|
||||
let top = if len == 0 { 0.0 } else { layout.rect(0, ScrollPos::default()).min.y };
|
||||
let built = if laid == 0 { 0.0 } else { layout.rect(laid - 1, ScrollPos::default()).max.y - top };
|
||||
built + (rows - laid) as f32 * row
|
||||
}
|
||||
|
||||
@@ -185,6 +185,22 @@ impl<'a> List<'a> {
|
||||
self
|
||||
}
|
||||
|
||||
/// `seen` takes the rect of the empty state.
|
||||
pub fn on_empty(mut self, seen: &'a Cell<Rect>) -> Self {
|
||||
self.on_empty = Some(seen);
|
||||
self
|
||||
}
|
||||
|
||||
/// At least `min` rows tall, at most `max` rows shown; with no most, the natural height.
|
||||
pub fn rows(mut self, min: u16, max: Option<u16>) -> Self {
|
||||
self.items = self.items.min_rows(min);
|
||||
self.items = match max {
|
||||
Some(max) => self.items.max_rows(max),
|
||||
None => self.items.natural_height(true),
|
||||
};
|
||||
self
|
||||
}
|
||||
|
||||
/// The arrows move the lead only.
|
||||
pub fn arrows_move_only(mut self) -> Self {
|
||||
self.items = self.items.arrows_select(false);
|
||||
|
||||
@@ -118,6 +118,38 @@ fn a_list_at_its_end_hands_the_wheel_to_the_enclosing_panel() {
|
||||
assert!(top0 - first_top.get() > SAME, "at its end the list hands the wheel to the panel: {top0} -> {}", first_top.get());
|
||||
}
|
||||
|
||||
/// A list with a row range is as tall as its rows between the least and the most it shows; past the most it scrolls
|
||||
/// them, inside a scrolling panel too.
|
||||
#[trial]
|
||||
fn a_row_range_bounds_the_list_and_it_scrolls_past_the_cap() {
|
||||
use craft_ui::contract::{ContainerProps, Scroll, ShowContainer as _};
|
||||
use craft_ui_test_kit::body::Slab;
|
||||
let cases: [(usize, u16, Option<u16>); 5] = [(ROWS, 0, Some(3)), (3, 0, Some(3)), (2, 0, Some(3)), (0, 0, None), (0, 2, None)];
|
||||
let seen: Vec<Seen> = cases.iter().map(|_| Seen::default()).collect();
|
||||
let empty: Vec<Cell<Rect>> = cases.iter().map(|_| Cell::new(Rect::NOTHING)).collect();
|
||||
let mut frames = Frames::new();
|
||||
let mut show = |ui: &mut Ui| {
|
||||
in_region(ui, Rect::from_min_size(ORIGIN, Vec2::new(VIEW.x, 1000.0)), |ui| {
|
||||
Slab::new(ContainerProps::new().scroll(Scroll::vertical())).id_salt("panel").show(ui, |ui, _| {
|
||||
for (at, (rows, min, max)) in cases.iter().enumerate() {
|
||||
let list = List::new(SelectMode::None).rows(*min, *max).on_empty(&empty[at]).id_salt(("list", at));
|
||||
seen[at].take(list.show_items(ui, &Count(*rows), &mut |_, _| Row::new()));
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
frames.idle(3, &mut show);
|
||||
let height = |at: usize| seen[at].rect.get().height();
|
||||
assert!((height(0) - height(1)).abs() < SAME, "past the cap the list is as tall as its first rows: {} vs {}", height(0), height(1));
|
||||
assert!(height(2) < height(1) - SAME, "under the cap the list is as tall as its rows");
|
||||
let (least, plain) = (empty[4].get().height(), empty[3].get().height());
|
||||
assert!(least > plain + SAME, "the least rows keep an empty list taller than its empty state: {least} vs {plain}");
|
||||
let first = seen[0].range().start;
|
||||
frames.frame(vec![pointer::moved(seen[0].rect.get().center())], &mut show);
|
||||
frames.frame(vec![pointer::wheel(-WHEEL)], &mut show);
|
||||
assert!(seen[0].range().start > first, "the capped list scrolls its rows under the wheel");
|
||||
}
|
||||
|
||||
/// One tab stop; the lead moves by arrows, Home and End, Page and type-ahead; selection by Space, Shift and Ctrl.
|
||||
#[trial]
|
||||
fn a_list_with_one_tab_stop_moves_its_lead_and_selects() {
|
||||
|
||||
@@ -189,6 +189,8 @@ const COLLECTION: &[Forwarded] = &[
|
||||
("deletable", "bool", "Delete and Backspace at the lead report the items to delete."),
|
||||
("drag_count", "::craft_ui::runtime::env::Counted", "The drag badge reads this count word for the dragged items, one included."),
|
||||
("natural_height", "bool", "The collection takes its full content height and owns no scroll: for a short collection inside a scrolling page."),
|
||||
("min_rows", "u16", "The collection is at least this many rows tall; rows it does not have count the control height."),
|
||||
("max_rows", "u16", "The collection shows at most this many rows and scrolls the rest, inside a scrolling body too."),
|
||||
(
|
||||
"current",
|
||||
"::core::option::Option<impl ::core::marker::Send + ::core::marker::Sync + 'static>",
|
||||
|
||||
Reference in New Issue
Block a user