use crate::Context;
use accesskit_winit::WindowEvent;
use winit::{
error::OsError,
event_loop::{ActiveEventLoop, EventLoopProxy},
window::{Window, WindowAttributes},
};
pub(crate) use accesskit_winit::Event;
pub(crate) struct Adapter(accesskit_winit::Adapter);
pub(crate) fn create_window<T: From<Event> + Send + 'static>(
event_loop: &ActiveEventLoop,
attributes: WindowAttributes,
proxy: Option<EventLoopProxy<T>>,
) -> Result<(Window, Option<Adapter>), OsError> {
let Some(proxy) = proxy else {
return Ok((event_loop.create_window(attributes)?, None));
};
let visible = attributes.visible;
let window = event_loop.create_window(attributes.with_visible(false))?;
let adapter = Adapter(accesskit_winit::Adapter::with_event_loop_proxy(
event_loop, &window, proxy,
));
if visible {
window.set_visible(true);
}
Ok((window, Some(adapter)))
}
impl Adapter {
pub(crate) fn process_event(&mut self, window: &Window, event: &winit::event::WindowEvent) {
self.0.process_event(window, event);
}
pub(crate) fn publish(&mut self, context: &mut Context) {
if context.accessibility_update_pending() {
self.0.update_if_active(|| {
context
.take_accessibility_update()
.expect("an update is pending")
});
}
}
}
pub(crate) fn handle(context: &mut Context, event: WindowEvent) -> bool {
match event {
WindowEvent::InitialTreeRequested => {
context.set_accessibility_active(false);
context.set_accessibility_active(true);
true
}
WindowEvent::ActionRequested(request) => context.on_accessibility_action(&request).repaint,
WindowEvent::AccessibilityDeactivated => {
context.set_accessibility_active(false);
false
}
}
}