Add support for divs

This commit is contained in:
JMS55 2023-12-10 12:55:49 -08:00
parent cad620fe11
commit 9033473974
3 changed files with 32 additions and 15 deletions

1
Cargo.lock generated
View file

@ -507,7 +507,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"dioxus", "dioxus",
"smallvec",
] ]
[[package]] [[package]]

View file

@ -6,7 +6,6 @@ edition = "2021"
[dependencies] [dependencies]
bevy = { git = "https://github.com/JMS55/bevy", branch = "query_new" } bevy = { git = "https://github.com/JMS55/bevy", branch = "query_new" }
dioxus = "0.4" dioxus = "0.4"
smallvec = "1.0"
[[example]] [[example]]
name = "basic" name = "basic"

View file

@ -3,11 +3,10 @@ use bevy::{
hierarchy::BuildChildren, hierarchy::BuildChildren,
prelude::default, prelude::default,
text::{Text, TextStyle}, text::{Text, TextStyle},
ui::node_bundles::TextBundle, ui::node_bundles::{NodeBundle, TextBundle},
utils::HashMap, utils::HashMap,
}; };
use dioxus::core::{ElementId, Mutation, Mutations, Template, TemplateNode}; use dioxus::core::{ElementId, Mutation, Mutations, Template, TemplateNode};
use smallvec::SmallVec;
pub fn apply_mutations( pub fn apply_mutations(
mutations: Mutations, mutations: Mutations,
@ -64,11 +63,12 @@ pub fn apply_mutations(
} }
pub struct BevyTemplate { pub struct BevyTemplate {
roots: SmallVec<[BevyTemplateNode; 4]>, roots: Box<[BevyTemplateNode]>,
} }
enum BevyTemplateNode { enum BevyTemplateNode {
Text(Text), Node { children: Box<[Self]> },
TextNode(Text),
} }
impl BevyTemplate { impl BevyTemplate {
@ -88,12 +88,19 @@ impl BevyTemplateNode {
match node { match node {
TemplateNode::Element { TemplateNode::Element {
tag, tag,
namespace, namespace: _,
attrs, attrs: _,
children, 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 } => { TemplateNode::Text { text } => {
Self::Text(Text::from_section(*text, TextStyle::default())) Self::TextNode(Text::from_section(*text, TextStyle::default()))
} }
TemplateNode::Dynamic { id } => todo!(), TemplateNode::Dynamic { id } => todo!(),
TemplateNode::DynamicText { id } => todo!(), TemplateNode::DynamicText { id } => todo!(),
@ -102,11 +109,23 @@ impl BevyTemplateNode {
fn spawn(&self, commands: &mut Commands) -> Entity { fn spawn(&self, commands: &mut Commands) -> Entity {
match self { match self {
Self::Text(text) => commands.spawn(TextBundle { BevyTemplateNode::Node { children } => {
text: text.clone(), // TODO: Can probably use with_children() instead
..default() let children = children
}), .iter()
.map(|child| child.spawn(commands))
.collect::<Box<[_]>>();
commands
.spawn(NodeBundle::default())
.push_children(&children)
.id()
}
Self::TextNode(text) => commands
.spawn(TextBundle {
text: text.clone(),
..default()
})
.id(),
} }
.id()
} }
} }