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::AccessibilityProvider;
use accesskit::{Node, NodeClassSet, NodeId as AccessibilityId};
use std::{
    num::NonZeroU128,
    sync::{Arc, Mutex},
};

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

pub const ROOT_ID: AccessibilityId = AccessibilityId(unsafe { NonZeroU128::new_unchecked(1) });

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

    /// Accessibility tree
    pub node_classes: NodeClassSet,

    /// Current focused Accessibility Node.
    pub focus: Option<AccessibilityId>,
}

impl AccessibilityState {
    pub fn new() -> Self {
        Self::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) -> Option<AccessibilityId> {
        self.focus
    }

    fn set_focus(&mut self, new_focus_id: Option<AccessibilityId>) {
        self.focus = new_focus_id;
    }

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