use cranpose_core::collections::map::HashMap;
use crate::accessibility::{
AccessibilityElement, AccessibilityIdentityError, AccessibilityRect, AccessibilitySnapshot,
CollectionItem, checked_state, spoken_changes, utf16_offset,
};
#[derive(Debug, Default, PartialEq)]
pub(crate) struct AccessibilityUpdate {
pub(crate) order: Vec<i32>,
pub(crate) records: Vec<u8>,
pub(crate) moves: Vec<i32>,
reordered: bool,
}
impl AccessibilityUpdate {
pub(crate) fn is_empty(&self) -> bool {
!self.reordered && self.records.is_empty() && self.moves.is_empty()
}
}
#[derive(Default)]
pub(crate) struct AccessibilityWire {
density: Option<u32>,
parents: Vec<i32>,
}
impl AccessibilityWire {
pub(crate) fn forget(&mut self) {
self.density = None;
}
pub(crate) fn publish(
&mut self,
snapshot: &mut AccessibilitySnapshot,
elements: Vec<AccessibilityElement>,
density: f32,
) -> Result<AccessibilityUpdate, AccessibilityIdentityError> {
let density = density.max(f32::EPSILON);
let mut replaced = snapshot.update(elements)?;
let changed = spoken_changes(&replaced.elements, &snapshot.elements, &replaced.was);
let parents = scroll_parent_ids(&snapshot.elements, &snapshot.ids);
let known = self.density == Some(density.to_bits());
let mut records = Vec::new();
let mut moves = Vec::new();
for (((element, id), parent), (spoken_change, was)) in snapshot
.elements
.iter()
.zip(&snapshot.ids)
.zip(&parents)
.zip(changed.into_iter().zip(&replaced.was))
{
let published = match *was {
Some(old) if known && !spoken_change && self.parents.get(old) == Some(parent) => {
replaced.elements.get_mut(old)
}
_ => None,
};
let kept =
published.and_then(|was| same_but_bounds(was, element).then_some(was.bounds));
match kept {
Some(was) => {
let bounds = pixel_bounds(element.bounds, density);
if pixel_bounds(was, density) != bounds {
moves.push(*id);
moves.extend(bounds);
}
}
None => {
encode_record(&mut records, element, *id, *parent, spoken_change, density);
}
}
}
let reordered = !known || snapshot.ids != replaced.ids;
snapshot.recycle(replaced.elements);
self.density = Some(density.to_bits());
self.parents = parents;
Ok(AccessibilityUpdate {
order: snapshot.ids.clone(),
records,
moves,
reordered,
})
}
}
fn same_but_bounds(was: &mut AccessibilityElement, now: &AccessibilityElement) -> bool {
let bounds = std::mem::replace(&mut was.bounds, now.bounds);
let same = *was == *now;
was.bounds = bounds;
same
}
fn pixel_bounds(bounds: AccessibilityRect, density: f32) -> [i32; 4] {
[
(bounds.x * density).round() as i32,
(bounds.y * density).round() as i32,
((bounds.x + bounds.width) * density).round() as i32,
((bounds.y + bounds.height) * density).round() as i32,
]
}
fn encode_record(
out: &mut Vec<u8>,
element: &AccessibilityElement,
id: i32,
parent: i32,
changed: bool,
density: f32,
) {
let details = element.details();
let [left, top, right, bottom] = pixel_bounds(element.bounds, density);
let (center_x, center_y) = element.bounds.center();
let progress = details.progress;
let scroll = details.vertical_scroll.or(details.horizontal_scroll);
let (selection_start, selection_end) = selection_in_utf16(element);
let mut record = RecordWriter(out);
record.number(id);
record.number(element.role.android_code());
record.number(left);
record.number(top);
record.number(right);
record.number(bottom);
record.float(center_x);
record.float(center_y);
record.number(i32::from(element.clickable));
record.text(&element.label);
record.text(element.value.as_deref().unwrap_or(""));
record.text(details.state_description.as_deref().unwrap_or(""));
record.text(details.click_label.as_deref().unwrap_or(""));
record.number(tristate(element.selected));
record.number(tristate(checked_state(element)));
record.number(i32::from(element.enabled));
record.actions(&details.custom_actions);
record.number(i32::from(element.focusable));
record.number(i32::from(element.focused));
record.number(i32::from(details.adjustable));
record.float(progress.map_or(0.0, |p| p.current));
record.float(progress.map_or(0.0, |p| p.start));
record.float(progress.map_or(0.0, |p| p.end));
record.number(i32::from(scroll.is_some()));
record.number(i32::from(
scroll.is_some_and(|range| range.can_scroll_forward()),
));
record.number(i32::from(
scroll.is_some_and(|range| range.can_scroll_backward()),
));
record.number(parent);
record.number(
details
.collection
.map_or(0, |collection| count(collection.rows)),
);
record.number(
details
.collection
.map_or(0, |collection| count(collection.columns)),
);
record.number(i32::from(changed));
record.number(element.collection_item.map_or(-1, item_row));
record.number(element.collection_item.map_or(-1, item_column));
record.text(details.pane_title.as_deref().unwrap_or(""));
record.text(details.error.as_deref().unwrap_or(""));
record.number(i32::from(details.password));
record.number(tristate(details.expanded));
record.text(details.long_click_label.as_deref().unwrap_or(""));
record.number(i32::from(details.dismissable));
record.number(i32::from(details.scroll_to_index));
record.number(selection_start);
record.number(selection_end);
}
struct RecordWriter<'a>(&'a mut Vec<u8>);
impl RecordWriter<'_> {
fn number(&mut self, value: i32) {
self.0.extend_from_slice(&value.to_le_bytes());
}
fn float(&mut self, value: f32) {
self.0.extend_from_slice(&value.to_le_bytes());
}
fn length(&mut self, length: usize) {
let length = u32::try_from(length).unwrap_or(u32::MAX);
self.0.extend_from_slice(&length.to_le_bytes());
}
fn text(&mut self, value: &str) {
self.length(value.len());
self.0.extend_from_slice(value.as_bytes());
}
fn actions(&mut self, labels: &[String]) {
self.length(labels.len());
for label in labels {
self.text(label);
}
}
}
fn selection_in_utf16(element: &AccessibilityElement) -> (i32, i32) {
match (&element.value, element.details().text_selection) {
(Some(value), Some((anchor, focus))) => (
utf16_offset(value, anchor) as i32,
utf16_offset(value, focus) as i32,
),
_ => (-1, -1),
}
}
fn scroll_parent_ids(elements: &[AccessibilityElement], ids: &[i32]) -> Vec<i32> {
let mut nodes: HashMap<cranpose_core::NodeId, i32> = HashMap::default();
for (element, id) in elements.iter().zip(ids) {
if element.canvas_key.is_none() {
nodes.entry(element.node_id).or_insert(*id);
}
}
elements
.iter()
.map(|element| {
element
.scroll_parent
.and_then(|parent| nodes.get(&parent).copied())
.unwrap_or(-1)
})
.collect()
}
fn count(value: usize) -> i32 {
i32::try_from(value).unwrap_or(i32::MAX)
}
fn item_row(item: CollectionItem) -> i32 {
if item.horizontal {
0
} else {
item.position as i32 - 1
}
}
fn item_column(item: CollectionItem) -> i32 {
if item.horizontal {
item.position as i32 - 1
} else {
0
}
}
fn tristate(value: Option<bool>) -> i32 {
match value {
None => -1,
Some(false) => 0,
Some(true) => 1,
}
}
#[cfg(test)]
#[path = "tests/android_accessibility_wire.rs"]
mod tests;