1 | // Copyright 2022 The AccessKit Authors. All rights reserved. |
2 | // Licensed under the Apache License, Version 2.0 (found in |
3 | // the LICENSE-APACHE file). |
4 | |
5 | use accesskit::{ActionHandler, ActivationHandler, DeactivationHandler, Rect, TreeUpdate}; |
6 | use accesskit_unix::Adapter as UnixAdapter; |
7 | use winit::{event::WindowEvent, window::Window}; |
8 | |
9 | pub struct Adapter { |
10 | adapter: UnixAdapter, |
11 | } |
12 | |
13 | impl Adapter { |
14 | pub fn new( |
15 | _: &Window, |
16 | activation_handler: impl 'static + ActivationHandler + Send, |
17 | action_handler: impl 'static + ActionHandler + Send, |
18 | deactivation_handler: impl 'static + DeactivationHandler + Send, |
19 | ) -> Self { |
20 | let adapter = UnixAdapter::new(activation_handler, action_handler, deactivation_handler); |
21 | Self { adapter } |
22 | } |
23 | |
24 | fn set_root_window_bounds(&mut self, outer: Rect, inner: Rect) { |
25 | self.adapter.set_root_window_bounds(outer, inner); |
26 | } |
27 | |
28 | pub fn update_if_active(&mut self, updater: impl FnOnce() -> TreeUpdate) { |
29 | self.adapter.update_if_active(updater); |
30 | } |
31 | |
32 | fn update_window_focus_state(&mut self, is_focused: bool) { |
33 | self.adapter.update_window_focus_state(is_focused); |
34 | } |
35 | |
36 | pub fn process_event(&mut self, window: &Window, event: &WindowEvent) { |
37 | match event { |
38 | WindowEvent::Moved(outer_position) => { |
39 | let outer_position: (_, _) = outer_position.cast::<f64>().into(); |
40 | let outer_size: (_, _) = window.outer_size().cast::<f64>().into(); |
41 | let inner_position: (_, _) = window |
42 | .inner_position() |
43 | .unwrap_or_default() |
44 | .cast::<f64>() |
45 | .into(); |
46 | let inner_size: (_, _) = window.inner_size().cast::<f64>().into(); |
47 | self.set_root_window_bounds( |
48 | Rect::from_origin_size(outer_position, outer_size), |
49 | Rect::from_origin_size(inner_position, inner_size), |
50 | ) |
51 | } |
52 | WindowEvent::Resized(inner_size) => { |
53 | let outer_position: (_, _) = window |
54 | .outer_position() |
55 | .unwrap_or_default() |
56 | .cast::<f64>() |
57 | .into(); |
58 | let outer_size: (_, _) = window.outer_size().cast::<f64>().into(); |
59 | let inner_position: (_, _) = window |
60 | .inner_position() |
61 | .unwrap_or_default() |
62 | .cast::<f64>() |
63 | .into(); |
64 | let inner_size: (_, _) = inner_size.cast::<f64>().into(); |
65 | self.set_root_window_bounds( |
66 | Rect::from_origin_size(outer_position, outer_size), |
67 | Rect::from_origin_size(inner_position, inner_size), |
68 | ) |
69 | } |
70 | WindowEvent::Focused(is_focused) => { |
71 | self.update_window_focus_state(*is_focused); |
72 | } |
73 | _ => (), |
74 | } |
75 | } |
76 | } |
77 | |