Merge pull request #3509 from dotCooCoo/fix/list-row-toggle-once

This commit is contained in:
BFlat authored and GitHub committed 2026-11-26 12:44:20 +07:00
commit b2e7113a41
7 files changed
+124 -40

No files matched your search

+1 -1
View File
@@ -69,7 +69,7 @@ pub use drag::{SweepApply, SweepStep};
pub use header::ColumnCommands;
pub(crate) use header::HeaderFacts;
#[doc(hidden)]
pub use header::put_toggle_names;
pub use header::{RowToggle, put_toggle_names};
pub use press::item_part_open;
#[doc(hidden)]
pub use request::lead_position;
@@ -11,7 +11,7 @@ use egui::{Context, Id, Rect};
impl<C: Collection> ItemsHooks<'_, '_, C> {
/// The access sub-node of each toggle cell of row `(at, key)`, named by its cell, else by its column's header, and carrying its state,
/// below the row's node `item`; an AccessKit click on it reaches `toggle_by_access`.
/// below the row's node for the cell, else the row's node `item`; an AccessKit click on it reaches `toggle_by_access`.
pub(super) fn report_toggle_cells(
&self,
ctx: &Context,
@@ -22,12 +22,16 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
) {
for (column, toggle) in self.header.toggle_columns() {
let Some((_, rect)) = cells.iter().find(|(held, _)| *held == column) else { continue };
let Some((name, mixed)) = self.header.cell_name(ctx, item, &column, env) else { continue };
let mut node =
access::SubNode::new(TOGGLE_CELL.at(key_hash(&(key, &column))), if toggle.exclusive { Role::RADIO_BUTTON } else { Role::SWITCH });
node.name = access::Name::Text(name);
let Some(cell) = self.header.toggle_cell(ctx, item, &column, env) else { continue };
let role = match (toggle.exclusive, cell.check_box) {
(true, _) => Role::RADIO_BUTTON,
(false, true) => Role::CHECK_BOX,
(false, false) => Role::SWITCH,
};
let mut node = access::SubNode::new(TOGGLE_CELL.at(key_hash(&(key, &column))), role);
node.name = access::Name::Text(cell.name);
node.flags.set(crate::style::Flags::CHECKED, self.source.toggled(at, &column));
node.flags.set(crate::style::Flags::MIXED, mixed);
node.flags.set(crate::style::Flags::MIXED, cell.mixed);
if let Some(chord) = toggle.key {
node.shortcut(chord.display(crate::runtime::windows::os_of(ctx)));
}
@@ -42,7 +46,7 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
active: false,
face: None,
label: "",
parent: item,
parent: cell.cell,
};
access::report_sub(ctx, &decl);
}
+40 -25
View File
@@ -91,18 +91,34 @@ impl DropTarget<ColumnDrag> for ColumnGap {
}
}
/// The names the toggle cells of one row gave themselves this pass, by column.
#[derive(Clone, Default)]
struct ToggleNames(std::sync::Arc<[(ColumnKey, String, bool)]>);
/// Records the toggle cells of the row `item` with their accessible names (`{row}` filled; empty when the cell gives
/// none) and whether each shows the mixed state, for the collection's cell nodes of the same pass. A row that has none records an empty list.
/// One toggle cell of a row as the row records it for the collection's node of that cell.
#[doc(hidden)]
pub fn put_toggle_names(ctx: &Context, item: Id, names: Vec<(ColumnKey, String, bool)>) {
#[derive(Clone, Debug)]
pub struct RowToggle {
/// The cell's column.
pub column: ColumnKey,
/// The accessible name, `{row}` filled; empty when the cell gives none.
pub name: String,
/// The cell shows the mixed state.
pub mixed: bool,
/// The cell is announced as a check box.
pub check_box: bool,
/// The row's node for the cell, which the collection's node for the cell goes under.
pub cell: Id,
}
/// The toggle cells of one row this pass, by column.
#[derive(Clone, Default)]
struct ToggleNames(std::sync::Arc<[RowToggle]>);
/// Records the toggle cells of the row `item`, for the collection's cell nodes of the same pass. A row that has none
/// records an empty list.
#[doc(hidden)]
pub fn put_toggle_names(ctx: &Context, item: Id, names: Vec<RowToggle>) {
crate::runtime::egui::memory::put(ctx, sub(item, "toggle_names"), ToggleNames(names.into()));
}
fn recorded_toggle_names(ctx: &Context, item: Id) -> Option<std::sync::Arc<[(ColumnKey, String, bool)]>> {
fn recorded_toggle_names(ctx: &Context, item: Id) -> Option<std::sync::Arc<[RowToggle]>> {
crate::runtime::egui::memory::peek_clone::<ToggleNames>(ctx, sub(item, "toggle_names")).map(|ToggleNames(names)| names)
}
@@ -349,27 +365,26 @@ impl Header {
self.toggle_columns().filter_map(|(column, toggle)| Some((column, toggle.key?))).collect()
}
/// The name of the cell of toggle column `key` in the row `item` and whether it shows the mixed state: the name the
/// row's cell gave (`put_toggle_names`), else the column's header text, else its icon's name, resolved in `env`.
/// `None` when the row recorded its toggle cells and has none in this column: the collection reports no node there.
pub(super) fn cell_name(&self, ctx: &Context, item: Id, key: &ColumnKey, env: &crate::runtime::env::EnvView<'_>) -> Option<(String, bool)> {
/// The cell of toggle column `key` in the row `item` as the row recorded it (`put_toggle_names`), else a cell under
/// the row's node. Its name is the one the row's cell gave, else the column's header text, else its icon's name,
/// resolved in `env`. `None` when the row recorded its toggle cells and has none in this column: the collection
/// reports no node there.
pub(super) fn toggle_cell(&self, ctx: &Context, item: Id, key: &ColumnKey, env: &crate::runtime::env::EnvView<'_>) -> Option<RowToggle> {
let recorded = recorded_toggle_names(ctx, item);
let (given, mixed) = match &recorded {
Some(names) => names.iter().find(|(held, ..)| held == key).map(|(_, name, mixed)| (name.as_str(), *mixed))?,
None => ("", false),
let mut cell = match &recorded {
Some(cells) => cells.iter().find(|cell| cell.column == *key).cloned()?,
None => RowToggle { column: key.clone(), name: String::new(), mixed: false, check_box: false, cell: item },
};
if !given.is_empty() {
return Some((given.to_owned(), mixed));
if !cell.name.is_empty() {
return Some(cell);
}
let Some(spec) = self.specs.iter().find(|spec| spec.key == *key) else { return Some((String::new(), mixed)) };
let Some(spec) = self.specs.iter().find(|spec| spec.key == *key) else { return Some(cell) };
let header = env.resolve(&spec.header);
Some((
match (header.as_str(), &spec.icon) {
("", Some(icon)) => icon.name().to_owned(),
(text, _) => text.to_owned(),
},
mixed,
))
cell.name = match (header.as_str(), &spec.icon) {
("", Some(icon)) => icon.name().to_owned(),
(text, _) => text.to_owned(),
};
Some(cell)
}
/// The key of the column at position `at`.
+2 -2
View File
@@ -88,9 +88,9 @@ pub use held::Held;
pub use hosted::{Hosted, HostedKey, Hosting};
pub use items::item_node;
pub use items::lead_position;
#[doc(hidden)]
pub use items::put_toggle_names;
pub use items::{ColumnCommands, SweepApply, SweepStep, item_part_open, request_rename, section_part};
#[doc(hidden)]
pub use items::{RowToggle, put_toggle_names};
pub use items::{measure_item, show_item, show_item_in_flow};
#[doc(hidden)]
pub use label::colon_of;
+15 -4
View File
@@ -1,12 +1,12 @@
//! Where a row's parts lie: its sizes, bands and the placement of label, cells and trailing items.
use super::ListRow;
use super::show::named;
use super::show::{TOGGLE_CELL, named};
use super::style::{ListRowStyle, ThumbRows};
use super::thumb::{Thumb, ThumbSize};
use super::trailing::{TrailingFace, TrailingItem, trailing_run};
use crate::buttons::Link;
use crate::contract::{ColumnAlign, PartsCx, RowHeight, WordSetCx as _};
use crate::contract::{ColumnAlign, PartsCx, RowHeight, RowToggle, WordSetCx as _};
use crate::layout::{ShrinkOrder, ShrinkUnit, shrink_widths};
use crate::list::toggle::ToggleState;
use crate::list::words::ListWords;
@@ -243,14 +243,25 @@ impl ListRow<'_> {
}
}
/// Hands the collection the names of this row's toggle cells, so its cell nodes read them.
/// Hands the collection this row's toggle cells, so its cell nodes read their names and roles and go under the
/// row's cell nodes.
pub(super) fn record_toggle_names(&self, cx: &PartsCx<'_, Self>) {
let words = cx.words::<ListWords>();
let label = cx.resolved.label;
let id = cx.resolved.id;
// A row without a node, or a spacer, reports no cell nodes: the collection's node goes under the row's.
let cells = self.node && !self.spacer;
let names = self
.toggles
.iter()
.map(|toggle| (toggle.column.clone(), named(toggle.name_pattern(&words), label), toggle.state == ToggleState::Mixed))
.enumerate()
.map(|(at, toggle)| RowToggle {
column: toggle.column.clone(),
name: named(toggle.name_pattern(&words), label),
mixed: toggle.state == ToggleState::Mixed,
check_box: toggle.check_box,
cell: if cells { crate::runtime::access::sub_node_id(id, TOGGLE_CELL.at(at as u64)) } else { id },
})
.collect();
crate::contract::put_toggle_names(cx.ui.ctx(), cx.resolved.id, names);
}
+7 -1
View File
@@ -28,7 +28,7 @@ use egui::{CornerRadius, Pos2, Rect, Vec2};
pub(super) const TOGGLE: PartKey = PartKey::new("toggle");
pub(super) const TEXT_CELL: PartKey = PartKey::new("text-cell");
const TOGGLE_CELL: PartKey = PartKey::new("toggle-cell");
pub(super) const TOGGLE_CELL: PartKey = PartKey::new("toggle-cell");
const LABEL_CELL: PartKey = PartKey::new("label-cell");
const DISCLOSURE: PartKey = PartKey::new("disclosure");
@@ -493,6 +493,12 @@ impl Component for ListRow<'_> {
cell.name = Name::Text(label.to_owned());
cell.grid.column_index = column_of(&toggle.column);
cell.flags.set(Flags::SELECTED, selected);
// The collection reports the toggle of its own toggle column under this cell, with its state and click: the
// painted toggle adds no node of its own.
if self.toggle_columns.contains(&toggle.column) {
access.node(TOGGLE.at(at as u64), Role::None);
continue;
}
let role = if toggle.check_box { Role::CHECK_BOX } else { Role::BUTTON };
let node = access.node(TOGGLE.at(at as u64), role);
node.name = Name::Text(named(toggle.name_pattern(&words), label));
+48
View File
@@ -1054,6 +1054,54 @@ fn each_cell_node_of_a_row_takes_its_columns_bounds() {
}
}
/// Each toggle cell of a row has one node, under its cell node and inside the cell: a leading eye reads as a switch
/// and a trailing check as a check box, each with its state and a click.
#[trial]
fn each_toggle_cell_of_a_row_has_one_node_with_its_state_and_click() {
use craft_behavior::{ColumnKey, ColumnWidth, Mixed};
use craft_ui::contract::{ColumnSpec, Role, ToggleColumn};
use craft_ui::list::{ToggleCell, ToggleState};
use craft_ui::testing::access::Declared;
const EYE: ColumnKey = ColumnKey::new("eye");
const NAME: ColumnKey = ColumnKey::new("name");
const KEEP: ColumnKey = ColumnKey::new("keep");
let mut fixture = Fixture::new(move |ui| {
let columns = vec![
ColumnSpec::toggle(EYE, "", ToggleColumn::sweepable()),
ColumnSpec::new(NAME, "Name", ColumnWidth::fill(1.0)),
ColumnSpec::toggle(KEEP, "", ToggleColumn::default()),
];
let keys = [0_usize];
let source = keyed(&keys, |key: &usize| *key).with_toggled(|_, column| *column == EYE);
let _ = List::new("Layers").columns(columns).show_items(ui, &source, &mut |_, icx| {
let keep = ToggleCell::check(KEEP, ToggleState::Off, "Keep {row}");
ListRow::new("Layer 1").cells(icx.cells()).toggle(ToggleCell::eye(EYE, true)).toggle(keep)
});
});
fixture.room(Vec2::new(400.0, 200.0));
fixture.settle();
let tree = fixture.tree();
let toggles: Vec<_> = tree
.keys()
.into_iter()
.filter(|key| matches!(tree.role_of(*key), Some(role) if role == Role::SWITCH || role == Role::CHECK_BOX || role == Role::BUTTON))
.collect();
let found: Vec<_> = toggles.iter().map(|key| (tree.role_of(*key), tree.name_of(*key))).collect();
assert_eq!(toggles.len(), 2, "one node per toggle cell: {found:?}");
for key in toggles {
let name = tree.name_of(key).unwrap_or_default().to_owned();
assert!(name.contains("Layer 1"), "each toggle node is named after its row: {found:?}");
let (role, state) = if name == "Keep Layer 1" { (Role::CHECK_BOX, false) } else { (Role::SWITCH, true) };
assert_eq!(tree.role_of(key), Some(role), "{name} has its cell's role");
assert_eq!(tree.toggled_of(key), Some(Mixed::Same(state)), "{name} carries its state");
assert!(tree.declares_key(key, Declared::Click), "{name} takes a click");
let cell = tree.parent_of_key(key).unwrap_or_else(|| panic!("{name} has a parent"));
assert_eq!(tree.role_of(cell), Some(Role::CELL), "{name} sits under its cell node");
let (inner, outer) = (tree.bounds_of(key).unwrap_or(egui::Rect::NOTHING), tree.bounds_of(cell).unwrap_or(egui::Rect::NOTHING));
assert!(outer.expand(0.5).contains_rect(inner), "{name} lies inside its cell: {inner:?} in {outer:?}");
}
}
/// 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() {