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