Name a Ui, Area, Popup or Tooltip in the accessibility tree (#8607)

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 <noreply@anthropic.com>
This commit is contained in:
Emil ErnerfeldtandClaude Fable 5.1 authored and GitHub committed 2026-09-21 12:47:24 +00:00
1 parent 65bf96beae
commit 0777ca67aa
6 files changed
+97

No files matched your search

+18
View File
@@ -123,6 +123,7 @@ pub struct Area {
layout: Layout,
sizing_pass: bool,
accessibility_parent: Option<Id>,
accessibility_label: Option<String>,
}
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<String>) -> 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<String>,
}
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();
}
+15
View File
@@ -186,6 +186,7 @@ pub struct Popup<'a> {
frame: Option<Frame>,
style: StyleModifier,
anchor_widget: Option<Id>,
accessibility_label: Option<String>,
}
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<String>) -> 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());
+9
View File
@@ -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<String>) -> Self {
self.popup = self.popup.accessible_name(name);
self
}
/// Set the width of the tooltip
#[inline]
pub fn width(mut self, width: f32) -> Self {
+8
View File
@@ -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
+11
View File
@@ -28,6 +28,7 @@ pub struct UiBuilder {
pub style: Option<Arc<Style>>,
pub sense: Option<Sense>,
pub accessibility_parent: Option<Id>,
pub accessibility_label: Option<String>,
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<String>) -> Self {
self.accessibility_label = Some(label.into());
self
}
}
impl HasClasses for UiBuilder {
+36
View File
@@ -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");
}