Skip to main content

vizia_core/context/
access.rs

1use accesskit::{Node, NodeId, Rect, TextDirection, TextSelection};
2use hashbrown::HashMap;
3
4use crate::{cache::CachedData, prelude::*, text::TextContext};
5
6/// A context used for configuring the accessibility features of a view.
7pub struct AccessContext<'a> {
8    pub(crate) current: Entity,
9    pub(crate) tree: &'a Tree<Entity>,
10    pub(crate) entity_identifiers: &'a HashMap<String, Entity>,
11    pub(crate) style: &'a Style,
12    pub(crate) cache: &'a CachedData,
13    pub(crate) text_context: &'a mut TextContext,
14}
15
16impl AccessContext<'_> {
17    /// Returns the bounds of the current view.
18    pub fn bounds(&self) -> BoundingBox {
19        self.cache.get_bounds(self.current)
20    }
21
22    /// Finds the entity that identifier identifies.
23    pub fn resolve_entity_identifier(&self, identity: &str) -> Option<Entity> {
24        self.entity_identifiers.get(identity).copied()
25    }
26}
27
28/// Wrapper around an accesskit node builder, a node id, and a list of children to be added to the node.
29#[derive(Debug)]
30pub struct AccessNode {
31    pub(crate) node_id: NodeId,
32    pub(crate) node_builder: Node,
33    pub(crate) children: Vec<AccessNode>,
34}
35
36impl AccessNode {
37    /// Creates a new [AccessNode] from a parent [NodeId] and a child index.
38    pub fn new_from_parent(parent_id: NodeId, index: usize) -> Self {
39        // Concatenate the parent id with the index of the text line to form a unique node id.
40        let mut node_id = parent_id.0 << 32;
41        node_id |= index as u64;
42        let node_id: NodeId = NodeId(node_id);
43
44        Self { node_id, node_builder: Node::default(), children: Vec::new() }
45    }
46
47    /// Returns the accesskit id of the access node.
48    pub(crate) fn node_id(&self) -> NodeId {
49        self.node_id
50    }
51
52    /// Adds a child accessibility node.
53    pub fn add_child(&mut self, child: AccessNode) {
54        self.children.push(child);
55    }
56
57    /// Sets the role of the node.
58    pub fn set_role(&mut self, role: Role) {
59        self.node_builder.set_role(role);
60    }
61
62    /// Sets the direction of any text within the node.
63    pub fn set_text_direction(&mut self, text_direction: TextDirection) {
64        self.node_builder.set_text_direction(text_direction);
65    }
66
67    /// Sets the specified selection of any text within the node.
68    pub fn set_text_selection(&mut self, text_selection: TextSelection) {
69        self.node_builder.set_text_selection(text_selection);
70    }
71
72    /// Sets the accessibility bounds of the node. This is not the same as the layout bounds.
73    pub fn set_bounds(&mut self, bounds: BoundingBox) {
74        self.node_builder.set_bounds(Rect {
75            x0: bounds.left() as f64,
76            y0: bounds.top() as f64,
77            x1: bounds.right() as f64,
78            y1: bounds.bottom() as f64,
79        });
80    }
81
82    /// Sets the value of a node.
83    pub fn set_value(&mut self, value: impl Into<Box<str>>) {
84        self.node_builder.set_value(value);
85    }
86
87    /// Sets the array of character lengths for a text node.
88    pub fn set_character_lengths(&mut self, character_lengths: impl Into<Box<[u8]>>) {
89        self.node_builder.set_character_lengths(character_lengths);
90    }
91
92    /// Sets the array of character positions for a text node.
93    pub fn set_character_positions(&mut self, character_positions: impl Into<Box<[f32]>>) {
94        self.node_builder.set_character_positions(character_positions);
95    }
96
97    /// Sets the array of character widths for a text node.
98    pub fn set_character_widths(&mut self, character_widths: impl Into<Box<[f32]>>) {
99        self.node_builder.set_character_widths(character_widths);
100    }
101
102    /// Sets the array of word start character positions for a text node.
103    pub fn set_word_starts(&mut self, word_starts: impl Into<Box<[u8]>>) {
104        self.node_builder.set_word_starts(word_starts);
105    }
106
107    /// Sets the placeholder of a text input node.
108    pub fn set_placeholder(&mut self, placeholder: impl Into<Box<str>>) {
109        self.node_builder.set_placeholder(placeholder);
110    }
111
112    /// Sets the step for a numerical node.
113    pub fn set_numeric_value_step(&mut self, value: f64) {
114        self.node_builder.set_numeric_value_step(value);
115    }
116
117    /// Sets the value for a numerical node.
118    pub fn set_numeric_value(&mut self, value: f64) {
119        self.node_builder.set_numeric_value(value);
120    }
121
122    /// Sets the jump for a numerical node.
123    pub fn set_numeric_value_jump(&mut self, value: f64) {
124        self.node_builder.set_numeric_value_jump(value);
125    }
126
127    /// Sets the minimum for a numerical node.
128    pub fn set_min_numeric_value(&mut self, value: f64) {
129        self.node_builder.set_min_numeric_value(value);
130    }
131
132    /// Sets the maximum for a numerical node.
133    pub fn set_max_numeric_value(&mut self, value: f64) {
134        self.node_builder.set_max_numeric_value(value);
135    }
136}