From 0777ca67aa7fdeaacf193db6f325fad5f2e1bd2a Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 21 Sep 2026 14:47:24 +0200 Subject: [PATCH] Name a `Ui`, `Area`, `Popup` or `Tooltip` in the accessibility tree (#8607) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #8581, which gave every `Ui` a role. This adds the name: `UiBuilder::accessibility_label` sets the label on the `Ui`'s node, and `Area`, `Popup` and `Tooltip` take an `accessible_name` and pass it on. A floating container can then be found by name (`get_by_role_and_label(Role::Dialog, "Options")`) instead of by reconstructing its node id. Rerun does that reconstruction today for its modals, toasts and hover tooltips. Written by Claude (an LLM) for @emilk. * [x] I have followed the instructions in the PR template 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Fable 5.1 --- crates/egui/src/containers/area.rs | 18 +++++++++++++ crates/egui/src/containers/popup.rs | 15 +++++++++++ crates/egui/src/containers/tooltip.rs | 9 +++++++ crates/egui/src/ui.rs | 8 ++++++ crates/egui/src/ui_builder.rs | 11 ++++++++ crates/egui_kittest/tests/accesskit.rs | 36 ++++++++++++++++++++++++++ 6 files changed, 97 insertions(+) diff --git a/crates/egui/src/containers/area.rs b/crates/egui/src/containers/area.rs index 6cc6bfc7..a4dbb3e0 100644 --- a/crates/egui/src/containers/area.rs +++ b/crates/egui/src/containers/area.rs @@ -123,6 +123,7 @@ pub struct Area { layout: Layout, sizing_pass: bool, accessibility_parent: Option, + accessibility_label: Option, } impl WidgetWithState for Area { @@ -151,6 +152,7 @@ impl Area { layout: Layout::default(), sizing_pass: false, accessibility_parent: None, + accessibility_label: None, } } @@ -191,6 +193,15 @@ impl Area { self } + /// Name the area in the accessibility tree. + /// + /// The role comes from the [`UiKind`]; the name is what a screen reader or a test finds it by. + #[inline] + pub fn accessible_name(mut self, name: impl Into) -> Self { + self.accessibility_label = Some(name.into()); + self + } + pub fn layer(&self) -> LayerId { LayerId::new(self.order, self.id) } @@ -412,6 +423,7 @@ pub(crate) struct Prepared { fade_in: bool, layout: Layout, + accessibility_label: Option, } impl Area { @@ -447,6 +459,7 @@ impl Area { layout, sizing_pass: force_sizing_pass, accessibility_parent, + accessibility_label, } = self; let constrain_rect = constrain_rect.unwrap_or_else(|| ctx.content_rect()); @@ -599,6 +612,7 @@ impl Area { sizing_pass, fade_in, layout, + accessibility_label, } } } @@ -637,6 +651,10 @@ impl Prepared { .accessibility_parent(self.move_response.id) .closable(); + if let Some(label) = self.accessibility_label.take() { + ui_builder = ui_builder.accessibility_label(label); + } + if !self.enabled { ui_builder = ui_builder.disabled(); } diff --git a/crates/egui/src/containers/popup.rs b/crates/egui/src/containers/popup.rs index 778c4d2e..ce87d135 100644 --- a/crates/egui/src/containers/popup.rs +++ b/crates/egui/src/containers/popup.rs @@ -186,6 +186,7 @@ pub struct Popup<'a> { frame: Option, style: StyleModifier, anchor_widget: Option, + accessibility_label: Option, } impl<'a> Popup<'a> { @@ -211,6 +212,7 @@ impl<'a> Popup<'a> { frame: None, style: StyleModifier::default(), anchor_widget: None, + accessibility_label: None, } } @@ -372,6 +374,15 @@ impl<'a> Popup<'a> { self } + /// Name the popup in the accessibility tree. + /// + /// See [`Area::accessible_name`]. + #[inline] + pub fn accessible_name(mut self, name: impl Into) -> Self { + self.accessibility_label = Some(name.into()); + self + } + /// Set the gap between the anchor and the popup. #[inline] pub fn gap(mut self, gap: f32) -> Self { @@ -591,6 +602,7 @@ impl<'a> Popup<'a> { frame, style, anchor_widget, + accessibility_label, } = self; if kind != PopupKind::Tooltip { @@ -630,6 +642,9 @@ impl<'a> Popup<'a> { if let Some(anchor_widget) = anchor_widget { area = area.accessibility_parent(anchor_widget); } + if let Some(label) = accessibility_label { + area = area.accessible_name(label); + } let mut response = area.show(&ctx, |ui| { style.apply(ui.style_mut()); diff --git a/crates/egui/src/containers/tooltip.rs b/crates/egui/src/containers/tooltip.rs index 73b46e46..d6dd38b5 100644 --- a/crates/egui/src/containers/tooltip.rs +++ b/crates/egui/src/containers/tooltip.rs @@ -91,6 +91,15 @@ impl Tooltip<'_> { self } + /// Name the tooltip in the accessibility tree. + /// + /// See [`Area::accessible_name`](crate::Area::accessible_name). + #[inline] + pub fn accessible_name(mut self, name: impl Into) -> Self { + self.popup = self.popup.accessible_name(name); + self + } + /// Set the width of the tooltip #[inline] pub fn width(mut self, width: f32) -> Self { diff --git a/crates/egui/src/ui.rs b/crates/egui/src/ui.rs index 3258b36c..e99fa070 100644 --- a/crates/egui/src/ui.rs +++ b/crates/egui/src/ui.rs @@ -104,6 +104,7 @@ impl Ui { style, sense, accessibility_parent, + accessibility_label, classes, } = ui_builder; @@ -186,6 +187,9 @@ impl Ui { .map_or(accesskit::Role::GenericContainer, UiKind::accesskit_role); ui.ctx().accesskit_node_builder(ui.unique_id, |node| { node.set_role(role); + if let Some(label) = accessibility_label { + node.set_label(label); + } }); ui @@ -213,6 +217,7 @@ impl Ui { style, sense, accessibility_parent, + accessibility_label, classes, } = ui_builder; @@ -314,6 +319,9 @@ impl Ui { .ctx() .accesskit_node_builder(child_ui.unique_id, |node| { node.set_role(role); + if let Some(label) = accessibility_label { + node.set_label(label); + } }); child_ui diff --git a/crates/egui/src/ui_builder.rs b/crates/egui/src/ui_builder.rs index 2eb14484..faa85f15 100644 --- a/crates/egui/src/ui_builder.rs +++ b/crates/egui/src/ui_builder.rs @@ -28,6 +28,7 @@ pub struct UiBuilder { pub style: Option>, pub sense: Option, pub accessibility_parent: Option, + pub accessibility_label: Option, pub classes: Classes, } @@ -203,6 +204,16 @@ impl UiBuilder { self.accessibility_parent = Some(parent_id); self } + + /// Name this [`Ui`] in the accessibility tree. + /// + /// Every `Ui` is a node there, with a role from its [`UiKind`](crate::UiKind). + /// A name lets a screen reader, or a test, tell this panel, popup or region from the others. + #[inline] + pub fn accessibility_label(mut self, label: impl Into) -> Self { + self.accessibility_label = Some(label.into()); + self + } } impl HasClasses for UiBuilder { diff --git a/crates/egui_kittest/tests/accesskit.rs b/crates/egui_kittest/tests/accesskit.rs index 2017f9c9..c9ec0c60 100644 --- a/crates/egui_kittest/tests/accesskit.rs +++ b/crates/egui_kittest/tests/accesskit.rs @@ -336,3 +336,39 @@ fn has_child_recursively(tree: &TreeUpdate, parent: NodeId, child: NodeId) -> bo false } + +/// A `Ui`, an area and a popup can be named, and then found by that name. +#[test] +fn named_ui_area_and_popup() { + let mut harness = Harness::new_ui(|ui| { + ui.scope_builder( + egui::UiBuilder::new().accessibility_label("Toolbox"), + |ui| { + let _ = ui.button("Hammer"); + }, + ); + + egui::Area::new(egui::Id::unique("area")) + .accessible_name("Floating notes") + .show(ui.ctx(), |ui| { + ui.label("A note"); + }); + + let response = ui.button("Open"); + egui::Popup::from_response(&response) + .open(true) + .accessible_name("Options") + .show(|ui| { + let _ = ui.button("Option A"); + }); + }); + harness.run(); + + harness.get_by_label("Toolbox").get_by_label("Hammer"); + harness + .get_by_label("Floating notes") + .get_by_label("A note"); + harness + .get_by_role_and_label(Role::Dialog, "Options") + .get_by_label("Option A"); +}