diff --git a/Cargo.lock b/Cargo.lock index 24cf106..5039ec6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -507,7 +507,6 @@ version = "0.1.0" dependencies = [ "bevy", "dioxus", - "smallvec", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 73d31df..0e86d62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,6 @@ edition = "2021" [dependencies] bevy = { git = "https://github.com/JMS55/bevy", branch = "query_new" } dioxus = "0.4" -smallvec = "1.0" [[example]] name = "basic" diff --git a/src/apply_mutations.rs b/src/apply_mutations.rs index 91db87a..4ea3d66 100644 --- a/src/apply_mutations.rs +++ b/src/apply_mutations.rs @@ -3,11 +3,10 @@ use bevy::{ hierarchy::BuildChildren, prelude::default, text::{Text, TextStyle}, - ui::node_bundles::TextBundle, + ui::node_bundles::{NodeBundle, TextBundle}, utils::HashMap, }; use dioxus::core::{ElementId, Mutation, Mutations, Template, TemplateNode}; -use smallvec::SmallVec; pub fn apply_mutations( mutations: Mutations, @@ -64,11 +63,12 @@ pub fn apply_mutations( } pub struct BevyTemplate { - roots: SmallVec<[BevyTemplateNode; 4]>, + roots: Box<[BevyTemplateNode]>, } enum BevyTemplateNode { - Text(Text), + Node { children: Box<[Self]> }, + TextNode(Text), } impl BevyTemplate { @@ -88,12 +88,19 @@ impl BevyTemplateNode { match node { TemplateNode::Element { tag, - namespace, - attrs, + namespace: _, + attrs: _, children, - } => todo!(), + } => { + if *tag != "div" { + panic!("Unsupported bevy_dioxus tag {tag}. Only `div` is supported."); + } + Self::Node { + children: children.iter().map(Self::from_dioxus).collect(), + } + } TemplateNode::Text { text } => { - Self::Text(Text::from_section(*text, TextStyle::default())) + Self::TextNode(Text::from_section(*text, TextStyle::default())) } TemplateNode::Dynamic { id } => todo!(), TemplateNode::DynamicText { id } => todo!(), @@ -102,11 +109,23 @@ impl BevyTemplateNode { fn spawn(&self, commands: &mut Commands) -> Entity { match self { - Self::Text(text) => commands.spawn(TextBundle { - text: text.clone(), - ..default() - }), + BevyTemplateNode::Node { children } => { + // TODO: Can probably use with_children() instead + let children = children + .iter() + .map(|child| child.spawn(commands)) + .collect::>(); + commands + .spawn(NodeBundle::default()) + .push_children(&children) + .id() + } + Self::TextNode(text) => commands + .spawn(TextBundle { + text: text.clone(), + ..default() + }) + .id(), } - .id() } }