use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct NodeId(pub u32);
impl fmt::Display for NodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "#{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum A11yRole {
Window,
Group,
Heading,
Paragraph,
Button,
Checkbox,
Textbox,
MultilineTextbox,
Slider,
ComboBox,
List,
ListItem,
TabList,
Tab,
Table,
Row,
Cell,
Toolbar,
Menu,
MenuItem,
StatusBar,
Dialog,
Alert,
ProgressBar,
Link,
Separator,
Image,
Canvas,
}
#[must_use]
pub const fn is_interactive(role: A11yRole) -> bool {
matches!(
role,
A11yRole::Button
| A11yRole::Checkbox
| A11yRole::Textbox
| A11yRole::MultilineTextbox
| A11yRole::Slider
| A11yRole::ComboBox
| A11yRole::ListItem
| A11yRole::Tab
| A11yRole::MenuItem
| A11yRole::Link
)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TriState {
Unchecked,
Checked,
Mixed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct States {
pub disabled: bool,
pub focused: bool,
pub selected: bool,
pub expanded: bool,
pub required: bool,
pub invalid: bool,
pub hidden: bool,
pub modal: bool,
pub checked: Option<TriState>,
pub busy: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Relation {
Controls(NodeId),
DescribedBy(NodeId),
LabelledBy(NodeId),
ActiveDescendant(NodeId),
}
#[derive(Debug, Clone, PartialEq)]
pub struct A11yNode {
pub id: NodeId,
pub role: A11yRole,
pub name: String,
pub description: Option<String>,
pub states: States,
pub parent: Option<NodeId>,
pub relations: Vec<Relation>,
}
impl A11yNode {
#[must_use]
pub fn new(id: NodeId, role: A11yRole, name: impl Into<String>) -> Self {
Self {
id,
role,
name: name.into(),
description: None,
states: States::default(),
parent: None,
relations: Vec::new(),
}
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
#[must_use]
pub fn with_states(mut self, states: States) -> Self {
self.states = states;
self
}
#[must_use]
pub fn with_parent(mut self, parent: NodeId) -> Self {
self.parent = Some(parent);
self
}
pub fn with_relation(mut self, relation: Relation) -> Self {
self.relations.push(relation);
self
}
}