use crate::*;
impl PartialEq for TextNode {
fn eq(&self, other: &Self) -> bool {
self.get_content() == other.get_content()
}
}
impl PartialEq for VirtualNode {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(VirtualNode::Text(old_text), VirtualNode::Text(new_text)) => old_text == new_text,
(
VirtualNode::Element {
tag: old_tag,
attributes: old_attrs,
children: old_children,
..
},
VirtualNode::Element {
tag: new_tag,
attributes: new_attrs,
children: new_children,
..
},
) => {
old_tag == new_tag
&& old_attrs.len() == new_attrs.len()
&& old_attrs.iter().zip(new_attrs.iter()).all(
|(old_attr, new_attr): (&AttributeEntry, &AttributeEntry)| {
old_attr == new_attr
},
)
&& old_children.len() == new_children.len()
&& old_children.iter().zip(new_children.iter()).all(
|(old_child, new_child): (&VirtualNode, &VirtualNode)| {
old_child == new_child
},
)
}
(VirtualNode::Fragment(old_children), VirtualNode::Fragment(new_children)) => {
old_children.len() == new_children.len()
&& old_children.iter().zip(new_children.iter()).all(
|(old_child, new_child): (&VirtualNode, &VirtualNode)| {
old_child == new_child
},
)
}
(VirtualNode::Dynamic(_), VirtualNode::Dynamic(_)) => false,
(VirtualNode::Empty, VirtualNode::Empty) => true,
_ => false,
}
}
}
impl Default for DynamicNode {
fn default() -> Self {
let render_fn_inner: Rc<RefCell<RenderFnInner>> =
Rc::new(RefCell::new(RenderFnInner::new(Box::new(|| {
VirtualNode::Empty
}))));
Self::new(render_fn_inner, HookContext::default())
}
}
impl Clone for DynamicNode {
fn clone(&self) -> Self {
let cloned: Self = Self::new(
self.get_render_fn().clone(),
self.get_hook_context().clone(),
);
cloned
}
}
impl DynamicNode {
pub(crate) fn get_hook_context_value(&self) -> HookContext {
self.get_hook_context().clone()
}
pub fn render(&self) -> VirtualNode {
let mut inner: RefMut<RenderFnInner> = self.get_render_fn().borrow_mut();
(inner.get_mut_render_fn())()
}
}
impl VirtualNode {
pub fn create_dynamic<F>(mut render_fn: F) -> Self
where
F: FnMut() -> Self + 'static,
{
let hook_context: HookContext = create_hook_context();
let mut hook_context_for_closure: HookContext = hook_context.clone();
let inner: Rc<RefCell<RenderFnInner>> =
Rc::new(RefCell::new(RenderFnInner::new(Box::new(move || {
hook_context_for_closure.reset_hook_index();
render_fn()
}))));
let dynamic_node: DynamicNode = DynamicNode::new(inner, hook_context);
Self::Dynamic(dynamic_node)
}
pub fn create_dynamic_with_context<F>(mut render_fn: F) -> Self
where
F: FnMut(&mut HookContext) -> Self + 'static,
{
let hook_context: HookContext = create_hook_context();
let mut hook_context_for_closure: HookContext = hook_context.clone();
let inner: Rc<RefCell<RenderFnInner>> =
Rc::new(RefCell::new(RenderFnInner::new(Box::new(move || {
hook_context_for_closure.reset_hook_index();
render_fn(&mut hook_context_for_closure)
}))));
let dynamic_node: DynamicNode = DynamicNode::new(inner, hook_context);
Self::Dynamic(dynamic_node)
}
pub fn get_element_node(tag_name: &str) -> Self {
Self::Element {
tag: Tag::Element(tag_name.to_string()),
attributes: Vec::new(),
children: Vec::new(),
key: None,
}
}
pub fn get_text_node(content: &str) -> Self {
Self::Text(TextNode::new(content.to_string(), None))
}
pub fn with_attribute(mut self, name: &str, value: AttributeValue) -> Self {
if let Self::Element {
ref mut attributes, ..
} = self
{
attributes.push(AttributeEntry::new(name.to_string(), value));
}
self
}
pub fn with_child(mut self, child: VirtualNode) -> Self {
if let Self::Element {
ref mut children, ..
} = self
{
children.push(child);
}
self
}
pub fn is_component(&self) -> bool {
matches!(
self,
Self::Element {
tag: Tag::Component(_),
..
}
)
}
pub fn tag_name(&self) -> Option<String> {
match self {
Self::Element { tag, .. } => match tag {
Tag::Element(name) => Some(name.clone()),
Tag::Component(name) => Some(name.clone()),
},
_ => None,
}
}
pub fn try_get_prop(&self, name: &str) -> Option<String> {
if let Self::Element { attributes, .. } = self {
for attr in attributes {
if attr.get_name() == name {
match attr.get_value() {
AttributeValue::Text(value) => return Some(value.clone()),
AttributeValue::Signal(signal) => return Some(signal.get()),
AttributeValue::Dynamic(value) => return Some(value.clone()),
_ => {}
}
}
}
}
None
}
pub fn try_get_typed_prop<T>(&self, name: &str) -> Option<T>
where
T: FromStr,
{
if let Self::Element { attributes, .. } = self {
for attr in attributes {
if attr.get_name() == name {
let raw: String = match attr.get_value() {
AttributeValue::Text(value) => value.clone(),
AttributeValue::Signal(signal) => signal.get(),
AttributeValue::Dynamic(value) => value.clone(),
_ => continue,
};
return raw.parse::<T>().ok();
}
}
}
None
}
pub fn try_get_signal_prop(&self, name: &str) -> Option<Signal<String>> {
if let Self::Element { attributes, .. } = self {
for attr in attributes {
if attr.get_name() == name
&& let AttributeValue::Signal(signal) = attr.get_value()
{
return Some(*signal);
}
}
}
None
}
pub fn get_children(&self) -> &[Self] {
if let Self::Element { children, .. } = self {
children
} else {
&[]
}
}
pub fn try_get_text(&self) -> Option<String> {
match self {
Self::Text(text_node) => Some(text_node.get_content().clone()),
Self::Element { children, .. } => children.first().and_then(Self::try_get_text),
_ => None,
}
}
pub fn try_get_event(&self, name: &str) -> Option<NativeEventHandler> {
if let Self::Element { attributes, .. } = self {
for attr in attributes {
if attr.get_name() == name
&& let AttributeValue::Event(handler) = attr.get_value()
{
return Some(handler.clone());
}
}
}
None
}
}