diff --git a/examples/tabs/tabs.rs b/examples/tabs/tabs.rs index efa60a918c9e06b7bf5084aa4084da0839ce3616..a695448d70733dcb7bea4112c2e85a8fb37c3fa4 100644 --- a/examples/tabs/tabs.rs +++ b/examples/tabs/tabs.rs @@ -75,10 +75,10 @@ fn startup( ..Default::default() }} > - <TabBundle tab={Tab { index: 0 }}> + <TabBundle key={"tab1"} tab={Tab { index: 0 }}> <TextWidgetBundle text={TextProps { content: "Tab 1 Content".into(), size: 14.0, line_height: Some(14.0), ..Default::default() }} /> </TabBundle> - <TabBundle tab={Tab { index: 1 }}> + <TabBundle key={"tab2"} tab={Tab { index: 1 }}> <TextWidgetBundle text={TextProps { content: "Tab 2 Content".into(), size: 14.0, line_height: Some(14.0), ..Default::default() }} /> </TabBundle> </ElementBundle> diff --git a/src/children.rs b/src/children.rs index 454e7041cae8499697a6416024cbe561c04db1fa..b6f5bbfe78ffeb68a4b633e6b8a1a836c18bf74d 100644 --- a/src/children.rs +++ b/src/children.rs @@ -63,6 +63,7 @@ impl KChildren { for child in self.inner.iter() { if let Some(parent_id) = parent_id { if let Some(mut entity_commands) = commands.get_entity(*child) { + entity_commands.remove::<Parent>(); entity_commands.set_parent(parent_id); } } diff --git a/src/context.rs b/src/context.rs index e9622a357e25d14b65bdc1725d42f62fd4e986c9..6d6c87020a6627ebae4677145de9294c14d5cd64 100644 --- a/src/context.rs +++ b/src/context.rs @@ -783,6 +783,14 @@ fn update_widgets( } } + // let had_change = diff.has_changes(); + // if had_change { + // println!("Tree Before:"); + // tree.dump_all_at(Some(world), entity.0); + // println!("Changes:"); + // diff.debug_print(world); + // } + // Children of this node need to be despawned. let mut despawn_list = Vec::default(); 'outer: for (_index, changed_entity, parent, changes) in diff.changes.iter() @@ -872,6 +880,7 @@ fn update_widgets( world.get_entity_mut(changed_entity.0) { entity_commands.insert(Mounted); + entity_commands.remove::<bevy::prelude::Parent>(); entity_commands.set_parent(parent.0); } if world.get_entity(changed_entity.0).is_some() { @@ -888,18 +897,21 @@ fn update_widgets( // If the child exists as a child of one of the children we do not need to remove it. // TODO: This is kinda of expensive we should think of a way of making this faster.. if let Ok(order_tree) = order_tree.try_read() { - for sibling in order_tree - .child_iter(order_tree.parent(*changed_entity).unwrap()) - { - for child in tree.down_iter_at(sibling, true) { - if let Some(entity_ref) = world.get_entity(child.0) { - if let Some(children) = - entity_ref.get::<KChildren>() + if let Some(parent) = order_tree.parent(*changed_entity) { + for sibling in order_tree.child_iter(parent) { + for child in tree.down_iter_at(sibling, true) { + if let Some(entity_ref) = world.get_entity(child.0) { - if children.contains_entity(changed_entity.0) { - trace!("Caught an entity that was marked as deleted but wasn't! {:?}", changed_entity.0); - // Don't despawn changed entity because it exists as a child passed via props - continue 'outer; + if let Some(children) = + entity_ref.get::<KChildren>() + { + if children + .contains_entity(changed_entity.0) + { + trace!("Caught an entity that was marked as deleted but wasn't! {:?}", changed_entity.0); + // Don't despawn changed entity because it exists as a child passed via props + continue 'outer; + } } } } @@ -926,14 +938,6 @@ fn update_widgets( } } - // let had_change = diff.has_changes(); - // if had_change { - // println!("Tree Before:"); - // tree.dump_all_at(Some(world), entity.0); - // println!("Changes:"); - // diff.debug_print(world); - // } - tree.merge(&widget_context, *entity, diff, UPDATE_DEPTH); // if had_change { @@ -966,19 +970,22 @@ fn update_widgets( // Remove state entity if let Some(state_entity) = widget_state.remove(entity) { - if let Some(entity_mut) = world.get_entity_mut(state_entity) { + if let Some(mut entity_mut) = world.get_entity_mut(state_entity) { + entity_mut.remove_parent(); entity_mut.despawn_recursive(); } } // Remove widget entity - if let Some(entity_mut) = world.get_entity_mut(entity) { + if let Some(mut entity_mut) = world.get_entity_mut(entity) { log::trace!( "Removing entity! {:?} - {:?} with parent {:?}", entity.index(), entity_mut.get::<WidgetName>(), parent.index(), ); + entity_mut.remove_parent(); + entity_mut.remove::<bevy::prelude::Children>(); entity_mut.despawn(); // Also remove all cloned widget entities @@ -1089,7 +1096,9 @@ fn update_widgets( order_tree.remove(*entity); } tree.remove(*entity); - if let Some(entity_mut) = world.get_entity_mut(entity.0) { + if let Some(mut entity_mut) = world.get_entity_mut(entity.0) { + entity_mut.remove_parent(); + entity_mut.remove::<bevy::prelude::Children>(); entity_mut.despawn(); } }