From 041736753219759a73e797c02e423ed694832d24 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 4 Nov 2026 04:39:49 -0800 Subject: [PATCH] Accessibility: screen readers can set a dialog slider's number Dialog sliders and the Blob Brush tip preview offered screen readers only the increment and decrement actions. They now also offer the set-value action: the last request in a frame sets the number within the slider's range, rounded to the places its field shows, and a value that is not a finite number leaves it as it is. --- crates/ui-egui/src/dialogs/tests.rs | 42 +++++++++++++++++++++++++++++ crates/ui-egui/src/widgets/color.rs | 39 ++++++++++++++++++++++----- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/dialogs/tests.rs b/crates/ui-egui/src/dialogs/tests.rs index ef3bf88483..9b6365ac87 100644 --- a/crates/ui-egui/src/dialogs/tests.rs +++ b/crates/ui-egui/src/dialogs/tests.rs @@ -2015,6 +2015,48 @@ fn the_blob_brush_tip_preview_is_a_named_slider_with_arrow_keys() { assert_eq!(angle(&app), -44.0, "the drag gives the preview the focus"); } +/// Screen readers can set the Blob Brush tip preview's Angle to a number: the slider offers the +/// set-value action, a fraction rounds to the whole degrees the field shows, a number past -180° to +/// 180° sets the nearer end, the last request in a frame wins, and a value that is not a finite +/// number leaves the Angle as it is. +#[test] +fn screen_readers_set_the_blob_brush_angle() { + use egui::accesskit::{Action, ActionData, ActionRequest, TreeId}; + let mut app = app(); + app.run("tool.options", json!({"tool": "blobBrush"})).unwrap(); + let angle = |app: &VectorcraftApp| app.ui.dialog.as_ref().expect("Blob Brush Tool Options is open").f64("angle", f64::NAN); + let mut f = Frames::new(); + f.run(&mut app, vec![]); + let out = f.run(&mut app, vec![]); + let (node, ..) = slider_node(&out, "Angle"); + let nodes = &out.platform_output.accesskit_update.as_ref().expect("an accessibility tree").nodes; + let advertised = nodes.iter().find(|(id, _)| *id == node).is_some_and(|(_, n)| n.supports_action(Action::SetValue)); + assert!(advertised, "the Angle slider offers the set-value action"); + let set = |value: f64| { + egui::Event::AccessKitActionRequest(ActionRequest { + action: Action::SetValue, + target_node: node, + target_tree: TreeId::ROOT, + data: Some(ActionData::NumericValue(value)), + }) + }; + f.run(&mut app, vec![set(30.0)]); + assert_eq!(angle(&app), 30.0); + f.run(&mut app, vec![set(12.4)]); + assert_eq!(angle(&app), 12.0, "the field shows whole degrees"); + let shown = crate::widgets::SliderValue::field(12.5, -180.0..=180.0, "°", 0).value; + f.run(&mut app, vec![set(12.5)]); + assert_eq!(angle(&app), shown, "as the field shows 12.5"); + f.run(&mut app, vec![set(500.0)]); + assert_eq!(angle(&app), 180.0, "past the end"); + f.run(&mut app, vec![set(-12.0), set(-90.0)]); + assert_eq!(angle(&app), -90.0, "the last request"); + f.run(&mut app, vec![set(f64::NAN)]); + assert_eq!(angle(&app), -90.0, "not a number"); + f.run(&mut app, vec![set(30.0), set(f64::INFINITY)]); + assert_eq!(angle(&app), -90.0, "the last request is not a finite number"); +} + #[test] fn polar_grid_shared_canvas_click_defaults_units_counts_and_history() { use vectorcraft_doc::NodeId; diff --git a/crates/ui-egui/src/widgets/color.rs b/crates/ui-egui/src/widgets/color.rs index bd40e174d8..747de05cb1 100644 --- a/crates/ui-egui/src/widgets/color.rs +++ b/crates/ui-egui/src/widgets/color.rs @@ -42,6 +42,9 @@ pub struct SliderValue { /// steps from `value` (±1 for an arrow, ±10 with Shift or Page Up/Down). `None` steps to whole /// numbers. pub step: Option f64>, + /// The places the field shows, which a screen reader's set-value action rounds to; `None` + /// keeps the number it sets. + pub decimals: Option, } /// Two values are the same when they show the same number, range and text, whatever their keys' steps @@ -55,20 +58,20 @@ impl PartialEq for SliderValue { impl SliderValue { /// `value` on a rail over `range`, without a text. pub fn number(value: f64, range: std::ops::RangeInclusive) -> Self { - Self { value, range, text: None, step: None } + Self { value, range, text: None, step: None, decimals: None } } /// `value` on a rail over `range` as [`plain_field`] shows it with `suffix` and at most /// `decimals` places: the number it shows (`value` rounded) and its text. pub fn field(value: f64, range: std::ops::RangeInclusive, suffix: &str, decimals: usize) -> Self { let number = field_text(value, "", decimals); - Self { value: number.parse().unwrap_or(value), range, text: Some(format!("{number}{suffix}")), step: None } + Self { value: number.parse().unwrap_or(value), range, text: Some(format!("{number}{suffix}")), step: None, decimals: Some(decimals) } } /// `value` on a rail over `range`, with `text`, for a field that writes its own text (a length /// in the document's unit, an egui `DragValue`). pub fn text(value: f64, range: std::ops::RangeInclusive, text: String) -> Self { - Self { value, range, text: Some(text), step: None } + Self { value, range, text: Some(text), step: None, decimals: None } } /// This value, stepped by the keys with `step` instead of to whole numbers (see @@ -119,8 +122,10 @@ pub fn color_slider_valued( /// While it has the focus, → and ↑ raise the number `shown` holds to the next whole number and ← /// and ↓ lower it to the next whole number, Shift with an arrow, Page Up and Page Down add or take /// ten, and Home and End set the start and the end of `shown`'s range. Screen readers' increment -/// and decrement actions step the number as → and ← do. A number past the range steps from the -/// range's nearer end, and no step goes past either end. With `shown` `None` (a blank field), or +/// and decrement actions step the number as → and ← do, and their set-value action sets it within +/// the range, rounded to [`SliderValue::decimals`] (a value that is not a finite number leaves it +/// as it is). A number past the range steps from the range's nearer end, and no step goes past +/// either end. With `shown` `None` (a blank field), or /// in a disabled `ui`, the keys and actions leave the rail as it is. Returns the new value from 0 /// to 1 that a click or a drag gives, its phase, and the number in `shown`'s units that a key or /// an action gives. @@ -167,6 +172,7 @@ fn slider_keys(ui: &Ui, resp: &Response, shown: Option) -> Option) -> Option Option { - use egui::accesskit::Action; + use egui::accesskit::{Action, ActionData}; use egui::{Event, Key}; let (lo, hi) = (*s.range.start(), *s.range.end()); if !lo.is_finite() || !hi.is_finite() || hi <= lo { @@ -235,11 +241,30 @@ fn rail_step(ui: &Ui, id: egui::Id, focused: bool, s: &SliderValue) -> Option Some(value), + _ => None, + }) + .last() + .filter(|value| value.is_finite()); + if let Some(value) = set { + // The number the field would show, as `SliderValue::field` reads it. + let shown = s.decimals.map_or(value, |d| field_text(value, "", d).parse().unwrap_or(value)); + v = shown.clamp(lo, hi); + end = true; + exact = true; + } }); - (v != from || (end && v != s.value)).then_some(round_to(v, STEP_DECIMALS).clamp(lo, hi)) + (v != from || (end && v != s.value)).then_some(if exact { v } else { round_to(v, STEP_DECIMALS).clamp(lo, hi) }) } /// The rail of [`color_slider_valued`] with the id `id`: its response, the new value from 0 to 1