Merge pull request #3501 from dotCooCoo/fix/list-row-cell-bounds

This commit is contained in:
BFlat authored and GitHub committed 2026-11-26 12:44:11 +07:00
commit aed392aebf
3 files changed
+94 -1

No files matched your search

+4 -1
View File
@@ -14,7 +14,7 @@ use crate::contract::{Common, OpenChange, RowHeight, RowPressed, RowState, Show}
use crate::primitives::{ContentPainterCx, HAlign, IconSpec, Span, TextRun};
use crate::runtime::env::Label;
use crate::style::DataColor;
use craft_behavior::{ColumnKey, Cursor};
use craft_behavior::{ColumnKey, Cursor, PartRef};
use craft_i18n::Word;
use craft_theme::variant::Tone;
use craft_theme::{LabelColor, TypeRole};
@@ -467,6 +467,8 @@ pub struct ListRow<'a> {
/// The row's menu button was clicked: its rect, where the menu hangs.
pub(super) menu: Option<Rect>,
pub(super) label_rect: Rect,
/// The placed rect of each cell node, by the node's part: the access step reads them as the cells' bounds.
pub(super) cell_bounds: Vec<(PartRef, Rect)>,
}
impl<'a> ListRow<'a> {
@@ -564,6 +566,7 @@ impl<'a> ListRow<'a> {
open: None,
menu: None,
label_rect: Rect::NOTHING,
cell_bounds: Vec::new(),
}
.label(label)
}
+28
View File
@@ -212,6 +212,7 @@ impl Component for ListRow<'_> {
self.measure_hosted(cx);
let widths = self.widths(&cx.style.sizes, &|run| cx.layout_at(run, f32::INFINITY).size().x);
let placed = self.place(&cx.style.sizes, &widths, rect, cx.flow.is_rtl());
self.record_cell_bounds(&placed);
self.toggle_parts(&placed, cx);
for (at, position, _) in &placed.texts {
if self.cell_selects(*at) {
@@ -477,12 +478,18 @@ impl Component for ListRow<'_> {
let column_of = |key: &ColumnKey| self.cells.iter().position(|(column, _)| column == key);
if let Some(column) = self.label_column(&self.cells) {
let node = access.node(LABEL_CELL, Role::CELL);
if let Some(rect) = self.cell_rect(LABEL_CELL) {
node.bounds(rect);
}
node.name = Name::Text(label.to_owned());
node.grid.column_index = column_of(column);
node.flags.set(Flags::SELECTED, selected);
}
for (at, toggle) in self.toggles.iter().enumerate() {
let cell = access.node(TOGGLE_CELL.at(at as u64), Role::CELL);
if let Some(rect) = self.cell_rect(TOGGLE_CELL.at(at as u64)) {
cell.bounds(rect);
}
cell.name = Name::Text(label.to_owned());
cell.grid.column_index = column_of(&toggle.column);
cell.flags.set(Flags::SELECTED, selected);
@@ -494,6 +501,9 @@ impl Component for ListRow<'_> {
for text in &self.texts {
let Some(position) = self.cells.iter().position(|(column, _)| *column == text.column) else { continue };
let node = access.node(TEXT_CELL.at(position as u64), Role::CELL);
if let Some(rect) = self.cell_rect(TEXT_CELL.at(position as u64)) {
node.bounds(rect);
}
node.name = Name::Text(text.text.as_str().to_owned());
node.description = self.tips.iter().find(|(column, _)| *column == text.column).map(|(_, tip)| tip.as_str().to_owned());
node.grid.column_index = Some(position);
@@ -502,6 +512,9 @@ impl Component for ListRow<'_> {
for (at, cell) in self.statuses.iter().enumerate() {
let Some(position) = self.cells.iter().position(|(column, _)| *column == cell.column) else { continue };
let node = access.node(STATUS.at(position as u64), Role::CELL);
if let Some(rect) = self.cell_rect(STATUS.at(position as u64)) {
node.bounds(rect);
}
node.name = Name::Text(cell.status.text.as_str().to_owned());
node.description = cell.status.tip.as_ref().map(|tip| tip.as_str().to_owned());
node.grid.column_index = Some(position);
@@ -750,6 +763,21 @@ impl ListRow<'_> {
if part == TrailingPart::MENU { Label::from(named(&words.more, row)) } else { name.clone() }
}
/// Keeps the placed rect of each cell node for the access step: the label cell's is the label band, which spans
/// the label column and the empty toggle columns beside it.
fn record_cell_bounds(&mut self, placed: &Placed) {
let toggles = placed.toggles.iter().map(|(at, cell)| (TOGGLE_CELL.at(*at as u64), *cell));
let texts = placed.texts.iter().map(|(_, position, cell)| (TEXT_CELL.at(*position as u64), *cell));
let statuses = placed.statuses.iter().map(|(_, position, cell)| (STATUS.at(*position as u64), *cell));
self.cell_bounds = std::iter::once((LABEL_CELL.into(), placed.band)).chain(toggles).chain(texts).chain(statuses).collect();
}
/// The placed rect of the cell node `part`, which `parts` recorded.
fn cell_rect(&self, part: impl Into<PartRef>) -> Option<Rect> {
let part = part.into();
self.cell_bounds.iter().find(|(key, _)| *key == part).map(|(_, rect)| *rect)
}
/// The parts of the row's toggle cells: their flags, held state and tooltip with the toggle's key.
fn toggle_parts(&self, placed: &Placed, cx: &mut PartsCx<'_, Self>) {
for (at, cell) in &placed.toggles {
+62
View File
@@ -992,6 +992,68 @@ fn a_toggle_column_holds_one_toggle_with_room_on_each_side() {
assert!(cell.width() < 2.0 * cell.height(), "the toggle column holds one toggle: {cell:?}");
}
/// Each cell node of a row lies in its own column: a leading and a trailing toggle cell, the label cell, a text cell
/// and a status cell each take their column's span of the row, not the whole row.
#[trial]
fn each_cell_node_of_a_row_takes_its_columns_bounds() {
use craft_behavior::{ColumnKey, ColumnWidth};
use craft_theme::variant::Tone;
use craft_ui::contract::{ColumnSpec, Role, ToggleColumn};
use craft_ui::list::{CellStatus, ToggleCell};
const EYE: ColumnKey = ColumnKey::new("eye");
const NAME: ColumnKey = ColumnKey::new("name");
const SIZE: ColumnKey = ColumnKey::new("size");
const WARNINGS: ColumnKey = ColumnKey::new("warnings");
const LOCK: ColumnKey = ColumnKey::new("lock");
let cells = Rc::new(RefCell::new(Vec::new()));
let held = cells.clone();
let mut fixture = Fixture::new(move |ui| {
let columns = vec![
ColumnSpec::toggle(EYE, "", ToggleColumn::sweepable()),
ColumnSpec::new(NAME, "Name", ColumnWidth::fill(1.0)),
ColumnSpec::new(SIZE, "Size", ColumnWidth::fill(1.0)),
ColumnSpec::new(WARNINGS, "Warnings", ColumnWidth::fill(1.0)),
ColumnSpec::toggle(LOCK, "", ToggleColumn::sweepable()),
];
let keys = [0_usize];
let source = keyed(&keys, |key: &usize| *key);
let _ = List::new("Layers").columns(columns).show_items(ui, &source, &mut |_, icx| {
held.replace(icx.cells().to_vec());
ListRow::new("Layer 1")
.cells(icx.cells())
.toggle(ToggleCell::eye(EYE, true))
.cell(SIZE, "12 KB")
.cell_status(WARNINGS, CellStatus::new(Tone::Warn, "2 warnings"))
.toggle(ToggleCell::lock(LOCK, false))
});
});
fixture.room(Vec2::new(600.0, 200.0));
fixture.settle();
let cells = cells.borrow();
let column = |key: &ColumnKey| cells.iter().find(|(column, _)| column == key).map(|(_, rect)| *rect).expect("the row lists the column");
let nodes = fixture.by_role(Role::CELL);
assert_eq!(nodes.len(), 5, "one cell node per column: {nodes:?}");
for node in &nodes {
let tree = fixture.tree();
let key = match tree.name_of(node.node) {
Some("12 KB") => SIZE,
Some("2 warnings") => WARNINGS,
Some("Layer 1") => match tree.children_of(node.node).first().and_then(|child| tree.name_of(*child)) {
Some(name) if name.contains("Lock") => LOCK,
Some(_) => EYE,
None => NAME,
},
other => panic!("an unexpected cell node {other:?}"),
};
let span = column(&key);
assert!(
(node.rect.left() - span.left()).abs() < 0.5 && (node.rect.right() - span.right()).abs() < 0.5,
"the {key:?} cell spans its column: {:?}, the column {span:?}",
node.rect
);
}
}
/// A toggle cell's glyph rests in `text.muted` and reads in `text.normal` under the pointer.
#[trial]
fn a_toggle_glyph_rests_muted_and_reads_normal_under_the_pointer() {