Skip to content

trigger Text & Text2d re-render when Children is removed #19666

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion crates/bevy_text/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ impl Plugin for TextPlugin {
.in_set(Text2dUpdateSystems)
.after(AnimationSystems),
)
.add_systems(Last, trim_cosmic_cache);
.add_systems(Last, trim_cosmic_cache)
.add_observer(enable_text_node_needs_rerender_detection::<TextSpan>)
.add_observer(enable_text_node_needs_rerender_detection::<Text2d>);

if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
render_app.add_systems(
Expand Down
41 changes: 41 additions & 0 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,44 @@ pub fn detect_text_needs_rerender<Root: Component>(
}
}
}

/// Observer system that ensures all entities with the `Tracked` [`Component`] will be able to trigger
/// [`detect_text_needs_rerender_on_child_removed`].
pub fn enable_text_node_needs_rerender_detection<Tracked: Component>(
trigger: On<Add, Tracked>,
mut commands: Commands,
) {
let Ok(mut entity) = commands.get_entity(trigger.target()) else {
return;
};

entity.observe(detect_text_needs_rerender_on_child_removed);
}

/// Observer system that detects when text blocks get their last child removed and sets
/// `ComputedTextBlock::should_rerender`.
pub fn detect_text_needs_rerender_on_child_removed(
trigger: On<Remove, Children>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work I think, it's going to trigger and do a ComputedTextBlock look up everytime any Children component is despawned, not just those belonging to text entities.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we explicitely observe entities that have Text/Text2d ? I don't know if that dupplicates observer entities for every observed text root, I'm not sure it would be an issue either.

// `With<TextFont>` included to bail on non-text nodes
mut texts: Query<(&ChildOf, Option<&mut ComputedTextBlock>), With<TextFont>>,
) {
let mut entity = trigger.target();
let mut computed = loop {
match texts.get_mut(entity) {
// we stop at the first computed text block encountered
Ok((_, Some(computed))) => break computed,
Ok((parent, None)) => entity = parent.0,
Err(_) => {
// This warning is less useful than the one in `detect_text_needs_rerender`,
// we don't know the type name of the root
once!(warn!(
"found entity {} with a TextFont that has no ancestor with a root text \
component; this warning only prints once",
entity,
));
return;
}
}
};
computed.needs_rerender = true;
}
1 change: 1 addition & 0 deletions crates/bevy_ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ fn build_text_interop(app: &mut App) {
.ambiguous_with(bevy_text::calculate_bounds_text2d),
),
);
app.add_observer(bevy_text::enable_text_node_needs_rerender_detection::<Text>);

app.add_plugins(accessibility::AccessibilityPlugin);

Expand Down