use super::*;
impl A11yRole {
#[must_use]
pub const fn native_role_name(&self, platform: PlatformKind) -> &'static str {
match platform {
PlatformKind::MacOs => match *self {
Self::Window => "NSAccessibilityWindowRole",
Self::Region | Self::Navigation | Self::Group => ROLE_MAC_GROUP,
Self::Heading | Self::Text => ROLE_MAC_STATIC_TEXT,
Self::Button => "NSAccessibilityButtonRole",
Self::TextInput => "NSAccessibilityTextFieldRole",
Self::Checkbox | Self::Switch => "NSAccessibilityCheckBoxRole",
Self::Slider => "NSAccessibilitySliderRole",
Self::Tab => "NSAccessibilityRadioButtonRole",
Self::TabList => "NSAccessibilityTabGroupRole",
Self::List => "NSAccessibilityListRole",
Self::ListItem => "NSAccessibilityRowRole",
Self::Dialog | Self::Alert => "NSAccessibilitySheetRole",
Self::ProgressBar => "NSAccessibilityProgressIndicatorRole",
Self::Image | Self::Canvas => "NSAccessibilityImageRole",
Self::ScrollView => "NSAccessibilityScrollAreaRole",
Self::Link => "NSAccessibilityLinkRole",
Self::Menu => "NSAccessibilityMenuRole",
Self::MenuItem => "NSAccessibilityMenuItemRole",
},
PlatformKind::Ios | PlatformKind::IpadOs => match *self {
Self::Button | Self::Tab | Self::MenuItem => "UIAccessibilityTraitButton",
Self::Heading => "UIAccessibilityTraitHeader",
Self::Link => "UIAccessibilityTraitLink",
Self::Image | Self::Canvas => "UIAccessibilityTraitImage",
Self::Slider | Self::ProgressBar => "UIAccessibilityTraitAdjustable",
Self::Text | Self::Alert => "UIAccessibilityTraitStaticText",
_ => "UIAccessibilityTraitNone",
},
PlatformKind::Windows => match *self {
Self::Window | Self::Dialog => "UIA_WindowControlTypeId",
Self::Button => "UIA_ButtonControlTypeId",
Self::TextInput => "UIA_EditControlTypeId",
Self::Checkbox | Self::Switch => "UIA_CheckBoxControlTypeId",
Self::Slider => "UIA_SliderControlTypeId",
Self::Tab => "UIA_TabItemControlTypeId",
Self::TabList => "UIA_TabControlTypeId",
Self::List => "UIA_ListControlTypeId",
Self::ListItem => "UIA_ListItemControlTypeId",
Self::ProgressBar => "UIA_ProgressBarControlTypeId",
Self::Image | Self::Canvas => "UIA_ImageControlTypeId",
Self::Link => "UIA_HyperlinkControlTypeId",
Self::Menu => "UIA_MenuControlTypeId",
Self::MenuItem => "UIA_MenuItemControlTypeId",
Self::Text | Self::Heading | Self::Alert => "UIA_TextControlTypeId",
Self::ScrollView => "UIA_PaneControlTypeId",
Self::Group | Self::Region | Self::Navigation => "UIA_GroupControlTypeId",
},
PlatformKind::Linux => match *self {
Self::Window => "ATK_ROLE_FRAME",
Self::Dialog => "ATK_ROLE_DIALOG",
Self::Alert => "ATK_ROLE_ALERT",
Self::Button => "ATK_ROLE_PUSH_BUTTON",
Self::TextInput => "ATK_ROLE_ENTRY",
Self::Checkbox => "ATK_ROLE_CHECK_BOX",
Self::Switch => "ATK_ROLE_TOGGLE_BUTTON",
Self::Slider => "ATK_ROLE_SLIDER",
Self::Tab => "ATK_ROLE_PAGE_TAB",
Self::TabList => "ATK_ROLE_PAGE_TAB_LIST",
Self::List => "ATK_ROLE_LIST",
Self::ListItem => "ATK_ROLE_LIST_ITEM",
Self::ProgressBar => "ATK_ROLE_PROGRESS_BAR",
Self::Image | Self::Canvas => "ATK_ROLE_IMAGE",
Self::ScrollView => "ATK_ROLE_SCROLL_PANE",
Self::Link => "ATK_ROLE_LINK",
Self::Menu => "ATK_ROLE_MENU",
Self::MenuItem => "ATK_ROLE_MENU_ITEM",
Self::Heading => "ATK_ROLE_HEADING",
Self::Text => "ATK_ROLE_LABEL",
Self::Group | Self::Region | Self::Navigation => "ATK_ROLE_PANEL",
},
PlatformKind::Android => match *self {
Self::Button | Self::MenuItem => "android.widget.Button",
Self::TextInput => "android.widget.EditText",
Self::Checkbox => "android.widget.CheckBox",
Self::Switch => "android.widget.Switch",
Self::Slider => "android.widget.SeekBar",
Self::ProgressBar => "android.widget.ProgressBar",
Self::Image | Self::Canvas => "android.widget.ImageView",
Self::ScrollView => "android.widget.ScrollView",
Self::List => "android.widget.ListView",
Self::Text | Self::Heading | Self::Alert | Self::Link => "android.widget.TextView",
_ => "android.view.View",
},
PlatformKind::Web | PlatformKind::Headless => match *self {
Self::Window => "application",
Self::Region | Self::ScrollView => ROLE_ARIA_REGION,
Self::Navigation => "navigation",
Self::Heading => "heading",
Self::Text => "paragraph",
Self::Button => "button",
Self::TextInput => "textbox",
Self::Checkbox => "checkbox",
Self::Switch => "switch",
Self::Slider => "slider",
Self::Tab => "tab",
Self::TabList => "tablist",
Self::List => "list",
Self::ListItem => "listitem",
Self::Dialog => "dialog",
Self::Alert => "alert",
Self::ProgressBar => "progressbar",
Self::Image | Self::Canvas => "img",
Self::Link => "link",
Self::Menu => "menu",
Self::MenuItem => "menuitem",
Self::Group => "group",
},
}
}
}
impl A11yProps {
#[must_use]
pub fn checked_state(&self) -> Option<bool> {
let Self { checked, .. } = self;
*checked
}
}
impl A11yTree {
#[must_use]
pub fn from_scene(tree: &SceneTree) -> Self {
let root: NodeId = tree.get_root();
let focused_id: Option<NodeId> = tree.get_focus().focused_node();
let initial_cap: usize = (tree.live_node_count() / 2).max(1);
let mut nodes: Vec<A11yNode> = Vec::with_capacity(initial_cap);
Self::collect_node(tree, root, root, focused_id, &mut nodes);
Self::new(root, nodes, focused_id)
}
#[must_use]
#[inline(always)]
fn is_node_exposed(node: &SceneNode) -> bool {
if node.style_ref().layout_ref().get_display() == Display::None {
return false;
}
!node.a11y_props_ref().is_some_and(A11yProps::get_hidden)
}
fn collect_node(
tree: &SceneTree,
node_id: NodeId,
root: NodeId,
focused_id: Option<NodeId>,
out: &mut Vec<A11yNode>,
) {
let Some(node) = tree.get_node(node_id) else {
return;
};
if node.style_ref().layout_ref().get_display() == Display::None {
return;
}
let explicit_ref: Option<&A11yProps> = node.a11y_props_ref();
if explicit_ref.is_some_and(A11yProps::get_hidden) {
return;
}
let props: A11yProps = match explicit_ref {
Some(explicit) => explicit.clone(),
None => {
let mut inferred: A11yProps = match node.get_kind() {
NodeKind::Text(t) => A11yProps::new(A11yRole::Text, t.clone()),
NodeKind::Input(state) => {
let mut p: A11yProps =
A11yProps::new(A11yRole::TextInput, state.get_placeholder().clone());
p.set_value(state.get_value().clone())
.set_disabled(state.get_disabled());
p
}
NodeKind::Image(_) => A11yProps::new(A11yRole::Image, String::new()),
NodeKind::Canvas => A11yProps::new(A11yRole::Canvas, String::new()),
NodeKind::ScrollView => A11yProps::new(A11yRole::ScrollView, String::new()),
NodeKind::Portal => A11yProps::new(A11yRole::Dialog, String::new()),
NodeKind::View | NodeKind::Fragment => {
let role: A11yRole = if node_id == root {
A11yRole::Window
} else if node
.get_handlers()
.handler_for(&UiEventKind::Click(Point::default()))
.is_some()
{
A11yRole::Button
} else {
A11yRole::Group
};
A11yProps::new(role, String::new())
}
};
if node.get_interaction().get_focused() {
inferred.set_live_region(A11yLiveRegion::Polite);
}
inferred
}
};
let raw_children: &[NodeId] = node.get_children().as_slice();
let visible_children: Vec<NodeId> = if raw_children.is_empty() {
Vec::new()
} else {
let mut vis: Vec<NodeId> = Vec::with_capacity(raw_children.len());
for &cid in raw_children {
if tree.get_node(cid).is_some_and(Self::is_node_exposed) {
vis.push(cid);
}
}
vis
};
out.push(A11yNode::new(
node_id,
props,
node.get_layout_box().get_bounds(),
focused_id == Some(node_id),
visible_children,
));
for &child_id in raw_children {
Self::collect_node(tree, child_id, root, focused_id, out);
}
}
#[must_use]
pub fn find(&self, id: NodeId) -> Option<&A11yNode> {
self.get_nodes()
.iter()
.find(|node: &&A11yNode| node.get_id() == id)
}
#[must_use]
pub fn focused_node(&self) -> Option<NodeId> {
let Self { focused_id, .. } = self;
*focused_id
}
#[must_use]
pub fn format_summary(&self, platform: PlatformKind) -> String {
let mut out: String = String::with_capacity(self.get_nodes().len().saturating_mul(24));
for node in self.get_nodes().iter() {
let role: &str = node.get_props().get_role().native_role_name(platform);
let label: &str = node.get_props().get_label().as_str();
out.push_str(role);
if !label.is_empty() {
out.push_str(": ");
out.push_str(label);
}
out.push('\n');
}
out
}
}