ui: Add text_ellipsis method to Labels (#22118)

This PR adds a `text_ellipsis` method to `Label`s.

This can be used to truncate the text with an ellipsis without needing
to wrap the `Label` in another element.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-12-16 18:43:01 -05:00 committed by GitHub
parent ac24f074df
commit 8e71e46867
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 57 additions and 39 deletions

View file

@ -66,6 +66,11 @@ impl LabelCommon for HighlightedLabel {
self
}
fn text_ellipsis(mut self) -> Self {
self.base = self.base.text_ellipsis();
self
}
fn single_line(mut self) -> Self {
self.base = self.base.single_line();
self

View file

@ -164,6 +164,11 @@ impl LabelCommon for Label {
self
}
fn text_ellipsis(mut self) -> Self {
self.base = self.base.text_ellipsis();
self
}
fn single_line(mut self) -> Self {
self.single_line = true;
self.base = self.base.single_line();

View file

@ -50,6 +50,9 @@ pub trait LabelCommon {
/// Sets the alpha property of the label, overwriting the alpha value of the color.
fn alpha(self, alpha: f32) -> Self;
/// Truncates overflowing text with an ellipsis (`…`) if needed.
fn text_ellipsis(self) -> Self;
/// Sets the label to render as a single line.
fn single_line(self) -> Self;
}
@ -67,6 +70,7 @@ pub struct LabelLike {
alpha: Option<f32>,
underline: bool,
single_line: bool,
text_ellipsis: bool,
}
impl Default for LabelLike {
@ -89,6 +93,7 @@ impl LabelLike {
alpha: None,
underline: false,
single_line: false,
text_ellipsis: false,
}
}
}
@ -145,6 +150,11 @@ impl LabelCommon for LabelLike {
self
}
fn text_ellipsis(mut self) -> Self {
self.text_ellipsis = true;
self
}
fn single_line(mut self) -> Self {
self.single_line = true;
self
@ -189,6 +199,9 @@ impl RenderOnce for LabelLike {
})
.when(self.strikethrough, |this| this.line_through())
.when(self.single_line, |this| this.whitespace_nowrap())
.when(self.text_ellipsis, |this| {
this.overflow_x_hidden().text_ellipsis()
})
.text_color(color)
.font_weight(self.weight.unwrap_or(settings.ui_font.weight))
.children(self.children)