#[cfg(all(
feature = "accesskit_unix",
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
not(feature = "async-io"),
not(feature = "tokio")
))]
compile_error!("Either \"async-io\" (default) or \"tokio\" feature must be enabled.");
#[cfg(all(
feature = "accesskit_unix",
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
),
feature = "async-io",
feature = "tokio"
))]
compile_error!(
"Both \"async-io\" (default) and \"tokio\" features cannot be enabled at the same time."
);
#[cfg(all(not(feature = "rwh_05"), not(feature = "rwh_06")))]
compile_error!("Either \"rwh_06\" (default) or \"rwh_05\" feature must be enabled.");
#[cfg(all(feature = "rwh_05", feature = "rwh_06"))]
compile_error!(
"Both \"rwh_06\" (default) and \"rwh_05\" features cannot be enabled at the same time."
);
use accesskit::{ActionHandler, ActionRequest, ActivationHandler, DeactivationHandler, TreeUpdate};
use winit::{
event::WindowEvent as WinitWindowEvent,
event_loop::{ActiveEventLoop, EventLoopProxy},
window::{Window, WindowId},
};
#[cfg(feature = "rwh_05")]
#[allow(unused)]
use rwh_05 as raw_window_handle;
#[cfg(feature = "rwh_06")]
#[allow(unused)]
use rwh_06 as raw_window_handle;
mod platform_impl;
#[derive(Debug)]
pub struct Event {
pub window_id: WindowId,
pub window_event: WindowEvent,
}
#[derive(Clone, Debug, PartialEq)]
pub enum WindowEvent {
InitialTreeRequested,
ActionRequested(ActionRequest),
AccessibilityDeactivated,
}
struct WinitActivationHandler<T: From<Event> + Send + 'static> {
window_id: WindowId,
proxy: EventLoopProxy<T>,
}
impl<T: From<Event> + Send + 'static> ActivationHandler for WinitActivationHandler<T> {
fn request_initial_tree(&mut self) -> Option<TreeUpdate> {
let event = Event {
window_id: self.window_id,
window_event: WindowEvent::InitialTreeRequested,
};
self.proxy.send_event(event.into()).ok();
None
}
}
struct WinitActionHandler<T: From<Event> + Send + 'static> {
window_id: WindowId,
proxy: EventLoopProxy<T>,
}
impl<T: From<Event> + Send + 'static> ActionHandler for WinitActionHandler<T> {
fn do_action(&mut self, request: ActionRequest) {
let event = Event {
window_id: self.window_id,
window_event: WindowEvent::ActionRequested(request),
};
self.proxy.send_event(event.into()).ok();
}
}
struct WinitDeactivationHandler<T: From<Event> + Send + 'static> {
window_id: WindowId,
proxy: EventLoopProxy<T>,
}
impl<T: From<Event> + Send + 'static> DeactivationHandler for WinitDeactivationHandler<T> {
fn deactivate_accessibility(&mut self) {
let event = Event {
window_id: self.window_id,
window_event: WindowEvent::AccessibilityDeactivated,
};
self.proxy.send_event(event.into()).ok();
}
}
pub struct Adapter {
inner: platform_impl::Adapter,
}
impl Adapter {
pub fn with_event_loop_proxy<T: From<Event> + Send + 'static>(
event_loop: &ActiveEventLoop,
window: &Window,
proxy: EventLoopProxy<T>,
) -> Self {
let window_id = window.id();
let activation_handler = WinitActivationHandler {
window_id,
proxy: proxy.clone(),
};
let action_handler = WinitActionHandler {
window_id,
proxy: proxy.clone(),
};
let deactivation_handler = WinitDeactivationHandler { window_id, proxy };
Self::with_direct_handlers(
event_loop,
window,
activation_handler,
action_handler,
deactivation_handler,
)
}
pub fn with_direct_handlers(
event_loop: &ActiveEventLoop,
window: &Window,
activation_handler: impl 'static + ActivationHandler + Send,
action_handler: impl 'static + ActionHandler + Send,
deactivation_handler: impl 'static + DeactivationHandler + Send,
) -> Self {
if window.is_visible() == Some(true) {
panic!("The AccessKit winit adapter must be created before the window is shown (made visible) for the first time.");
}
let inner = platform_impl::Adapter::new(
event_loop,
window,
activation_handler,
action_handler,
deactivation_handler,
);
Self { inner }
}
pub fn with_mixed_handlers<T: From<Event> + Send + 'static>(
event_loop: &ActiveEventLoop,
window: &Window,
activation_handler: impl 'static + ActivationHandler + Send,
proxy: EventLoopProxy<T>,
) -> Self {
let window_id = window.id();
let action_handler = WinitActionHandler {
window_id,
proxy: proxy.clone(),
};
let deactivation_handler = WinitDeactivationHandler { window_id, proxy };
Self::with_direct_handlers(
event_loop,
window,
activation_handler,
action_handler,
deactivation_handler,
)
}
pub fn process_event(&mut self, window: &Window, event: &WinitWindowEvent) {
self.inner.process_event(window, event);
}
pub fn update_if_active(&mut self, updater: impl FnOnce() -> TreeUpdate) {
self.inner.update_if_active(updater);
}
}