Add Area::role and UiBuilder::accessibility_role (#8612)

A `Ui` gets its accessibility role from its `UiKind` (#8581). This lets
a caller pick another one, e.g. `Role::Alert` for a toast:
`Area::new(id).role(Role::Alert).accessible_name("Saved")`, or
`UiBuilder::new().accessibility_role(role)` for any `Ui`.

Rerun sets the toast role by reaching into the node from inside the `Ui`
today; @emilk asked for a builder method instead.

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 19:10:35 +00:00
1 parent f45d74baab
commit 8709bd14b4
4 files changed
+55 -8

No files matched your search

+16
View File
@@ -124,6 +124,7 @@ pub struct Area {
sizing_pass: bool,
accessibility_parent: Option<Id>,
accessibility_label: Option<String>,
accessibility_role: Option<crate::accesskit::Role>,
}
impl WidgetWithState for Area {
@@ -153,6 +154,7 @@ impl Area {
sizing_pass: false,
accessibility_parent: None,
accessibility_label: None,
accessibility_role: None,
}
}
@@ -202,6 +204,14 @@ impl Area {
self
}
/// Give the area a role in the accessibility tree other than the one its [`UiKind`] implies,
/// e.g. [`Role::Alert`](crate::accesskit::Role::Alert) for a toast.
#[inline]
pub fn role(mut self, role: crate::accesskit::Role) -> Self {
self.accessibility_role = Some(role);
self
}
pub fn layer(&self) -> LayerId {
LayerId::new(self.order, self.id)
}
@@ -424,6 +434,7 @@ pub(crate) struct Prepared {
fade_in: bool,
layout: Layout,
accessibility_label: Option<String>,
accessibility_role: Option<crate::accesskit::Role>,
}
impl Area {
@@ -460,6 +471,7 @@ impl Area {
sizing_pass: force_sizing_pass,
accessibility_parent,
accessibility_label,
accessibility_role,
} = self;
let constrain_rect = constrain_rect.unwrap_or_else(|| ctx.content_rect());
@@ -613,6 +625,7 @@ impl Area {
fade_in,
layout,
accessibility_label,
accessibility_role,
}
}
}
@@ -654,6 +667,9 @@ impl Prepared {
if let Some(label) = self.accessibility_label.take() {
ui_builder = ui_builder.accessibility_label(label);
}
if let Some(role) = self.accessibility_role {
ui_builder = ui_builder.accessibility_role(role);
}
if !self.enabled {
ui_builder = ui_builder.disabled();
+13 -8
View File
@@ -105,6 +105,7 @@ impl Ui {
sense,
accessibility_parent,
accessibility_label,
accessibility_role,
classes,
} = ui_builder;
@@ -181,10 +182,11 @@ impl Ui {
ui.set_invisible();
}
let role = ui
.stack
.kind()
.map_or(accesskit::Role::GenericContainer, UiKind::accesskit_role);
let role = accessibility_role.unwrap_or_else(|| {
ui.stack
.kind()
.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 {
@@ -218,6 +220,7 @@ impl Ui {
sense,
accessibility_parent,
accessibility_label,
accessibility_role,
classes,
} = ui_builder;
@@ -311,10 +314,12 @@ impl Ui {
Default::default(),
);
let role = child_ui
.stack
.kind()
.map_or(accesskit::Role::GenericContainer, UiKind::accesskit_role);
let role = accessibility_role.unwrap_or_else(|| {
child_ui
.stack
.kind()
.map_or(accesskit::Role::GenericContainer, UiKind::accesskit_role)
});
child_ui
.ctx()
.accesskit_node_builder(child_ui.unique_id, |node| {
+9
View File
@@ -29,6 +29,7 @@ pub struct UiBuilder {
pub sense: Option<Sense>,
pub accessibility_parent: Option<Id>,
pub accessibility_label: Option<String>,
pub accessibility_role: Option<crate::accesskit::Role>,
pub classes: Classes,
}
@@ -214,6 +215,14 @@ impl UiBuilder {
self.accessibility_label = Some(label.into());
self
}
/// Give this [`Ui`]'s node a role other than the one its [`UiKind`](crate::UiKind) implies,
/// e.g. [`Role::Alert`](crate::accesskit::Role::Alert) for a toast.
#[inline]
pub fn accessibility_role(mut self, role: crate::accesskit::Role) -> Self {
self.accessibility_role = Some(role);
self
}
}
impl HasClasses for UiBuilder {
+17
View File
@@ -674,3 +674,20 @@ fn window_resize_handles_are_named() {
harness.get_by_role_and_label(Role::Splitter, name);
}
}
/// An area can take a role of its own, e.g. a toast is an alert.
#[test]
fn area_with_a_role() {
let mut harness = Harness::new_ui(|ui| {
egui::Area::new(egui::Id::unique("toast"))
.role(Role::Alert)
.accessible_name("Saved")
.show(ui.ctx(), |ui| {
ui.label("The file was saved");
});
});
harness.run();
harness
.get_by_role_and_label(Role::Alert, "Saved")
.get_by_label("The file was saved");
}