-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathadapter.rs
More file actions
269 lines (235 loc) · 8.19 KB
/
adapter.rs
File metadata and controls
269 lines (235 loc) · 8.19 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use accesskit::{Point, Role, Toggled, TreeUpdate};
use accesskit_consumer::{Node, Tree, TreeChangeHandler};
use crate::filter::filter;
fn describe_role(node: &Node<'_>) -> Option<String> {
let role_desc = match node.role() {
Role::Button => "button",
Role::CheckBox => "checkbox",
Role::TextInput => "text input",
Role::MultilineTextInput => "multiline text input",
Role::Document => "document",
Role::ProgressIndicator => "progress indicator",
Role::ScrollBar => "scrollbar",
Role::ScrollView => "scroll view",
Role::Splitter => "splitter",
Role::Image => "image",
_ => return None,
};
Some(role_desc.to_string())
}
fn describe_value(node: &Node<'_>) -> Option<String> {
node.value()
.map(|value| {
if value.is_empty() {
"blank".to_string()
} else if node.role() == Role::PasswordInput {
format!("{} characters", value.len())
} else {
value
}
})
.or_else(|| {
node.numeric_value().map(|numeric_value| {
let min = node.min_numeric_value();
let max = node.max_numeric_value();
match (min, max) {
(Some(min), Some(max)) if max > min => {
let percentage = ((numeric_value - min) / (max - min)) * 100.0;
format!("{:.1}%", percentage)
}
_ => numeric_value.to_string(),
}
})
})
}
fn describe_state(node: &Node<'_>) -> String {
let mut states = Vec::new();
if node.is_disabled() {
states.push("disabled");
}
if node.is_read_only_supported() && node.is_read_only() {
states.push("readonly");
}
if let Some(toggled) = node.toggled() {
match toggled {
Toggled::True => states.push("checked"),
Toggled::False => states.push("unchecked"),
Toggled::Mixed => states.push("partially checked"),
}
}
states.join(", ")
}
fn describe_node(node: &Node<'_>) -> String {
let mut parts = Vec::new();
if !node.label_comes_from_value() {
if let Some(label) = node.label() {
parts.push(label);
}
} else if let Some(value) = node.value() {
parts.push(value);
}
if let Some(role_desc) = describe_role(node) {
parts.push(role_desc);
}
let state_info = describe_state(node);
if !state_info.is_empty() {
parts.push(state_info);
}
if let Some(value_info) = describe_value(node) {
parts.push(value_info);
}
if let Some(placeholder) = node.placeholder() {
parts.push(format!("placeholder: {}", placeholder));
}
parts.join(", ")
}
struct ScreenReaderChangeHandler {
messages: Vec<String>,
}
impl ScreenReaderChangeHandler {
fn new() -> Self {
Self {
messages: Vec::new(),
}
}
}
impl TreeChangeHandler for ScreenReaderChangeHandler {
fn node_added(&mut self, _node: &Node<'_>) {}
fn node_updated(&mut self, old_node: &Node<'_>, new_node: &Node<'_>) {
if new_node.is_focused() {
let old_toggled = old_node.toggled();
let new_toggled = new_node.toggled();
if old_toggled != new_toggled {
let description = describe_node(new_node);
self.messages.push(format!("Updated: {}", description));
}
} else if new_node.role() == Role::ProgressIndicator {
let old_value = old_node.numeric_value();
let new_value = new_node.numeric_value();
if old_value != new_value
&& new_value.is_some()
&& let Some(value_desc) = describe_value(new_node)
{
self.messages.push(value_desc);
}
}
}
fn focus_moved(&mut self, _old_node: Option<&Node<'_>>, new_node: Option<&Node<'_>>) {
if let Some(new_node) = new_node {
self.messages.push(describe_node(new_node));
}
}
fn node_removed(&mut self, _node: &Node<'_>) {}
}
#[derive(Debug)]
enum State {
Inactive { is_host_focused: bool },
Active { tree: Box<Tree> },
}
impl Default for State {
fn default() -> Self {
Self::Inactive {
is_host_focused: false,
}
}
}
/// A screen reader simulator that generates human-readable descriptions of accessibility tree changes.
///
/// `ScreenReader` monitors accessibility tree updates and produces text descriptions of what
/// would be announced to screen reader users. It starts in an inactive state and becomes active
/// when the first tree update is received.
#[derive(Debug, Default)]
pub struct ScreenReader {
state: State,
}
impl ScreenReader {
/// Creates a new `ScreenReader` in an inactive state.
///
/// The screen reader will become active when the first accessibility tree update is received
/// via [`update`](Self::update).
pub fn new() -> Self {
Self {
state: State::Inactive {
is_host_focused: false,
},
}
}
/// Processes an accessibility tree update and returns descriptions of changes.
///
/// On the first call, this activates the screen reader and initializes it with the provided
/// tree structure. Subsequent calls process tree changes and generate descriptions.
///
/// # Returns
///
/// A vector of strings describing the changes that would be announced to a screen reader user.
pub fn update(&mut self, update: TreeUpdate) -> Vec<String> {
match &mut self.state {
State::Inactive { is_host_focused } => {
let tree = Box::new(Tree::new(update, *is_host_focused));
let messages = if let Some(focused_node) = tree.state().focus() {
vec![describe_node(&focused_node)]
} else {
Vec::new()
};
self.state = State::Active { tree };
messages
}
State::Active { tree } => {
let mut change_handler = ScreenReaderChangeHandler::new();
tree.update_and_process_changes(update, &mut change_handler);
change_handler.messages
}
}
}
/// Updates the window focus state and returns any resulting announcements.
///
/// This should be called when the application window gains or loses focus.
///
/// # Arguments
///
/// * `is_focused` - Whether the window has focus
///
/// # Returns
///
/// A vector of strings describing any changes that result from the focus state change.
pub fn update_window_focus_state(&mut self, is_focused: bool) -> Vec<String> {
match &mut self.state {
State::Inactive { is_host_focused } => {
*is_host_focused = is_focused;
Vec::new()
}
State::Active { tree } => {
let mut change_handler = ScreenReaderChangeHandler::new();
tree.update_host_focus_state_and_process_changes(is_focused, &mut change_handler);
change_handler.messages
}
}
}
/// Performs a hit test at the given coordinates and returns a description of the element found.
///
/// This simulates what a screen reader would announce when the user touches or hovers over
/// a specific point in the interface.
///
/// # Arguments
///
/// * `x` - The x coordinate in logical pixels
/// * `y` - The y coordinate in logical pixels
///
/// # Returns
///
/// A vector containing a description of the element at the given point.
pub fn hit_test(&self, x: f64, y: f64) -> Vec<String> {
match &self.state {
State::Inactive { .. } => Vec::new(),
State::Active { tree } => {
let root = tree.state().root();
let point = Point::new(x, y);
if let Some(node) = root.node_at_point(point, &filter) {
vec![describe_node(&node)]
} else {
Vec::new()
}
}
}
}
}