dioxus/packages/core/src/nodes.rs

248 lines
7 KiB
Rust
Raw Normal View History

2022-11-23 05:32:26 +00:00
use crate::{any_props::AnyProps, arena::ElementId, Element, ScopeId, ScopeState, UiEvent};
2022-11-29 21:31:04 +00:00
use bumpalo::boxed::Box as BumpBox;
2022-11-03 00:29:18 +00:00
use std::{
2022-11-04 00:34:42 +00:00
any::{Any, TypeId},
2022-11-03 00:29:18 +00:00
cell::{Cell, RefCell},
};
pub type TemplateId = &'static str;
/// A reference to a template along with any context needed to hydrate it
2022-11-20 01:07:29 +00:00
///
/// The dynamic parts of the template are stored separately from the static parts. This allows faster diffing by skipping
/// static parts of the template.
2022-11-23 05:32:26 +00:00
#[derive(Debug, Clone)]
2022-11-02 01:42:29 +00:00
pub struct VNode<'a> {
2022-11-20 01:07:29 +00:00
/// The key given to the root of this template.
///
/// In fragments, this is the key of the first child. In other cases, it is the key of the root.
2022-11-03 08:24:20 +00:00
pub key: Option<&'a str>,
2022-11-20 01:07:29 +00:00
/// When rendered, this template will be linked to its parent manually
2022-11-18 04:00:39 +00:00
pub parent: Option<ElementId>,
2022-11-02 01:42:29 +00:00
2022-11-20 01:07:29 +00:00
/// The static nodes and static descriptor of the template
2022-11-02 08:00:37 +00:00
pub template: Template<'static>,
2022-11-20 01:07:29 +00:00
/// The IDs for the roots of this template - to be used when moving the template around and removing it from
/// the actual Dom
pub root_ids: &'a [Cell<ElementId>],
2022-11-20 01:07:29 +00:00
/// The dynamic parts of the template
2022-11-03 00:29:18 +00:00
pub dynamic_nodes: &'a [DynamicNode<'a>],
2022-11-20 01:07:29 +00:00
/// The dynamic parts of the template
2022-11-03 00:29:18 +00:00
pub dynamic_attrs: &'a [Attribute<'a>],
}
2022-11-06 22:28:41 +00:00
impl<'a> VNode<'a> {
2022-11-23 05:32:26 +00:00
pub fn empty() -> Element<'a> {
Ok(VNode {
key: None,
parent: None,
root_ids: &[],
dynamic_nodes: &[],
dynamic_attrs: &[],
template: Template {
id: "dioxus-empty",
roots: &[],
node_paths: &[],
attr_paths: &[],
},
})
}
2022-11-25 07:12:29 +00:00
pub fn dynamic_root(&self, idx: usize) -> Option<&'a DynamicNode<'a>> {
match &self.template.roots[idx] {
TemplateNode::Element { .. } | TemplateNode::Text(_) => None,
TemplateNode::Dynamic(id) | TemplateNode::DynamicText(id) => {
Some(&self.dynamic_nodes[*id])
}
}
}
2022-11-06 22:28:41 +00:00
}
#[derive(Debug, Clone, Copy)]
2022-11-02 08:00:37 +00:00
pub struct Template<'a> {
pub id: &'a str,
pub roots: &'a [TemplateNode<'a>],
2022-11-03 08:24:20 +00:00
pub node_paths: &'a [&'a [u8]],
pub attr_paths: &'a [&'a [u8]],
2022-11-02 08:00:37 +00:00
}
/// A weird-ish variant of VNodes with way more limited types
#[derive(Debug, Clone, Copy)]
pub enum TemplateNode<'a> {
Element {
tag: &'a str,
namespace: Option<&'a str>,
attrs: &'a [TemplateAttribute<'a>],
children: &'a [TemplateNode<'a>],
2022-11-16 00:05:22 +00:00
inner_opt: bool,
},
Text(&'a str),
Dynamic(usize),
DynamicText(usize),
}
2022-11-16 07:22:41 +00:00
#[derive(Debug)]
2022-11-03 08:24:20 +00:00
pub enum DynamicNode<'a> {
2022-11-20 01:07:29 +00:00
Component(VComponent<'a>),
Text(VText<'a>),
Fragment(VFragment<'a>),
}
2022-11-20 01:07:29 +00:00
impl<'a> DynamicNode<'a> {
pub fn is_component(&self) -> bool {
matches!(self, DynamicNode::Component(_))
}
}
pub struct VComponent<'a> {
pub name: &'static str,
pub static_props: bool,
pub scope: Cell<Option<ScopeId>>,
2022-11-20 23:58:05 +00:00
pub props: Cell<Option<Box<dyn AnyProps<'a> + 'a>>>,
pub render_fn: *const (),
}
impl<'a> std::fmt::Debug for VComponent<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VComponent")
.field("name", &self.name)
.field("static_props", &self.static_props)
.field("scope", &self.scope)
.finish()
}
2022-11-20 01:07:29 +00:00
}
#[derive(Debug)]
pub struct VText<'a> {
pub id: Cell<ElementId>,
pub value: &'a str,
}
#[derive(Debug)]
2022-11-23 02:38:27 +00:00
pub enum VFragment<'a> {
Empty(Cell<ElementId>),
NonEmpty(&'a [VNode<'a>]),
2022-11-20 01:07:29 +00:00
}
#[derive(Debug)]
2022-11-02 08:00:37 +00:00
pub enum TemplateAttribute<'a> {
Static {
name: &'static str,
value: &'a str,
namespace: Option<&'static str>,
volatile: bool,
},
2022-11-03 08:24:20 +00:00
Dynamic(usize),
}
2022-11-16 07:22:41 +00:00
#[derive(Debug)]
pub struct Attribute<'a> {
2022-11-03 00:29:18 +00:00
pub name: &'a str,
pub value: AttributeValue<'a>,
pub namespace: Option<&'static str>,
2022-11-03 00:29:18 +00:00
pub mounted_element: Cell<ElementId>,
2022-11-03 08:24:20 +00:00
pub volatile: bool,
}
pub enum AttributeValue<'a> {
Text(&'a str),
Float(f32),
Int(i32),
Bool(bool),
2022-11-29 21:31:04 +00:00
Listener(RefCell<Option<BumpBox<'a, dyn FnMut(UiEvent<dyn Any>) + 'a>>>),
Any(BumpBox<'a, dyn AnyValue>),
2022-11-03 00:29:18 +00:00
None,
}
impl<'a> AttributeValue<'a> {
pub fn new_listener<T: 'static>(
cx: &'a ScopeState,
2022-11-29 21:31:04 +00:00
mut callback: impl FnMut(UiEvent<T>) + 'a,
) -> AttributeValue<'a> {
2022-11-29 21:31:04 +00:00
let boxed: BumpBox<'a, dyn FnMut(_) + 'a> = unsafe {
BumpBox::from_raw(cx.bump().alloc(move |event: UiEvent<dyn Any>| {
2022-11-20 01:07:29 +00:00
if let Ok(data) = event.data.downcast::<T>() {
2022-11-29 21:31:04 +00:00
callback(UiEvent {
2022-11-20 01:07:29 +00:00
bubbles: event.bubbles,
data,
})
}
2022-11-29 21:31:04 +00:00
}))
};
AttributeValue::Listener(RefCell::new(Some(boxed)))
}
}
2022-11-04 00:34:42 +00:00
impl<'a> std::fmt::Debug for AttributeValue<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Text(arg0) => f.debug_tuple("Text").field(arg0).finish(),
Self::Float(arg0) => f.debug_tuple("Float").field(arg0).finish(),
Self::Int(arg0) => f.debug_tuple("Int").field(arg0).finish(),
Self::Bool(arg0) => f.debug_tuple("Bool").field(arg0).finish(),
2022-11-08 06:55:22 +00:00
Self::Listener(_) => f.debug_tuple("Listener").finish(),
Self::Any(_) => f.debug_tuple("Any").finish(),
2022-11-04 00:34:42 +00:00
Self::None => write!(f, "None"),
}
}
}
impl<'a> PartialEq for AttributeValue<'a> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Text(l0), Self::Text(r0)) => l0 == r0,
(Self::Float(l0), Self::Float(r0)) => l0 == r0,
(Self::Int(l0), Self::Int(r0)) => l0 == r0,
(Self::Bool(l0), Self::Bool(r0)) => l0 == r0,
2022-11-08 06:55:22 +00:00
(Self::Listener(_), Self::Listener(_)) => true,
2022-11-29 21:31:04 +00:00
(Self::Any(l0), Self::Any(r0)) => l0.any_cmp(r0.as_ref()),
2022-11-04 00:34:42 +00:00
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
}
}
}
2022-11-03 00:29:18 +00:00
impl<'a> AttributeValue<'a> {
2022-11-04 00:34:42 +00:00
pub fn matches_type(&self, other: &'a AttributeValue<'a>) -> bool {
match (self, other) {
(Self::Text(_), Self::Text(_)) => true,
(Self::Float(_), Self::Float(_)) => true,
(Self::Int(_), Self::Int(_)) => true,
(Self::Bool(_), Self::Bool(_)) => true,
(Self::Listener(_), Self::Listener(_)) => true,
(Self::Any(_), Self::Any(_)) => true,
_ => return false,
}
}
}
pub trait AnyValue {
2022-11-04 00:34:42 +00:00
fn any_cmp(&self, other: &dyn AnyValue) -> bool;
fn our_typeid(&self) -> TypeId;
}
2022-11-08 06:55:22 +00:00
impl<T: PartialEq + Any> AnyValue for T {
2022-11-04 00:34:42 +00:00
fn any_cmp(&self, other: &dyn AnyValue) -> bool {
if self.type_id() != other.our_typeid() {
return false;
}
self == unsafe { &*(other as *const _ as *const T) }
}
2022-11-04 00:34:42 +00:00
fn our_typeid(&self) -> TypeId {
self.type_id()
}
}
#[test]
fn what_are_the_sizes() {
2022-11-02 01:42:29 +00:00
dbg!(std::mem::size_of::<VNode>());
dbg!(std::mem::size_of::<Template>());
dbg!(std::mem::size_of::<TemplateNode>());
}