#[cfg(feature = "accesskit")]
mod accesskit_roles;
#[cfg(feature = "accesskit")]
pub use accesskit_roles::{accesskit_aria_role, accesskit_role_token};
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[repr(transparent)]
pub struct DocumentA11yNodeId(u64);
impl DocumentA11yNodeId {
pub const fn new(value: u64) -> Self {
Self(value)
}
pub const fn get(self) -> u64 {
self.0
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct DocumentA11yPoint {
pub x: f32,
pub y: f32,
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct DocumentA11yBounds {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentA11yRole {
Window,
Document,
Article,
Region,
Group,
Navigation,
Main,
Heading { level: u8 },
Paragraph,
StaticText,
Link,
Button,
TextField,
CheckBox,
RadioButton,
RadioGroup,
Switch,
ComboBox,
List,
ListItem,
ListBox,
ListBoxOption,
Table,
Row,
Cell,
Image,
Form,
Dialog,
Alert,
Menu,
MenuBar,
MenuItem,
MenuItemCheckBox,
MenuItemRadio,
TabList,
Tab,
TabPanel,
Tree,
TreeItem,
Slider,
SpinButton,
Splitter,
Toolbar,
ProgressIndicator,
Label,
Status,
Log,
Note,
Unknown,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentA11yLive {
Off,
Polite,
Assertive,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentA11yToggled {
Off,
On,
Mixed,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentA11yOrientation {
Horizontal,
Vertical,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentA11yHasPopup {
Menu,
ListBox,
Tree,
Grid,
Dialog,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct DocumentA11yState {
pub disabled: bool,
pub hidden: bool,
pub selected: Option<bool>,
pub expanded: Option<bool>,
pub checked: Option<bool>,
pub toggled: Option<DocumentA11yToggled>,
pub focused: bool,
pub editable: bool,
pub multiline: bool,
pub read_only: bool,
pub required: bool,
pub live: Option<DocumentA11yLive>,
pub orientation: Option<DocumentA11yOrientation>,
pub has_popup: Option<DocumentA11yHasPopup>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentA11yAction {
Click,
Focus,
SetValue,
ScrollIntoView,
Increment,
Decrement,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentA11yActionData {
Value(String),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DocumentA11yActionRequest {
pub revision: u64,
pub target: DocumentA11yNodeId,
pub action: DocumentA11yAction,
pub data: Option<DocumentA11yActionData>,
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
pub struct DocumentA11yClickTarget {
pub revision: u64,
pub point: DocumentA11yPoint,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DocumentA11yNode {
pub id: DocumentA11yNodeId,
pub parent: Option<DocumentA11yNodeId>,
pub children: Vec<DocumentA11yNodeId>,
pub role: DocumentA11yRole,
pub name: Option<String>,
#[serde(default)]
pub description: Option<String>,
pub value: Option<String>,
pub numeric_value: Option<f64>,
pub numeric_minimum: Option<f64>,
pub numeric_maximum: Option<f64>,
pub bounds: Option<DocumentA11yBounds>,
pub state: DocumentA11yState,
pub actions: Vec<DocumentA11yAction>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DocumentA11ySupportError {
MissingLimitation { capability: A11yCapability },
}
impl std::fmt::Display for DocumentA11ySupportError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::MissingLimitation { capability } => {
write!(
f,
"{capability:?} accessibility support requires a limitation"
)
},
}
}
}
impl std::error::Error for DocumentA11ySupportError {}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DocumentA11ySupport {
capability: A11yCapability,
limitations: Vec<String>,
}
impl DocumentA11ySupport {
pub fn new(
capability: A11yCapability,
limitations: impl IntoIterator<Item = impl Into<String>>,
) -> Result<Self, DocumentA11ySupportError> {
let limitations = limitations
.into_iter()
.map(Into::into)
.filter(|limitation: &String| !limitation.trim().is_empty())
.collect::<Vec<_>>();
if capability == A11yCapability::Partial && limitations.is_empty() {
return Err(DocumentA11ySupportError::MissingLimitation { capability });
}
Ok(Self {
capability,
limitations,
})
}
pub fn capability(&self) -> A11yCapability {
self.capability
}
pub fn limitations(&self) -> &[String] {
&self.limitations
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DocumentA11yProjection {
revision: u64,
support: DocumentA11ySupport,
root: DocumentA11yNodeId,
nodes: Vec<DocumentA11yNode>,
}
impl DocumentA11yProjection {
pub fn new(
revision: u64,
support: DocumentA11ySupport,
root: DocumentA11yNodeId,
nodes: Vec<DocumentA11yNode>,
) -> Self {
Self {
revision,
support,
root,
nodes,
}
}
pub const fn revision(&self) -> u64 {
self.revision
}
pub fn support(&self) -> &DocumentA11ySupport {
&self.support
}
pub const fn root(&self) -> DocumentA11yNodeId {
self.root
}
pub fn nodes(&self) -> &[DocumentA11yNode] {
&self.nodes
}
pub fn node(&self, id: DocumentA11yNodeId) -> Option<&DocumentA11yNode> {
self.nodes.iter().find(|node| node.id == id)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum A11yCapability {
Opaque,
Partial,
Full,
}
impl A11yCapability {
pub fn is_inspectable(self) -> bool {
self != Self::Opaque
}
}
#[cfg(test)]
mod tests {
use super::*;
fn document_node(id: u64) -> DocumentA11yNode {
DocumentA11yNode {
id: DocumentA11yNodeId::new(id),
parent: None,
children: Vec::new(),
role: DocumentA11yRole::Document,
description: None,
name: Some("Example".into()),
value: None,
numeric_value: None,
numeric_minimum: None,
numeric_maximum: None,
bounds: Some(DocumentA11yBounds {
x: 0.0,
y: 0.0,
width: 800.0,
height: 600.0,
}),
state: DocumentA11yState::default(),
actions: vec![DocumentA11yAction::Focus],
}
}
#[test]
fn partial_support_requires_a_specific_limitation() {
assert_eq!(
DocumentA11ySupport::new(A11yCapability::Partial, ["", " "])
.expect_err("partial support must explain its aperture"),
DocumentA11ySupportError::MissingLimitation {
capability: A11yCapability::Partial
}
);
let support = DocumentA11ySupport::new(
A11yCapability::Partial,
["visible links are exposed; activation is unavailable"],
)
.expect("specific limitation makes partial support honest");
assert_eq!(support.capability(), A11yCapability::Partial);
assert_eq!(support.limitations().len(), 1);
}
#[test]
fn projection_carries_stable_local_ids_revision_and_semantics() {
let root = DocumentA11yNodeId::new(41);
let projection = DocumentA11yProjection::new(
9,
DocumentA11ySupport::new(A11yCapability::Full, std::iter::empty::<String>())
.expect("full support needs no limitation"),
root,
vec![document_node(root.get())],
);
assert_eq!(projection.revision(), 9);
assert_eq!(projection.root(), root);
let node = projection
.node(root)
.expect("local root remains addressable");
assert_eq!(node.role, DocumentA11yRole::Document);
assert_eq!(node.name.as_deref(), Some("Example"));
assert_eq!(node.state.live, None, "absence differs from explicit Off");
assert_eq!(node.actions, vec![DocumentA11yAction::Focus]);
assert_eq!(projection.support().capability(), A11yCapability::Full);
}
}