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

Reducing nesting in UI examples #3852

Closed
wants to merge 4 commits into from

Conversation

MiniaczQ
Copy link
Contributor

@MiniaczQ MiniaczQ commented Feb 3, 2022

Objective

Reduce the nesting in UI examples through extraction.
Fixes #3850

Status

Testing which layouts scale better.

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Feb 3, 2022
@alice-i-cecile alice-i-cecile added A-UI Graphical user interfaces, styles, layouts, and widgets C-Code-Quality A section of code that is hard to understand or change C-Examples An addition or correction to our examples and removed S-Needs-Triage This issue needs to be labelled labels Feb 3, 2022
@MiniaczQ MiniaczQ marked this pull request as draft February 3, 2022 00:11
// vertically center child text
align_items: AlignItems::Center,
..Default::default()
fn button_text(asset_server: &AssetServer) -> TextBundle {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd prefer passing in the text as an argument to at least hint at the fact that it is an abstraction.

Comment on lines 17 to 18
commands.spawn_bundle(button()).with_children(|parent| {
parent.spawn_bundle(button_text(&asset_server));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is fine for now, but it still scales awfully with more elements nested.

Copy link
Contributor

@IceSentry IceSentry left a comment

Choose a reason for hiding this comment

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

I like the direction this is going. It's nice that layout and styling isn't mixed together, but this also shows how painful the whole parent,with_children is

@MiniaczQ
Copy link
Contributor Author

MiniaczQ commented Feb 3, 2022

Yeah, there is also a rare insert in between,

There is also the option of putting Commands into the functions,
but that reduces the layout readability.
It may be worth it if spaced well.

@alice-i-cecile
Copy link
Member

but this also shows how painful the whole parent.with_children is

Yep. My opinion is that this API should just be removed.

@MiniaczQ
Copy link
Contributor Author

MiniaczQ commented Feb 3, 2022

What would replace it though?

@alice-i-cecile
Copy link
Member

What would replace it though?

.push_children, and the friends in #3855.

color: Color::NONE.into(),
..Default::default()
})
.spawn_bundle(root_element())
Copy link
Contributor

@ickshonpe ickshonpe Feb 6, 2022

Choose a reason for hiding this comment

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

You could reduce the nesting by reusing the root element's EntityCommands instead of having a root level with_children,

let root = root.spawn_bundle(root_element());

 // left vertical fill (border)
 root.spawn_bundle(left_vertical_fill_border())
    .with_children(|parent| {
       ...
    });

// right vertical fill
root.spawn_bundle(right_vertical_fill())
    .with_children(|parent| {
        ...
    });

etc

@MiniaczQ
Copy link
Contributor Author

Gonna wait for a UI api refactor

@MiniaczQ MiniaczQ closed this Mar 23, 2022
@MiniaczQ MiniaczQ deleted the prettier-ui-examples branch January 14, 2024 00:47
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-Code-Quality A section of code that is hard to understand or change C-Examples An addition or correction to our examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UI examples are deeply nested and not fun to read
4 participants