1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::accessibility::*;
use accesskit::{Node, NodeClassSet, NodeId as AccessibilityId};
use std::sync::{Arc, Mutex};

pub type SharedAccessibilityState = Arc<Mutex<AccessibilityState>>;

pub const ACCESSIBILITY_ROOT_ID: AccessibilityId = AccessibilityId(0);

/// Manages the Accessibility integration.
pub struct AccessibilityState {
    /// Accessibility Nodes
    pub nodes: Vec<(AccessibilityId, Node)>,

    /// Accessibility tree
    pub node_classes: NodeClassSet,

    /// Current focused Accessibility Node.
    pub focused_id: AccessibilityId,
}

impl AccessibilityState {
    pub fn new(focused_id: AccessibilityId) -> Self {
        Self {
            focused_id,
            node_classes: NodeClassSet::default(),
            nodes: Vec::default(),
        }
    }

    /// Wrap it in a `Arc<Mutex<T>>`.
    pub fn wrap(self) -> SharedAccessibilityState {
        Arc::new(Mutex::new(self))
    }

    /// Clear the Accessibility Nodes.
    pub fn clear(&mut self) {
        self.nodes.clear();
    }
}

impl AccessibilityProvider for AccessibilityState {
    fn node_classes(&mut self) -> &mut NodeClassSet {
        &mut self.node_classes
    }

    fn nodes(&self) -> std::slice::Iter<(AccessibilityId, Node)> {
        self.nodes.iter()
    }

    fn focus_id(&self) -> AccessibilityId {
        self.focused_id
    }

    fn set_focus(&mut self, new_focus_id: AccessibilityId) {
        self.focused_id = new_focus_id;
    }

    fn push_node(&mut self, id: AccessibilityId, node: Node) {
        self.nodes.push((id, node))
    }
}