use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};
use std::task::Waker;
use unicode_segmentation::UnicodeSegmentation;
use crate::geometry::Rect;
use crate::slider::Range;
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,
pub text: Option<AccessText>,
pub value: Option<AccessValue>,
}
#[doc(hidden)]
#[derive(Clone, Debug, PartialEq)]
pub struct AccessText {
pub text: String,
pub caret: usize,
pub selection: Option<(usize, usize)>,
}
impl AccessText {
pub fn segment(&self, at: usize, granularity: u32) -> Option<(usize, usize)> {
let text = self.text.as_str();
let pieces: Vec<&str> = match granularity {
0 => text.split_inclusive(|_| true).collect(),
1 => {
let mut cuts: Vec<usize> = text
.split_word_bound_indices()
.filter(|(at, piece)| *at > 0 && piece.chars().any(char::is_alphanumeric))
.map(|(at, _)| at)
.collect();
cuts.insert(0, 0);
cuts.push(text.len());
cuts.windows(2).map(|cut| &text[cut[0]..cut[1]]).collect()
}
2 => text.split_sentence_bounds().collect(),
3 | 4 => text.split_inclusive('\n').collect(),
_ => return None,
};
let mut start = 0;
let mut last = (0, 0);
for piece in pieces {
let end = start + piece.chars().count();
if at < end {
return Some((start, end));
}
last = (start, end);
start = end;
}
Some(
if granularity == 0 || (granularity >= 3 && text.ends_with('\n')) {
(start, start)
} else {
last
},
)
}
}
#[doc(hidden)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AccessValue {
pub min: f64,
pub max: f64,
pub current: f64,
pub increment: f64,
}
#[doc(hidden)]
#[derive(Clone, 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,
TextChanged {
id: AccessId,
start: usize,
text: String,
inserted: bool,
},
CaretMoved(AccessId, usize),
ValueChanged(AccessId),
}
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 slider = (role == AccessRole::Slider)
.then(|| slider_button(&tree, node_id))
.flatten();
let (enabled, clickable) = match (&node.kind, slider) {
(_, Some((enabled, _))) => (enabled, false),
(NodeKind::Button(button), None) => (button.enabled, button.enabled),
_ => (true, false),
};
let (editable, multi_line, text) = match &node.kind {
NodeKind::Input(input) => (true, input.edit.multiline, Some(input.access_text())),
_ => (false, false, None),
};
let focusable = match role {
AccessRole::Button | AccessRole::Slider => enabled,
AccessRole::Entry | AccessRole::PasswordText => true,
_ => false,
};
let children = match role {
AccessRole::Slider => Vec::new(),
_ => exposed_children(&tree, node_id),
};
let name = name(&tree, node_id);
let rect = node.rect.unwrap_or_default();
let (children, shown, active) = (
children.into_iter().map(at).collect(),
tree.shown(node_id),
is_root && tree.keyboard.entered,
);
drop(tree);
let value = slider.and_then(|(_, range)| range).map(Range::access);
Some(AccessNode {
role,
name,
parent,
children,
index,
rect,
focusable,
focused,
enabled,
shown,
active,
editable,
multi_line,
clickable,
text,
value,
})
}
pub fn set_value(&self, id: AccessId, value: f64) -> bool {
let Some(rc) = self.tree(id.window).filter(|_| !value.is_nan()) else {
return false;
};
let range = {
let Ok(tree) = rc.try_borrow() else {
return false;
};
let node_id = NodeId::from(id.node);
if role(&tree, node_id) != AccessRole::Slider || !tree.shown(node_id) {
return false;
}
match slider_button(&tree, node_id) {
Some((true, Some(range))) => range,
_ => return false,
}
};
drop(rc);
range.write(value as f32);
true
}
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 field_before(&self, id: NodeId) -> Option<AccessText> {
match &self.node(id)?.kind {
NodeKind::Input(input) if self.listening() && self.node(id)?.rect.is_some() => {
Some(input.access_text())
}
_ => None,
}
}
pub(crate) fn field_changed(&self, id: NodeId, before: Option<AccessText>) {
let (Some(before), Some(NodeKind::Input(input))) = (before, self.node(id).map(|n| &n.kind))
else {
return;
};
let after = input.access_text();
let (old, new): (Vec<char>, Vec<char>) =
(before.text.chars().collect(), after.text.chars().collect());
let start = old.iter().zip(&new).take_while(|(a, b)| a == b).count();
let end = old[start..]
.iter()
.rev()
.zip(new[start..].iter().rev())
.take_while(|(a, b)| a == b)
.count();
let changes = [
(&old[start..old.len() - end], false),
(&new[start..new.len() - end], true),
];
for (text, inserted) in changes.into_iter().filter(|(text, _)| !text.is_empty()) {
let text = text.iter().collect();
self.access_event(|window| AccessEvent::TextChanged {
id: self.access_id(window, id),
start,
text,
inserted,
});
}
if after.caret != before.caret && self.keyboard.focused == Some(id) {
self.access_event(|window| AccessEvent::CaretMoved(self.access_id(window, id), after.caret));
}
}
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 slider_button(tree: &Tree, id: NodeId) -> Option<(bool, Option<Range>)> {
tree
.taffy
.children(id)
.ok()?
.into_iter()
.find_map(|child| match &tree.node(child)?.kind {
NodeKind::Button(button) => Some((button.enabled, button.value)),
_ => None,
})
}
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(),
}
}