use accesskit::{
Action, ActionRequest, Node, NodeId, Rect, Role, Toggled, TreeId, TreeInfo, TreeUpdate,
};
use super::focus::{self, Scope};
use super::widgets::{MASK, WidgetKind, WidgetState};
use super::{Content, NodeKey, ROOT_KEY, Ui};
use crate::style::PseudoClasses;
impl Ui {
#[must_use]
pub fn accessibility_update(&self) -> TreeUpdate {
let root = NodeId(ROOT_KEY.0);
let mut window = Node::new(Role::Window);
let mut nodes = Vec::with_capacity(self.nodes.len() + 1);
for index in 0..self.nodes.len() {
if self.nodes[index].parent.is_none() {
self.push_accessible(index, &mut window, &mut nodes);
}
}
let focus = self
.focused()
.map(|key| NodeId(key.0))
.filter(|focused| nodes.iter().any(|(id, _)| id == focused))
.unwrap_or(root);
nodes.push((root, window));
TreeUpdate {
nodes,
tree: Some(TreeInfo::new(root)),
tree_id: TreeId::ROOT,
focus,
}
}
pub fn accessibility_action(&mut self, request: &ActionRequest) -> bool {
if request.target_tree != TreeId::ROOT {
return false;
}
let key = NodeKey(request.target_node.0);
let Some(node) = self.store.by_key(key) else {
return false;
};
match request.action {
Action::Focus if node.behavior.is_focusable() => {
self.set_focus(key);
true
}
Action::Blur if self.focused() == Some(key) => {
self.clear_focus();
true
}
Action::Click if clickable(node.behavior) => {
self.requested_click = Some(key);
true
}
_ => false,
}
}
pub(super) fn resolve_requested_click(&mut self) -> Option<NodeKey> {
let key = self.requested_click.take()?;
let slot = self.store.find(key)?;
let node = self.store.get_mut(slot);
if !clickable(node.behavior) {
return None;
}
node.interaction.clicked = true;
Some(key)
}
fn push_accessible(&self, index: usize, parent: &mut Node, out: &mut Vec<(NodeId, Node)>) {
let frame = &self.nodes[index];
let stored = self.store.get(frame.slot);
let kind = stored.kind;
if kind == WidgetKind::Decoration {
return;
}
let id = NodeId(frame.key.0);
parent.push_child(id);
let mut node = Node::new(role_of(frame.content, kind));
let (min, max) = stored.rect;
node.set_bounds(Rect {
x0: f64::from(min.x),
y0: f64::from(min.y),
x1: f64::from(max.x),
y1: f64::from(max.y),
});
if !stored.hittable {
node.set_hidden();
}
if stored.behavior.disabled {
node.set_disabled();
}
if stored.behavior.scope == Scope::Modal {
node.set_modal();
}
if stored.behavior.is_focusable() {
node.add_action(Action::Focus);
}
if self.focused() == Some(frame.key) {
node.add_action(Action::Blur);
}
if clickable(stored.behavior) {
node.add_action(Action::Click);
}
if let Content::Text { start, end } | Content::Run { start, end } = frame.content {
node.set_value(&self.text[start..end]);
}
let state = stored.state;
match kind {
WidgetKind::Checkbox => node.set_toggled(if state.contains(PseudoClasses::CHECKED) {
Toggled::True
} else {
Toggled::False
}),
WidgetKind::Disclosure | WidgetKind::Select => {
node.set_expanded(state.contains(PseudoClasses::OPEN));
}
WidgetKind::MenuItem if stored.widget == WidgetState::MenuBranch => {
node.set_expanded(state.contains(PseudoClasses::OPEN));
}
WidgetKind::TreeRow | WidgetKind::OutlinerRow => {
if let WidgetState::TreeItem {
open, branch: true, ..
} = stored.widget
{
node.set_expanded(open);
}
}
_ => {}
}
if matches!(
kind,
WidgetKind::Tab | WidgetKind::SelectOption | WidgetKind::OutlinerRow
) {
node.set_selected(state.contains(PseudoClasses::CHECKED));
}
if is_whole(kind) {
self.name_from_contents(index, kind, &mut node);
} else {
let mut next = frame.first_child;
while let Some(child) = next {
self.push_accessible(child, &mut node, out);
next = self.nodes[child].next_sibling;
}
}
out.push((id, node));
}
fn name_from_contents(&self, index: usize, kind: WidgetKind, node: &mut Node) {
let mut text = String::new();
self.gather_text(index, &mut text);
let key = self.nodes[index].key;
match kind {
WidgetKind::TextInput { masked } => {
let value = self
.edits
.get(&key)
.map(|state| state.text())
.unwrap_or_default();
if masked {
node.set_value(String::from_iter(std::iter::repeat_n(
MASK,
value.chars().count(),
)));
} else {
node.set_value(value);
}
if value.is_empty() && !text.is_empty() {
node.set_placeholder(text);
}
}
WidgetKind::DragValue => {
let typed = self
.store
.by_key(key)
.is_some_and(|stored| stored.widget.is_typed_number());
match self.edits.get(&key).filter(|_| typed) {
Some(state) => node.set_value(state.text()),
None => node.set_value(text),
}
}
WidgetKind::Select => node.set_value(text),
_ if !text.is_empty() => node.set_label(text),
_ => {}
}
}
fn gather_text(&self, index: usize, out: &mut String) {
let mut next = self.nodes[index].first_child;
while let Some(child) = next {
let node = &self.nodes[child];
if self.store.get(node.slot).kind != WidgetKind::Decoration {
if let Content::Text { start, end } | Content::Run { start, end } = node.content {
let text = self.text[start..end].trim();
if !text.is_empty() {
if !out.is_empty() {
out.push(' ');
}
out.push_str(text);
}
}
self.gather_text(child, out);
}
next = node.next_sibling;
}
}
}
fn clickable(behavior: focus::Behavior) -> bool {
behavior.role != focus::Role::None && !behavior.disabled
}
const fn role_of(content: Content, kind: WidgetKind) -> Role {
match kind {
WidgetKind::None | WidgetKind::Decoration => match content {
Content::Block => Role::GenericContainer,
Content::Text { .. } | Content::Run { .. } => Role::Label,
Content::Paragraph => Role::Paragraph,
Content::Image(_) => Role::Image,
},
WidgetKind::Button | WidgetKind::Disclosure => Role::Button,
WidgetKind::Checkbox => Role::CheckBox,
WidgetKind::Slider => Role::Slider,
WidgetKind::DragValue => Role::SpinButton,
WidgetKind::TextInput { masked: false } => Role::TextInput,
WidgetKind::TextInput { masked: true } => Role::PasswordInput,
WidgetKind::Select => Role::ComboBox,
WidgetKind::SelectList => Role::ListBox,
WidgetKind::SelectOption => Role::ListBoxOption,
WidgetKind::TreeRow | WidgetKind::OutlinerRow => Role::TreeItem,
WidgetKind::Outliner => Role::Tree,
WidgetKind::OutlinerToggle => Role::DisclosureTriangle,
WidgetKind::List => Role::List,
WidgetKind::ListRow => Role::ListItem,
WidgetKind::TabStrip => Role::TabList,
WidgetKind::Tab => Role::Tab,
WidgetKind::TabPanel => Role::TabPanel,
WidgetKind::Splitter => Role::Splitter,
WidgetKind::Pane => Role::Pane,
WidgetKind::ScrollBar => Role::ScrollBar,
WidgetKind::Popup => Role::Dialog,
WidgetKind::Menu => Role::Menu,
WidgetKind::MenuItem => Role::MenuItem,
WidgetKind::Tooltip => Role::Tooltip,
WidgetKind::Inspector => Role::Group,
}
}
const fn is_whole(kind: WidgetKind) -> bool {
matches!(
kind,
WidgetKind::Button
| WidgetKind::Checkbox
| WidgetKind::Slider
| WidgetKind::DragValue
| WidgetKind::TextInput { .. }
| WidgetKind::Select
| WidgetKind::SelectOption
| WidgetKind::Disclosure
| WidgetKind::TreeRow
| WidgetKind::Tab
| WidgetKind::MenuItem
| WidgetKind::Splitter
| WidgetKind::OutlinerToggle
)
}