Skip to content

In TextLayout, when deleting all TextSpans, TextLayout still retains the last text #19657

Open
@rendaoer

Description

@rendaoer

Bevy version

v0.16.1 and current main

What you did

When I wanted to make a small Text Input, I found that deleting all TextSpans would still keep the last character.

What went wrong

When deleting all TextSpan, shouldn't it be blank?

Additional information

Image Image When deleting all TextSpans, the last character will be left, white
use bevy::input::common_conditions::input_just_pressed;
use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        // .add_plugins(dev_tools::plugin)
        .add_systems(Startup, setup)
        .add_systems(
            Update,
            delete.run_if(input_just_pressed(KeyCode::Backspace)),
        )
        .run();
}

fn setup(mut commands: Commands) {
    commands.spawn(Camera2d);

    commands.spawn((
        Node {
            width: Val::Percent(100.0),
            height: Val::Percent(100.0),
            justify_content: JustifyContent::Center,
            align_items: AlignItems::Center,
            ..default()
        },
        BackgroundColor(Srgba::GREEN.into()),
        children![(
            Text::default(),
            TextLayout::default(),
            TextFont {
                font_size: 60.0,
                ..Default::default()
            },
            TextColor(Srgba::BLACK.into()),
            TextRoot,
            children![
                (
                    TextSpan::new("1"),
                    TextFont {
                        font_size: 60.0,
                        ..Default::default()
                    },
                    TextColor(Srgba::BLACK.into()),
                ),
                (
                    TextSpan::new("2"),
                    TextFont {
                        font_size: 60.0,
                        ..Default::default()
                    },
                    TextColor(Srgba::BLACK.into()),
                ),
                (
                    TextSpan::new("3"),
                    TextFont {
                        font_size: 60.0,
                        ..Default::default()
                    },
                    TextColor(Srgba::BLACK.into()),
                ),
            ]
        )],
    ));
}

fn delete(mut commands: Commands, texts: Single<&Children, With<TextRoot>>) {
    let children = texts.into_inner();
    if let Some(t) = children.last() {
        commands.entity(*t).despawn();
    }
}

#[derive(Component, Reflect, Debug)]
#[reflect(Component)]
struct TextRoot;

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-UIGraphical user interfaces, styles, layouts, and widgetsC-BugAn unexpected or incorrect behaviorS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions