Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Clean up taffy nodes when UI node entities are removed #5886

Closed
wants to merge 4 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions crates/bevy_ui/src/flex/mod.rs
Expand Up @@ -5,7 +5,7 @@ use bevy_ecs::{
entity::Entity,
event::EventReader,
query::{Changed, With, Without, WorldQuery},
system::{Query, Res, ResMut, Resource},
system::{Query, RemovedComponents, Res, ResMut, Resource},
};
use bevy_hierarchy::{Children, Parent};
use bevy_log::warn;
Expand Down Expand Up @@ -172,6 +172,15 @@ without UI components as a child of an entity with UI components, results may be
}
}

/// Removes each entity from the internal map and then removes their associated node from taffy
pub fn remove_entities(&mut self, entities: impl IntoIterator<Item = Entity>) {
for entity in entities.into_iter() {
oceantume marked this conversation as resolved.
Show resolved Hide resolved
if let Some(node) = self.entity_to_taffy.remove(&entity) {
self.taffy.remove(node);
}
}
}

pub fn get_layout(&self, entity: Entity) -> Result<&taffy::layout::Layout, FlexError> {
if let Some(taffy_node) = self.entity_to_taffy.get(&entity) {
self.taffy
Expand Down Expand Up @@ -208,6 +217,7 @@ pub fn flex_node_system(
>,
children_query: Query<(Entity, &Children), (With<Node>, Changed<Children>)>,
mut node_transform_query: Query<(Entity, &mut Node, &mut Transform, Option<&Parent>)>,
removed_nodes: RemovedComponents<Node>,
) {
// update window root nodes
for window in windows.iter() {
Expand Down Expand Up @@ -244,7 +254,8 @@ pub fn flex_node_system(
flex_surface.upsert_leaf(entity, style, *calculated_size, scale_factor);
}

// TODO: handle removed nodes
// clean up removed nodes
flex_surface.remove_entities(removed_nodes.iter());
oceantume marked this conversation as resolved.
Show resolved Hide resolved

// update window children (for now assuming all Nodes live in the primary window)
if let Some(primary_window) = windows.get_primary() {
Expand Down