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 pub text_selection: Option<(usize, usize)>,
77 pub read_only: bool,
79}
80
81#[derive(Clone, Debug, PartialEq)]
86pub struct VirtualSemanticItem {
87 pub id: u64,
89 pub role: SemanticsRole,
90 pub label: String,
91 pub selected: Option<bool>,
92 pub expanded: Option<bool>,
93 pub enabled: bool,
94 pub bounds: Rect,
96 pub children: Vec<VirtualSemanticItem>,
98 pub text_run: Option<VirtualTextRun>,
100}
101
102#[derive(Clone, Debug, PartialEq)]
107pub struct VirtualTextRun {
108 pub character_lengths: Vec<u8>,
109 pub start_char: usize,
111 pub character_positions: Option<Vec<f32>>,
113 pub character_widths: Option<Vec<f32>>,
115 pub rtl: bool,
117}
118
119#[derive(Clone, Copy, Debug, PartialEq, Eq)]
121pub enum LiveRegion {
122 Polite,
124 Assertive,
126}
127
128impl WidgetSemantics {
129 pub fn new(role: SemanticsRole) -> Self {
130 Self {
131 role,
132 label: None,
133 value: None,
134 toggled: None,
135 expanded: None,
136 selected: None,
137 enabled: true,
138 focusable: matches!(
139 role,
140 SemanticsRole::Button | SemanticsRole::CheckBox | SemanticsRole::Slider | SemanticsRole::TextInput
141 ),
142 numeric: None,
143 modal: false,
144 live: None,
145 virtual_items: Vec::new(),
146 text_selection: None,
147 read_only: false,
148 }
149 }
150
151 pub fn with_read_only(mut self, read_only: bool) -> Self {
152 self.read_only = read_only;
153 self
154 }
155
156 pub fn with_label(mut self, label: impl Into<String>) -> Self {
157 self.label = Some(label.into());
158 self
159 }
160
161 pub fn with_value(mut self, value: impl Into<String>) -> Self {
162 self.value = Some(value.into());
163 self
164 }
165
166 pub fn with_toggled(mut self, v: bool) -> Self {
167 self.toggled = Some(v);
168 self
169 }
170
171 pub fn with_expanded(mut self, expanded: bool) -> Self {
172 self.expanded = Some(expanded);
173 self
174 }
175
176 pub fn with_selected(mut self, selected: bool) -> Self {
177 self.selected = Some(selected);
178 self
179 }
180
181 pub fn with_focusable(mut self, focusable: bool) -> Self {
182 self.focusable = focusable && self.enabled;
183 self
184 }
185
186 pub fn with_modal(mut self, modal: bool) -> Self {
187 self.modal = modal;
188 self
189 }
190
191 pub fn with_live(mut self, live: LiveRegion) -> Self {
192 self.live = Some(live);
193 self
194 }
195
196 pub fn with_virtual_items(mut self, items: Vec<VirtualSemanticItem>) -> Self {
198 self.virtual_items = items;
199 self
200 }
201
202 pub fn with_text_selection(mut self, anchor: usize, focus: usize) -> Self {
204 self.text_selection = Some((anchor, focus));
205 self
206 }
207
208 pub fn with_enabled(mut self, enabled: bool) -> Self {
210 self.enabled = enabled;
211 if !enabled {
212 self.focusable = false;
213 }
214 self
215 }
216
217 pub fn with_numeric_value(mut self, value: f64, min: f64, max: f64, step: f64) -> Self {
218 self.numeric = Some(SemanticsNumericValue { value, min, max, step });
219 self
220 }
221
222 pub fn non_focusable(mut self) -> Self {
223 self.focusable = false;
224 self
225 }
226}
227
228#[derive(Clone, Copy, Debug, PartialEq, Eq)]
233pub enum SemanticsRole {
234 Button,
236 CheckBox,
238 Slider,
240 TextInput,
242 TextRun,
244 Label,
246 Image,
248 Container,
250 Unknown,
252 Dialog,
254 Tab,
256 TabList,
258 TabPanel,
260 List,
262 ListItem,
264 ListOption,
266 Grid,
268 GridRow,
270 GridCell,
272 Tree,
274 TreeItem,
276 Progress,
278 Toast,
280 Menu,
282 MenuItem,
284 ComboBox,
286 RadioGroup,
288 RadioButton,
290 Tooltip,
292 Splitter,
294}
295
296#[derive(Clone, Debug)]
304pub struct SemanticsData {
305 pub role: SemanticsRole,
307 pub label: Option<String>,
312 pub value: Option<String>,
317 pub toggled: Option<bool>,
319 pub expanded: Option<bool>,
321 pub selected: Option<bool>,
323 pub modal: bool,
325 pub live: Option<LiveRegion>,
327 pub virtual_items: Vec<VirtualSemanticItem>,
329 pub text_selection: Option<(usize, usize)>,
331 pub read_only: bool,
333 pub enabled: bool,
335 pub bounds: Rect,
337 pub children: Vec<crate::id::ElementId>,
339 pub focusable: bool,
341 pub numeric: Option<SemanticsNumericValue>,
343}
344
345impl SemanticsData {
346 pub fn children(&self) -> &[crate::id::ElementId] {
348 &self.children
349 }
350
351 pub fn new(role: SemanticsRole, bounds: Rect) -> Self {
353 Self {
354 role,
355 label: None,
356 value: None,
357 toggled: None,
358 expanded: None,
359 selected: None,
360 modal: false,
361 live: None,
362 virtual_items: Vec::new(),
363 text_selection: None,
364 read_only: false,
365 enabled: true,
366 bounds,
367 children: Vec::new(),
368 focusable: matches!(
369 role,
370 SemanticsRole::Button | SemanticsRole::CheckBox | SemanticsRole::Slider | SemanticsRole::TextInput
371 ),
372 numeric: None,
373 }
374 }
375
376 pub fn with_label(mut self, label: impl Into<String>) -> Self {
377 self.label = Some(label.into());
378 self
379 }
380
381 pub fn with_numeric_value(mut self, value: f64, min: f64, max: f64, step: f64) -> Self {
382 self.numeric = Some(SemanticsNumericValue { value, min, max, step });
383 self
384 }
385
386 pub fn with_value(mut self, value: impl Into<String>) -> Self {
387 self.value = Some(value.into());
388 self
389 }
390
391 pub fn with_toggled(mut self, v: bool) -> Self {
392 self.toggled = Some(v);
393 self
394 }
395
396 pub fn with_expanded(mut self, expanded: bool) -> Self {
397 self.expanded = Some(expanded);
398 self
399 }
400
401 pub fn with_selected(mut self, selected: bool) -> Self {
402 self.selected = Some(selected);
403 self
404 }
405
406 pub fn with_modal(mut self, modal: bool) -> Self {
407 self.modal = modal;
408 self
409 }
410
411 pub fn with_live(mut self, live: LiveRegion) -> Self {
412 self.live = Some(live);
413 self
414 }
415
416 pub fn with_children(mut self, children: Vec<crate::id::ElementId>) -> Self {
417 self.children = children;
418 self
419 }
420
421 pub fn with_text_selection(mut self, anchor: usize, focus: usize) -> Self {
423 self.text_selection = Some((anchor, focus));
424 self
425 }
426}