cranpose_ui/layout/
semantics_labels.rs1use std::borrow::Cow;
2
3use super::{SemanticsNode, SemanticsRole, SemanticsWidgetRole};
4
5impl SemanticsNode {
6 pub fn merges_accessibility_descendants(&self) -> bool {
9 self.merge_descendants
10 || !self.actions.is_empty()
11 || self.editable_text
12 || self.password
13 || self.focusable
14 || !self.custom_actions.is_empty()
15 || self.set_progress.is_some()
16 || self.set_text.is_some()
17 || self.set_selection.is_some()
18 || self.on_long_click.is_some()
19 || self.on_magic_tap.is_some()
20 || self.expand.is_some()
21 || self.collapse.is_some()
22 || self.dismiss.is_some()
23 }
24
25 pub fn is_accessibility_boundary(&self) -> bool {
28 self.merges_accessibility_descendants()
29 || self.vertical_scroll.is_some()
30 || self.horizontal_scroll.is_some()
31 || self.selectable_group
32 || self.pane_title.is_some()
33 || matches!(
34 self.widget_role,
35 Some(
36 SemanticsWidgetRole::Dialog
37 | SemanticsWidgetRole::Toolbar
38 | SemanticsWidgetRole::Menu
39 | SemanticsWidgetRole::TabBar
40 | SemanticsWidgetRole::List
41 | SemanticsWidgetRole::RadioGroup
42 )
43 )
44 }
45
46 pub fn accessibility_children(&self) -> Vec<&Self> {
49 let mut children: Vec<_> = self.children.iter().collect();
50 let index = |node: &&Self| {
51 if node.traversal_index.is_finite() {
52 node.traversal_index
53 } else {
54 0.0
55 }
56 };
57 if children.iter().any(|child| index(child) != 0.0) {
58 children.sort_by(|left, right| {
59 index(left)
60 .partial_cmp(&index(right))
61 .unwrap_or(std::cmp::Ordering::Equal)
62 });
63 }
64 children
65 }
66
67 pub fn accessibility_label(&self) -> Option<Cow<'_, str>> {
72 if self.hidden {
73 return None;
74 }
75 let own = self
76 .description
77 .as_deref()
78 .filter(|label| !label.trim().is_empty())
79 .or_else(|| match &self.role {
80 SemanticsRole::Text { value } if !value.trim().is_empty() => Some(value.as_str()),
81 _ => None,
82 });
83 if self.password {
84 return Some(Cow::Borrowed(
85 self.description
86 .as_deref()
87 .filter(|label| {
88 !label.trim().is_empty() && Some(*label) != self.text.as_deref()
89 })
90 .unwrap_or("password"),
91 ));
92 }
93 own.map(Cow::Borrowed)
94 .or_else(|| {
95 if !self.merges_accessibility_descendants() {
96 return None;
97 }
98 let mut words = Vec::new();
99 self.collect_accessibility_words(&mut words);
100 (!words.is_empty()).then(|| Cow::Owned(words.join(", ")))
101 })
102 .or_else(|| self.editable_text.then_some(Cow::Borrowed("")))
103 }
104
105 fn collect_accessibility_words<'a>(&'a self, words: &mut Vec<&'a str>) {
106 for child in self.accessibility_children() {
107 if child.hidden || child.is_accessibility_boundary() {
108 continue;
109 }
110 match child.accessibility_label() {
111 Some(Cow::Borrowed(label)) if !label.trim().is_empty() => words.push(label),
112 _ => child.collect_accessibility_words(words),
113 }
114 }
115 }
116}
117
118#[cfg(test)]
119#[path = "tests/semantics_labels.rs"]
120mod tests;