1use accesskit::{Node, NodeId, Rect, TextDirection, TextSelection};
23use crate::{cache::CachedData, prelude::*, text::TextContext};
45/// A context used for configuring the accessibility features of a view.
6pub struct AccessContext<'a> {
7pub(crate) current: Entity,
8pub(crate) tree: &'a Tree<Entity>,
9pub(crate) style: &'a Style,
10pub(crate) cache: &'a CachedData,
11pub(crate) text_context: &'a mut TextContext,
12}
1314impl AccessContext<'_> {
15/// Returns the bounds of the current view.
16pub fn bounds(&self) -> BoundingBox {
17self.cache.get_bounds(self.current)
18 }
19}
2021/// Wrapper around an accesskit node builder, a node id, and a list of children to be added to the node.
22#[derive(Debug)]
23pub struct AccessNode {
24pub(crate) node_id: NodeId,
25pub(crate) node_builder: Node,
26pub(crate) children: Vec<AccessNode>,
27}
2829impl AccessNode {
30/// Creates a new [AccessNode] from a parent [NodeId] and a child index.
31pub fn new_from_parent(parent_id: NodeId, index: usize) -> Self {
32// Concatenate the parent id with the index of the text line to form a unique node id.
33let mut node_id = parent_id.0 << 32;
34 node_id |= index as u64;
35let node_id: NodeId = NodeId(node_id);
3637Self { node_id, node_builder: Node::default(), children: Vec::new() }
38 }
3940/// Returns the accesskit id of the access node.
41pub(crate) fn node_id(&self) -> NodeId {
42self.node_id
43 }
4445/// Adds a child accessibility node.
46pub fn add_child(&mut self, child: AccessNode) {
47self.children.push(child);
48 }
4950/// Sets the role of the node.
51pub fn set_role(&mut self, role: Role) {
52self.node_builder.set_role(role);
53 }
5455/// Sets the direction of any text within the node.
56pub fn set_text_direction(&mut self, text_direction: TextDirection) {
57self.node_builder.set_text_direction(text_direction);
58 }
5960/// Sets the specified selection of any text within the node.
61pub fn set_text_selection(&mut self, text_selection: TextSelection) {
62self.node_builder.set_text_selection(text_selection);
63 }
6465/// Sets the accessibility bounds of the node. This is not the same as the layout bounds.
66pub fn set_bounds(&mut self, bounds: BoundingBox) {
67self.node_builder.set_bounds(Rect {
68 x0: bounds.left() as f64,
69 y0: bounds.top() as f64,
70 x1: bounds.right() as f64,
71 y1: bounds.bottom() as f64,
72 });
73 }
7475/// Sets the value of a node.
76pub fn set_value(&mut self, value: impl Into<Box<str>>) {
77self.node_builder.set_value(value);
78 }
7980/// Sets the array of character lengths for a text node.
81pub fn set_character_lengths(&mut self, character_lengths: impl Into<Box<[u8]>>) {
82self.node_builder.set_character_lengths(character_lengths);
83 }
8485/// Sets the array of character positions for a text node.
86pub fn set_character_positions(&mut self, character_positions: impl Into<Box<[f32]>>) {
87self.node_builder.set_character_positions(character_positions);
88 }
8990/// Sets the array of character widths for a text node.
91pub fn set_character_widths(&mut self, character_widths: impl Into<Box<[f32]>>) {
92self.node_builder.set_character_widths(character_widths);
93 }
9495/// Sets the array of word lengths for a text node.
96pub fn set_word_lengths(&mut self, word_lengths: impl Into<Box<[u8]>>) {
97self.node_builder.set_word_lengths(word_lengths);
98 }
99100/// Sets the step for a numerical node.
101pub fn set_numeric_value_step(&mut self, value: f64) {
102self.node_builder.set_numeric_value_step(value);
103 }
104105/// Sets the value for a numerical node.
106pub fn set_numeric_value(&mut self, value: f64) {
107self.node_builder.set_numeric_value(value);
108 }
109110/// Sets the jump for a numerical node.
111pub fn set_numeric_value_jump(&mut self, value: f64) {
112self.node_builder.set_numeric_value_jump(value);
113 }
114115/// Sets the minimum for a numerical node.
116pub fn set_min_numeric_value(&mut self, value: f64) {
117self.node_builder.set_min_numeric_value(value);
118 }
119120/// Sets the maximum for a numerical node.
121pub fn set_max_numeric_value(&mut self, value: f64) {
122self.node_builder.set_max_numeric_value(value);
123 }
124}