fltk_accesskit/
fltk_adapter.rs1#![allow(unused_imports)]
2#![allow(unused_variables)]
3#![allow(clippy::missing_transmute_annotations)]
4use accesskit::{
5 ActionHandler, ActionRequest, ActivationHandler, DeactivationHandler, Node, NodeId, Point,
6 Rect, Size, Tree, TreeUpdate,
7};
8use fltk::{enums::*, prelude::*, *};
9use std::cell::RefCell;
10use std::rc::Rc;
11
12use crate::platform_adapter;
13
14#[derive(Debug)]
15pub struct ActionRequestEvent {
16 pub window_id: window::Window,
17 pub request: ActionRequest,
18}
19
20pub(crate) struct FltkActivationHandler {
21 pub wids: Vec<(NodeId, Node)>,
22 pub win_id: NodeId,
23}
24
25impl ActivationHandler for FltkActivationHandler {
26 fn request_initial_tree(&mut self) -> Option<TreeUpdate> {
27 Some(TreeUpdate {
28 nodes: self.wids.clone(),
29 tree: Some(Tree::new(self.win_id)),
30 focus: if let Some(focused) = app::focus() {
31 let focused = focused.as_widget_ptr() as usize as u64;
32 NodeId(focused)
33 } else {
34 self.win_id
35 },
36 })
37 }
38}
39
40pub(crate) struct FltkActionHandler {
41 window_id: window::Window,
42}
43
44impl ActionHandler for FltkActionHandler {
45 fn do_action(&mut self, request: ActionRequest) {
46 unsafe {
47 app::handle_raw(
48 std::mem::transmute(request.action as i32 + 100),
49 self.window_id.as_widget_ptr() as _,
50 );
51 }
52 }
53}
54
55pub(crate) struct FltkDeactivationHandler {}
56
57impl DeactivationHandler for FltkDeactivationHandler {
58 fn deactivate_accessibility(&mut self) {}
59}
60
61#[derive(Clone)]
62pub struct Adapter {
63 adapter: Rc<RefCell<platform_adapter::Adapter>>,
64}
65
66impl Adapter {
67 pub fn new(window: &window::Window, source: impl 'static + ActivationHandler + Send) -> Self {
68 let action_handler = FltkActionHandler {
69 window_id: window.clone(),
70 };
71 Self::with_action_handler(window, source, action_handler)
72 }
73
74 pub fn with_action_handler(
75 window: &window::Window,
76 source: impl 'static + ActivationHandler + Send,
77 action_handler: impl 'static + ActionHandler + Send,
78 ) -> Self {
79 let deactivation_handler = FltkDeactivationHandler {};
80 let adapter =
81 platform_adapter::Adapter::new(window, source, action_handler, deactivation_handler);
82 window.clone().resize_callback({
83 let adapter = adapter.clone();
84 move |_, x, y, w, h| {
85 #[cfg(not(any(target_os = "windows", target_os = "macos")))]
86 adapter.borrow_mut().set_root_window_bounds(
87 Rect::from_origin_size(
88 Point {
89 x: x as _,
90 y: y as _,
91 },
92 Size {
93 width: w as _,
94 height: h as _,
95 },
96 ),
97 Rect::from_origin_size(
98 Point {
99 x: x as _,
100 y: y as _,
101 },
102 Size {
103 width: w as _,
104 height: h as _,
105 },
106 ),
107 );
108 }
109 });
110 Self { adapter }
111 }
112
113 #[cfg(all(
114 not(target_os = "linux"),
115 not(target_os = "dragonfly"),
116 not(target_os = "freebsd"),
117 not(target_os = "netbsd"),
118 not(target_os = "openbsd")
119 ))]
120 #[must_use]
121 pub fn on_event(&self, window: &window::Window, event: &Event) -> bool {
122 unsafe { app::handle_raw(*event, window.as_widget_ptr() as _) }
123 }
124 #[cfg(any(
125 target_os = "linux",
126 target_os = "dragonfly",
127 target_os = "freebsd",
128 target_os = "netbsd",
129 target_os = "openbsd"
130 ))]
131 #[must_use]
132 pub fn on_event(&self, window: &mut window::Window, event: &Event) -> bool {
133 unsafe { app::handle_raw(*event, window.as_widget_ptr() as _) }
134 }
135
136 pub fn update_if_active(&mut self, updater: impl FnOnce() -> TreeUpdate) {
141 self.adapter.borrow_mut().update_if_active(updater)
142 }
143}