mod chain;
mod pointer;
use std::cell::RefCell;
use nativeview_abi::{EventSet, InteractionEvent, NodeId, ValidatedEvent};
use crate::LayoutRect;
pub use chain::{InputChain, InputHookId, InputPriority};
pub use pointer::{
annotate_pointer_norms, capture_owner, has_ancestor_listener, has_direct_listener, local_norm,
needs_pointer_norms, pointer_norm_owner,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Disposition {
#[default]
PassThrough,
Consumed,
ConsumedPreventDefault,
}
impl Disposition {
#[inline]
pub const fn is_consumed(self) -> bool {
!matches!(self, Self::PassThrough)
}
#[inline]
pub const fn prevents_default(self) -> bool {
matches!(self, Self::ConsumedPreventDefault)
}
}
pub type InputKinds = EventSet<InteractionEvent>;
pub const KEY_DOWN: InputKinds = InputKinds::from_raw(1 << InteractionEvent::KeyDown as u32);
pub trait InputInterceptor<Cx: ?Sized> {
fn intercept(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition;
#[inline]
fn is_active(&self) -> bool {
true
}
}
pub trait DefaultAction<Cx: ?Sized> {
fn run(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition;
}
pub trait PointerGeometry {
fn local_rect(&self, node: NodeId) -> Option<LayoutRect>;
}
pub type InterceptorChain<Cx> = InputChain<dyn InputInterceptor<Cx>>;
pub type DefaultActionChain<Cx> = InputChain<dyn DefaultAction<Cx>>;
impl<Cx: ?Sized> InputChain<dyn InputInterceptor<Cx>> {
#[inline]
pub fn has_active(&self) -> bool {
self.any_hook(|hook| hook.is_active())
}
#[inline]
pub fn intercept(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
self.run(ev, |hook| hook.intercept(ev, cx))
}
#[inline]
pub fn intercept_shared(cell: &RefCell<Self>, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
Self::run_shared(cell, ev, |hook| hook.intercept(ev, cx))
}
}
impl<Cx: ?Sized> InputChain<dyn DefaultAction<Cx>> {
#[inline]
pub fn run_default(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
self.run(ev, |hook| hook.run(ev, cx))
}
#[inline]
pub fn run_default_shared(cell: &RefCell<Self>, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
Self::run_shared(cell, ev, |hook| hook.run(ev, cx))
}
}
#[cfg(test)]
mod tests;