nativeview-engine 0.1.0-rc.1

Framework-agnostic scene engine traits, hot-reload cdylib loader, and Frame-0 snapshot codecs.
Documentation
//! Generic drag-start payload contribution registry (`[INV:zero-overhead-component-dispatch]`).
//!
//! Capability modules (`Link`, `TextContent`, or custom draggable widgets) implement
//! [`DragPayloadContributor`] and register by [`TableSlot`] on [`DragPayloadRegistry`] (backed by
//! [`SlotRegistry`]). An empty registry occupies `<= 32 B` inline and exits in 1 branch with zero
//! heap allocations.

use std::sync::Arc;

use nativeview_abi::{DragPayload, NodeId};
use nativeview_store::{NodeStore, TableSet, TableSlot};

use crate::ext::slot_registry::SlotRegistry;

use super::build_internal_drag_payload;

/// Capability-module hook supplying a [`DragPayload`] on drag start for nodes carrying its table
/// (consumers: `Link`, `TextContent`, custom draggable components).
pub trait DragPayloadContributor: Send + Sync {
    /// Returns a [`DragPayload`] when this contributor handles `node`, or `None` to fall through
    /// to the next matching contributor / default `DragDrop` row.
    fn contribute(&self, store: &NodeStore, node: NodeId) -> Option<DragPayload>;
}

/// Zero-overhead `TableSlot`-indexed registry of [`DragPayloadContributor`] hooks (32 B inline
/// when empty; 1 branch and 0 heap allocations on the empty fast path).
#[derive(Default)]
pub struct DragPayloadRegistry {
    slots: SlotRegistry<Arc<dyn DragPayloadContributor>>,
}

impl DragPayloadRegistry {
    /// Creates an empty registry with zero heap allocations.
    #[inline]
    pub const fn new() -> Self {
        Self {
            slots: SlotRegistry::new(),
        }
    }

    /// Registers `contributor` at `slot`, returning `true` if a prior contributor at `slot` was
    /// replaced.
    pub fn register(
        &mut self,
        slot: TableSlot,
        contributor: Arc<dyn DragPayloadContributor>,
    ) -> bool {
        self.slots.register(slot, contributor, TableSet::EMPTY)
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.slots.is_empty()
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.slots.len()
    }

    /// Queries registered contributors matching `node`'s attached tables in registration order,
    /// returning the first `Some(DragPayload)`. Empty registry exits in 1 branch with 0 allocs.
    #[inline]
    pub fn contribute(&self, store: &NodeStore, node: NodeId) -> Option<DragPayload> {
        if self.slots.is_empty() {
            return None;
        }
        let mask = store.node_tables(node).raw();
        self.slots
            .find_map_in_mask(mask, |c| c.contribute(store, node))
    }

    /// Resolves a drag-start [`DragPayload`] for `node`: non-empty `explicit_data` takes
    /// precedence via [`build_internal_drag_payload`]; otherwise registered contributors are
    /// queried before falling back to [`build_internal_drag_payload(explicit_mime, explicit_data)`].
    #[inline]
    pub fn resolve_internal_payload(
        &self,
        store: &NodeStore,
        node: NodeId,
        explicit_mime: &str,
        explicit_data: &str,
    ) -> DragPayload {
        if explicit_data.is_empty()
            && let Some(contributed) = self.contribute(store, node)
        {
            return contributed;
        }
        build_internal_drag_payload(explicit_mime, explicit_data)
    }
}