Finish removing unsafe from tick.rs

This commit is contained in:
JMS55 2023-12-15 17:04:29 -08:00
parent 9355cc32e2
commit 344f4983c3

View file

@ -9,7 +9,7 @@ use bevy::{
ecs::{ ecs::{
entity::Entity, entity::Entity,
query::With, query::With,
world::{unsafe_world_cell::UnsafeWorldCell, Mut, World}, world::{Mut, World},
}, },
hierarchy::Parent, hierarchy::Parent,
prelude::{Deref, DerefMut}, prelude::{Deref, DerefMut},
@ -30,7 +30,7 @@ pub fn tick_dioxus_ui(world: &mut World) {
.collect(); .collect();
for root_entity in root_entities { for root_entity in root_entities {
dispatch_ui_events(&ui_events, root_entity, world.as_unsafe_world_cell()); dispatch_ui_events(&ui_events, root_entity, world);
schedule_ui_renders_from_ecs_subscriptions(root_entity, world); schedule_ui_renders_from_ecs_subscriptions(root_entity, world);
@ -57,29 +57,17 @@ fn run_deferred_systems(world: &mut World) {
fn dispatch_ui_events( fn dispatch_ui_events(
events: &Vec<(Entity, &str, Rc<dyn Any>)>, events: &Vec<(Entity, &str, Rc<dyn Any>)>,
root_entity: Entity, root_entity: Entity,
world_cell: UnsafeWorldCell, world: &mut World,
) { ) {
let mut ui_root = unsafe { let mut ui_root = world
world_cell .entity_mut(root_entity)
.get_entity(root_entity) .take::<DioxusUiRoot>()
.unwrap() .unwrap();
.get_mut::<DioxusUiRoot>()
.unwrap()
};
let get_parent = |entity| unsafe {
world_cell
.get_entity(entity)
.unwrap()
.get::<Parent>()
.unwrap()
.get()
};
for (mut target, name, data) in events { for (mut target, name, data) in events {
let mut target_element_id = ui_root.bevy_ui_entity_to_element_id.get(&target).copied(); let mut target_element_id = ui_root.bevy_ui_entity_to_element_id.get(&target).copied();
while target_element_id.is_none() { while target_element_id.is_none() {
target = get_parent(target); target = world.entity(target).get::<Parent>().unwrap().get();
target_element_id = ui_root.bevy_ui_entity_to_element_id.get(&target).copied(); target_element_id = ui_root.bevy_ui_entity_to_element_id.get(&target).copied();
} }
@ -90,6 +78,8 @@ fn dispatch_ui_events(
true, true,
); );
} }
world.entity_mut(root_entity).insert(ui_root);
} }
fn schedule_ui_renders_from_ecs_subscriptions(root_entity: Entity, world: &mut World) { fn schedule_ui_renders_from_ecs_subscriptions(root_entity: Entity, world: &mut World) {
@ -117,30 +107,39 @@ fn schedule_ui_renders_from_ecs_subscriptions(root_entity: Entity, world: &mut W
} }
fn render_ui(root_entity: Entity, world: &mut World) { fn render_ui(root_entity: Entity, world: &mut World) {
virtual_dom let mut ui_root = world
.entity_mut(root_entity)
.take::<DioxusUiRoot>()
.unwrap();
ui_root
.virtual_dom
.get()
.base_scope() .base_scope()
.provide_context(EcsContext { world }); .provide_context(EcsContext { world });
if *needs_rebuild { if ui_root.needs_rebuild {
apply_mutations( apply_mutations(
virtual_dom.rebuild(), ui_root.virtual_dom.get().rebuild(),
element_id_to_bevy_ui_entity, &mut ui_root.element_id_to_bevy_ui_entity,
bevy_ui_entity_to_element_id, &mut ui_root.bevy_ui_entity_to_element_id,
templates, &mut ui_root.templates,
root_entity, root_entity,
world, world,
); );
*needs_rebuild = false; ui_root.needs_rebuild = false;
} }
apply_mutations( apply_mutations(
virtual_dom.render_immediate(), ui_root.virtual_dom.get().render_immediate(),
element_id_to_bevy_ui_entity, &mut ui_root.element_id_to_bevy_ui_entity,
bevy_ui_entity_to_element_id, &mut ui_root.bevy_ui_entity_to_element_id,
templates, &mut ui_root.templates,
root_entity, root_entity,
world, world,
); );
world.entity_mut(root_entity).insert(ui_root);
} }
#[derive(Deref, DerefMut)] #[derive(Deref, DerefMut)]