#[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."
);
use std::sync::Arc;
use accesskit::{
ActionHandler, ActionRequest, ActivationHandler, DeactivationHandler, Rect, TreeUpdate,
};
#[cfg(target_os = "android")]
use android_activity::AndroidApp;
#[cfg(not(target_os = "android"))]
use raw_window_handle::RawWindowHandle;
mod platform_impl;
#[derive(Clone, Debug, PartialEq)]
pub enum WindowEvent {
InitialTreeRequested,
ActionRequested(ActionRequest),
AccessibilityDeactivated,
}
pub trait EventHandler: Send + Sync + 'static {
fn handle_accesskit_event(&self, event: WindowEvent);
}
#[derive(Clone)]
struct CombinedHandler(Arc<dyn EventHandler>);
impl ActivationHandler for CombinedHandler {
fn request_initial_tree(&mut self) -> Option<TreeUpdate> {
self.0
.handle_accesskit_event(WindowEvent::InitialTreeRequested);
None
}
}
impl DeactivationHandler for CombinedHandler {
fn deactivate_accessibility(&mut self) {
self.0
.handle_accesskit_event(WindowEvent::AccessibilityDeactivated);
}
}
impl ActionHandler for CombinedHandler {
fn do_action(&mut self, request: ActionRequest) {
self.0
.handle_accesskit_event(WindowEvent::ActionRequested(request));
}
}
pub struct Adapter {
inner: platform_impl::Adapter,
}
impl Adapter {
pub fn with_split_handlers(
#[cfg(target_os = "android")] android_app: &AndroidApp,
#[cfg(not(target_os = "android"))] window_handle: RawWindowHandle,
activation_handler: impl 'static + ActivationHandler + Send,
action_handler: impl 'static + ActionHandler + Send,
deactivation_handler: impl 'static + DeactivationHandler + Send,
) -> Self {
let inner = platform_impl::Adapter::new(
#[cfg(target_os = "android")]
android_app,
#[cfg(not(target_os = "android"))]
window_handle,
activation_handler,
action_handler,
deactivation_handler,
);
Self { inner }
}
pub fn with_combined_handler(
#[cfg(target_os = "android")] android_app: &AndroidApp,
#[cfg(not(target_os = "android"))] window_handle: RawWindowHandle,
handler: Arc<dyn EventHandler>,
) -> Self {
let handler = CombinedHandler(handler);
let inner = platform_impl::Adapter::new(
#[cfg(target_os = "android")]
android_app,
#[cfg(not(target_os = "android"))]
window_handle,
handler.clone(),
handler.clone(),
handler,
);
Self { inner }
}
pub fn update_if_active(&mut self, updater: impl FnOnce() -> TreeUpdate) {
self.inner.update_if_active(updater);
}
pub fn set_focus(&mut self, is_focused: bool) {
self.inner.set_focus(is_focused);
}
pub fn set_window_bounds(&mut self, outer_bounds: Rect, inner_bounds: Rect) {
self.inner.set_window_bounds(outer_bounds, inner_bounds);
}
}