New reconciler finally renders, just need to do some cleanup then can finally move on

This commit is contained in:
Ryan McGrath 2019-05-28 16:48:04 -07:00
parent da96abff6a
commit 2a73b399d9
No known key found for this signature in database
GPG key ID: 811674B62B666830
6 changed files with 56 additions and 38 deletions

View file

@ -58,11 +58,12 @@ impl Component for View {
} }
fn render(&self, props: &Props) -> Result<RSX, Error> { fn render(&self, props: &Props) -> Result<RSX, Error> {
println!("WTF: {}", props.children.len());
Ok(RSX::node("Fragment", |key| Box::new(Fragment::constructor(key)), Props { Ok(RSX::node("Fragment", |key| Box::new(Fragment::constructor(key)), Props {
attributes: std::collections::HashMap::new(), attributes: std::collections::HashMap::new(),
key: "".into(), key: "".into(),
styles: StylesList::new(), styles: StylesList::new(),
children: vec![] children: props.children.clone()
}, props.children.clone())) }))
} }
} }

View file

@ -7,7 +7,7 @@ use std::sync::Mutex;
use std::error::Error; use std::error::Error;
use alchemy_styles::THEME_ENGINE; use alchemy_styles::THEME_ENGINE;
use alchemy_styles::styles::{Appearance, Number, Size, Style}; use alchemy_styles::styles::{Appearance, Dimension, Number, Size, Style};
use crate::traits::Component; use crate::traits::Component;
use crate::rsx::{Props, RSX, VirtualNode}; use crate::rsx::{Props, RSX, VirtualNode};
@ -92,15 +92,12 @@ impl RenderEngine {
let mut component_store = self.components.lock().unwrap(); let mut component_store = self.components.lock().unwrap();
let mut layout_store = self.layouts.lock().unwrap(); let mut layout_store = self.layouts.lock().unwrap();
println!("Child: {:?}", child); let new_root_node = RSX::node("root", |_| {
let new_root_node = RSX::node("root", |_| Box::new(GenericRootView {}), { Box::new(GenericRootView {})
let mut props = Props::default(); }, Props::root(match child {
props.styles = "root".into();
props
}, match child {
RSX::VirtualNode(node) => { RSX::VirtualNode(node) => {
if node.tag == "Fragment" { if node.tag == "Fragment" {
node.children node.props.children
} else { } else {
println!("Def here..."); println!("Def here...");
vec![RSX::VirtualNode(node)] vec![RSX::VirtualNode(node)]
@ -108,13 +105,20 @@ impl RenderEngine {
}, },
_ => vec![] _ => vec![]
}); }));
recursively_diff_tree(key, new_root_node, &mut component_store, &mut layout_store)?; recursively_diff_tree(key, new_root_node, &mut component_store, &mut layout_store)?;
let layout_node = { let layout_node = {
let root_instance = component_store.get(key)?; let mut root_instance = component_store.get_mut(key)?;
root_instance.layout.unwrap() let layout = root_instance.layout.unwrap();
let mut style = Style::default();
style.size = Size {
width: Dimension::Points(600.),
height: Dimension::Points(600.)
};
layout_store.set_style(layout, style);
layout
}; };
layout_store.compute_layout(layout_node, Size { layout_store.compute_layout(layout_node, Size {
@ -172,7 +176,7 @@ fn recursively_diff_tree(
old_children.reverse(); old_children.reverse();
if let RSX::VirtualNode(mut child) = new_tree { if let RSX::VirtualNode(mut child) = new_tree {
for new_child_tree in child.children { for new_child_tree in child.props.children {
match old_children.pop() { match old_children.pop() {
// If there's a key in the old children for this position, it's // If there's a key in the old children for this position, it's
// something we need to update, so let's recurse right back into it. // something we need to update, so let's recurse right back into it.
@ -196,7 +200,8 @@ fn recursively_diff_tree(
layout_store layout_store
)?; )?;
link_nodes(key, new_child_key, component_store, layout_store)?; component_store.add_child(key, new_child_key)?;
link_layout_nodess(key, new_child_key, component_store, layout_store)?;
} }
} }
} }
@ -225,7 +230,6 @@ fn mount_component_tree(
component_store: &mut ComponentStore, component_store: &mut ComponentStore,
layout_store: &mut LayoutStore layout_store: &mut LayoutStore
) -> Result<ComponentKey, Box<Error>> { ) -> Result<ComponentKey, Box<Error>> {
println!(" Mounting Component");
let key = component_store.new_key(); let key = component_store.new_key();
let component = (tree.create_component_fn)(key); let component = (tree.create_component_fn)(key);
let is_native_backed = component.has_native_backing_node(); let is_native_backed = component.has_native_backing_node();
@ -250,22 +254,24 @@ fn mount_component_tree(
// tag similar to what React does, which just hoists the children out of it and // tag similar to what React does, which just hoists the children out of it and
// discards the rest. // discards the rest.
if child.tag == "Fragment" { if child.tag == "Fragment" {
println!(" In Fragment {}", child.children.len()); println!(" In Fragment {}", child.props.children.len());
for child_tree in child.children { for child_tree in child.props.children {
println!(" > WHAT"); println!(" > WHAT");
if let RSX::VirtualNode(child_tree) = child_tree { if let RSX::VirtualNode(child_tree) = child_tree {
let child_key = mount_component_tree(child_tree, component_store, layout_store)?; let child_key = mount_component_tree(child_tree, component_store, layout_store)?;
component_store.add_child(key, child_key)?;
if is_native_backed { if is_native_backed {
link_nodes(key, child_key, component_store, layout_store)?; link_layout_nodess(key, child_key, component_store, layout_store)?;
} }
} }
} }
} else { } else {
println!(" In regular"); println!(" In regular");
let child_key = mount_component_tree(child, component_store, layout_store)?; let child_key = mount_component_tree(child, component_store, layout_store)?;
component_store.add_child(key, child_key)?;
if is_native_backed { if is_native_backed {
link_nodes(key, child_key, component_store, layout_store)?; link_layout_nodess(key, child_key, component_store, layout_store)?;
} }
} }
} else { println!("WTF"); }, } else { println!("WTF"); },
@ -327,7 +333,7 @@ fn unmount_component_tree(
/// find a pattern that didn't feel like some utter magic in Rust. /// find a pattern that didn't feel like some utter magic in Rust.
/// ///
/// It might be because I'm writing this at 3AM. Feel free to improve it. /// It might be because I'm writing this at 3AM. Feel free to improve it.
fn link_nodes( fn link_layout_nodess(
parent: ComponentKey, parent: ComponentKey,
child: ComponentKey, child: ComponentKey,
components: &mut ComponentStore, components: &mut ComponentStore,
@ -337,14 +343,13 @@ fn link_nodes(
if let (Some(parent_layout), Some(child_layout)) = (parent_instance.layout, child_instance.layout) { if let (Some(parent_layout), Some(child_layout)) = (parent_instance.layout, child_instance.layout) {
layouts.add_child(parent_layout, child_layout)?; layouts.add_child(parent_layout, child_layout)?;
parent_instance.component.append_child_component(&*child_instance.component); parent_instance.component.append_child_component(&*child_instance.component);
println!("APPENDED NODE!");
return Ok(()); return Ok(());
} }
} }
let children = components.children(child)?; let children = components.children(child)?;
for child_key in children { for child_key in children {
link_nodes(parent, child_key, components, layouts)?; link_layout_nodess(parent, child_key, components, layouts)?;
} }
Ok(()) Ok(())
@ -358,7 +363,7 @@ fn walk_and_apply_styles(
layouts: &mut LayoutStore layouts: &mut LayoutStore
) -> Result<(), Box<Error>> { ) -> Result<(), Box<Error>> {
let instance = components.get_mut(key)?; let instance = components.get_mut(key)?;
if let Some(layout_key) = instance.layout { if let Some(layout_key) = instance.layout {
instance.component.apply_styles( instance.component.apply_styles(
&instance.appearance, &instance.appearance,
@ -367,6 +372,7 @@ fn walk_and_apply_styles(
} }
for child in components.children(key)? { for child in components.children(key)? {
println!("Nesting");
walk_and_apply_styles(child, components, layouts)?; walk_and_apply_styles(child, components, layouts)?;
} }

View file

@ -32,14 +32,12 @@ impl RSX {
pub fn node( pub fn node(
tag: &'static str, tag: &'static str,
create_fn: fn(key: ComponentKey) -> Box<Component>, create_fn: fn(key: ComponentKey) -> Box<Component>,
props: Props, props: Props
children: Vec<RSX>
) -> RSX { ) -> RSX {
RSX::VirtualNode(VirtualNode { RSX::VirtualNode(VirtualNode {
tag: tag, tag: tag,
create_component_fn: create_fn, create_component_fn: create_fn,
props: props, props: props
children: children
}) })
} }

View file

@ -37,15 +37,31 @@ pub struct Props {
} }
impl Props { impl Props {
pub fn new(key: String, styles: StylesList, attributes: HashMap<&'static str, AttributeType>) -> Props { /// A helper method for constructing Properties.
pub fn new(
key: String,
styles: StylesList,
attributes: HashMap<&'static str, AttributeType>,
children: Vec<RSX>
) -> Props {
Props { Props {
attributes: attributes, attributes: attributes,
children: vec![], children: children,
key: key, key: key,
styles: styles styles: styles
} }
} }
/// A helper method used for constructing root-level Properties.
pub(crate) fn root(children: Vec<RSX>) -> Props {
Props {
attributes: HashMap::new(),
children: children,
key: "".into(),
styles: "root".into()
}
}
/// Returns a Vec of RSX nodes, which are really just cloned pointers for the most part. /// Returns a Vec of RSX nodes, which are really just cloned pointers for the most part.
pub fn children(&self) -> Vec<RSX> { pub fn children(&self) -> Vec<RSX> {
self.children.clone() self.children.clone()

View file

@ -24,10 +24,7 @@ pub struct VirtualNode {
/// ownership of a VirtualNode. /// ownership of a VirtualNode.
/// ///
/// This aspect of functionality may be pulled in a later release if it causes too many issues. /// This aspect of functionality may be pulled in a later release if it causes too many issues.
pub props: Props, pub props: Props
///
pub children: Vec<RSX>
} }
impl Display for VirtualNode { impl Display for VirtualNode {
@ -35,7 +32,7 @@ impl Display for VirtualNode {
fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "<{}>", self.tag)?; write!(f, "<{}>", self.tag)?;
for child in &self.children { for child in &self.props.children {
write!(f, "{:?}", child)?; write!(f, "{:?}", child)?;
} }

View file

@ -226,11 +226,11 @@ impl Element {
let mut attributes = std::collections::HashMap::new(); let mut attributes = std::collections::HashMap::new();
#attributes #attributes
attributes attributes
}), { }, {
let mut children = vec![]; let mut children = vec![];
#children #children
children children
}) }))
)) ))
} }
} }