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

Add CSS Grid support to bevy_ui #8026

Merged
merged 76 commits into from
Apr 17, 2023
Merged

Conversation

nicoburns
Copy link
Contributor

@nicoburns nicoburns commented Mar 10, 2023

Objective

An easy way to create 2D grid layouts

Solution

Enable the grid feature in Taffy and add new style types for defining grids.

Notes

  • I'm having a bit of trouble getting #[derive(Reflect)] to work properly. Help with that would be appreciated (EDIT: got it to compile by ignoring the problematic fields, but this presumably can't be merged). This is now fixed

  • The alignment types now have a Normal variant because I couldn't get reflect to work with Option. I've decided to stick with the flattened variant, as it saves a level of wrapping when authoring styles. But I've renamed the variants from Normal to Default.

  • This currently exposes a simplified API on top of grid. In particular the following is not currently supported:

    • Negative grid indices Now supported.
    • Custom end values for grid placement (you can only use start and span) Now supported
    • minmax() track sizing functions minmax is now support through a GridTrack::minmax() constructor
    • repeat() repeat is now implemented as RepeatedGridTrack
  • Documentation still needs to be improved. An initial pass over the documentation has been completed.

Screenshot

Screenshot 2023-03-10 at 17 56 21


Changelog

  • Support for CSS Grid layout added to bevy_ui
  • Renamed the UiSystem::Flex system set to UiSystem::Layout

Migration guide

-The UiSystem::Flex system set has been renamed to UiSystem::Layout

@nicoburns nicoburns added A-UI Graphical user interfaces, styles, layouts, and widgets C-Feature A new feature, making something new possible labels Mar 10, 2023
@nicoburns nicoburns added this to the 0.11 milestone Mar 10, 2023
@nicoburns nicoburns changed the title Add CSS Grid support to bevy_ui Add CSS Grid support to bevy_ui Mar 10, 2023
@nicoburns
Copy link
Contributor Author

Example added:cargo run --release --example grid if anyone want to play around with it.

@github-actions
Copy link
Contributor

You added a new example but didn't add metadata for it. Please update the root Cargo.toml file.

1 similar comment
@github-actions
Copy link
Contributor

You added a new example but didn't add metadata for it. Please update the root Cargo.toml file.

Copy link
Contributor

@rparrett rparrett left a comment

Choose a reason for hiding this comment

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

Just took a quick look at the docs for now. Hope to look closer and play with this later.

crates/bevy_ui/src/ui_node.rs Outdated Show resolved Hide resolved
crates/bevy_ui/src/ui_node.rs Outdated Show resolved Hide resolved
crates/bevy_ui/src/ui_node.rs Outdated Show resolved Hide resolved
crates/bevy_ui/src/ui_node.rs Outdated Show resolved Hide resolved
crates/bevy_ui/src/ui_node.rs Outdated Show resolved Hide resolved
crates/bevy_ui/src/ui_node.rs Outdated Show resolved Hide resolved
crates/bevy_ui/src/ui_node.rs Outdated Show resolved Hide resolved
examples/ui/grid.rs Outdated Show resolved Hide resolved
@nicoburns
Copy link
Contributor Author

@rparrett The docs needs a full once-over by the way. I haven't paid any attention to docs yet. All the new types need docs, and quite a few of the existing style properties need updates to reflect how they work with CSS Grid.

@nicoburns
Copy link
Contributor Author

The text wrapping isn't working properly, but I don't think that's grid specific. And I suspect it will be fixed by #7779

@ickshonpe
Copy link
Contributor

ickshonpe commented Mar 10, 2023

Looks great. Having the default variants instead of optional fields is a really good compromise.
I'll try and rework my layout example and see how that goes.

crates/bevy_ui/src/flex/convert.rs Outdated Show resolved Hide resolved
crates/bevy_ui/src/flex/convert.rs Outdated Show resolved Hide resolved
crates/bevy_ui/src/flex/convert.rs Outdated Show resolved Hide resolved
@nicoburns
Copy link
Contributor Author

@ickshonpe Scaling implemented :)

@ickshonpe
Copy link
Contributor

@ickshonpe Scaling implemented :)

I really like the map_fn additions, convert looks so much better now.
My test example is working perfectly with scaling too:
grid_cap
https://github.com/ickshonpe/grid_layout

@nicoburns
Copy link
Contributor Author

@ickshonpe I see you have repeated track definitions in your test example. I've added support for repeat(). So you can now do :

grid_template_columns: vec![GridTrack::px(100.).repeat(alignments.len())]
grid_template_rows: vec![GridTrack::auto()), GridTrack::px(80.)).repeat(justifications.len())]

instead of

grid_template_columns: iter::repeat(GridTrack::px(100.))
    .take(alignments.len())
    .collect(),
grid_template_rows: iter::once(GridTrack::auto())
    .chain(iter::repeat(GridTrack::px(80.)).take(justifications.len()))
    .collect(),

You can also use GridPlacement::start(4) instead of:

GridPlacement {
    start: Some(4),
    span: 1,
}

Copy link
Contributor

@Weibye Weibye left a comment

Choose a reason for hiding this comment

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

From what I can tell, this the implementation looks good and correct. Comments and documentation is what needs some improvements before this is released, but that could also be done in follow up PRs.

crates/bevy_ui/src/ui_node.rs Show resolved Hide resolved
examples/ui/grid.rs Outdated Show resolved Hide resolved
examples/ui/grid.rs Outdated Show resolved Hide resolved
examples/ui/grid.rs Outdated Show resolved Hide resolved
Cargo.toml Outdated Show resolved Hide resolved
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

The doc comment on NodeBundle::style wasn't updated with its siblings.

Once that's done, the rest has my approval and I'll merge :)

@nicoburns
Copy link
Contributor Author

@alice-i-cecile Doc comment updated (also resolved conflicts with #7779)

@alice-i-cecile alice-i-cecile added this pull request to the merge queue Apr 17, 2023
Merged via the queue into bevyengine:main with commit 363d0f0 Apr 17, 2023
@nicoburns nicoburns deleted the css-grid branch April 17, 2023 17:01
alice-i-cecile pushed a commit that referenced this pull request Apr 17, 2023
# Objective

- Incorrectly resolved merge conflicts in
#8026 have caused UI text to not
render at all.

## Solution

Restore correct system schedule for text systems
@NiklasEi
Copy link
Member

Could you add the rename from UiSystem::Flex to UiSystem::Layout to the changelog please 🙂

@alice-i-cecile alice-i-cecile added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Jun 22, 2023
@alice-i-cecile
Copy link
Member

Done for you :)

@github-actions
Copy link
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Could you add some context on what users should update when this change get released in a new version of Bevy?
It will be used to help writing the migration guide for the version. Putting it after a ## Migration Guide will help it get automatically picked up by our tooling.

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-Feature A new feature, making something new possible M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants