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

Fix the double leaf node updates in flex_node_system #8264

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions crates/bevy_ui/src/flex/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bevy_ecs::{
change_detection::DetectChanges,
entity::Entity,
event::EventReader,
query::{Changed, ReadOnlyWorldQuery, With, Without},
query::{Changed, Or, With, Without},
removal_detection::RemovedComponents,
system::{Query, Res, ResMut, Resource},
};
Expand Down Expand Up @@ -250,11 +250,14 @@ pub fn flex_node_system(
mut resize_events: EventReader<bevy_window::WindowResized>,
mut flex_surface: ResMut<FlexSurface>,
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
node_query: Query<(Entity, &Style, Option<&CalculatedSize>), (With<Node>, Changed<Style>)>,
full_node_query: Query<(Entity, &Style, Option<&CalculatedSize>), With<Node>>,
changed_style_query: Query<
(Entity, &Style),
(With<Node>, Without<CalculatedSize>, Changed<Style>),
>,
changed_size_query: Query<
(Entity, &Style, &CalculatedSize),
(With<Node>, Changed<CalculatedSize>),
(With<Node>, Or<(Changed<CalculatedSize>, Changed<Style>)>),
>,
children_query: Query<(Entity, &Children), (With<Node>, Changed<Children>)>,
mut removed_children: RemovedComponents<Children>,
Expand Down Expand Up @@ -288,33 +291,28 @@ pub fn flex_node_system(

let scale_factor = logical_to_physical_factor * ui_scale.scale;

let viewport_values = LayoutContext::new(scale_factor, physical_size);
let layout_context = LayoutContext::new(scale_factor, physical_size);

fn update_changed<F: ReadOnlyWorldQuery>(
flex_surface: &mut FlexSurface,
viewport_values: &LayoutContext,
query: Query<(Entity, &Style, Option<&CalculatedSize>), F>,
) {
// update changed nodes
for (entity, style, calculated_size) in &query {
// TODO: remove node from old hierarchy if its root has changed
if !scale_factor_events.is_empty() || ui_scale.is_changed() || resized {
scale_factor_events.clear();
// update all nodes
for (entity, style, calculated_size) in &full_node_query {
if let Some(calculated_size) = calculated_size {
flex_surface.upsert_leaf(entity, style, *calculated_size, viewport_values);
flex_surface.upsert_leaf(entity, style, *calculated_size, &layout_context);
} else {
flex_surface.upsert_node(entity, style, viewport_values);
flex_surface.upsert_node(entity, style, &layout_context);
}
}
}

if !scale_factor_events.is_empty() || ui_scale.is_changed() || resized {
scale_factor_events.clear();
update_changed(&mut flex_surface, &viewport_values, full_node_query);
} else {
update_changed(&mut flex_surface, &viewport_values, node_query);
}
// update changed nodes without a calculated size
for (entity, style) in changed_style_query.iter() {
flex_surface.upsert_node(entity, style, &layout_context);
}

for (entity, style, calculated_size) in &changed_size_query {
flex_surface.upsert_leaf(entity, style, *calculated_size, &viewport_values);
// update changed nodes with a calculated size
for (entity, style, calculated_size) in changed_size_query.iter() {
flex_surface.upsert_leaf(entity, style, *calculated_size, &layout_context);
}
}

// clean up removed nodes
Expand Down