-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
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.
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.
There was a problem hiding this 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.
There was a problem hiding this 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.
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 |
There was a problem hiding this 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!
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_section
tofrom_section
.It no longer takes a
TextAlignment
as argument, as the vast majority of cases left itDefault::default()
.Text::from_sections
which creates aText
from a list ofTextSections
.Reduces line-count and reduces indentation by one level.
Text::with_alignment
.A builder style method for setting the
TextAlignment
of 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_style
which creates an emptyTextSection
with a style.No more empty strings! Reduces indentation.
TextAlignment::CENTER
and friends.TextBundle
.from_section
,from_sections
,with_text_alignment
andwith_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_section
was renamed tofrom_section
and no longer takes aTextAlignment
as argument.Use
with_alignment
to set the alignment instead.