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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use accesskit::NodeId as AccessibilityId;
use accesskit_winit::Adapter;
use freya_common::EventMessage;
use freya_core::{
    prelude::{
        AccessibilityFocusDirection, AccessibilityProvider, AccessibilityState,
        SharedAccessibilityState, ACCESSIBILITY_ROOT_ID,
    },
    types::FocusSender,
};
use winit::{event::WindowEvent, event_loop::EventLoopProxy, window::Window};

/// Manages the accessibility integration with Accesskit.
pub struct NativeAccessibility {
    accessibility_state: SharedAccessibilityState,
    accessibility_adapter: Adapter,
}

impl NativeAccessibility {
    pub fn new(window: &Window, proxy: EventLoopProxy<EventMessage>) -> Self {
        let title = window.title();
        let accessibility_state = AccessibilityState::new(ACCESSIBILITY_ROOT_ID).wrap();
        let accessibility_adapter = {
            let accessibility_state = accessibility_state.clone();
            Adapter::new(
                window,
                move || {
                    let mut accessibility_state = accessibility_state.lock().unwrap();
                    accessibility_state.process(ACCESSIBILITY_ROOT_ID, title.as_str())
                },
                proxy,
            )
        };
        Self {
            accessibility_state,
            accessibility_adapter,
        }
    }

    pub fn accessibility_state(&self) -> &SharedAccessibilityState {
        &self.accessibility_state
    }

    /// Focus a new accessibility node
    pub fn set_accessibility_focus(&mut self, id: AccessibilityId) {
        let tree = self
            .accessibility_state
            .lock()
            .unwrap()
            .set_focus_with_update(id);
        if let Some(tree) = tree {
            self.accessibility_adapter.update_if_active(|| tree);
        }
    }

    /// Process an Accessibility event
    pub fn process_accessibility_event(&mut self, window: &Window, event: &WindowEvent) {
        self.accessibility_adapter.process_event(window, event)
    }

    /// Remove the accessibility nodes
    pub fn clear_accessibility(&mut self) {
        self.accessibility_state.lock().unwrap().clear();
    }

    /// Process the accessibility nodes
    pub fn render_accessibility(&mut self, title: &str) {
        let tree = self
            .accessibility_state
            .lock()
            .unwrap()
            .process(ACCESSIBILITY_ROOT_ID, title);
        self.accessibility_adapter.update_if_active(|| tree);
    }

    /// Focus the next accessibility node
    pub fn focus_next_node(
        &mut self,
        direction: AccessibilityFocusDirection,
        focus_sender: &FocusSender,
    ) {
        let tree = self
            .accessibility_state
            .lock()
            .unwrap()
            .set_focus_on_next_node(direction);

        focus_sender
            .send(tree.focus)
            .expect("Failed to focus the Node.");

        self.accessibility_adapter.update_if_active(|| tree);
    }
}