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

Use add_child API in Bevy's examples instead of with_children #9601

Closed
Dimchikkk opened this issue Aug 27, 2023 · 5 comments
Closed

Use add_child API in Bevy's examples instead of with_children #9601

Dimchikkk opened this issue Aug 27, 2023 · 5 comments
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Examples An addition or correction to our examples

Comments

@Dimchikkk
Copy link
Contributor

What problem does this solve or what need does it fill?

Encourages flat UI code structure instead of deep nesting.

What solution would you like?

Use add_child instead of with_children API, something like:

let root = commands.spawn(NodeBundle::default()).id();
let child = commands.spawn(NodeBundle::default()).id()
commands.entity(root).add_child(child);

What alternative(s) have you considered?

Additional context

@Dimchikkk Dimchikkk added C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Aug 27, 2023
@alice-i-cecile alice-i-cecile added C-Examples An addition or correction to our examples C-Code-Quality A section of code that is hard to understand or change and removed C-Feature A new feature, making something new possible S-Needs-Triage This issue needs to be labelled labels Aug 27, 2023
@alice-i-cecile
Copy link
Member

Similar style discussions were previously had in #3855.

I personally prefer this style, but it's not for everyone.

@alice-i-cecile alice-i-cecile added A-UI Graphical user interfaces, styles, layouts, and widgets and removed C-Code-Quality A section of code that is hard to understand or change labels Aug 27, 2023
@Dimchikkk
Copy link
Contributor Author

Similar style discussions were previously had in #3855.

I personally prefer this style, but it's not for everyone.

I've just noticed by following the #ui channel for half a year or more that quite a few people disliked bevy-ui because they didn't know about the add_child API and used with_children all the time. If Bevy's examples use add_child, it would encourage newcomers to write a flat UI code structure and not hate bevy-ui for deep nesting :)

@tbillington
Copy link
Contributor

tbillington commented Aug 28, 2023

I quite like the nesting coming from html/css/react world, it's just the verbosity in number of lines that is the killer for me.

<div id="shoot-lasers" class="flex-col justify-center items-center w-24 h-24">
  <button id="shoot-lasers-button" class="font-serif text-2xl text-red-500 text-center">
    Shoot lasers
  </button>
</div>
p.spawn((
    Name::new("shoot-lasers"),
    ButtonBundle {
        style: Style {
            flex_direction: FlexDirection::Column,
            justify_content: JustifyContent::Center,
            align_items: AlignItems::Center,
            width: Val::Px(256.),
            height: Val::Px(256.),
            ..default()
        },
        ..default()
    },
))
.with_children(|p| {
    p.spawn((
        Name::new("shoot-lasers-button"),
        TextBundle::from_section(
            c.render(),
            TextStyle {
                font: asset_server.load("Silver.ttf"),
                font_size: 40.,
                color: Color::RED,
            },
        )
        .with_text_alignment(TextAlignment::Center),
    ));
});

@mockersf
Copy link
Member

In my opinion, nesting is a lot preferable for UI than flat.

Examples could showcase both patterns, by migrating only a few, with an explanation on top.

@Selene-Amanita Selene-Amanita added the D-Trivial Nice and easy! A great choice to get started with Bevy label Aug 28, 2023
@alice-i-cecile alice-i-cecile removed the D-Trivial Nice and easy! A great choice to get started with Bevy label Aug 28, 2023
@ickshonpe
Copy link
Contributor

ickshonpe commented Aug 28, 2023

The "display_and_visibility" example I wrote is by far the worst offender. The code that spawns the left and right panels is all nesting and duplication. It could really do with a rework, maybe using a for loop and add_child to construct the sections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Examples An addition or correction to our examples
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants