mod bounds;
mod diff;
mod identity;
mod outline;
mod policy;
mod select;
use super::AccessNode;
use crate::{Context, Id, Vec2};
use accesskit::TreeUpdate;
use std::{collections::HashMap, ops::Range};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct Entry {
pub nid: u64,
pub parent: u32,
pub alive: bool,
pub focusable: bool,
pub clickable: bool,
pub scrolled: bool,
pub links: u64,
children: (u32, u32),
runs: (u32, u32),
}
fn span((start, end): (u32, u32)) -> Range<usize> {
start as usize..end as usize
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct AccessStats {
pub passes: u64,
pub updates: u64,
pub full_updates: u64,
pub nodes_sent: u64,
pub nodes: u64,
}
struct Built {
entries: Vec<Entry>,
children: Vec<u64>,
runs: Vec<u64>,
root: Vec<u64>,
window: (Option<String>, Vec2),
announced: [u64; 2],
focus: u64,
by_id: HashMap<Id, u64>,
scale: f32,
}
impl Built {
fn children(&self, entry: &Entry) -> &[u64] {
&self.children[span(entry.children)]
}
}
#[derive(Default)]
pub(crate) struct Snapshot {
pub nodes: Vec<AccessNode>,
pub entries: Vec<Entry>,
children: Vec<u64>,
pub index: HashMap<u64, u32>,
pub runs: HashMap<u64, (u32, u32)>,
root: Vec<u64>,
window: (Option<String>, Vec2),
announced: [u64; 2],
pub focus: u64,
pub scale: f32,
built: bool,
pub update: Option<TreeUpdate>,
hold: policy::MotionHold,
pub stats: AccessStats,
}
impl Snapshot {
pub(crate) fn reset(&mut self) {
let stats = self.stats;
*self = Self {
stats,
..Self::default()
};
}
pub(crate) fn children(&self, entry: &Entry) -> &[u64] {
&self.children[span(entry.children)]
}
fn keep(&mut self, built: Built, nodes: &mut Vec<AccessNode>, update: Option<TreeUpdate>) {
self.stats.passes += 1;
self.stats.nodes = built.entries.iter().filter(|entry| entry.alive).count() as u64;
if let Some(update) = update {
self.stats.updates += 1;
self.stats.full_updates += u64::from(update.tree.is_some());
self.stats.nodes_sent += update.nodes.len() as u64;
self.update = Some(update);
}
self.index.clear();
self.runs.clear();
for (i, entry) in built.entries.iter().enumerate() {
if !entry.alive {
continue;
}
self.index.insert(entry.nid, i as u32);
for (run, id) in built.runs[span(entry.runs)].iter().enumerate() {
self.runs.insert(*id, (i as u32, run as u32));
}
}
std::mem::swap(&mut self.nodes, nodes);
self.entries = built.entries;
self.children = built.children;
self.root = built.root;
self.window = built.window;
self.announced = built.announced;
self.focus = built.focus;
self.scale = built.scale;
self.built = true;
}
}
impl Context {
pub(crate) fn finish_accessibility(&mut self) {
if !self.a11y.active {
return;
}
self.a11y.stack.clear();
let scale = self.scale_factor();
let modal = self.top_modal_id();
let layers = select::Layers {
modal: modal.map(|layer| self.layer_rank(layer)),
rank: |layer| self.layer_rank(layer),
};
let select::Selection { mut entries, top } =
select::published(&self.a11y.nodes, &self.a11y.slots, layers);
bounds::resolve(&mut self.a11y.nodes, &self.a11y.slots, &entries, scale);
let hits = identity::Hits::new(&self.interaction.previous_hits, &self.a11y.clickable);
let identity = identity::assign(&self.a11y.nodes, &mut entries, &hits);
identity::relate(&self.a11y.nodes, &mut entries, &identity.by_id);
let announced = self.a11y.announcements.each_ref().map(|a| a.serial);
let outline = outline::build(&mut entries, &identity.runs, &top, announced);
let dialog = outline::dialog(&self.a11y.nodes, &entries, &top, modal);
let built = Built {
focus: outline::focus(self.focused(), &identity.focus_owner, dialog),
window: (
self.a11y.title.clone(),
bounds::physical(self.viewport(), scale).size(),
),
entries,
children: outline.children,
runs: identity.runs,
root: outline.root,
announced,
by_id: identity.by_id,
scale,
};
let motion = policy::Motion {
moving: self.repaint_requested() || self.wants_animation_frame(),
now: self.frame_time(),
};
let tree = &mut self.a11y.tree;
let rules = tree.rules(scale, motion);
let mut changes = diff::changes(&mut self.a11y.nodes, &built, tree, rules);
diff::window(
&built,
tree,
rules.full,
&self.a11y.announcements,
&mut changes.nodes,
);
let update = diff::update(changes.nodes, rules.full, built.focus, tree.focus);
tree.keep(built, &mut self.a11y.nodes, update);
if let Some(delay) = tree.hold.settle(changes.held, motion.now) {
self.request_repaint_after(delay);
}
}
}