nativeview-engine 0.1.0-rc.1

Framework-agnostic scene engine traits, hot-reload cdylib loader, and Frame-0 snapshot codecs.
Documentation
//! Shared input extension points: what every host runs around an app dispatch, so backends and
//! hosts keep only platform glue.
//!
//! - [`InputInterceptor`] (X1): pre-dispatch hooks; the first that consumes an event stops it
//!   before the app sees it (web key registry and `nativeview-tv-web`, Skia shell key routing).
//! - [`DefaultAction`] (X2): runs on an event the app did not consume, with the same shape. The
//!   close request is the statically ordered built-in #0 (`nativeview_layout::CloseRequestAction`); a
//!   registered chain follows it (`nativeview-tv-web` Back → overlay dismissal, HTML CloseWatcher order).
//! - [`PointerGeometry`] (X3): the box a pointer norm is relative to (laid-out scene, Skia visual
//!   rects, Web DOM rects); [`annotate_pointer_norms`] fills `TARGET_LOCAL_NORM` with it.
//!
//! Hooks live in an [`InputChain`]: ordered stably by `(`[`InputPriority`] DESC, registration
//! order ASC)`, each entry carries an [`InputKinds`] mask (cached as a chain-wide union), an
//! empty chain or non-matching kind costs one branch, and dispatch never allocates.

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,
};

/// What a hook did with an event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Disposition {
    /// Not handled: later hooks (and, for interceptors, the app) see the event.
    #[default]
    PassThrough,
    /// Handled: stop here, keep the platform's own default action (e.g. a browser's native
    /// activation `click`).
    Consumed,
    /// Handled: stop here and suppress the platform default (`preventDefault()`).
    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)
    }
}

/// Per-entry kind mask: the interaction kinds a hook is called for.
pub type InputKinds = EventSet<InteractionEvent>;

/// [`InputKinds`] of key hooks.
pub const KEY_DOWN: InputKinds = InputKinds::from_raw(1 << InteractionEvent::KeyDown as u32);

/// Pre-dispatch hook (X1). `cx` is the host's context: the facts only the host knows (Web: DOM
/// target facts; Skia shell: the session it acts on).
pub trait InputInterceptor<Cx: ?Sized> {
    fn intercept(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition;

    /// Whether this interceptor currently has active rules/work requiring the host to keep input
    /// listeners attached. Defaults to `true`.
    #[inline]
    fn is_active(&self) -> bool {
        true
    }
}

/// Post-dispatch hook (X2) for an event the app did not consume; same shape as
/// [`InputInterceptor`].
pub trait DefaultAction<Cx: ?Sized> {
    fn run(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition;
}

/// Geometry pointer norms are relative to (X3).
pub trait PointerGeometry {
    /// `node`'s visual box in the events' coordinate space (scroll offsets and transforms
    /// applied); `None` when it has none.
    fn local_rect(&self, node: NodeId) -> Option<LayoutRect>;
}

/// Registered [`InputInterceptor`]s.
pub type InterceptorChain<Cx> = InputChain<dyn InputInterceptor<Cx>>;
/// Registered [`DefaultAction`]s.
pub type DefaultActionChain<Cx> = InputChain<dyn DefaultAction<Cx>>;

impl<Cx: ?Sized> InputChain<dyn InputInterceptor<Cx>> {
    /// Returns `true` when at least one registered interceptor reports [`InputInterceptor::is_active`].
    #[inline]
    pub fn has_active(&self) -> bool {
        self.any_hook(|hook| hook.is_active())
    }

    /// Runs the matching interceptors in order until one consumes `ev`.
    #[inline]
    pub fn intercept(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
        self.run(ev, |hook| hook.intercept(ev, cx))
    }

    /// [`Self::intercept`] through a shared cell: interceptors may register, remove or dispatch
    /// again while they run (see [`InputChain::run_shared`]).
    #[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>> {
    /// Runs the matching default actions in order until one consumes `ev`.
    #[inline]
    pub fn run_default(&mut self, ev: &ValidatedEvent<'_>, cx: &mut Cx) -> Disposition {
        self.run(ev, |hook| hook.run(ev, cx))
    }

    /// [`Self::run_default`] through a shared cell (see [`InputChain::run_shared`]).
    #[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;