Skip to main content

repose_core/
semantics.rs

1/// High‑level semantic role of a view, similar to ARIA roles.
2#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
3pub enum Role {
4    Text,
5    Button,
6    Tab,
7    TextField,
8    #[default]
9    Container,
10    Checkbox,
11    RadioButton,
12    Switch,
13    Slider,
14    ProgressBar,
15    Dialog,
16    Image,
17    DropdownList,
18    ValuePicker,
19    Carousel,
20}
21
22/// Live-region politeness for screen-reader announcements.
23#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
24pub enum LiveRegion {
25    #[default]
26    Polite,
27    Assertive,
28}
29
30/// Numeric range info for sliders and progress indicators.
31#[derive(Clone, Copy, Debug, PartialEq)]
32pub struct ProgressRangeInfo {
33    pub current: f32,
34    pub min: f32,
35    pub max: f32,
36    pub step: Option<f32>,
37}
38
39/// Semantics attached to a `View`, used to build the accessibility tree.
40#[derive(Clone, Debug)]
41pub struct Semantics {
42    /// Primary role of this node (what kind of thing it is).
43    pub role: Role,
44    /// Human‑readable label for screen readers. For buttons, this is the
45    /// “name” that is announced.
46    pub label: Option<String>,
47    /// Whether this node is currently focused.
48    pub focused: bool,
49    /// Whether this node is actionable. Disabled nodes remain in the tree
50    /// but are marked not enabled.
51    pub enabled: bool,
52    /// Marks this node as a collection of horizontally or vertically stacked
53    /// selectable elements (ex: Tabs, RadioButtons).
54    pub selectable_group: bool,
55    pub checked: Option<bool>,
56    pub selected: Option<bool>,
57    pub value: Option<String>,
58    pub heading: bool,
59    pub live_region: Option<LiveRegion>,
60    pub state_description: Option<String>,
61    pub traversal_index: Option<f32>,
62    pub is_traversal_group: bool,
63    pub hidden: bool,
64    pub collection_rows: Option<usize>,
65    pub collection_columns: Option<usize>,
66    pub row_index: Option<usize>,
67    pub column_index: Option<usize>,
68    pub progress: Option<ProgressRangeInfo>,
69    pub error: Option<String>,
70    pub expanded: Option<bool>,
71    /// Test-only identifier, mirroring Compose's `Modifier.testTag`.
72    pub test_tag: Option<String>,
73}
74
75impl Semantics {
76    pub fn new(role: Role) -> Self {
77        Self {
78            role,
79            label: None,
80            focused: false,
81            enabled: true,
82            selectable_group: false,
83            checked: None,
84            selected: None,
85            value: None,
86            heading: false,
87            live_region: None,
88            state_description: None,
89            traversal_index: None,
90            is_traversal_group: false,
91            hidden: false,
92            collection_rows: None,
93            collection_columns: None,
94            row_index: None,
95            column_index: None,
96            progress: None,
97            error: None,
98            expanded: None,
99            test_tag: None,
100        }
101    }
102    pub fn with_label(mut self, label: impl Into<String>) -> Self {
103        self.label = Some(label.into());
104        self
105    }
106    pub fn with_selectable_group(mut self) -> Self {
107        self.selectable_group = true;
108        self
109    }
110    pub fn with_checked(mut self, checked: bool) -> Self {
111        self.checked = Some(checked);
112        self
113    }
114    pub fn with_selected(mut self, selected: bool) -> Self {
115        self.selected = Some(selected);
116        self
117    }
118    pub fn with_value(mut self, value: impl Into<String>) -> Self {
119        self.value = Some(value.into());
120        self
121    }
122    pub fn with_heading(mut self) -> Self {
123        self.heading = true;
124        self
125    }
126    pub fn with_live_region(mut self, region: LiveRegion) -> Self {
127        self.live_region = Some(region);
128        self
129    }
130    pub fn with_state_description(mut self, description: impl Into<String>) -> Self {
131        self.state_description = Some(description.into());
132        self
133    }
134    pub fn with_traversal_index(mut self, index: f32) -> Self {
135        self.traversal_index = Some(index);
136        self
137    }
138    pub fn with_traversal_group(mut self) -> Self {
139        self.is_traversal_group = true;
140        self
141    }
142    pub fn with_hidden(mut self) -> Self {
143        self.hidden = true;
144        self
145    }
146    pub fn with_collection(mut self, rows: usize, columns: usize) -> Self {
147        self.collection_rows = Some(rows);
148        self.collection_columns = Some(columns);
149        self
150    }
151    pub fn with_collection_item(mut self, row: usize, column: usize) -> Self {
152        self.row_index = Some(row);
153        self.column_index = Some(column);
154        self
155    }
156    pub fn with_progress_range(mut self, info: ProgressRangeInfo) -> Self {
157        self.progress = Some(info);
158        self
159    }
160    pub fn with_error(mut self, message: impl Into<String>) -> Self {
161        self.error = Some(message.into());
162        self
163    }
164    pub fn with_expanded(mut self, expanded: bool) -> Self {
165        self.expanded = Some(expanded);
166        self
167    }
168    pub fn with_test_tag(mut self, tag: impl Into<String>) -> Self {
169        self.test_tag = Some(tag.into());
170        self
171    }
172}
173
174impl Default for Semantics {
175    fn default() -> Self {
176        Self::new(Role::default())
177    }
178}