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
155
156
157
use accesskit::{ActionHandler, TreeUpdate};
use once_cell::unsync::Lazy;
use std::{cell::Cell, ffi::c_void, mem::transmute};
use windows::{
core::*,
Win32::{Foundation::*, UI::WindowsAndMessaging::*},
};
use crate::{Adapter, QueuedEvents, UiaInitMarker};
#[cfg(any(target_arch = "x86_64", target_arch = "aarch64"))]
type LongPtr = isize;
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
type LongPtr = i32;
const PROP_NAME: PCWSTR = w!("AccessKitAdapter");
type LazyAdapter = Lazy<Adapter, Box<dyn FnOnce() -> Adapter>>;
struct SubclassImpl {
hwnd: HWND,
adapter: LazyAdapter,
prev_wnd_proc: WNDPROC,
window_destroyed: Cell<bool>,
}
extern "system" fn wnd_proc(window: HWND, message: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
let handle = unsafe { GetPropW(window, PROP_NAME) };
let impl_ptr = handle.0 as *const SubclassImpl;
assert!(!impl_ptr.is_null());
let r#impl = unsafe { &*impl_ptr };
if message == WM_GETOBJECT {
let adapter = Lazy::force(&r#impl.adapter);
if let Some(result) = adapter.handle_wm_getobject(wparam, lparam) {
return result.into();
}
}
if message == WM_NCDESTROY {
r#impl.window_destroyed.set(true);
}
unsafe { CallWindowProcW(r#impl.prev_wnd_proc, window, message, wparam, lparam) }
}
impl SubclassImpl {
fn new(hwnd: HWND, adapter: LazyAdapter) -> Box<Self> {
Box::new(Self {
hwnd,
adapter,
prev_wnd_proc: None,
window_destroyed: Cell::new(false),
})
}
fn install(&mut self) {
unsafe {
SetPropW(
self.hwnd,
PROP_NAME,
HANDLE(self as *const SubclassImpl as _),
)
}
.unwrap();
let result =
unsafe { SetWindowLongPtrW(self.hwnd, GWLP_WNDPROC, wnd_proc as *const c_void as _) };
if result == 0 {
let result: Result<()> = Err(Error::from_win32());
result.unwrap();
}
self.prev_wnd_proc = unsafe { transmute::<LongPtr, WNDPROC>(result) };
}
fn uninstall(&self) {
if self.window_destroyed.get() {
return;
}
let result = unsafe {
SetWindowLongPtrW(
self.hwnd,
GWLP_WNDPROC,
transmute::<WNDPROC, LongPtr>(self.prev_wnd_proc),
)
};
if result == 0 {
let result: Result<()> = Err(Error::from_win32());
result.unwrap();
}
unsafe { RemovePropW(self.hwnd, PROP_NAME) }.unwrap();
}
}
pub struct SubclassingAdapter(Box<SubclassImpl>);
impl SubclassingAdapter {
pub fn new(
hwnd: HWND,
source: impl 'static + FnOnce() -> TreeUpdate,
action_handler: Box<dyn ActionHandler + Send + Sync>,
) -> Self {
let uia_init_marker = UiaInitMarker::new();
let adapter: LazyAdapter = Lazy::new(Box::new(move || {
Adapter::new(hwnd, source(), action_handler, uia_init_marker)
}));
let mut r#impl = SubclassImpl::new(hwnd, adapter);
r#impl.install();
Self(r#impl)
}
pub fn update(&self, update: TreeUpdate) -> QueuedEvents {
let adapter = Lazy::force(&self.0.adapter);
adapter.update(update)
}
pub fn update_if_active(
&self,
update_factory: impl FnOnce() -> TreeUpdate,
) -> Option<QueuedEvents> {
Lazy::get(&self.0.adapter).map(|adapter| adapter.update(update_factory()))
}
}
impl Drop for SubclassingAdapter {
fn drop(&mut self) {
self.0.uninstall();
}
}