diff --git a/src/implementation.rs b/src/implementation.rs index 815ce90..2c32280 100644 --- a/src/implementation.rs +++ b/src/implementation.rs @@ -15,35 +15,34 @@ pub fn tick_dioxus_ui(world: &mut World) { unsafe { let world_cell = world.as_unsafe_world_cell(); - let apply_mutations = |mutations: Mutations, bevy_ui_root: Entity| { + let apply_mutations = |mutations: Mutations, root_entity: Entity| { todo!("Modify bevy_ui entities based on mutations"); }; let mut command_queue = CommandQueue::default(); + let ecs_context = EcsContext { + world_read_only: transmute(world_cell.world()), + commands: Rc::new(RefCell::new(Commands::new( + transmute(&mut command_queue), + transmute(world_cell.world()), + ))), + }; - for mut dioxus_ui_root in world_cell + for (root_entity, mut dioxus_ui_root) in world_cell .world_mut() - .query::<&mut DioxusUiRoot>() + .query::<(Entity, &mut DioxusUiRoot)>() .iter_mut(world_cell.world_mut()) { dioxus_ui_root .virtual_dom .get() .base_scope() - .provide_context(EcsContext { - world_read_only: transmute(world_cell.world()), - commands: Rc::new(RefCell::new(Commands::new( - transmute(&mut command_queue), - transmute(world_cell.world()), - ))), - }); + .provide_context(ecs_context.clone()); - let bevy_ui_root = dioxus_ui_root.root_entity.unwrap_or_else(|| { - // TODO: Spawn bevy_ui_root as a child of dioxus_ui_root - let bevy_ui_root = world_cell.world_mut().spawn(()).id(); - apply_mutations(dioxus_ui_root.virtual_dom.get().rebuild(), bevy_ui_root); - bevy_ui_root - }); + if !dioxus_ui_root.initial_build { + apply_mutations(dioxus_ui_root.virtual_dom.get().rebuild(), root_entity); + dioxus_ui_root.initial_build = true; + } // TODO: Handle events from winit // dioxus_ui_root @@ -53,7 +52,7 @@ pub fn tick_dioxus_ui(world: &mut World) { apply_mutations( dioxus_ui_root.virtual_dom.get().render_immediate(), - bevy_ui_root, + root_entity, ); } diff --git a/src/lib.rs b/src/lib.rs index 88cac86..d094044 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ mod implementation; use self::implementation::{tick_dioxus_ui, VirtualDomUnsafe}; use bevy::{ app::{App, Plugin, Update}, - ecs::{component::Component, entity::Entity}, + ecs::component::Component, }; pub use self::implementation::{use_commands, use_res, use_world}; @@ -20,14 +20,14 @@ impl Plugin for DioxusUiPlugin { #[derive(Component)] pub struct DioxusUiRoot { virtual_dom: VirtualDomUnsafe, - root_entity: Option, + initial_build: bool, } impl DioxusUiRoot { pub fn new(root_component: fn(Scope) -> Element) -> Self { Self { virtual_dom: VirtualDomUnsafe::new(root_component), - root_entity: None, + initial_build: false, } } }