accesskit_winit/platform_impl/
unix.rs

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