1use crate::Rect;
27
28#[derive(Clone, Copy, Debug, PartialEq)]
34pub struct SemanticsNumericValue {
35 pub value: f64,
36 pub min: f64,
37 pub max: f64,
38 pub step: f64,
39}
40
41#[derive(Clone, Debug)]
47pub struct WidgetSemantics {
48 pub role: SemanticsRole,
50 pub label: Option<String>,
52 pub value: Option<String>,
57 pub toggled: Option<bool>,
59 pub expanded: Option<bool>,
61 pub selected: Option<bool>,
63 pub enabled: bool,
65 pub focusable: bool,
67 pub numeric: Option<SemanticsNumericValue>,
69 pub modal: bool,
71 pub live: Option<LiveRegion>,
73 pub virtual_items: Vec<VirtualSemanticItem>,
75}
76
77#[derive(Clone, Debug, PartialEq)]
82pub struct VirtualSemanticItem {
83 pub id: u64,
85 pub role: SemanticsRole,
86 pub label: String,
87 pub selected: Option<bool>,
88 pub expanded: Option<bool>,
89 pub enabled: bool,
90 pub bounds: Rect,
92 pub children: Vec<VirtualSemanticItem>,
94 pub text_run: Option<VirtualTextRun>,
96}
97
98#[derive(Clone, Debug, PartialEq)]
103pub struct VirtualTextRun {
104 pub character_lengths: Vec<u8>,
105 pub selection: Option<(usize, usize)>,
106 pub character_positions: Option<Vec<f32>>,
108 pub character_widths: Option<Vec<f32>>,
110 pub rtl: bool,
112}
113
114#[derive(Clone, Copy, Debug, PartialEq, Eq)]
116pub enum LiveRegion {
117 Polite,
119 Assertive,
121}
122
123impl WidgetSemantics {
124 pub fn new(role: SemanticsRole) -> Self {
125 Self {
126 role,
127 label: None,
128 value: None,
129 toggled: None,
130 expanded: None,
131 selected: None,
132 enabled: true,
133 focusable: matches!(
134 role,
135 SemanticsRole::Button | SemanticsRole::CheckBox | SemanticsRole::Slider | SemanticsRole::TextInput
136 ),
137 numeric: None,
138 modal: false,
139 live: None,
140 virtual_items: Vec::new(),
141 }
142 }
143
144 pub fn with_label(mut self, label: impl Into<String>) -> Self {
145 self.label = Some(label.into());
146 self
147 }
148
149 pub fn with_value(mut self, value: impl Into<String>) -> Self {
150 self.value = Some(value.into());
151 self
152 }
153
154 pub fn with_toggled(mut self, v: bool) -> Self {
155 self.toggled = Some(v);
156 self
157 }
158
159 pub fn with_expanded(mut self, expanded: bool) -> Self {
160 self.expanded = Some(expanded);
161 self
162 }
163
164 pub fn with_selected(mut self, selected: bool) -> Self {
165 self.selected = Some(selected);
166 self
167 }
168
169 pub fn with_focusable(mut self, focusable: bool) -> Self {
170 self.focusable = focusable && self.enabled;
171 self
172 }
173
174 pub fn with_modal(mut self, modal: bool) -> Self {
175 self.modal = modal;
176 self
177 }
178
179 pub fn with_live(mut self, live: LiveRegion) -> Self {
180 self.live = Some(live);
181 self
182 }
183
184 pub fn with_virtual_items(mut self, items: Vec<VirtualSemanticItem>) -> Self {
186 self.virtual_items = items;
187 self
188 }
189
190 pub fn with_enabled(mut self, enabled: bool) -> Self {
192 self.enabled = enabled;
193 if !enabled {
194 self.focusable = false;
195 }
196 self
197 }
198
199 pub fn with_numeric_value(mut self, value: f64, min: f64, max: f64, step: f64) -> Self {
200 self.numeric = Some(SemanticsNumericValue { value, min, max, step });
201 self
202 }
203
204 pub fn non_focusable(mut self) -> Self {
205 self.focusable = false;
206 self
207 }
208}
209
210#[derive(Clone, Copy, Debug, PartialEq, Eq)]
215pub enum SemanticsRole {
216 Button,
218 CheckBox,
220 Slider,
222 TextInput,
224 TextRun,
226 Label,
228 Image,
230 Container,
232 Unknown,
234 Dialog,
236 Tab,
238 TabList,
240 TabPanel,
242 List,
244 ListItem,
246 ListOption,
248 Grid,
250 GridRow,
252 GridCell,
254 Tree,
256 TreeItem,
258 Progress,
260 Toast,
262 Menu,
264 MenuItem,
266 ComboBox,
268 RadioGroup,
270 RadioButton,
272 Tooltip,
274 Splitter,
276}
277
278#[derive(Clone, Debug)]
286pub struct SemanticsData {
287 pub role: SemanticsRole,
289 pub label: Option<String>,
294 pub value: Option<String>,
299 pub toggled: Option<bool>,
301 pub expanded: Option<bool>,
303 pub selected: Option<bool>,
305 pub modal: bool,
307 pub live: Option<LiveRegion>,
309 pub virtual_items: Vec<VirtualSemanticItem>,
311 pub enabled: bool,
313 pub bounds: Rect,
315 pub children: Vec<crate::id::ElementId>,
317 pub focusable: bool,
319 pub numeric: Option<SemanticsNumericValue>,
321}
322
323impl SemanticsData {
324 pub fn children(&self) -> &[crate::id::ElementId] {
326 &self.children
327 }
328
329 pub fn new(role: SemanticsRole, bounds: Rect) -> Self {
331 Self {
332 role,
333 label: None,
334 value: None,
335 toggled: None,
336 expanded: None,
337 selected: None,
338 modal: false,
339 live: None,
340 virtual_items: Vec::new(),
341 enabled: true,
342 bounds,
343 children: Vec::new(),
344 focusable: matches!(
345 role,
346 SemanticsRole::Button | SemanticsRole::CheckBox | SemanticsRole::Slider | SemanticsRole::TextInput
347 ),
348 numeric: None,
349 }
350 }
351
352 pub fn with_label(mut self, label: impl Into<String>) -> Self {
353 self.label = Some(label.into());
354 self
355 }
356
357 pub fn with_numeric_value(mut self, value: f64, min: f64, max: f64, step: f64) -> Self {
358 self.numeric = Some(SemanticsNumericValue { value, min, max, step });
359 self
360 }
361
362 pub fn with_value(mut self, value: impl Into<String>) -> Self {
363 self.value = Some(value.into());
364 self
365 }
366
367 pub fn with_toggled(mut self, v: bool) -> Self {
368 self.toggled = Some(v);
369 self
370 }
371
372 pub fn with_expanded(mut self, expanded: bool) -> Self {
373 self.expanded = Some(expanded);
374 self
375 }
376
377 pub fn with_selected(mut self, selected: bool) -> Self {
378 self.selected = Some(selected);
379 self
380 }
381
382 pub fn with_modal(mut self, modal: bool) -> Self {
383 self.modal = modal;
384 self
385 }
386
387 pub fn with_live(mut self, live: LiveRegion) -> Self {
388 self.live = Some(live);
389 self
390 }
391
392 pub fn with_children(mut self, children: Vec<crate::id::ElementId>) -> Self {
393 self.children = children;
394 self
395 }
396}