1#[cfg(all(
15 feature = "accesskit_unix",
16 any(
17 target_os = "linux",
18 target_os = "dragonfly",
19 target_os = "freebsd",
20 target_os = "netbsd",
21 target_os = "openbsd"
22 ),
23 not(feature = "async-io"),
24 not(feature = "tokio")
25))]
26compile_error!("Either \"async-io\" (default) or \"tokio\" feature must be enabled.");
27
28#[cfg(all(
29 feature = "accesskit_unix",
30 any(
31 target_os = "linux",
32 target_os = "dragonfly",
33 target_os = "freebsd",
34 target_os = "netbsd",
35 target_os = "openbsd"
36 ),
37 feature = "async-io",
38 feature = "tokio"
39))]
40compile_error!(
41 "Both \"async-io\" (default) and \"tokio\" features cannot be enabled at the same time."
42);
43
44#[cfg(all(not(feature = "rwh_05"), not(feature = "rwh_06")))]
45compile_error!("Either \"rwh_06\" (default) or \"rwh_05\" feature must be enabled.");
46
47#[cfg(all(feature = "rwh_05", feature = "rwh_06"))]
48compile_error!(
49 "Both \"rwh_06\" (default) and \"rwh_05\" features cannot be enabled at the same time."
50);
51
52use accesskit::{ActionHandler, ActionRequest, ActivationHandler, DeactivationHandler, TreeUpdate};
53use winit::{
54 event::WindowEvent as WinitWindowEvent,
55 event_loop::{ActiveEventLoop, EventLoopProxy},
56 window::{Window, WindowId},
57};
58
59#[cfg(feature = "rwh_05")]
60#[allow(unused)]
61use rwh_05 as raw_window_handle;
62#[cfg(feature = "rwh_06")]
63#[allow(unused)]
64use rwh_06 as raw_window_handle;
65
66mod platform_impl;
67
68#[derive(Debug)]
69pub struct Event {
70 pub window_id: WindowId,
71 pub window_event: WindowEvent,
72}
73
74#[derive(Debug)]
75pub enum WindowEvent {
76 InitialTreeRequested,
77 ActionRequested(ActionRequest),
78 AccessibilityDeactivated,
79}
80
81struct WinitActivationHandler<T: From<Event> + Send + 'static> {
82 window_id: WindowId,
83 proxy: EventLoopProxy<T>,
84}
85
86impl<T: From<Event> + Send + 'static> ActivationHandler for WinitActivationHandler<T> {
87 fn request_initial_tree(&mut self) -> Option<TreeUpdate> {
88 let event = Event {
89 window_id: self.window_id,
90 window_event: WindowEvent::InitialTreeRequested,
91 };
92 self.proxy.send_event(event.into()).ok();
93 None
94 }
95}
96
97struct WinitActionHandler<T: From<Event> + Send + 'static> {
98 window_id: WindowId,
99 proxy: EventLoopProxy<T>,
100}
101
102impl<T: From<Event> + Send + 'static> ActionHandler for WinitActionHandler<T> {
103 fn do_action(&mut self, request: ActionRequest) {
104 let event = Event {
105 window_id: self.window_id,
106 window_event: WindowEvent::ActionRequested(request),
107 };
108 self.proxy.send_event(event.into()).ok();
109 }
110}
111
112struct WinitDeactivationHandler<T: From<Event> + Send + 'static> {
113 window_id: WindowId,
114 proxy: EventLoopProxy<T>,
115}
116
117impl<T: From<Event> + Send + 'static> DeactivationHandler for WinitDeactivationHandler<T> {
118 fn deactivate_accessibility(&mut self) {
119 let event = Event {
120 window_id: self.window_id,
121 window_event: WindowEvent::AccessibilityDeactivated,
122 };
123 self.proxy.send_event(event.into()).ok();
124 }
125}
126
127pub struct Adapter {
128 inner: platform_impl::Adapter,
129}
130
131impl Adapter {
132 pub fn with_event_loop_proxy<T: From<Event> + Send + 'static>(
146 event_loop: &ActiveEventLoop,
147 window: &Window,
148 proxy: EventLoopProxy<T>,
149 ) -> Self {
150 let window_id = window.id();
151 let activation_handler = WinitActivationHandler {
152 window_id,
153 proxy: proxy.clone(),
154 };
155 let action_handler = WinitActionHandler {
156 window_id,
157 proxy: proxy.clone(),
158 };
159 let deactivation_handler = WinitDeactivationHandler { window_id, proxy };
160 Self::with_direct_handlers(
161 event_loop,
162 window,
163 activation_handler,
164 action_handler,
165 deactivation_handler,
166 )
167 }
168
169 pub fn with_direct_handlers(
183 event_loop: &ActiveEventLoop,
184 window: &Window,
185 activation_handler: impl 'static + ActivationHandler + Send,
186 action_handler: impl 'static + ActionHandler + Send,
187 deactivation_handler: impl 'static + DeactivationHandler + Send,
188 ) -> Self {
189 let inner = platform_impl::Adapter::new(
190 event_loop,
191 window,
192 activation_handler,
193 action_handler,
194 deactivation_handler,
195 );
196 Self { inner }
197 }
198
199 pub fn with_mixed_handlers<T: From<Event> + Send + 'static>(
212 event_loop: &ActiveEventLoop,
213 window: &Window,
214 activation_handler: impl 'static + ActivationHandler + Send,
215 proxy: EventLoopProxy<T>,
216 ) -> Self {
217 let window_id = window.id();
218 let action_handler = WinitActionHandler {
219 window_id,
220 proxy: proxy.clone(),
221 };
222 let deactivation_handler = WinitDeactivationHandler { window_id, proxy };
223 Self::with_direct_handlers(
224 event_loop,
225 window,
226 activation_handler,
227 action_handler,
228 deactivation_handler,
229 )
230 }
231
232 pub fn process_event(&mut self, window: &Window, event: &WinitWindowEvent) {
237 self.inner.process_event(window, event);
238 }
239
240 pub fn update_if_active(&mut self, updater: impl FnOnce() -> TreeUpdate) {
247 self.inner.update_if_active(updater);
248 }
249}