1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! What the platform says about the assistive technology in use, for an app
//! that speaks guidance, slows an automatic step down or drops a gesture-only
//! path while a screen reader is on.
use cranpose_core::{CompositionLocal, CompositionLocalProvider, compositionLocalOf};
use cranpose_macros::composable;
/// What the platform reports about the assistive technology in use.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct AccessibilityState {
/// Whether a screen reader is on: VoiceOver on iOS, a service such as
/// TalkBack on Android, or a reader that connected to the app through
/// accesskit on the desktop. The web has no such signal and says no.
pub screen_reader_on: bool,
}
/// Installs what the platform reports. A backend calls this on every check
/// and forces a root render when the answer is true, so composition reads
/// the new state.
pub fn set_platform_accessibility_state(state: AccessibilityState) -> bool {
crate::thread_state::with(|thread| thread.accessibility_state.replace(state) != state)
}
/// What the platform last reported.
pub fn platform_accessibility_state() -> AccessibilityState {
crate::thread_state::with(|state| state.accessibility_state.get())
}
/// The state a composable reads: what the platform reported, unless a
/// [`ProvideAccessibilityState`] above it says otherwise.
pub fn local_accessibility_state() -> CompositionLocal<AccessibilityState> {
crate::composition_locals::cached_local(
|locals| &locals.accessibility_state,
|| compositionLocalOf(platform_accessibility_state),
)
}
/// Gives the content below it a fixed state, for a preview or a test that
/// wants to see the app as a screen reader user does.
#[composable]
pub fn ProvideAccessibilityState(state: AccessibilityState, content: impl FnOnce()) {
let local = local_accessibility_state();
CompositionLocalProvider([local.provides(state)], move || {
content();
});
}
#[cfg(test)]
#[path = "tests/accessibility_state_tests.rs"]
mod tests;