use accesskit::{ActionHandler, ActionRequest, TreeUpdate};
use winit::{
event::WindowEvent,
event_loop::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;
#[cfg(all(feature = "rwh_05", feature = "rwh_06"))]
compile_error!("Cannot enable both 'rwh_05' and 'rwh_06' features at the same time");
#[derive(Debug)]
pub struct ActionRequestEvent {
pub window_id: WindowId,
pub request: ActionRequest,
}
struct WinitActionHandler<T: From<ActionRequestEvent> + Send + 'static> {
window_id: WindowId,
proxy: EventLoopProxy<T>,
}
impl<T: From<ActionRequestEvent> + Send + 'static> WinitActionHandler<T> {
fn new(window_id: WindowId, proxy: EventLoopProxy<T>) -> Self {
Self { window_id, proxy }
}
}
impl<T: From<ActionRequestEvent> + Send + 'static> ActionHandler for WinitActionHandler<T> {
fn do_action(&mut self, request: ActionRequest) {
let event = ActionRequestEvent {
window_id: self.window_id,
request,
};
self.proxy.send_event(event.into()).ok();
}
}
pub struct Adapter {
adapter: platform_impl::Adapter,
}
impl Adapter {
pub fn new<T: From<ActionRequestEvent> + Send + 'static>(
window: &Window,
source: impl 'static + FnOnce() -> TreeUpdate + Send,
event_loop_proxy: EventLoopProxy<T>,
) -> Self {
let action_handler = WinitActionHandler::new(window.id(), event_loop_proxy);
Self::with_action_handler(window, source, Box::new(action_handler))
}
pub fn with_action_handler(
window: &Window,
source: impl 'static + FnOnce() -> TreeUpdate + Send,
action_handler: platform_impl::ActionHandlerBox,
) -> Self {
let adapter = platform_impl::Adapter::new(window, source, action_handler);
Self { adapter }
}
pub fn process_event(&self, window: &Window, event: &WindowEvent) {
self.adapter.process_event(window, event);
}
pub fn update(&self, update: TreeUpdate) {
self.adapter.update(update);
}
pub fn update_if_active(&self, updater: impl FnOnce() -> TreeUpdate) {
self.adapter.update_if_active(updater);
}
}