Merge pull request #8148 from dotCooCoo/fix/list-item-selected-state

UI: list rows report a selected state and section headers report an expanded state
This commit is contained in:
BFlat
2026-11-16 23:53:35 +07:00
committed by GitHub
5 changed files with 66 additions and 3 deletions
+1 -1
View File
@@ -126,7 +126,7 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui, d: &mut Dialog) -> bool {
let size_at = r.right_center() - vec2(6.0, 0.0);
p.text(size_at, egui::Align2::RIGHT_CENTER, &size, font.clone(), t.text_dim);
p.with_clip_rect(r.with_max_x(size_at.x - 80.0)).text(mid + vec2(NUMBER_W, 0.0), egui::Align2::LEFT_CENTER, name, font, t.text);
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::ListItem, true, on, format!("{} {name} {size}", i + 1)));
widgets::list_item_info(&resp, on, || format!("{} {name} {size}", i + 1));
if resp.double_clicked() {
double = Some(i);
} else if resp.clicked() {
+18
View File
@@ -82,6 +82,24 @@ fn select_by_artboards_lists_the_artboards_and_selects_on_ok() {
assert_eq!(app.session.active().unwrap().selection.objects, vec![vectorcraft_doc::NodeId(inside)]);
}
#[test]
fn the_rows_report_their_choice_as_the_selected_state() {
use egui::accesskit::Role;
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
app.run("artboard.new", json!({"x": 150, "y": 0, "width": 200, "height": 50, "name": "Banner"})).unwrap();
app.run("view.goToArtboard", json!({"index": 0})).unwrap();
menus::invoke(&mut app, super::COMMAND, serde_json::Value::Null);
let tree = crate::widgets::accessibility_tests::tree(|ui| crate::dialogs::show(&mut app, ui.ctx()));
let mut rows: Vec<_> =
tree.iter().filter(|n| n.role() == Role::ListItem).map(|n| (n.label().map(str::to_string), n.is_selected(), n.toggled())).collect();
rows.sort_by(|a, b| a.0.cmp(&b.0));
assert_eq!(
rows,
[(Some("1 Artboard 1 100 × 100 pt".to_string()), Some(true), None), (Some("2 Banner 200 × 50 pt".to_string()), Some(false), None)]
);
}
#[test]
fn double_clicking_a_row_fits_its_artboard_and_keeps_the_choice() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
+18 -1
View File
@@ -114,7 +114,7 @@ fn variable_row(ui: &mut Ui, row: &VarRow, selected: bool) -> Response {
let object_cell = Rect::from_min_max(pos2(name_right, rect.top()), rect.max);
ui.painter().with_clip_rect(object_cell).text(object_cell.left_center() + pad, left, &row.object, font, t.text_dim);
let full = if row.object.is_empty() { row.name.clone() } else { format!("{} · {}", row.name, row.object) };
r.widget_info(|| egui::WidgetInfo::selected(egui::Role::ListItem, true, selected, &full));
widgets::list_item_info(&r, selected, || full.clone());
// A column clips a long name, so the whole of it shows on hover.
r.on_hover_text(full)
}
@@ -707,6 +707,23 @@ mod tests {
assert_eq!(app.ui.status, "");
}
/// Each variable row is a list item, and only the highlighted one is selected.
#[test]
fn the_rows_report_the_highlight_as_the_selected_state() {
use egui::accesskit::Role;
let mut app = app_with_data();
app.session.execute("variable.define", &json!({"name": "age", "kind": "visibility"})).unwrap();
app.session.execute("variable.highlight", &json!({"variable": "Name"})).unwrap();
let nodes = Driver::new().nodes(&mut app);
let mut rows: Vec<_> = nodes
.iter()
.filter(|(_, n)| n.role() == Role::ListItem)
.map(|(_, n)| (n.label().map(str::to_string), n.is_selected(), n.toggled()))
.collect();
rows.sort_by(|a, b| a.0.cmp(&b.0));
assert_eq!(rows, [(Some("Name · Alice".to_string()), Some(true), None), (Some("age".to_string()), Some(false), None)]);
}
/// A click on a column header sorts the list by that column, and a second click on it
/// reverses the order; the rows are drawn in the order `variable.list` reports.
#[test]
+27
View File
@@ -105,6 +105,13 @@ pub(crate) fn id_with(parent: egui::Id, salt: impl std::hash::Hash) -> egui::Id
parent.with(HashOnly(salt))
}
/// Gives `resp` the screen-reader role of a list item named `label`, with `selected` as its
/// selected state. [`egui::WidgetInfo::selected`] would set the toggled state instead.
pub(crate) fn list_item_info(resp: &Response, selected: bool, label: impl Fn() -> String) {
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ListItem, true, label()));
resp.ctx.accesskit_node_builder(resp.id, |node| node.set_selected(selected));
}
#[cfg(test)]
pub(crate) mod accessibility_tests {
use egui::accesskit::{Node, Role, Toggled};
@@ -219,6 +226,26 @@ pub(crate) mod accessibility_tests {
on("Saved");
}
#[test]
fn list_items_report_selection_and_section_headers_report_expansion() {
let tree = tree(|ui| {
for (name, on) in [("Artboard 1", true), ("Artboard 2", false)] {
let resp = ui.allocate_response(vec2(120.0, 20.0), Sense::click());
list_item_info(&resp, on, || name.to_string());
}
section_toggle(ui, "Open Section", true);
section_toggle(ui, "Closed Section", false);
});
let state = |label| {
let n = named(&tree, label);
(n.role(), n.toggled(), n.is_selected(), n.is_expanded())
};
assert_eq!(state("Artboard 1"), (Role::ListItem, None, Some(true), None));
assert_eq!(state("Artboard 2"), (Role::ListItem, None, Some(false), None));
assert_eq!(state("Open Section"), (Role::DisclosureTriangle, None, None, Some(true)));
assert_eq!(state("Closed Section"), (Role::DisclosureTriangle, None, None, Some(false)));
}
#[test]
fn checkboxes_and_radio_buttons_are_named_and_checked() {
let tree = tree(|ui| {
+2 -1
View File
@@ -36,7 +36,8 @@ pub fn section_toggle_name(ui: &mut Ui, text: &str, open: bool) -> Response {
let chevron = Rect::from_center_size(pos2(rect.left() + 8.0, rect.center().y), vec2(12.0, 12.0));
icons::paint(ui, if open { "chevron-down" } else { "chevron-right" }, chevron, if resp.hovered() { t.text_strong } else { t.icon });
ui.painter().text(pos2(rect.left() + 18.0, rect.center().y), egui::Align2::LEFT_CENTER, text, theme::semibold(12.5), t.text);
resp.widget_info(|| egui::WidgetInfo::selected(egui::Role::DisclosureTriangle, true, open, text));
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::DisclosureTriangle, true, text));
ui.ctx().accesskit_node_builder(resp.id, |node| node.set_expanded(open));
resp.on_hover_cursor(egui::CursorIcon::PointingHand)
}