use crate::NodeId;
use crate::geometry::Rect;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Role {
None,
Window,
Group,
StaticText,
Heading,
Button,
TextField,
Checkbox,
Switch,
Image,
Slider,
List,
ListItem,
ScrollArea,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct A11yProps {
pub role: Option<Role>,
pub label: Option<String>,
pub description: Option<String>,
pub value: Option<String>,
pub labelled_by: Option<NodeId>,
pub hidden: bool,
}
impl A11yProps {
pub fn new(role: Role) -> A11yProps {
A11yProps { role: Some(role), ..A11yProps::default() }
}
pub fn label(mut self, label: impl Into<String>) -> A11yProps {
self.label = Some(label.into());
self
}
pub fn description(mut self, description: impl Into<String>) -> A11yProps {
self.description = Some(description.into());
self
}
pub fn value(mut self, value: impl Into<String>) -> A11yProps {
self.value = Some(value.into());
self
}
pub fn is_empty(&self) -> bool {
*self == A11yProps::default()
}
pub fn overridden_by(&self, overrides: &A11yProps) -> A11yProps {
A11yProps {
role: overrides.role.or(self.role),
label: overrides.label.clone().or_else(|| self.label.clone()),
description: overrides.description.clone().or_else(|| self.description.clone()),
value: overrides.value.clone().or_else(|| self.value.clone()),
labelled_by: overrides.labelled_by.or(self.labelled_by),
hidden: overrides.hidden || self.hidden,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum A11yAction {
Activate,
Focus,
SetValue(String),
Increment,
Decrement,
ScrollIntoView,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ActionError {
UnknownNode,
Disabled,
Unsupported,
}
impl std::fmt::Display for ActionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
ActionError::UnknownNode => "the node does not exist",
ActionError::Disabled => "the control is disabled",
ActionError::Unsupported => "the control does not support this action",
})
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct A11yNode {
pub id: NodeId,
pub role: Role,
pub name: Option<String>,
pub description: Option<String>,
pub value: Option<String>,
pub checked: Option<bool>,
pub enabled: bool,
pub test_id: Option<String>,
pub frame: Rect,
pub children: Vec<A11yNode>,
}
impl A11yNode {
pub fn walk(&self) -> Vec<&A11yNode> {
let mut out = vec![self];
for child in &self.children {
out.extend(child.walk());
}
out
}
}