[Merged by Bors] - Improve ergonomics and reduce boilerplate around creating text elements.#5343
[Merged by Bors] - Improve ergonomics and reduce boilerplate around creating text elements.#5343tim-blackbird wants to merge 13 commits intobevyengine:mainfrom
Conversation
|
The other related change I really want is a helper method for quickly getting the first section of text, both immutably and mutably. Maybe |
|
Ping @plof27 for review :) |
|
@Weibye @TimJentzsch, can I get your opinions on this? |
There was a problem hiding this comment.
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
Defaultimplementation 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.
Weibye
left a comment
There was a problem hiding this comment.
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.
IceSentry
left a comment
There was a problem hiding this comment.
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.
|
I added the methods to @alice-i-cecile I tried experimenting with some getter methods but it didn't really improve much. Mostly because if you needed it I do have some questionableo: API ideas but those will go in another PR. I really have to stop updating every example in the API PRs I make. I should just update one or two, Sorry reviewers :c |
alice-i-cecile
left a comment
There was a problem hiding this comment.
These new methods need a pass for const-ability.
Everything else looks good to me!
|
bors r+ |
|
That new diff looks great! |
…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 <justthecooldude@gmail.com>
|
Pull request successfully merged into main. Build succeeded: |
…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 <justthecooldude@gmail.com>
…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 <justthecooldude@gmail.com>
…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 <justthecooldude@gmail.com>
Objective
Creating UI elements is very boilerplate-y with lots of indentation.
This PR aims to reduce boilerplate around creating text elements.
Changelog
Text::with_sectiontofrom_section.It no longer takes a
TextAlignmentas argument, as the vast majority of cases left itDefault::default().Text::from_sectionswhich creates aTextfrom a list ofTextSections.Reduces line-count and reduces indentation by one level.
Text::with_alignment.A builder style method for setting the
TextAlignmentof aText.TextSection::new.Does not reduce line count, but reduces character count and made it easier to read. No more
.to_string()calls!TextSection::from_stylewhich creates an emptyTextSectionwith a style.No more empty strings! Reduces indentation.
TextAlignment::CENTERand friends.TextBundle.from_section,from_sections,with_text_alignmentandwith_style.Note for reviewers.
Because of the nature of these changes I recommend setting diff view to 'split'.
Look for the book iconcog in the top-left of the Files changed tab.Have fun reviewing ❤️
>:D
Migration Guide
Text::with_sectionwas renamed tofrom_sectionand no longer takes aTextAlignmentas argument.Use
with_alignmentto set the alignment instead.