vizia_core/context/
access.rs

1use accesskit::{Node, NodeId, Rect, TextDirection, TextSelection};
2
3use crate::{cache::CachedData, prelude::*, text::TextContext};
4
5/// A context used for configuring the accessibility features of a view.
6pub struct AccessContext<'a> {
7    pub(crate) current: Entity,
8    pub(crate) tree: &'a Tree<Entity>,
9    pub(crate) style: &'a Style,
10    pub(crate) cache: &'a CachedData,
11    pub(crate) text_context: &'a mut TextContext,
12}
13
14impl AccessContext<'_> {
15    /// Returns the bounds of the current view.
16    pub fn bounds(&self) -> BoundingBox {
17        self.cache.get_bounds(self.current)
18    }
19}
20
21/// 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 {
24    pub(crate) node_id: NodeId,
25    pub(crate) node_builder: Node,
26    pub(crate) children: Vec<AccessNode>,
27}
28
29impl AccessNode {
30    /// Creates a new [AccessNode] from a parent [NodeId] and a child index.
31    pub 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.
33        let mut node_id = parent_id.0 << 32;
34        node_id |= index as u64;
35        let node_id: NodeId = NodeId(node_id);
36
37        Self { node_id, node_builder: Node::default(), children: Vec::new() }
38    }
39
40    /// Returns the accesskit id of the access node.
41    pub(crate) fn node_id(&self) -> NodeId {
42        self.node_id
43    }
44
45    /// Adds a child accessibility node.
46    pub fn add_child(&mut self, child: AccessNode) {
47        self.children.push(child);
48    }
49
50    /// Sets the role of the node.
51    pub fn set_role(&mut self, role: Role) {
52        self.node_builder.set_role(role);
53    }
54
55    /// Sets the direction of any text within the node.
56    pub fn set_text_direction(&mut self, text_direction: TextDirection) {
57        self.node_builder.set_text_direction(text_direction);
58    }
59
60    /// Sets the specified selection of any text within the node.
61    pub fn set_text_selection(&mut self, text_selection: TextSelection) {
62        self.node_builder.set_text_selection(text_selection);
63    }
64
65    /// Sets the accessibility bounds of the node. This is not the same as the layout bounds.
66    pub fn set_bounds(&mut self, bounds: BoundingBox) {
67        self.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    }
74
75    /// Sets the value of a node.
76    pub fn set_value(&mut self, value: impl Into<Box<str>>) {
77        self.node_builder.set_value(value);
78    }
79
80    /// Sets the array of character lengths for a text node.
81    pub fn set_character_lengths(&mut self, character_lengths: impl Into<Box<[u8]>>) {
82        self.node_builder.set_character_lengths(character_lengths);
83    }
84
85    /// Sets the array of character positions for a text node.
86    pub fn set_character_positions(&mut self, character_positions: impl Into<Box<[f32]>>) {
87        self.node_builder.set_character_positions(character_positions);
88    }
89
90    /// Sets the array of character widths for a text node.
91    pub fn set_character_widths(&mut self, character_widths: impl Into<Box<[f32]>>) {
92        self.node_builder.set_character_widths(character_widths);
93    }
94
95    /// Sets the array of word lengths for a text node.
96    pub fn set_word_lengths(&mut self, word_lengths: impl Into<Box<[u8]>>) {
97        self.node_builder.set_word_lengths(word_lengths);
98    }
99
100    /// Sets the step for a numerical node.
101    pub fn set_numeric_value_step(&mut self, value: f64) {
102        self.node_builder.set_numeric_value_step(value);
103    }
104
105    /// Sets the value for a numerical node.
106    pub fn set_numeric_value(&mut self, value: f64) {
107        self.node_builder.set_numeric_value(value);
108    }
109
110    /// Sets the jump for a numerical node.
111    pub fn set_numeric_value_jump(&mut self, value: f64) {
112        self.node_builder.set_numeric_value_jump(value);
113    }
114
115    /// Sets the minimum for a numerical node.
116    pub fn set_min_numeric_value(&mut self, value: f64) {
117        self.node_builder.set_min_numeric_value(value);
118    }
119
120    /// Sets the maximum for a numerical node.
121    pub fn set_max_numeric_value(&mut self, value: f64) {
122        self.node_builder.set_max_numeric_value(value);
123    }
124}