1use std::collections::HashSet;
10
11use crate::context::{Id, Order};
12use crate::geometry::Rect;
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16pub enum Role {
17 Button,
19 Link,
21 CheckBox,
23 Switch,
25 RadioButton,
27 Slider,
29 SpinButton,
31 TextInput,
33 MultilineTextInput,
35 PasswordInput,
37 SearchInput,
39 Label,
41 Heading,
43 Tab,
45 TabList,
47 MenuItem,
49 ComboBox,
51 ProgressIndicator,
53 Meter,
55 Image,
57 ColorWell,
59 ScrollView,
61 List,
63 ListItem,
65 Group,
67}
68
69impl Role {
70 pub fn clickable(self) -> bool {
72 matches!(
73 self,
74 Role::Button
75 | Role::Link
76 | Role::CheckBox
77 | Role::Switch
78 | Role::RadioButton
79 | Role::Tab
80 | Role::MenuItem
81 | Role::ComboBox
82 | Role::ColorWell
83 )
84 }
85 fn text_input(self) -> bool {
86 matches!(self, Role::TextInput | Role::MultilineTextInput | Role::PasswordInput | Role::SearchInput)
87 }
88}
89
90#[derive(Clone, Copy, Debug, PartialEq)]
92pub struct Numeric {
93 pub value: f64,
95 pub min: f64,
97 pub max: f64,
99 pub step: Option<f64>,
101}
102
103#[derive(Clone, Debug)]
105pub struct Node {
106 pub id: Id,
108 pub role: Role,
110 pub rect: Rect,
112 pub label: String,
114 pub value: Option<String>,
116 pub numeric: Option<Numeric>,
118 pub toggled: Option<bool>,
120 pub selected: Option<bool>,
122 pub expanded: Option<bool>,
124 pub disabled: bool,
126 pub focusable: bool,
128 pub(crate) layer: usize,
129}
130
131impl Node {
132 pub fn new(id: Id, role: Role, rect: Rect) -> Self {
134 Self {
135 id,
136 role,
137 rect,
138 label: String::new(),
139 value: None,
140 numeric: None,
141 toggled: None,
142 selected: None,
143 expanded: None,
144 disabled: false,
145 focusable: false,
146 layer: 0,
147 }
148 }
149 pub fn label(&mut self, label: impl Into<String>) -> &mut Self {
151 self.label = label.into();
152 self
153 }
154 pub fn value(&mut self, value: impl Into<String>) -> &mut Self {
156 self.value = Some(value.into());
157 self
158 }
159 pub fn numeric(&mut self, value: f64, min: f64, max: f64, step: Option<f64>) -> &mut Self {
161 self.numeric = Some(Numeric { value, min, max, step });
162 self
163 }
164 pub fn toggled(&mut self, on: bool) -> &mut Self {
166 self.toggled = Some(on);
167 self
168 }
169 pub fn selected(&mut self, on: bool) -> &mut Self {
171 self.selected = Some(on);
172 self
173 }
174 pub fn expanded(&mut self, on: bool) -> &mut Self {
176 self.expanded = Some(on);
177 self
178 }
179}
180
181#[derive(Clone, Debug, PartialEq)]
185pub enum Action {
186 Click,
188 Focus,
190 Blur,
192 Increment,
194 Decrement,
196 SetValue(String),
198 SetNumber(f64),
200 ReplaceSelection(String),
202}
203
204#[derive(Default)]
206pub(crate) struct Access {
207 pub on: bool,
208 pub nodes: Vec<Node>,
209 pub requests: Vec<(Id, Action)>,
210}
211
212pub(crate) const ROOT: Id = Id(0x9e37_79b9_7f4a_7c15);
214
215pub(crate) struct LayerInfo {
216 pub index: usize,
217 pub id: Id,
218 pub order: Order,
219}
220
221pub(crate) fn tree(
223 nodes: &[Node],
224 layers: &[LayerInfo],
225 focus: Option<Id>,
226 ppp: f32,
227 screen: Rect,
228 title: &str,
229) -> accesskit::TreeUpdate {
230 use accesskit::{
231 Action as A, Affine, Node as N, NodeId, Rect as R, Role as AR, Toggled, TreeInfo, TreeUpdate,
232 };
233 let rect = |r: Rect| R { x0: r.min.x as f64, y0: r.min.y as f64, x1: r.max.x as f64, y1: r.max.y as f64 };
234 let mut out: Vec<(NodeId, N)> = Vec::with_capacity(nodes.len() + layers.len() + 1);
235 let mut seen = HashSet::new();
236 seen.insert(ROOT);
237 let mut root_children = Vec::new();
238 let mut focus_found = None;
239 for l in layers {
240 let mut children = Vec::new();
241 let mut bounds = Rect::NOTHING;
242 for n in nodes.iter().filter(|n| n.layer == l.index) {
243 if !seen.insert(n.id) {
245 continue;
246 }
247 children.push(NodeId(n.id.0));
248 bounds = bounds.union(n.rect);
249 if focus == Some(n.id) {
250 focus_found = Some(n.id);
251 }
252 let mut a = N::new(match n.role {
253 Role::Button => AR::Button,
254 Role::Link => AR::Link,
255 Role::CheckBox => AR::CheckBox,
256 Role::Switch => AR::Switch,
257 Role::RadioButton => AR::RadioButton,
258 Role::Slider => AR::Slider,
259 Role::SpinButton => AR::SpinButton,
260 Role::TextInput => AR::TextInput,
261 Role::MultilineTextInput => AR::MultilineTextInput,
262 Role::PasswordInput => AR::PasswordInput,
263 Role::SearchInput => AR::SearchInput,
264 Role::Label => AR::Label,
265 Role::Heading => AR::Heading,
266 Role::Tab => AR::Tab,
267 Role::TabList => AR::TabList,
268 Role::MenuItem => AR::MenuItem,
269 Role::ComboBox => AR::ComboBox,
270 Role::ProgressIndicator => AR::ProgressIndicator,
271 Role::Meter => AR::Meter,
272 Role::Image => AR::Image,
273 Role::ColorWell => AR::ColorWell,
274 Role::ScrollView => AR::ScrollView,
275 Role::List => AR::List,
276 Role::ListItem => AR::ListItem,
277 Role::Group => AR::Group,
278 });
279 a.set_bounds(rect(n.rect));
280 match (&n.value, n.role) {
281 (None, Role::Label) => a.set_value(n.label.as_str()),
283 (v, _) => {
284 if !n.label.is_empty() {
285 a.set_label(n.label.as_str());
286 }
287 if let Some(v) = v {
288 a.set_value(v.as_str());
289 }
290 }
291 }
292 if let Some(v) = n.numeric {
293 a.set_numeric_value(v.value);
294 a.set_min_numeric_value(v.min);
295 a.set_max_numeric_value(v.max);
296 if let Some(s) = v.step {
297 a.set_numeric_value_step(s);
298 }
299 }
300 if let Some(t) = n.toggled {
301 a.set_toggled(if t { Toggled::True } else { Toggled::False });
302 }
303 if let Some(s) = n.selected {
304 a.set_selected(s);
305 }
306 if let Some(e) = n.expanded {
307 a.set_expanded(e);
308 }
309 if n.disabled {
310 a.set_disabled();
311 } else {
312 if n.role.clickable() {
313 a.add_action(A::Click);
314 }
315 if n.focusable {
316 a.add_action(A::Focus);
317 }
318 if n.numeric.is_some() && matches!(n.role, Role::Slider | Role::SpinButton) {
319 a.add_action(A::Increment);
320 a.add_action(A::Decrement);
321 a.add_action(A::SetValue);
322 }
323 if n.role.text_input() {
324 a.add_action(A::SetValue);
325 a.add_action(A::ReplaceSelectedText);
326 }
327 }
328 out.push((NodeId(n.id.0), a));
329 }
330 if children.is_empty() {
331 continue;
332 }
333 let group_id = l.id.with("access");
334 let mut g = N::new(match l.order {
335 Order::Window => AR::Window,
336 Order::Modal => AR::Dialog,
337 Order::Toast => AR::Alert,
338 Order::Tooltip => AR::Tooltip,
339 _ => AR::Group,
340 });
341 g.set_bounds(rect(bounds));
342 g.set_children(children);
343 root_children.push(NodeId(group_id.0));
344 out.push((NodeId(group_id.0), g));
345 }
346 let mut root = N::new(AR::Window);
347 root.set_label(title);
348 root.set_bounds(rect(screen));
349 root.set_transform(Affine::scale(ppp as f64));
350 root.set_children(root_children);
351 out.push((NodeId(ROOT.0), root));
352 let mut info = TreeInfo::new(NodeId(ROOT.0));
353 info.toolkit_name = Some("rust-glass".into());
354 info.toolkit_version = Some(env!("CARGO_PKG_VERSION").into());
355 TreeUpdate {
356 nodes: out,
357 tree: Some(info),
358 tree_id: accesskit::TreeId::ROOT,
359 focus: NodeId(focus_found.unwrap_or(ROOT).0),
360 }
361}
362
363pub(crate) fn from_accesskit(req: &accesskit::ActionRequest) -> Option<(Id, Action)> {
366 use accesskit::{Action as A, ActionData as D};
367 let id = Id(req.target_node.0);
368 let action = match (req.action, &req.data) {
369 (A::Click | A::Expand | A::Collapse, _) => Action::Click,
370 (A::Focus, _) => Action::Focus,
371 (A::Blur, _) => Action::Blur,
372 (A::Increment, _) => Action::Increment,
373 (A::Decrement, _) => Action::Decrement,
374 (A::SetValue, Some(D::Value(v))) => Action::SetValue(v.to_string()),
375 (A::SetValue, Some(D::NumericValue(v))) => Action::SetNumber(*v),
376 (A::ReplaceSelectedText, Some(D::Value(v))) => Action::ReplaceSelection(v.to_string()),
377 _ => return None,
378 };
379 Some((id, action))
380}