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

[Merged by Bors] - Improve ergonomics and reduce boilerplate around creating text elements. #5343

Closed
wants to merge 13 commits into from

Conversation

tim-blackbird
Copy link
Contributor

@tim-blackbird tim-blackbird commented Jul 16, 2022

Objective

Creating UI elements is very boilerplate-y with lots of indentation.
This PR aims to reduce boilerplate around creating text elements.

Changelog

  • Renamed Text::with_section to from_section.
    It no longer takes a TextAlignment as argument, as the vast majority of cases left it Default::default().
  • Added Text::from_sections which creates a Text from a list of TextSections.
    Reduces line-count and reduces indentation by one level.
  • Added Text::with_alignment.
    A builder style method for setting the TextAlignment of a Text.
  • Added TextSection::new.
    Does not reduce line count, but reduces character count and made it easier to read. No more .to_string() calls!
  • Added TextSection::from_style which creates an empty TextSection with a style.
    No more empty strings! Reduces indentation.
  • Added TextAlignment::CENTER and friends.
  • Added methods to TextBundle. from_section, from_sections, with_text_alignment and with_style.

Note for reviewers.

Because of the nature of these changes I recommend setting diff view to 'split'.
Look for the book icon cog in the top-left of the Files changed tab.

Have fun reviewing ❤️
>:D

Migration Guide

Text::with_section was renamed to from_section and no longer takes a TextAlignment as argument.
Use with_alignment to set the alignment instead.

@alice-i-cecile alice-i-cecile requested a review from mockersf July 16, 2022 20:21
@alice-i-cecile
Copy link
Member

alice-i-cecile commented Jul 16, 2022

The other related change I really want is a helper method for quickly getting the first section of text, both immutably and mutably. Maybe first and first_mut?

@alice-i-cecile alice-i-cecile added A-UI Graphical user interfaces, styles, layouts, and widgets C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jul 16, 2022
@alice-i-cecile
Copy link
Member

Ping @plof27 for review :)

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Jul 16, 2022

@Weibye @TimJentzsch, can I get your opinions on this?

Copy link
Contributor

@TimJentzsch TimJentzsch left a comment

Choose a reason for hiding this comment

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

To be honest, I haven't really used Bevy UI yet so please take this with a grain of salt. But my initial thoughts:

  • The diffs look nice, definitely a readability improvement. I personally really don't like all the ..default() stuff, so seeing less of that is a plus for me.
  • Specifying the styles still looks quite annoying, but I'm not sure if we could easily improve that right now, since we don't have any kind of style inheritance AFAIK.
  • Not having to specify the default style in most cases is nice. However, I wonder if that would actually make a difference in a real setting? I assume that you'll want a custom design for the text in your game and apply that everywhere. So you have a default, but it's not the Default implementation that Bevy provides. I assume you'd then have to create a custom text style and then insert it for every text object you want to create? So then you'd still have to use the old syntax as far as I understand.

crates/bevy_text/src/text.rs Show resolved Hide resolved
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.

I'm very much in the same situation as @TimJentzsch : Not yet having used bevy_ui myself (it's next on my list)

I really like these changes and as it does improve ergonomics around creating text sections. To me this makes sense as an intermediate improvement step, then we should revisit when we're building further abstractions like style inheritance and widgets ++.

What I would be interested in seeing is if it would be possible to further improve so that we don't have to define a font for each of the TextStyle, but default assets seems like a big problem to tackle and not something for this PR.

crates/bevy_text/src/text.rs Show resolved Hide resolved
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 this! It's nothing major, but those things add up so it's nice whenever we can save a few lines or indentation here and there.

This also made me wonder. Could we have a TextBundle::from_section() and TextBundle::from_sections(). It looks like a lot of the places are just creating a TextBundle with a text and default everything else.

@tim-blackbird
Copy link
Contributor Author

I added the methods to TextBundle as @IceSentry suggested. Which makes the diffs even nicer.

@alice-i-cecile I tried experimenting with some getter methods but it didn't really improve much. Mostly because if you needed it text.first() isn't much better than text.sections.first(), and would involve a lot of code duplication.

I do have some questionableo: API ideas but those will go in another PR.
This is hard obnoxious enough to review as is.

I really have to stop updating every example in the API PRs I make. I should just update one or two, Sorry reviewers :c

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.

These new methods need a pass for const-ability.

Everything else looks good to me!

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jul 20, 2022
@alice-i-cecile
Copy link
Member

bors r+

@IceSentry
Copy link
Contributor

That new diff looks great!

