Skip to content

Commit

Permalink
Remove the corresponding measure from Taffy when a CalculatedSize c…
Browse files Browse the repository at this point in the history
…omponent is removed. (#8294)

# Objective

When a `CalculatedSize` component from a UI Node entity is removed, the
corresponding Taffy measure isn't removed which will mess up the layout
in confusing, unpredictable ways.

## Solution

Iterate through all the entities with removed `CalculatedSize`
components and remove the corresponding Taffy measures.
  • Loading branch information
ickshonpe authored Apr 5, 2023
1 parent 1575481 commit 6e67d3e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ without UI components as a child of an entity with UI components, results may be
}
}

/// Removes the measure from the entity's taffy node if it exists. Does nothing otherwise.
pub fn try_remove_measure(&mut self, entity: Entity) {
if let Some(taffy_node) = self.entity_to_taffy.get(&entity) {
self.taffy.set_measure(*taffy_node, None).unwrap();
}
}

pub fn update_window(&mut self, window: Entity, window_resolution: &WindowResolution) {
let taffy = &mut self.taffy;
let node = self
Expand Down Expand Up @@ -258,6 +265,7 @@ pub fn flex_node_system(
>,
children_query: Query<(Entity, &Children), (With<Node>, Changed<Children>)>,
mut removed_children: RemovedComponents<Children>,
mut removed_calculated_sizes: RemovedComponents<CalculatedSize>,
mut node_transform_query: Query<(Entity, &mut Node, &mut Transform, Option<&Parent>)>,
mut removed_nodes: RemovedComponents<Node>,
) {
Expand Down Expand Up @@ -320,6 +328,11 @@ pub fn flex_node_system(
// clean up removed nodes
flex_surface.remove_entities(removed_nodes.iter());

// When a `CalculatedSize` component is removed from an entity, we need to remove the measure from the corresponding taffy node.
for entity in removed_calculated_sizes.iter() {
flex_surface.try_remove_measure(entity);
}

// update window children (for now assuming all Nodes live in the primary window)
flex_surface.set_window_children(primary_window_entity, root_node_query.iter());

Expand Down

0 comments on commit 6e67d3e

Please sign in to comment.