diff --git a/crates/bevy_ui/src/layout/mod.rs b/crates/bevy_ui/src/layout/mod.rs index a8ea44f664b4e..80cd0c9f86092 100644 --- a/crates/bevy_ui/src/layout/mod.rs +++ b/crates/bevy_ui/src/layout/mod.rs @@ -274,6 +274,11 @@ pub fn ui_layout_system( } } + // When a `ContentSize` component is removed from an entity, we need to remove the measure from the corresponding taffy node. + for entity in removed_content_sizes.read() { + ui_surface.try_remove_measure(entity); + } + for (entity, mut content_size) in measure_query.iter_mut() { if let Some(measure_func) = content_size.measure_func.take() { ui_surface.update_measure(entity, measure_func); @@ -283,11 +288,6 @@ pub fn ui_layout_system( // clean up removed nodes ui_surface.remove_entities(removed_nodes.read()); - // When a `ContentSize` component is removed from an entity, we need to remove the measure from the corresponding taffy node. - for entity in removed_content_sizes.read() { - ui_surface.try_remove_measure(entity); - } - // update window children (for now assuming all Nodes live in the primary window) ui_surface.set_window_children(primary_window_entity, root_node_query.iter()); diff --git a/crates/bevy_ui/src/widget/image.rs b/crates/bevy_ui/src/widget/image.rs index 022c174adf462..f7a54c0da6f42 100644 --- a/crates/bevy_ui/src/widget/image.rs +++ b/crates/bevy_ui/src/widget/image.rs @@ -3,6 +3,7 @@ use crate::{ }; use bevy_asset::{Assets, Handle}; +use bevy_ecs::change_detection::DetectChanges; use bevy_ecs::query::Without; use bevy_ecs::{ prelude::Component, @@ -96,7 +97,10 @@ pub fn update_image_content_size_system( texture.texture_descriptor.size.height as f32, ); // Update only if size or scale factor has changed to avoid needless layout calculations - if size != image_size.size || combined_scale_factor != *previous_combined_scale_factor { + if size != image_size.size + || combined_scale_factor != *previous_combined_scale_factor + || content_size.is_added() + { image_size.size = size; content_size.set(ImageMeasure { // multiply the image size by the scale factor to get the physical size @@ -135,7 +139,10 @@ pub fn update_atlas_content_size_system( if let Some(atlas) = atlases.get(atlas) { let size = atlas.textures[atlas_image.index].size(); // Update only if size or scale factor has changed to avoid needless layout calculations - if size != image_size.size || combined_scale_factor != *previous_combined_scale_factor { + if size != image_size.size + || combined_scale_factor != *previous_combined_scale_factor + || content_size.is_added() + { image_size.size = size; content_size.set(ImageMeasure { // multiply the image size by the scale factor to get the physical size diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index 9700d77c967bd..0f96057eafa9a 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -128,7 +128,7 @@ pub fn measure_text_system( if *last_scale_factor == scale_factor { // scale factor unchanged, only create new measure funcs for modified text for (text, content_size, text_flags) in text_query.iter_mut() { - if text.is_changed() || text_flags.needs_new_measure_func { + if text.is_changed() || text_flags.needs_new_measure_func || content_size.is_added() { create_text_measure(&fonts, scale_factor, text, content_size, text_flags); } }