serge_native/
accessibility.rs1use super::{Ui, tree::NKind};
4use accesskit::{
5 Action, ActionData, ActionRequest, Node, Rect, Role, Toggled, TreeId, TreeInfo, TreeUpdate,
6};
7use serge_core::{Attr, Kind};
8
9impl Ui {
10 pub fn accessibility_tree(&self, title: &str, scale: f64) -> TreeUpdate {
13 let tree = self.shared.tree.borrow();
14 let root = tree.node(tree.root).expect("root").access_id;
15 let mut nodes = Vec::new();
16 tree.walk(tree.root, &mut |_, n| {
17 let role = match n.kind {
18 NKind::Root => Role::Window,
19 NKind::Group | NKind::Widget(Kind::Stack | Kind::View | Kind::Card) => {
20 Role::GenericContainer
21 }
22 NKind::Widget(Kind::Text) => Role::Label,
23 NKind::Widget(Kind::Button) => Role::Button,
24 NKind::Widget(Kind::Link) => Role::Link,
25 NKind::Widget(Kind::Checkbox) => Role::CheckBox,
26 NKind::Widget(Kind::Input) => Role::TextInput,
27 NKind::Widget(Kind::ProgressBar) => Role::ProgressIndicator,
28 NKind::Widget(Kind::Slider) => Role::Slider,
29 NKind::Widget(Kind::Canvas) => Role::Canvas,
30 };
31 let mut node = Node::new(role);
32 if let Some(slot) = &n.viewport {
33 if slot.unavailable() {
34 node.set_label(format!("{} (viewport unavailable)", slot.label()));
35 } else {
36 node.set_label(slot.label());
37 }
38 }
39 for child in n.children.iter().filter_map(|id| tree.node(*id)) {
40 node.push_child(child.access_id);
41 }
42 let b = n.rect;
43 let y = if n.kind == NKind::Root {
44 b.y
45 } else {
46 b.y - tree.scroll_y
47 };
48 node.set_bounds(Rect {
49 x0: f64::from(b.x) * scale,
50 y0: f64::from(y) * scale,
51 x1: f64::from(b.right()) * scale,
52 y1: f64::from(y + b.height) * scale,
53 });
54 if n.kind == NKind::Root {
55 node.set_label(title);
56 }
57 if !n.text().is_empty() {
58 if role == Role::Label {
59 node.set_value(n.text());
60 } else {
61 node.set_label(n.text());
62 }
63 }
64 if let Some(p) = n.attr(Attr::Placeholder) {
65 node.set_placeholder(p.as_str());
66 }
67 let disabled = n.flag(Attr::Disabled);
68 if disabled {
69 node.set_disabled();
70 }
71 if n.focusable() {
72 node.add_action(Action::Focus);
73 }
74 match role {
75 Role::Button | Role::Link | Role::CheckBox => {
76 if !disabled {
77 node.add_action(Action::Click);
78 }
79 if role == Role::CheckBox {
80 node.set_toggled(if n.flag(Attr::Checked) {
81 Toggled::True
82 } else {
83 Toggled::False
84 });
85 }
86 if let Some(href) = n.attr(Attr::Href) {
87 node.set_url(href.as_str());
88 }
89 }
90 Role::TextInput => {
91 node.set_value(n.input.as_str());
92 if !disabled {
93 node.add_action(Action::SetValue);
94 }
95 }
96 Role::Slider | Role::ProgressIndicator => {
97 node.set_numeric_value(f64::from(n.num(Attr::Value, 0.0)));
98 node.set_min_numeric_value(f64::from(n.num(Attr::Min, 0.0)));
99 node.set_max_numeric_value(f64::from(n.num(Attr::Max, 1.0)));
100 if role == Role::Slider && !disabled {
101 let step = n.num(Attr::Step, 0.0);
102 if step > 0.0 {
103 node.set_numeric_value_step(f64::from(step));
104 }
105 node.add_action(Action::SetValue);
106 node.add_action(Action::Increment);
107 node.add_action(Action::Decrement);
108 }
109 }
110 _ => {}
111 }
112 nodes.push((n.access_id, node));
113 });
114 let focus = tree
115 .focus
116 .and_then(|id| tree.node(id))
117 .map_or(root, |n| n.access_id);
118 TreeUpdate {
119 nodes,
120 tree: Some(TreeInfo::new(root)),
121 tree_id: TreeId::ROOT,
122 focus,
123 }
124 }
125
126 pub fn accessibility_action(&self, request: ActionRequest) {
129 if request.target_tree != TreeId::ROOT {
130 return;
131 }
132 let tree = self.shared.tree.borrow();
133 let Some((id, n)) = tree
134 .nodes
135 .iter()
136 .enumerate()
137 .filter_map(|(id, n)| Some((id, n.as_ref()?)))
138 .find(|(_, n)| n.access_id == request.target_node)
139 else {
140 return;
141 };
142 if n.flag(Attr::Disabled) {
143 return;
144 }
145 let NKind::Widget(kind) = n.kind else {
146 return;
147 };
148 match (request.action, kind, request.data) {
149 (
150 Action::Focus,
151 Kind::Button | Kind::Link | Kind::Checkbox | Kind::Input | Kind::Slider,
152 _,
153 ) => {
154 drop(tree);
155 self.shared.tree.borrow_mut().focus = Some(id);
156 self.shared.changed();
157 }
158 (Action::Click, Kind::Button | Kind::Link | Kind::Checkbox, _) => {
159 drop(tree);
160 self.activate(id);
161 }
162 (Action::SetValue, Kind::Input, Some(ActionData::Value(value))) => {
163 drop(tree);
164 self.shared.tree.borrow_mut().focus = Some(id);
165 self.edit(|s| *s = value.chars().filter(|c| !c.is_control()).collect());
166 }
167 (action, Kind::Slider, data) => {
168 drop(tree);
169 match (action, data) {
170 (Action::SetValue, Some(ActionData::NumericValue(v))) => {
171 self.set_slider(id, |_, _| v as f32)
172 }
173 (Action::Increment | Action::Decrement, _) => self.set_slider(id, |n, step| {
174 n.num(Attr::Value, n.num(Attr::Min, 0.0))
175 + if action == Action::Increment {
176 step
177 } else {
178 -step
179 }
180 }),
181 _ => {}
182 }
183 }
184 _ => {}
185 }
186 }
187}