From f0abce9bb8591466ce01f741eced5b271d3a4dc1 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Wed, 11 Jun 2025 23:00:59 +0200 Subject: [PATCH] `Button` inherits the `alt_text` of the `Image` in it, if any (#7136) If a `Button` has an `Image` in it (and no text), then the `Image::alt_text` will be used as the accessibility label for the button. --- crates/egui/src/widgets/button.rs | 10 +++++++--- crates/egui/src/widgets/image.rs | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/egui/src/widgets/button.rs b/crates/egui/src/widgets/button.rs index a75997ef..9ae573ae 100644 --- a/crates/egui/src/widgets/button.rs +++ b/crates/egui/src/widgets/button.rs @@ -314,11 +314,15 @@ impl Widget for Button<'_> { let (rect, mut response) = ui.allocate_at_least(desired_size, sense); response.widget_info(|| { + let mut widget_info = WidgetInfo::new(WidgetType::Button); + widget_info.enabled = ui.is_enabled(); + if let Some(galley) = &galley { - WidgetInfo::labeled(WidgetType::Button, ui.is_enabled(), galley.text()) - } else { - WidgetInfo::new(WidgetType::Button) + widget_info.label = Some(galley.text().to_owned()); + } else if let Some(image) = &image { + widget_info.label = image.alt_text.clone(); } + widget_info }); if ui.is_rect_visible(rect) { diff --git a/crates/egui/src/widgets/image.rs b/crates/egui/src/widgets/image.rs index e0ff7d36..07b08e53 100644 --- a/crates/egui/src/widgets/image.rs +++ b/crates/egui/src/widgets/image.rs @@ -54,7 +54,7 @@ pub struct Image<'a> { sense: Sense, size: ImageSize, pub(crate) show_loading_spinner: Option, - alt_text: Option, + pub(crate) alt_text: Option, } impl<'a> Image<'a> {