Skip to content

Commit e41c5c2

Browse files
committed
Fix UI node Transform change detection (#4138)
# Objective Fixes #4133 ## Solution Add comparisons to make sure we don't dereference `Mut<>` in the two places where `Transform` is being mutated. `GlobalTransform` implementation already works properly so fixing Transform automatically fixed that as well.
1 parent b4483db commit e41c5c2

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

crates/bevy_ui/src/flex/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,17 +278,22 @@ pub fn flex_node_system(
278278
to_logical(layout.size.width),
279279
to_logical(layout.size.height),
280280
);
281+
// only trigger change detection when the new value is different
281282
if node.size != new_size {
282283
node.size = new_size;
283284
}
284-
let position = &mut transform.translation;
285-
position.x = to_logical(layout.location.x + layout.size.width / 2.0);
286-
position.y = to_logical(layout.location.y + layout.size.height / 2.0);
285+
let mut new_position = transform.translation;
286+
new_position.x = to_logical(layout.location.x + layout.size.width / 2.0);
287+
new_position.y = to_logical(layout.location.y + layout.size.height / 2.0);
287288
if let Some(parent) = parent {
288289
if let Ok(parent_layout) = flex_surface.get_layout(parent.0) {
289-
position.x -= to_logical(parent_layout.size.width / 2.0);
290-
position.y -= to_logical(parent_layout.size.height / 2.0);
290+
new_position.x -= to_logical(parent_layout.size.width / 2.0);
291+
new_position.y -= to_logical(parent_layout.size.height / 2.0);
291292
}
292293
}
294+
// only trigger change detection when the new value is different
295+
if transform.translation != new_position {
296+
transform.translation = new_position;
297+
}
293298
}
294299
}

crates/bevy_ui/src/update.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ fn update_hierarchy(
4545
) -> f32 {
4646
current_global_z += UI_Z_STEP;
4747
if let Ok(mut transform) = node_query.get_mut(entity) {
48-
transform.translation.z = current_global_z - parent_global_z;
48+
let new_z = current_global_z - parent_global_z;
49+
// only trigger change detection when the new value is different
50+
if transform.translation.z != new_z {
51+
transform.translation.z = new_z;
52+
}
4953
}
5054
if let Ok(children) = children_query.get(entity) {
5155
let current_parent_global_z = current_global_z;

0 commit comments

Comments
 (0)