From 9473dbdde16e32d3ecfca604fb4de1608620d805 Mon Sep 17 00:00:00 2001 From: Matt Campbell Date: Tue, 29 Nov 2022 14:03:27 -0600 Subject: [PATCH] Properly associate the slider label with both the slider and the drag value --- crates/egui/src/widgets/slider.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/crates/egui/src/widgets/slider.rs b/crates/egui/src/widgets/slider.rs index d55960c9..30a5948a 100644 --- a/crates/egui/src/widgets/slider.rs +++ b/crates/egui/src/widgets/slider.rs @@ -756,7 +756,9 @@ impl<'a> Slider<'a> { } } - if self.show_value { + let slider_response = response.clone(); + + let value_response = if self.show_value { let position_range = self.position_range(&response.rect); let value_response = self.value_ui(ui, position_range); if value_response.gained_focus() @@ -768,12 +770,23 @@ impl<'a> Slider<'a> { response = value_response.union(response); } else { // Use the slider id as the id for the whole widget - response = response.union(value_response); + response = response.union(value_response.clone()); } - } + Some(value_response) + } else { + None + }; if !self.text.is_empty() { - ui.add(Label::new(self.text.clone()).wrap(false)); + let label_response = ui.add(Label::new(self.text.clone()).wrap(false)); + // The slider already has an accessibility label via widget info, + // but sometimes it's useful for a screen reader to know + // that a piece of text is a label for another widget, + // e.g. so the text itself can be excluded from navigation. + slider_response.labelled_by(label_response.id); + if let Some(value_response) = value_response { + value_response.labelled_by(label_response.id); + } } response