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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use accesskit_winit::Adapter;
use freya_common::EventMessage;
use freya_core::{
    prelude::{
        AccessibilityFocusDirection,
        AccessibilityManager,
        SharedAccessibilityManager,
        ACCESSIBILITY_ROOT_ID,
    },
    types::{
        AccessibilityId,
        NativePlatformSender,
    },
};
use winit::{
    dpi::{
        LogicalPosition,
        LogicalSize,
    },
    event::WindowEvent,
    event_loop::EventLoopProxy,
    window::Window,
};

/// Manages the accessibility integration with Accesskit.
pub struct AccessKitManager {
    accessibility_manager: SharedAccessibilityManager,
    accessibility_adapter: Adapter,
    title: String,
}

impl AccessKitManager {
    pub fn new(window: &Window, proxy: EventLoopProxy<EventMessage>) -> Self {
        let title = window.title();
        let accessibility_manager = AccessibilityManager::new(ACCESSIBILITY_ROOT_ID).wrap();
        let accessibility_adapter = Adapter::with_event_loop_proxy(window, proxy);
        Self {
            accessibility_manager,
            accessibility_adapter,
            title,
        }
    }

    pub fn accessibility_manager(&self) -> &SharedAccessibilityManager {
        &self.accessibility_manager
    }

    /// Focus a new accessibility node
    pub fn focus_node(
        &mut self,
        id: AccessibilityId,
        platform_sender: &NativePlatformSender,
        window: &Window,
    ) {
        let tree = self
            .accessibility_manager
            .lock()
            .unwrap()
            .set_focus_with_update(id);
        if let Some(tree) = tree {
            // Notify the components
            platform_sender.send_modify(|state| {
                state.focused_id = tree.focus;
            });

            // Update the IME Cursor area
            self.update_ime_position(tree.focus, window);

            // Update the adapter
            self.accessibility_adapter.update_if_active(|| tree);
        }
    }

    fn update_ime_position(&self, accessibility_id: AccessibilityId, window: &Window) {
        let accessibility_manager = self.accessibility_manager.lock().unwrap();
        let node = accessibility_manager.nodes.iter().find_map(|(id, n)| {
            if *id == accessibility_id {
                Some(n)
            } else {
                None
            }
        });
        if let Some(node) = node {
            let node_bounds = node.bounds();
            if let Some(node_bounds) = node_bounds {
                return window.set_ime_cursor_area(
                    LogicalPosition::new(node_bounds.min_x(), node_bounds.min_y()),
                    LogicalSize::new(node_bounds.width(), node_bounds.height()),
                );
            }
        }

        window.set_ime_cursor_area(
            window.inner_position().unwrap_or_default(),
            LogicalSize::<u32>::default(),
        );
    }

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

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

    /// Process the accessibility nodes
    pub fn render_accessibility(&mut self, title: &str) {
        let tree = self
            .accessibility_manager
            .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,
        platform_sender: &NativePlatformSender,
        window: &Window,
    ) {
        let tree = self
            .accessibility_manager
            .lock()
            .unwrap()
            .set_focus_on_next_node(direction);

        // Notify the components
        platform_sender.send_modify(|state| {
            state.focused_id = tree.focus;
        });

        // Update the IME Cursor area
        self.update_ime_position(tree.focus, window);

        // Update the Adapter
        self.accessibility_adapter.update_if_active(|| tree);
    }

    /// Process the initial tree
    pub fn process_initial_tree(&mut self) {
        let tree = self
            .accessibility_manager
            .lock()
            .unwrap()
            .process(ACCESSIBILITY_ROOT_ID, &self.title);

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