Support trailing text beside hosted table fields

This commit is contained in:
Dylan M. Taylor
2026-11-20 19:19:17 -05:00
parent ec985c5e59
commit 4eeb6ae139
4 changed files with 118 additions and 2 deletions
+10
View File
@@ -379,6 +379,8 @@ pub struct ListRow<'a> {
pub(super) below: Vec<Hosted<'a>>,
/// The components shown in a column's cell in place of its text (`cell_field`).
pub(super) cell_fields: Vec<(ColumnKey, Hosted<'a>)>,
/// Columns whose field is followed by their text cell.
pub(super) field_suffixes: Vec<ColumnKey>,
/// The row's depth in a tree, which indents it; `None` for a row of a flat list.
/// The row is at `depth` levels of a tree: indented by it, an `AccessKit` tree item.
#[prop]
@@ -470,6 +472,7 @@ impl<'a> ListRow<'a> {
trailing: Vec::new(),
below: Vec::new(),
cell_fields: Vec::new(),
field_suffixes: Vec::new(),
depth: None,
group: None,
disclosure_by_host: false,
@@ -612,6 +615,13 @@ impl<'a> ListRow<'a> {
self
}
/// Text following a hosted field in the same column. The field takes the room the text and their gap leave.
#[must_use]
pub fn cell_field_suffix(mut self, column: ColumnKey, text: impl Into<Label>) -> Self {
self.field_suffixes.push(column.clone());
self.cell(column, text)
}
/// Like `cell`, with the text wrapped to the column's width; the row is as high as its tallest wrapped cell.
#[must_use]
pub fn cell_lines(mut self, column: ColumnKey, text: impl Into<Label>) -> Self {
+30 -2
View File
@@ -241,13 +241,22 @@ impl ListRow<'_> {
let rtl = cx.env.direction().is_rtl();
for (at, position, cell) in &placed.texts {
let text = &self.texts[*at];
if self.cell_fields.iter().any(|(column, _)| *column == text.column) {
let field = self.cell_fields.iter().any(|(column, _)| *column == text.column);
if field && !self.field_suffixes.contains(&text.column) {
continue;
}
let look = self.cell_look(cx.style, &text.column);
let cell = Rect::from_x_y_ranges(cell.x_range(), cx.rect.y_range());
let room = Flow::new(cell, rtl).span(sizes.cell_inset, sizes.cell_room(cell.width()));
let mut room = if field && self.label_column(&self.cells) == Some(&text.column) {
placed.label
} else {
Flow::new(cell, rtl).span(sizes.cell_inset, sizes.cell_room(cell.width()))
};
let run = self.cell_run(cx.style, *at).color(cx.resolve(&look.color));
if field {
let width = ui.draw().layout(&run, f32::INFINITY).size().x;
room = split_field_text(room, rtl, width, sizes.gap).1;
}
let layout = ui.draw().layout(&run, room.width());
let top =
if text.wrap { room.top() + ((room.height() - layout.size().y) / 2.0).max(0.0) } else { room.center().y - sizes.label_line / 2.0 };
@@ -303,6 +312,15 @@ impl ListRow<'_> {
let rtl = cx.env.direction().is_rtl();
let label = self.label_column(&self.cells).cloned();
let band = placed.label.y_range();
let suffixes: Vec<_> = self
.texts
.iter()
.filter(|text| self.field_suffixes.contains(&text.column))
.map(|text| {
let run = TextRun::new(text.text.as_str()).role(cx.style.cell.role);
(text.column.clone(), ui.draw().layout(&run, f32::INFINITY).size().x)
})
.collect();
for (at, (column, field)) in self.cell_fields.iter_mut().enumerate() {
let (Some(item), Some(size)) = (field.item.take(), field.size) else { continue };
let room = if label.as_ref() == Some(column) {
@@ -312,12 +330,22 @@ impl ListRow<'_> {
let cell = Rect::from_x_y_ranges(cell.x_range(), band);
Flow::new(cell, rtl).span(sizes.cell_inset, sizes.cell_room(cell.width()))
};
let room = suffixes.iter().find(|(key, _)| key == column).map_or(room, |(_, width)| split_field_text(room, rtl, *width, sizes.gap).0);
let height = size.ideal.y.min(room.height());
item.show(ui, cell_key(at), Rect::from_center_size(room.center(), Vec2::new(room.width(), height)));
}
}
}
/// Divides a cell's inset room into a field and its trailing text, in reading order.
fn split_field_text(room: Rect, rtl: bool, text_width: f32, gap: f32) -> (Rect, Rect) {
let text = text_width.min(room.width());
let gap = gap.min((room.width() - text).max(0.0));
let field = (room.width() - text - gap).max(0.0);
let flow = Flow::new(room, rtl);
(flow.span(0.0, field), flow.span(field + gap, text))
}
/// The ink of a toggle or link glyph that is `on` or not: its own part's hover and press, over the row's selection and
/// lead.
fn glyph_ink(cx: &Cx<'_, ListRow<'_>>, part: PartRef, on: bool) -> StyleColor {
+10
View File
@@ -667,6 +667,16 @@ impl ListRow<'_> {
cx.cell_width(&text.column, line + inset);
}
}
for (column, field) in &self.cell_fields {
if let Some(size) = field.size
&& self.cells.iter().any(|(key, _)| key == column)
{
let suffix = self.texts.iter().find(|text| text.column == *column && self.field_suffixes.contains(column)).map_or(0.0, |text| {
cx.layout_at(&TextRun::new(text.text.as_str()).role(cx.style.cell.role), f32::INFINITY).size().x + cx.style.sizes.gap
});
cx.cell_width(column, size.ideal.x + suffix + inset);
}
}
// A status cell holds its icon, text and action whole, with the gaps the slots keep between them.
let (role, sizes) = (cx.style.cell.role, &cx.style.sizes);
for cell in &self.statuses {
+68
View File
@@ -47,6 +47,74 @@ fn a_table_cell_text_field_reports_edits() {
assert_eq!(edited.borrow().as_deref(), Some("1"), "typing reports an edit before Enter");
}
#[trial]
fn a_table_cell_can_show_a_field_followed_by_text() {
use craft_ui::contract::Bind;
use craft_ui::text::TextField;
for (direction, column) in [
(craft_i18n::Direction::Ltr, NAME),
(craft_i18n::Direction::Rtl, NAME),
(craft_i18n::Direction::Ltr, SIZE),
(craft_i18n::Direction::Rtl, SIZE),
] {
let mut fixture = Fixture::new(move |ui| {
let columns = vec![ColumnSpec::new(NAME, "Name", ColumnWidth::fill(1.0)), ColumnSpec::new(SIZE, "Pages", ColumnWidth::Content)];
let keys = [0_usize];
let source = keyed(&keys, |key: &usize| *key);
Table::new("Files", columns).show_items(ui, &source, &mut |_, icx| {
ListRow::new("File").cells(icx.cells()).cell_field_suffix(column.clone(), "2 of 3").cell_field(
column.clone(),
TextField::new("Page range", Bind::given(String::new())).placeholder("Choose pages 1–3"),
|_| {},
)
});
});
fixture.in_room(Room::Dialog).direction(direction).settle();
let field = fixture.by_label("Page range").expect("the range field");
let count = fixture.texts().iter().find(|text| text.text == "2 of 3").expect("the count is painted").rect;
assert!(
if direction == craft_i18n::Direction::Ltr { field.rect.right() <= count.left() } else { count.right() <= field.rect.left() },
"field {:?} and count {count:?} follow reading order",
field.rect
);
assert!(field.rect.width() >= 80.0, "the field keeps room for a range: {:?}", field.rect);
}
}
#[trial]
fn an_accessibility_focused_table_cell_field_reports_typing() {
use craft_behavior::EditEvent;
use craft_ui::contract::Bind;
use craft_ui::testing::focus::AccessActionKind;
use craft_ui::text::TextField;
let edited = Rc::new(RefCell::new(None::<String>));
let seen = edited.clone();
let mut fixture = Fixture::new(move |ui| {
let columns = vec![ColumnSpec::new(NAME, "Name", ColumnWidth::fill(1.0)), ColumnSpec::new(SIZE, "Pages", ColumnWidth::Content)];
let keys = [0_usize];
let source = keyed(&keys, |key: &usize| *key);
Table::new("Files", columns).show_items(ui, &source, &mut |_, icx| {
let seen = seen.clone();
ListRow::new("File").cells(icx.cells()).cell_field(
SIZE,
TextField::new("Page range", Bind::given(String::new())).placeholder("All pages"),
move |out| {
for event in out.events {
if let EditEvent::Update { value, .. } | EditEvent::Commit { after: value, .. } | EditEvent::Set { after: value, .. } = event
{
*seen.borrow_mut() = Some(value);
}
}
},
)
});
});
fixture.in_room(Room::Dialog).settle();
let field = fixture.by_label("Page range").expect("the field");
fixture.access(AccessActionKind::Focus, &field).type_text("1").settle();
assert_eq!(edited.borrow().as_deref(), Some("1"), "typing after accessibility focus reports the edit");
}
const NAME: ColumnKey = ColumnKey::new("name");
const SIZE: ColumnKey = ColumnKey::new("size");
const DATE: ColumnKey = ColumnKey::new("date");