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;
pub trait DragPayloadContributor: Send + Sync {
fn contribute(&self, store: &NodeStore, node: NodeId) -> Option<DragPayload>;
}
#[derive(Default)]
pub struct DragPayloadRegistry {
slots: SlotRegistry<Arc<dyn DragPayloadContributor>>,
}
impl DragPayloadRegistry {
#[inline]
pub const fn new() -> Self {
Self {
slots: SlotRegistry::new(),
}
}
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()
}
#[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))
}
#[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)
}
}