bors bot pushed a commit that referenced this pull request Jul 20, 2022
…ts. (#5343)

# Objective

Creating UI elements is very boilerplate-y with lots of indentation.
This PR aims to reduce boilerplate around creating text elements.

## Changelog

* Renamed `Text::with_section` to `from_section`.
  It no longer takes a `TextAlignment` as argument, as the vast majority of cases left it `Default::default()`.
* Added `Text::from_sections` which creates a `Text` from a list of `TextSections`.
  Reduces line-count and reduces indentation by one level.
* Added `Text::with_alignment`.
  A builder style method for setting the `TextAlignment` of a `Text`.
* Added `TextSection::new`.
  Does not reduce line count, but reduces character count and made it easier to read. No more `.to_string()` calls!
* Added `TextSection::from_style` which creates an empty `TextSection` with a style.
  No more empty strings! Reduces indentation.
* Added `TextAlignment::CENTER` and friends.
* Added methods to `TextBundle`. `from_section`, `from_sections`, `with_text_alignment` and `with_style`.

## Note for reviewers.
Because of the nature of these changes I recommend setting diff view to 'split'.
~~Look for the book icon~~ cog in the top-left of the Files changed tab.

Have fun reviewing ❤️
<sup> >:D </sup>

## Migration Guide

`Text::with_section` was renamed to `from_section` and no longer takes a `TextAlignment` as argument.
Use `with_alignment` to set the alignment instead.

Co-authored-by: devil-ira <[email protected]>
@bors bors bot changed the title Improve ergonomics and reduce boilerplate around creating text elements. [Merged by Bors] - Improve ergonomics and reduce boilerplate around creating text elements. Jul 20, 2022
@bors bors bot closed this Jul 20, 2022
inodentry pushed a commit to IyesGames/bevy that referenced this pull request Aug 8, 2022
…ts. (bevyengine#5343)

# Objective

Creating UI elements is very boilerplate-y with lots of indentation.
This PR aims to reduce boilerplate around creating text elements.

## Changelog

* Renamed `Text::with_section` to `from_section`.
  It no longer takes a `TextAlignment` as argument, as the vast majority of cases left it `Default::default()`.
* Added `Text::from_sections` which creates a `Text` from a list of `TextSections`.
  Reduces line-count and reduces indentation by one level.
* Added `Text::with_alignment`.
  A builder style method for setting the `TextAlignment` of a `Text`.
* Added `TextSection::new`.
  Does not reduce line count, but reduces character count and made it easier to read. No more `.to_string()` calls!
* Added `TextSection::from_style` which creates an empty `TextSection` with a style.
  No more empty strings! Reduces indentation.
* Added `TextAlignment::CENTER` and friends.
* Added methods to `TextBundle`. `from_section`, `from_sections`, `with_text_alignment` and `with_style`.

## Note for reviewers.
Because of the nature of these changes I recommend setting diff view to 'split'.
~~Look for the book icon~~ cog in the top-left of the Files changed tab.

Have fun reviewing ❤️
<sup> >:D </sup>

## Migration Guide

`Text::with_section` was renamed to `from_section` and no longer takes a `TextAlignment` as argument.
Use `with_alignment` to set the alignment instead.

Co-authored-by: devil-ira <[email protected]>
@tim-blackbird tim-blackbird deleted the text-ergo branch October 21, 2022 15:11
james7132 pushed a commit to james7132/bevy that referenced this pull request Oct 28, 2022
…ts. (bevyengine#5343)

# Objective

Creating UI elements is very boilerplate-y with lots of indentation.
This PR aims to reduce boilerplate around creating text elements.

## Changelog

* Renamed `Text::with_section` to `from_section`.
  It no longer takes a `TextAlignment` as argument, as the vast majority of cases left it `Default::default()`.
* Added `Text::from_sections` which creates a `Text` from a list of `TextSections`.
  Reduces line-count and reduces indentation by one level.
* Added `Text::with_alignment`.
  A builder style method for setting the `TextAlignment` of a `Text`.
* Added `TextSection::new`.
  Does not reduce line count, but reduces character count and made it easier to read. No more `.to_string()` calls!
* Added `TextSection::from_style` which creates an empty `TextSection` with a style.
  No more empty strings! Reduces indentation.
* Added `TextAlignment::CENTER` and friends.
* Added methods to `TextBundle`. `from_section`, `from_sections`, `with_text_alignment` and `with_style`.

## Note for reviewers.
Because of the nature of these changes I recommend setting diff view to 'split'.
~~Look for the book icon~~ cog in the top-left of the Files changed tab.

Have fun reviewing ❤️
<sup> >:D </sup>

## Migration Guide

`Text::with_section` was renamed to `from_section` and no longer takes a `TextAlignment` as argument.
Use `with_alignment` to set the alignment instead.

Co-authored-by: devil-ira <[email protected]>
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
…ts. (bevyengine#5343)

# Objective

Creating UI elements is very boilerplate-y with lots of indentation.
This PR aims to reduce boilerplate around creating text elements.

## Changelog

* Renamed `Text::with_section` to `from_section`.
  It no longer takes a `TextAlignment` as argument, as the vast majority of cases left it `Default::default()`.
* Added `Text::from_sections` which creates a `Text` from a list of `TextSections`.
  Reduces line-count and reduces indentation by one level.
* Added `Text::with_alignment`.
  A builder style method for setting the `TextAlignment` of a `Text`.
* Added `TextSection::new`.
  Does not reduce line count, but reduces character count and made it easier to read. No more `.to_string()` calls!
* Added `TextSection::from_style` which creates an empty `TextSection` with a style.
  No more empty strings! Reduces indentation.
* Added `TextAlignment::CENTER` and friends.
* Added methods to `TextBundle`. `from_section`, `from_sections`, `with_text_alignment` and `with_style`.

## Note for reviewers.
Because of the nature of these changes I recommend setting diff view to 'split'.
~~Look for the book icon~~ cog in the top-left of the Files changed tab.

Have fun reviewing ❤️
<sup> >:D </sup>

## Migration Guide

`Text::with_section` was renamed to `from_section` and no longer takes a `TextAlignment` as argument.
Use `with_alignment` to set the alignment instead.

Co-authored-by: devil-ira <[email protected]>
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-Usability A targeted quality-of-life change that makes Bevy easier to use 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.

5 participants