Replace remaining uses of &T, Changed<T> with Ref in UI system queries (#8567)

# Objective

Replace `Query<&T, Changed<T>>` style queries with the more efficient
`Query<Ref<T>>` form in two of the UI systems.

---

## Changelog

Replaced use of `Changed` with `Ref` in queries in the
`ui_layout_system` and `calc_bounds` UI systems.
This commit is contained in:
ickshonpe 2023-05-08 21:49:55 +01:00 committed by GitHub
parent 613b5a69ae
commit 8581f607f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 19 deletions

View file

@ -8,9 +8,10 @@ use bevy_a11y::{
};
use bevy_app::{App, Plugin, Update};
use bevy_ecs::{
prelude::Entity,
query::{Changed, Or, Without},
prelude::{DetectChanges, Entity},
query::{Changed, Without},
system::{Commands, Query},
world::Ref,
};
use bevy_hierarchy::Children;
use bevy_render::prelude::Camera;
@ -34,23 +35,22 @@ fn calc_name(texts: &Query<&Text>, children: &Children) -> Option<Box<str>> {
fn calc_bounds(
camera: Query<(&Camera, &GlobalTransform)>,
mut nodes: Query<
(&mut AccessibilityNode, &Node, &GlobalTransform),
Or<(Changed<Node>, Changed<GlobalTransform>)>,
>,
mut nodes: Query<(&mut AccessibilityNode, Ref<Node>, Ref<GlobalTransform>)>,
) {
if let Ok((camera, camera_transform)) = camera.get_single() {
for (mut accessible, node, transform) in &mut nodes {
if let Some(translation) =
camera.world_to_viewport(camera_transform, transform.translation())
{
let bounds = Rect::new(
translation.x.into(),
translation.y.into(),
(translation.x + node.calculated_size.x).into(),
(translation.y + node.calculated_size.y).into(),
);
accessible.set_bounds(bounds);
if node.is_changed() || transform.is_changed() {
if let Some(translation) =
camera.world_to_viewport(camera_transform, transform.translation())
{
let bounds = Rect::new(
translation.x.into(),
translation.y.into(),
(translation.x + node.calculated_size.x).into(),
(translation.y + node.calculated_size.y).into(),
);
accessible.set_bounds(bounds);
}
}
}
}

View file

@ -6,7 +6,7 @@ use bevy_ecs::{
change_detection::DetectChanges,
entity::Entity,
event::EventReader,
query::{Changed, With, Without},
query::{With, Without},
removal_detection::RemovedComponents,
system::{Query, Res, ResMut, Resource},
world::Ref,
@ -222,7 +222,7 @@ pub fn ui_layout_system(
root_node_query: Query<Entity, (With<Node>, Without<Parent>)>,
style_query: Query<(Entity, Ref<Style>), With<Node>>,
mut measure_query: Query<(Entity, &mut ContentSize)>,
children_query: Query<(Entity, &Children), (With<Node>, Changed<Children>)>,
children_query: Query<(Entity, Ref<Children>), With<Node>>,
mut removed_children: RemovedComponents<Children>,
mut removed_content_sizes: RemovedComponents<ContentSize>,
mut node_transform_query: Query<(Entity, &mut Node, &mut Transform, Option<&Parent>)>,
@ -293,7 +293,9 @@ pub fn ui_layout_system(
ui_surface.try_remove_children(entity);
}
for (entity, children) in &children_query {
ui_surface.update_children(entity, children);
if children.is_changed() {
ui_surface.update_children(entity, &children);
}
}
// compute layouts