# Accessibility
Every window has one semantics tree. Screen readers read it (UI Automation on Windows,
NSAccessibility on macOS, AT-SPI on Linux, Android's accessibility service), tests check it,
and agents operate through it ([`crate::guide::agents`]). Getting it right once serves all
three.
The tree is built from what elements declare. The engine's widgets declare it for you: a
button is a button named by its label, a switch says whether it is on, a password field
gives its label and never its text. Custom elements declare their own. Nothing is sent to a
platform until a screen reader asks, and after that only what changed.
This page covers reading the tree, describing custom elements, merging, announcements,
the keyboard, and the accessibility check.
## The tree as a screen reader meets it
[`Ui::reader_view`](crate::Ui::reader_view) lists the window as a screen reader meets it,
in reading order: each node's role, name, value, states and actions, with hidden content
left out and merged nodes folded. It is the most useful view for tests.
```
use remedy_ui::testing::TestApp;
use remedy_ui::reactive::Signal;
use remedy_ui::widgets as w;
let wifi = Signal::new(true);
.reader_view()
.into_iter()
.filter_map(|n| n.name.map(|name| format!("{name} {:?} {}", n.role, n.states.join(" "))))
.collect();
assert!(lines.contains(&"Network Heading(1) ".to_string()));
assert!(lines.contains(&"Wi-Fi Switch checked".to_string()));
// An icon button is named by the label it was given, not by its picture.
assert!(lines.contains(&"Network settings Button ".to_string()));
```
[`widgets::icon_button`](crate::widgets::icon_button) takes its label as a required
argument, and shows it as a tooltip too. A control with only a picture and no name is the
most common accessibility bug, so the API does not let you write one.
## Describing a custom element
An element built from scratch (a canvas, a custom control) declares what it is with
[`El::semantics`](crate::El::semantics), given a closure when it follows signals. `Semantics` and `Role` are in
[`a11y`](crate::a11y). Say what the element is (its role), what it
is called, its value, its states, and what can be done to it. The actions it lists are what
[`Ui::accessibility_action`](crate::Ui::accessibility_action), screen readers and agents can
ask for; [`El::on_click`](crate::El::on_click) handles `Click`.
```
use remedy_ui::testing::TestApp;
use remedy_ui::reactive::Signal;
use remedy_ui::view::A11yAction;
use remedy_ui::widgets as w;
use remedy_ui::a11y::{Action, Role, Semantics};
let stars = Signal::new(3u8);
unreachable!("the rating is in the tree")
};
assert_eq!(node.value.as_deref(), Some("3 of 5 stars"));
assert!(ui.accessibility_action(node.id, A11yAction::Click));
app.frames(1);
assert_eq!(stars.get(), 4);
```
Prefer the engine's widgets where one fits: a [`widgets::slider`](crate::widgets::slider)
already handles the arrow keys, increment and decrement, and its value.
## Merging compound rows
A chat message is an avatar, a name, a time and the text. A screen reader should move
message by message, not stop at each piece.
[`Semantics::merge_descendants`](crate::a11y::Semantics::merge_descendants) folds a row's
descendants into one node with a composed name:
```
use remedy_ui::testing::TestApp;
use remedy_ui::widgets as w;
use remedy_ui::a11y::{Role, Semantics};
ui.root()
.child(w::button(ui, "Back", || {}))
.child(w::button(ui, "Next", || {}));
});
let Some(ui) = app.ui() else { unreachable!("the window is open") };
for expected in ["Back", "Next"] {
app.key(KeyEvent::press(Key::Tab));
assert_eq!(focused(&ui).as_deref(), Some(expected));
}
```
## The accessibility check
[`devtools::audit`](crate::devtools::audit) (feature `devtools`) checks a window the way an
accessibility review would and returns each [`Problem`](crate::devtools::Problem) with the
source line that built the element:
- controls with no name, and controls a screen reader cannot reach or activate;
- text below WCAG AA contrast against what is behind it;
- pointer handling with no keyboard way to do the same;
- focusable elements whose focus does not show;
- targets smaller than 24 × 24;
- content drawn past the window's edge.
The inspector (F12 in a debug build) runs it live on its Accessibility page, next to the
window as a screen reader meets it. Run it in tests too:
```
use remedy_ui::testing::TestApp;
use remedy_ui::devtools::{ProblemKind, audit};
use remedy_ui::widgets as w;
// A clickable square with no name and nothing to tell a screen reader.
let mut app = TestApp::window(|ui, _| {
ui.root().child(w::icon_button(ui, w::Icon::Plus, "Add color", || {}));
});
let Some(ui) = app.ui() else { unreachable!("the window is open") };
let problems = audit(&ui);
assert!(problems.is_empty(), "{:?}", problems.iter().map(|p| p.message()).collect::<Vec<_>>());
```
Every color pair the engine's widgets use meets WCAG contrast in all 24 Remedy themes (the
twelve palettes, each also in high contrast), and the engine's tests hold it there.
## Contrast and size
- Text colors come from [`Theme::derive`](crate::style::Theme::derive), balanced to meet
WCAG contrast on the background. Use them, not fixed colors.
- [`widgets::MIN_TARGET`](crate::widgets::MIN_TARGET) is the smallest size of anything a
pointer can press (24 px, WCAG 2.2); [`widgets::min_target`](crate::widgets::min_target)
keeps an element at least that tall.
- The person's text size setting scales text; layouts must leave text room to grow. Previews
at larger text scales show where they do not (see [`crate::guide::testing`]).