Add Response::accessible_name (#8604)

Useful for adding an accessibility name to a widget

---------

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:17:58 +00:00
1 parent 5705148d42
commit 65bf96beae
3 files changed
+34 -2

No files matched your search

+5 -2
View File
@@ -4083,8 +4083,11 @@ impl Context {
self.write(|ctx| ctx.accesskit_node_builder(id).map(writer))
}
/// Does the widget already have an accessibility node this pass?
pub(crate) fn has_accesskit_node(&self, id: Id) -> bool {
/// Does the widget with this id have a node in the accessibility tree this pass?
///
/// Only widgets that report [`WidgetInfo`](crate::WidgetInfo) do; a plain
/// [`Ui::allocate_rect`](crate::Ui::allocate_rect) does not.
pub fn has_accesskit_node(&self, id: Id) -> bool {
self.write(|ctx| {
ctx.viewport()
.this_pass
+19
View File
@@ -1083,6 +1083,25 @@ impl Response {
self
}
/// Name the widget in the accessibility tree when nothing visible names it,
/// e.g. an icon-only button or a text field without a label next to it.
///
/// Prefer [`Self::labelled_by`] when a visible label sits next to the widget.
///
/// ```
/// # egui::__run_test_ui(|ui| {
/// # let mut text = String::new();
/// ui.text_edit_singleline(&mut text).accessible_name("Search");
/// # });
/// ```
pub fn accessible_name(self, name: impl Into<String>) -> Self {
let name = name.into();
self.ctx.accesskit_node_builder(self.id, |builder| {
builder.set_label(name);
});
self
}
/// Response to secondary clicks (right-clicks) by showing the given menu.
///
/// Make sure the widget senses clicks (e.g. [`crate::Button`] does, [`crate::Label`] does not).
@@ -115,3 +115,13 @@ fn labelled_by_wins_over_drag_value_suffix() {
[Some("Width".to_owned()), Some("x: px".to_owned())]
);
}
#[test]
fn accessible_name_names_a_widget_without_a_label() {
let mut text = String::new();
let mut harness = Harness::new_ui(|ui| {
ui.text_edit_singleline(&mut text).accessible_name("Search");
});
harness.run();
harness.get_by_label("Search");
}