Skip to main content

nativeview_engine/drag/
contributor.rs

1//! Generic drag-start payload contribution registry (`[INV:zero-overhead-component-dispatch]`).
2//!
3//! Capability modules (`Link`, `TextContent`, or custom draggable widgets) implement
4//! [`DragPayloadContributor`] and register by [`TableSlot`] on [`DragPayloadRegistry`] (backed by
5//! [`SlotRegistry`]). An empty registry occupies `<= 32 B` inline and exits in 1 branch with zero
6//! heap allocations.
7
8use std::sync::Arc;
9
10use nativeview_abi::{DragPayload, NodeId};
11use nativeview_store::{NodeStore, TableSet, TableSlot};
12
13use crate::ext::slot_registry::SlotRegistry;
14
15use super::build_internal_drag_payload;
16
17/// Capability-module hook supplying a [`DragPayload`] on drag start for nodes carrying its table
18/// (consumers: `Link`, `TextContent`, custom draggable components).
19pub trait DragPayloadContributor: Send + Sync {
20    /// Returns a [`DragPayload`] when this contributor handles `node`, or `None` to fall through
21    /// to the next matching contributor / default `DragDrop` row.
22    fn contribute(&self, store: &NodeStore, node: NodeId) -> Option<DragPayload>;
23}
24
25/// Zero-overhead `TableSlot`-indexed registry of [`DragPayloadContributor`] hooks (32 B inline
26/// when empty; 1 branch and 0 heap allocations on the empty fast path).
27#[derive(Default)]
28pub struct DragPayloadRegistry {
29    slots: SlotRegistry<Arc<dyn DragPayloadContributor>>,
30}
31
32impl DragPayloadRegistry {
33    /// Creates an empty registry with zero heap allocations.
34    #[inline]
35    pub const fn new() -> Self {
36        Self {
37            slots: SlotRegistry::new(),
38        }
39    }
40
41    /// Registers `contributor` at `slot`, returning `true` if a prior contributor at `slot` was
42    /// replaced.
43    pub fn register(
44        &mut self,
45        slot: TableSlot,
46        contributor: Arc<dyn DragPayloadContributor>,
47    ) -> bool {
48        self.slots.register(slot, contributor, TableSet::EMPTY)
49    }
50
51    #[inline]
52    pub fn is_empty(&self) -> bool {
53        self.slots.is_empty()
54    }
55
56    #[inline]
57    pub fn len(&self) -> usize {
58        self.slots.len()
59    }
60
61    /// Queries registered contributors matching `node`'s attached tables in registration order,
62    /// returning the first `Some(DragPayload)`. Empty registry exits in 1 branch with 0 allocs.
63    #[inline]
64    pub fn contribute(&self, store: &NodeStore, node: NodeId) -> Option<DragPayload> {
65        if self.slots.is_empty() {
66            return None;
67        }
68        let mask = store.node_tables(node).raw();
69        self.slots
70            .find_map_in_mask(mask, |c| c.contribute(store, node))
71    }
72
73    /// Resolves a drag-start [`DragPayload`] for `node`: non-empty `explicit_data` takes
74    /// precedence via [`build_internal_drag_payload`]; otherwise registered contributors are
75    /// queried before falling back to [`build_internal_drag_payload(explicit_mime, explicit_data)`].
76    #[inline]
77    pub fn resolve_internal_payload(
78        &self,
79        store: &NodeStore,
80        node: NodeId,
81        explicit_mime: &str,
82        explicit_data: &str,
83    ) -> DragPayload {
84        if explicit_data.is_empty()
85            && let Some(contributed) = self.contribute(store, node)
86        {
87            return contributed;
88        }
89        build_internal_drag_payload(explicit_mime, explicit_data)
90    }
91}