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.
This commit is contained in:
dotCooCoo
2026-11-04 21:37:57 +07:00
committed by bflatastic
parent 84f528f5bb
commit 0417367532
2 changed files with 74 additions and 7 deletions
+42
View File
@@ -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;
+32 -7
View File
@@ -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<fn(f64, f64) -> 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<usize>,
}
/// 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<f64>) -> 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<f64>, 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<f64>, 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<SliderValue>) -> Option<f
let stepped = shown.filter(|_| ui.is_enabled()).and_then(|s| {
ui.ctx().accesskit_node_builder(id, |node| {
node.set_numeric_value_step(1.0);
node.add_action(egui::accesskit::Action::SetValue);
node.add_action(egui::accesskit::Action::Increment);
node.add_action(egui::accesskit::Action::Decrement);
});
@@ -185,7 +191,7 @@ fn slider_keys(ui: &Ui, resp: &Response, shown: Option<SliderValue>) -> Option<f
/// The number of slider `id` (`s`) after this frame's keys while it is `focused`, and after screen
/// readers' actions on it ([`color_slider_stepped`]); `None` when they leave it as it was.
fn rail_step(ui: &Ui, id: egui::Id, focused: bool, s: &SliderValue) -> Option<f64> {
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<f6
ui.memory_mut(|m| m.move_focus(egui::FocusDirection::None));
}
}
// A set-value request is rounded to the field's places when it has them and otherwise kept as
// asked; only the keys' and the steps' numbers are rounded to `STEP_DECIMALS`.
let mut exact = false;
ui.input(|i| {
let n = i.num_accesskit_action_requests(id, Action::Increment) as f64 - i.num_accesskit_action_requests(id, Action::Decrement) as f64;
v = by(v, n).clamp(lo, hi);
// The last set-value request this frame, within the range, which also replaces a number past it.
let set = i
.accesskit_action_requests(id, Action::SetValue)
.filter_map(|request| match request.data {
Some(ActionData::NumericValue(value)) => 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