ListRow: a row's ideal width counts its optional items

A content-sized row asks for room for its optional trailing items, so a
content-fitted box (the CommandPalette) shows each row's path with its
label whole. A row given less than its ideal width still drops them
first; its least width is unchanged.
This commit is contained in:
aiongg
2026-11-12 14:32:50 +00:00
parent 4a6d07a02e
commit 0c064ca7d3
3 changed files with 36 additions and 4 deletions
+4 -2
View File
@@ -464,7 +464,8 @@ impl<'a> ListRow<'a> {
self
}
/// A text at the trailing end that drops out when the label would get narrower than its least width with it.
/// A text at the trailing end that drops out when the label would get narrower than its least width with it. A row
/// sized to its content makes room for it.
#[must_use]
pub fn optional(mut self, text: impl Into<Label>) -> Self {
self.trailing.push(Trailing { item: TrailingItem::Text { text: text.into(), name: None, strong: false }, optional: true });
@@ -522,7 +523,8 @@ impl<'a> ListRow<'a> {
}
/// A component at the trailing end as [`trailing_field`](Self::trailing_field) adds one, that drops out when the
/// label would get narrower than its least width with it (a binding's badge).
/// label would get narrower than its least width with it (a binding's badge). A row sized to its content makes
/// room for it.
#[must_use]
pub fn optional_field<C: Show + 'a>(self, field: C, output: impl FnOnce(C::Output) + 'a) -> Self {
self.hosted(Hosting::new(field, output), true)
+4 -2
View File
@@ -158,7 +158,7 @@ pub(super) struct Placed {
pub(super) items: Vec<Option<Rect>>,
/// The row's least width: the label at its least, the required items at theirs.
pub(super) min_width: f32,
/// The row's least width with the required items at their widths where the row has room.
/// The row's least width with every trailing item, optional ones included, at its width where the row has room.
pub(super) ideal_width: f32,
}
@@ -463,7 +463,9 @@ impl ListRow<'_> {
let dividers = cells.iter().filter(|(_, _, leading)| *leading).map(|(_, cell, _)| if rtl { cell.left() } else { cell.right() }).collect();
let least: f32 = widths.items.iter().filter(|item| !item.optional).map(|item| item.least + sizes.gap).sum();
let min_width = lead + trail + base + column + leading_width + sizes.min_label + least + sizes.trailing_pad;
let ideal_width = min_width - least + required;
// Every item, optional ones included: at its ideal width the row shows everything.
let all: f32 = widths.items.iter().map(|item| item.width + sizes.gap).sum();
let ideal_width = min_width - least + all;
Placed {
toggles,
texts,
+28
View File
@@ -257,6 +257,34 @@ fn hosted_items_take_their_own_width_and_an_optional_one_drops_first() {
assert!(field.right() <= row_rect.right() + 0.5, "the row's items stay inside it: {field:?} in {row_rect:?}");
}
/// A list sized to its content makes room for each row's optional item and shows its label whole, whether the label
/// is shorter or longer than its least width.
#[trial]
fn a_content_sized_list_makes_room_for_its_rows_optional_items() {
use craft_ui::contract::Sizing;
use std::cell::Cell;
const ROWS: [(&str, &str); 2] = [("Levels", "Image > Adjustments"), ("Gaussian Blur and Sharpen More", "Filter > Blur")];
let shown = Rc::new(Cell::new(egui::Rect::NOTHING));
let held = shown.clone();
let mut fixture = Fixture::new(move |ui| {
let keys = [0_usize, 1];
let source = keyed(&keys, |key: &usize| *key).with_text(|key| std::borrow::Cow::Borrowed(ROWS[*key].0));
let out = List::new("Commands")
.sizing(Sizing::Content)
.show_items(ui, &source, &mut |key, _| ListRow::new(ROWS[*key].0).trailing("Ctrl+L").optional(ROWS[*key].1));
held.set(out.response.rect);
});
fixture.room(Vec2::new(800.0, 300.0));
fixture.settle();
let list = shown.get();
assert!(list.width() < 800.0, "the list takes its width from its rows: {list:?}");
let whole = |text: &str| fixture.texts().iter().find(|run| run.text == text && list.contains_rect(run.rect)).map(|run| run.elided);
for (label, path) in ROWS {
assert_eq!(whole(label), Some(false), "{label} shows whole");
assert_eq!(whole(path), Some(false), "the path of {label} shows whole");
}
}
/// The components a row hosts under its label stand in one line inside the row, which grows to hold them, and a click on
/// one reaches its output.
#[trial]