use std::{cell::RefCell, rc::Rc};
use super::{
DynModifierElement, EntryElement, HeldElement, ModifierNode, NodeCapabilities, NodeMaker,
PLAIN_WORDS_MAX, PlainFns, PlainLayoutFns, PlainValue, detach_node_tree,
};
#[derive(Debug)]
pub(super) enum ModifierNodeEntry {
Node {
element: EntryElement,
node: Rc<RefCell<dyn ModifierNode>>,
capabilities: NodeCapabilities,
aggregate: NodeCapabilities,
},
Plain {
fns: &'static PlainFns,
words: [u32; PLAIN_WORDS_MAX],
aggregate: NodeCapabilities,
},
}
impl ModifierNodeEntry {
pub(super) fn of_node(
element: &DynModifierElement,
maker: &dyn NodeMaker,
capabilities: NodeCapabilities,
) -> Self {
let node = maker.create_node();
node.borrow().node_state().set_capabilities(capabilities);
Self::Node {
element: EntryElement::new(element, maker),
node,
capabilities,
aggregate: NodeCapabilities::empty(),
}
}
pub(super) fn of_plain(value: &dyn PlainValue) -> Self {
let fns = value.fns();
let mut words = [0; PLAIN_WORDS_MAX];
value.write_words(&mut words[..fns.words()]);
Self::Plain {
fns,
words,
aggregate: NodeCapabilities::empty(),
}
}
pub(super) fn capabilities(&self) -> NodeCapabilities {
match self {
Self::Node { capabilities, .. } => *capabilities,
Self::Plain { fns, .. } => fns.capabilities(),
}
}
pub(super) fn aggregate(&self) -> NodeCapabilities {
match self {
Self::Node { aggregate, .. } | Self::Plain { aggregate, .. } => *aggregate,
}
}
pub(super) fn set_aggregate(&mut self, union: NodeCapabilities) {
match self {
Self::Node { aggregate, .. } | Self::Plain { aggregate, .. } => *aggregate = union,
}
}
pub(super) fn held(&self) -> HeldElement<'_> {
match self {
Self::Node {
element,
node,
capabilities,
..
} => element.view(node, *capabilities),
Self::Plain { fns, words, .. } => HeldElement::of_words(fns, &words[..fns.words()]),
}
}
pub(super) fn plain_words(&self) -> Option<(&'static PlainFns, &[u32])> {
match self {
Self::Plain { fns, words, .. } => Some((fns, &words[..fns.words()])),
Self::Node { .. } => None,
}
}
pub(super) fn plain_layout(
&self,
) -> Option<(Option<&'static PlainLayoutFns>, &[u32; PLAIN_WORDS_MAX])> {
match self {
Self::Plain { fns, words, .. } => Some((fns.layout(), words)),
Self::Node { .. } => None,
}
}
pub(super) fn node(&self) -> Option<&Rc<RefCell<dyn ModifierNode>>> {
match self {
Self::Node { node, .. } => Some(node),
Self::Plain { .. } => None,
}
}
pub(super) fn key(&self) -> Option<u64> {
match self {
Self::Node { element, .. } => element.key(),
Self::Plain { .. } => None,
}
}
pub(super) fn detach(&self) {
if let Some(node) = self.node() {
detach_node_tree(&mut *node.borrow_mut());
}
}
}