Add ui.colored_label(color, text) helper function

This commit is contained in:
Emil Ernerfeldt 2020-12-11 12:25:28 +01:00
parent c173ea6b2f
commit 2075cb4676
3 changed files with 10 additions and 5 deletions

View file

@ -51,8 +51,8 @@ impl Widgets {
ui.horizontal_wrapped_for_text(TextStyle::Body, |ui| {
ui.label("Long text will wrap, just as you would expect.");
ui.add(Label::new("Text can have").text_color(srgba(110, 255, 110, 255)));
ui.add(Label::new("color").text_color(srgba(128, 140, 255, 255)));
ui.add(Label::new("and tooltips.")).on_hover_text(
ui.colored_label(srgba(128, 140, 255, 255), "color"); // Shortcut version
ui.label("and tooltips.").on_hover_text(
"This is a multiline tooltip that demonstrates that you can easily add tooltips to any element.\nThis is the second line.\nThis is the third.",
);

View file

@ -531,6 +531,11 @@ impl Ui {
self.add(label.into())
}
/// Shortcut for `add(Label::new(text).text_color(color))`
pub fn colored_label(&mut self, color: impl Into<Srgba>, label: impl Into<Label>) -> Response {
self.add(label.into().text_color(color))
}
/// Shortcut for `add(Label::new(text).heading())`
pub fn heading(&mut self, label: impl Into<Label>) -> Response {
self.add(label.into().heading())

View file

@ -1,7 +1,7 @@
//! Widgets are pieces of GUI such as labels, buttons, sliders etc.
//!
//! Example widget uses:
//! * `ui.add(Label::new("Text").text_color(color::red));`//!
//! * `ui.add(Label::new("Text").text_color(color::red));`
//! * `if ui.add(Button::new("Click me")).clicked { ... }`
#![allow(clippy::new_without_default)]
@ -73,8 +73,8 @@ impl Label {
self.text_style(TextStyle::Small)
}
pub fn text_color(mut self, text_color: Srgba) -> Self {
self.text_color = Some(text_color);
pub fn text_color(mut self, text_color: impl Into<Srgba>) -> Self {
self.text_color = Some(text_color.into());
self
}