use crate::internal_prelude::*;
use crate::node_list::NodeList;
crate::use_behaviors!(sandbox_member);
use contents::{AnyNodeStorage, NodeContentsArc, NodeContentsWeak};
use graph_storage::NodeGraphStorage;
pub mod concrete;
pub mod contents;
pub mod element;
pub(crate) mod graph_storage;
pub trait Buildable {
type Storage: AnyNodeStorage;
}
pub struct InputEvent {}
pub(crate) struct NodeCommon {
pub(crate) node_graph: NodeGraphStorage,
pub context: Weak<Sandbox>,
}
#[derive(Clone)]
pub struct AnyNodeArc {
pub(crate) contents: NodeContentsArc,
pub(crate) common: Arc<NodeCommon>,
}
#[derive(Clone)]
pub struct AnyNodeWeak {
pub(crate) contents: NodeContentsWeak,
pub(crate) common: Weak<NodeCommon>,
}
pub trait NodeBehavior {
fn first_child(&self) -> Option<AnyNodeArc>;
fn last_child(&self) -> Option<AnyNodeArc>;
fn append_child(&self, other: AnyNodeArc);
fn child_nodes(&self) -> Arc<NodeList>;
fn clone_node(&self) -> AnyNodeArc;
fn get_node_type(&self) -> isize;
}
impl AnyNodeWeak {
fn upgrade(&self) -> Option<AnyNodeArc> {
Some(AnyNodeArc {
common: self.common.upgrade()?,
contents: self.contents.upgrade()?,
})
}
}
impl AnyNodeArc {
fn downgrade(&self) -> AnyNodeWeak {
AnyNodeWeak {
common: Arc::downgrade(&self.common),
contents: self.contents.downgrade(),
}
}
}
impl AnyNodeArc {
pub(crate) fn new(context: Weak<Sandbox>, contents: NodeContentsArc) -> AnyNodeArc {
let common = Arc::new_cyclic(|construction_weak| NodeCommon {
node_graph: NodeGraphStorage::new(AnyNodeWeak {
common: construction_weak.clone(),
contents: contents.downgrade(),
}),
context,
});
AnyNodeArc { contents, common }
}
}
impl SandboxMemberBehavior for AnyNodeArc {
fn get_context(&self) -> Weak<Sandbox> {
self.common.context.clone()
}
}
impl NodeBehavior for AnyNodeArc {
fn first_child(&self) -> Option<AnyNodeArc> {
self.common.node_graph.first_child()
}
fn last_child(&self) -> Option<AnyNodeArc> {
self.common.node_graph.last_child()
}
fn append_child(&self, other: AnyNodeArc) {
self.common.node_graph.append_child(other)
}
fn child_nodes(&self) -> Arc<NodeList> {
self.common.node_graph.child_nodes()
}
fn clone_node(&self) -> AnyNodeArc {
let contents = self.contents.clone();
AnyNodeArc::new(self.get_context(), contents)
}
fn get_node_type(&self) -> isize {
self.contents.to_node_type().get_node_number()
}
}