use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};
use std::task::Waker;
use crate::geometry::Rect;
use crate::tree::{NodeId, NodeKind, Tree};
#[doc(hidden)]
#[derive(Default)]
pub struct Accessibility {
windows: RefCell<Vec<(u32, Weak<RefCell<Tree>>)>>,
next: Cell<u32>,
listening: Cell<bool>,
events: RefCell<Vec<AccessEvent>>,
waker: RefCell<Option<Waker>>,
}
#[doc(hidden)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct AccessId {
pub window: u32,
pub node: u64,
}
#[doc(hidden)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AccessRole {
Frame,
Window,
Filler,
Button,
Label,
Entry,
PasswordText,
Slider,
Image,
}
#[doc(hidden)]
#[derive(Clone, Debug, PartialEq)]
pub struct AccessNode {
pub role: AccessRole,
pub name: String,
pub parent: Option<AccessId>,
pub children: Vec<AccessId>,
pub index: usize,
pub rect: Rect,
pub focusable: bool,
pub focused: bool,
pub enabled: bool,
pub shown: bool,
pub active: bool,
pub editable: bool,
pub multi_line: bool,
pub clickable: bool,
}
#[doc(hidden)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum AccessEvent {
Focused(AccessId, bool),
NameChanged(AccessId),
Active(AccessId, bool),
Shown(AccessId, bool),
Enabled(AccessId, bool),
Children {
parent: AccessId,
child: AccessId,
index: usize,
added: bool,
},
WindowsChanged,
}
impl Accessibility {
pub(crate) fn register(&self, tree: Weak<RefCell<Tree>>) -> u32 {
let id = self.next.get();
self.next.set(id + 1);
let mut windows = self.windows.borrow_mut();
windows.retain(|(_, tree)| tree.strong_count() > 0);
windows.push((id, tree));
if self.listening() {
self.push(AccessEvent::WindowsChanged);
}
id
}
pub fn listen(&self, on: bool) {
self.listening.set(on);
if !on {
self.events.borrow_mut().clear();
if let Some(waker) = self.waker.take() {
waker.wake();
}
}
}
pub(crate) fn listening(&self) -> bool {
self.listening.get()
}
pub(crate) fn push(&self, event: AccessEvent) {
self.events.borrow_mut().push(event);
if let Some(waker) = self.waker.take() {
waker.wake();
}
}
pub fn take_events(&self, waker: &Waker) -> Vec<AccessEvent> {
self.waker.replace(Some(waker.clone()));
std::mem::take(&mut self.events.borrow_mut())
}
fn tree(&self, window: u32) -> Option<Rc<RefCell<Tree>>> {
if !self.windows().iter().any(|root| root.window == window) {
return None;
}
let windows = self.windows.borrow();
let (_, tree) = windows.iter().find(|(id, _)| *id == window)?;
tree.upgrade()
}
pub fn windows(&self) -> Vec<AccessId> {
let windows = self.windows.borrow();
let shown: Vec<(AccessId, bool)> = windows
.iter()
.filter_map(|(id, tree)| {
let tree = tree.upgrade()?;
let tree = tree.try_borrow().ok()?;
let shown = tree
.floating
.as_ref()
.is_none_or(|floating| floating.shown());
let lock = tree
.node(tree.root)
.is_some_and(|root| root.name == "SessionLock");
let root = AccessId {
window: *id,
node: tree.root.into(),
};
shown.then_some((root, lock))
})
.collect();
let locked = shown.iter().any(|(_, lock)| *lock);
let listed = shown.into_iter().filter(|(_, lock)| *lock || !locked);
listed.map(|(root, _)| root).collect()
}
pub fn node(&self, id: AccessId) -> Option<AccessNode> {
let rc = self.tree(id.window)?;
let tree = rc.try_borrow().ok()?;
let node_id = NodeId::from(id.node);
let node = tree.node(node_id)?;
let is_root = node_id == tree.root;
let at = |node: NodeId| AccessId {
window: id.window,
node: node.into(),
};
let (parent, index) = if is_root {
let index = self.windows().iter().position(|root| *root == id);
(None, index.unwrap_or_default())
} else {
let parent = tree
.taffy
.parent(node_id)
.expect("invariant: a node other than the root has a parent");
let index = exposed_children(&tree, parent)
.iter()
.position(|&n| n == node_id);
(Some(at(parent)), index.unwrap_or_default())
};
let role = role(&tree, node_id);
let focused = tree.keyboard.entered
&& tree.keyboard.focused.map(|focused| exposed(&tree, focused)) == Some(node_id);
let (enabled, clickable) = match &node.kind {
NodeKind::Button(button) => (button.enabled, button.enabled),
_ => (true, false),
};
let (editable, multi_line) = match &node.kind {
NodeKind::Input(input) => (true, input.edit.multiline),
_ => (false, false),
};
let focusable = match role {
AccessRole::Button => enabled,
AccessRole::Entry | AccessRole::PasswordText | AccessRole::Slider => true,
_ => false,
};
let children = match role {
AccessRole::Slider => Vec::new(),
_ => exposed_children(&tree, node_id),
};
Some(AccessNode {
role,
name: name(&tree, node_id),
parent,
children: children.into_iter().map(at).collect(),
index,
rect: node.rect.unwrap_or_default(),
focusable,
focused,
enabled,
shown: tree.shown(node_id),
active: is_root && tree.keyboard.entered,
editable,
multi_line,
clickable,
})
}
pub fn click(&self, id: AccessId) -> bool {
let Some(rc) = self.tree(id.window) else {
return false;
};
let call = {
let Ok(tree) = rc.try_borrow() else {
return false;
};
let node_id = NodeId::from(id.node);
let enabled = matches!(
tree.node(node_id).map(|node| &node.kind),
Some(NodeKind::Button(button)) if button.enabled
);
if !enabled || !tree.shown(node_id) {
return false;
}
tree.click(node_id)
};
drop(rc);
if let Some(call) = call {
call.run();
}
true
}
}
impl Tree {
pub(crate) fn access_event(&self, event: impl FnOnce(u32) -> AccessEvent) {
if let Some((access, window)) = &self.access
&& access.listening()
{
access.push(event(*window));
}
}
pub(crate) fn node_event(&self, id: NodeId, event: impl FnOnce(AccessId) -> AccessEvent) {
if self.node(id).is_some_and(|node| node.rect.is_some()) {
self.access_event(|window| event(self.access_id(window, id)));
}
}
pub(crate) fn access_id(&self, window: u32, node: NodeId) -> AccessId {
AccessId {
window,
node: exposed(self, node).into(),
}
}
fn listening(&self) -> bool {
self
.access
.as_ref()
.is_some_and(|(access, _)| access.listening())
}
pub(crate) fn text_renamed(&self, id: NodeId) {
if !self.listening() {
return;
}
self.node_event(id, AccessEvent::NameChanged);
self.button_renamed(id);
}
pub(crate) fn button_renamed(&self, id: NodeId) {
if !self.listening() {
return;
}
let mut at = self.taffy.parent(id);
while let Some(parent) = at {
if matches!(
self.node(parent).map(|node| &node.kind),
Some(NodeKind::Button(_))
) {
if exposed(self, parent) == parent {
self.node_event(parent, AccessEvent::NameChanged);
}
return;
}
at = self.taffy.parent(parent);
}
}
}
fn exposed(tree: &Tree, id: NodeId) -> NodeId {
let mut found = id;
let mut at = Some(id);
while let Some(current) = at {
let Some(node) = tree.node(current) else {
break;
};
if node.name == "Slider" {
found = current;
}
at = tree.taffy.parent(current);
}
found
}
fn exposed_children(tree: &Tree, id: NodeId) -> Vec<NodeId> {
tree.taffy.children(id).unwrap_or_default()
}
impl Tree {
pub(crate) fn access_children(&self, parent: NodeId) -> Option<Vec<NodeId>> {
let seen = self.node(parent).is_some_and(|node| node.rect.is_some());
(self.listening()
&& seen
&& exposed(self, parent) == parent
&& role(self, parent) != AccessRole::Slider)
.then(|| {
self
.node(parent)
.map(|node| node.groups.concat())
.unwrap_or_default()
})
}
pub(crate) fn children_changed(&self, parent: NodeId, before: &[NodeId], after: &[NodeId]) {
let removed = before
.iter()
.enumerate()
.filter(|(_, id)| !after.contains(id));
let added = after
.iter()
.enumerate()
.filter(|(_, id)| !before.contains(id));
let changes = removed
.map(|change| (change, false))
.chain(added.map(|change| (change, true)));
for ((index, &child), added) in changes {
let id = |window, node: NodeId| AccessId {
window,
node: node.into(),
};
self.access_event(|window| AccessEvent::Children {
parent: id(window, parent),
child: id(window, child),
index,
added,
});
}
}
}
fn role(tree: &Tree, id: NodeId) -> AccessRole {
let Some(node) = tree.node(id) else {
return AccessRole::Filler;
};
if node.name == "Slider" {
return AccessRole::Slider;
}
match &node.kind {
NodeKind::Root if tree.popup.is_some() => AccessRole::Window,
NodeKind::Root => AccessRole::Frame,
NodeKind::Button(_) => AccessRole::Button,
NodeKind::Text(_) => AccessRole::Label,
NodeKind::Input(input) if input.password => AccessRole::PasswordText,
NodeKind::Input(_) => AccessRole::Entry,
NodeKind::Icon(_) | NodeKind::Image(_) | NodeKind::Capture(_) | NodeKind::Canvas(_) => {
AccessRole::Image
}
_ => AccessRole::Filler,
}
}
fn name(tree: &Tree, id: NodeId) -> String {
let Some(node) = tree.node(id) else {
return String::new();
};
if let Some(name) = &node.accessible_name {
return name.clone();
}
match &node.kind {
NodeKind::Root => match &tree.floating {
Some(floating) => floating.title().to_owned(),
None if tree.popup.is_none() && node.name == "PanelWindow" => tree.surface.namespace.clone(),
None => String::new(),
},
NodeKind::Text(text) => text.content.clone(),
NodeKind::Input(input) => input.placeholder.clone(),
NodeKind::Button(_) => {
let mut texts = Vec::new();
let mut pending = vec![id];
while let Some(at) = pending.pop() {
match tree.node(at) {
Some(node) if !node.visible => continue,
Some(node) => {
if let NodeKind::Text(text) = &node.kind
&& !text.content.trim().is_empty()
{
texts.push(text.content.trim().to_owned());
}
}
None => continue,
}
let mut children = tree.taffy.children(at).unwrap_or_default();
children.reverse();
pending.extend(children);
}
texts.join(" ")
}
_ => String::new(),
}
}