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

Rich text for all widgets #855

Merged
merged 19 commits into from
Nov 1, 2021
Merged

Rich text for all widgets #855

merged 19 commits into from
Nov 1, 2021

Conversation

emilk
Copy link
Owner

@emilk emilk commented Oct 29, 2021

This introduces the new types RichText and WidgetText, used for specifying text colors etc for Button, Checkbox, RadioButton, CollapsingHeader and Window.

This enables user to have fine control over the labels used in many widgets, for instance to change their fonts, colors, add underlining, or even mix fonts and colors.

WidgetText can be created from a simple String, from a RichText, LayoutJob or a Galley. The last two are the most powerful, but least ergonomic.

RichText is a builder that takes a String and can then apply color, TextStyle, strikethrough, underlining, etc.

Splitting out text formatting from the widgets makes them more orthogonal, and less code is needed in the widget implementations.

Code breakage

Most old code should work with a helpful deprecation notice.

// Before:
ui.add(Button::new("delete").text_color(Color32::RED))

// After:
ui.add(Button::new(RichText::new("delete").color(Color32::RED)))
// or:
ui.button(RichText::new("delete").color(Color32::RED))


// This also works now:
ui.checkbox(&mut my_bool, RichText::new("delete").color(Color32::RED));

TODO:

  • Use WidgetText in Button, Checkbox, RadioButton, CollapsingHeader, SelectableLabel, Window title
  • Add more builders to RichText: italics, code, underline strikethrough, weak, …
  • Use WidgetText in Label
  • Use WidgetText in Hyperlink
  • Use WidgetText in ComboBox
  • Do not re-layout WidgetText::Galley.
  • Benchmark (faster by around 5%, thanks to avoiding a text clone in Label)
  • Add a line to CHANGELOG.md
  • More places in ComboBox
  • Menu
  • Painter too complicated
  • Any place with #[allow(clippy::needless_pass_by_value)]
  • Any place with impl ToString
  • Check if there are any more places that could make use of RichText.
  • Plots later PR

@emilk emilk force-pushed the rich-text branch 2 times, most recently from f116fba to 4059cdf Compare October 30, 2021 09:05
@emilk emilk changed the title Rich text for buttons, Window and CollapsingHeader Rich text for all widgets Oct 30, 2021
@emilk emilk marked this pull request as ready for review October 31, 2021 20:57
@emilk emilk merged commit 09b8269 into master Nov 1, 2021
@emilk emilk deleted the rich-text branch November 1, 2021 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant