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}
16
17/// Semantics attached to a `View`, used to build the accessibility tree.
18#[derive(Clone, Debug)]
19pub struct Semantics {
20    /// Primary role of this node (what kind of thing it is).
21    pub role: Role,
22    /// Human‑readable label for screen readers. For buttons, this is the
23    /// “name” that is announced.
24    pub label: Option<String>,
25    /// Whether this node is currently focused.
26    pub focused: bool,
27    /// Whether this node is actionable; disabled nodes remain in the tree
28    /// but are marked not enabled.
29    pub enabled: bool,
30    /// Marks this node as a collection of horizontally or vertically stacked
31    /// selectable elements (ex: Tabs, RadioButtons).
32    pub selectable_group: bool,
33    pub checked: Option<bool>,
34    pub selected: Option<bool>,
35    pub value: Option<String>,
36}
37
38impl Semantics {
39    pub fn new(role: Role) -> Self {
40        Self {
41            role,
42            label: None,
43            focused: false,
44            enabled: true,
45            selectable_group: false,
46            checked: None,
47            selected: None,
48            value: None,
49        }
50    }
51    pub fn with_label(mut self, label: impl Into<String>) -> Self {
52        self.label = Some(label.into());
53        self
54    }
55    pub fn with_selectable_group(mut self) -> Self {
56        self.selectable_group = true;
57        self
58    }
59    pub fn with_checked(mut self, checked: bool) -> Self {
60        self.checked = Some(checked);
61        self
62    }
63    pub fn with_selected(mut self, selected: bool) -> Self {
64        self.selected = Some(selected);
65        self
66    }
67    pub fn with_value(mut self, value: impl Into<String>) -> Self {
68        self.value = Some(value.into());
69        self
70    }
71}
72
73impl Default for Semantics {
74    fn default() -> Self {
75        Self::new(Role::default())
76    }
77}