1use sui_core::{SemanticsNode, SemanticsRole, SemanticsValue};
2use sui_platform::AccessibilitySnapshot;
3
4#[derive(Debug, Clone, PartialEq, Eq, Default)]
5pub struct Selector {
6 role: Option<SemanticsRole>,
7 name: Option<String>,
8 text: Option<String>,
9 description: Option<String>,
10 focused: Option<bool>,
11 root_only: bool,
12}
13
14impl Selector {
15 pub fn by_role(role: SemanticsRole) -> Self {
16 Self {
17 role: Some(role),
18 ..Self::default()
19 }
20 }
21
22 pub fn by_text(text: impl Into<String>) -> Self {
23 Self {
24 text: Some(text.into()),
25 ..Self::default()
26 }
27 }
28
29 pub fn by_description(text: impl Into<String>) -> Self {
30 Self {
31 description: Some(text.into()),
32 ..Self::default()
33 }
34 }
35
36 pub fn focused() -> Self {
37 Self {
38 focused: Some(true),
39 ..Self::default()
40 }
41 }
42
43 pub fn root() -> Self {
44 Self {
45 root_only: true,
46 ..Self::default()
47 }
48 }
49
50 pub fn with_name(mut self, name: impl Into<String>) -> Self {
51 self.name = Some(name.into());
52 self
53 }
54
55 pub fn with_description(mut self, description: impl Into<String>) -> Self {
56 self.description = Some(description.into());
57 self
58 }
59
60 pub fn matches(&self, snapshot: &AccessibilitySnapshot, node: &SemanticsNode) -> bool {
61 if self.root_only && snapshot.root != Some(node.id) {
62 return false;
63 }
64
65 if self.role.as_ref().is_some_and(|role| role != &node.role) {
66 return false;
67 }
68
69 if self
70 .name
71 .as_ref()
72 .is_some_and(|name| node.name.as_ref() != Some(name))
73 {
74 return false;
75 }
76
77 if self
78 .description
79 .as_ref()
80 .is_some_and(|description| node.description.as_ref() != Some(description))
81 {
82 return false;
83 }
84
85 if self
86 .focused
87 .is_some_and(|focused| node.state.focused != focused)
88 {
89 return false;
90 }
91
92 if self
93 .text
94 .as_ref()
95 .is_some_and(|text| self.node_text(node).as_deref() != Some(text.as_str()))
96 {
97 return false;
98 }
99
100 true
101 }
102
103 pub fn is_visible(&self, node: &SemanticsNode) -> bool {
104 !node.state.hidden && !node.bounds.is_empty()
105 }
106
107 pub fn node_text(&self, node: &SemanticsNode) -> Option<String> {
108 node.name.clone().or_else(|| match node.value.as_ref() {
109 Some(SemanticsValue::Text(text)) => Some(text.clone()),
110 _ => None,
111 })
112 }
113
114 pub fn describe(&self) -> String {
115 let mut parts = Vec::new();
116
117 if let Some(role) = &self.role {
118 parts.push(format!("role={role:?}"));
119 }
120 if let Some(name) = &self.name {
121 parts.push(format!("name={name:?}"));
122 }
123 if let Some(text) = &self.text {
124 parts.push(format!("text={text:?}"));
125 }
126 if let Some(description) = &self.description {
127 parts.push(format!("description={description:?}"));
128 }
129 if let Some(focused) = self.focused {
130 parts.push(format!("focused={focused}"));
131 }
132 if self.root_only {
133 parts.push("root=true".to_string());
134 }
135
136 if parts.is_empty() {
137 "<all nodes>".to_string()
138 } else {
139 parts.join(", ")
140 }
141 }
142}