From fe2ac1ec91235679fdd8f5806cfa5719d17a64b6 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 3 Dec 2024 13:31:27 +0800 Subject: [PATCH 1/2] Fix to Label single_line to ensure no wrap --- .../src/components/label/highlighted_label.rs | 5 +++ crates/ui/src/components/label/label.rs | 32 ++++++++++--------- crates/ui/src/components/label/label_like.rs | 11 +++++++ 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/crates/ui/src/components/label/highlighted_label.rs b/crates/ui/src/components/label/highlighted_label.rs index f9617139565113..0e6cc26b182729 100644 --- a/crates/ui/src/components/label/highlighted_label.rs +++ b/crates/ui/src/components/label/highlighted_label.rs @@ -65,6 +65,11 @@ impl LabelCommon for HighlightedLabel { self.base = self.base.underline(underline); self } + + fn single_line(mut self) -> Self { + self.base = self.base.single_line(); + self + } } pub fn highlight_ranges( diff --git a/crates/ui/src/components/label/label.rs b/crates/ui/src/components/label/label.rs index f65596184191c2..f4c2d5fa0ae694 100644 --- a/crates/ui/src/components/label/label.rs +++ b/crates/ui/src/components/label/label.rs @@ -56,20 +56,6 @@ impl Label { single_line: false, } } - - /// Make the label display in a single line mode - /// - /// # Examples - /// - /// ``` - /// use ui::prelude::*; - /// - /// let my_label = Label::new("Hello, World!").single_line(); - /// ``` - pub fn single_line(mut self) -> Self { - self.single_line = true; - self - } } // Style methods. @@ -177,6 +163,20 @@ impl LabelCommon for Label { self.base = self.base.underline(underline); self } + + /// Make the label display in a single line mode + /// + /// # Examples + /// + /// ``` + /// use ui::prelude::*; + /// + /// let my_label = Label::new("Hello, World!").single_line(); + /// ``` + fn single_line(mut self) -> Self { + self.single_line = true; + self + } } impl RenderOnce for Label { @@ -186,6 +186,8 @@ impl RenderOnce for Label { } else { self.label }; - self.base.child(target_label) + self.base + .child(target_label) + .when(self.single_line, |this| this.single_line()) } } diff --git a/crates/ui/src/components/label/label_like.rs b/crates/ui/src/components/label/label_like.rs index fd7303082af488..79052a16a6aec0 100644 --- a/crates/ui/src/components/label/label_like.rs +++ b/crates/ui/src/components/label/label_like.rs @@ -49,6 +49,9 @@ pub trait LabelCommon { /// Sets the alpha property of the label, overwriting the alpha value of the color. fn alpha(self, alpha: f32) -> Self; + + /// Sets the no_wrap property of the label. + fn single_line(self) -> Self; } #[derive(IntoElement)] @@ -63,6 +66,7 @@ pub struct LabelLike { children: SmallVec<[AnyElement; 2]>, alpha: Option, underline: bool, + single_line: bool, } impl Default for LabelLike { @@ -84,6 +88,7 @@ impl LabelLike { children: SmallVec::new(), alpha: None, underline: false, + single_line: false, } } } @@ -139,6 +144,11 @@ impl LabelCommon for LabelLike { self.alpha = Some(alpha); self } + + fn single_line(mut self) -> Self { + self.single_line = true; + self + } } impl ParentElement for LabelLike { @@ -178,6 +188,7 @@ impl RenderOnce for LabelLike { this }) .when(self.strikethrough, |this| this.line_through()) + .when(self.single_line, |this| this.whitespace_nowrap()) .text_color(color) .font_weight(self.weight.unwrap_or(settings.ui_font.weight)) .children(self.children) From 516cbfbfd0587ad5a49decdb65a87761a836d3ae Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Tue, 3 Dec 2024 10:19:16 -0500 Subject: [PATCH 2/2] Clean up --- crates/ui/src/components/label/label.rs | 14 ++------------ crates/ui/src/components/label/label_like.rs | 2 +- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/crates/ui/src/components/label/label.rs b/crates/ui/src/components/label/label.rs index f4c2d5fa0ae694..1df33d27403be2 100644 --- a/crates/ui/src/components/label/label.rs +++ b/crates/ui/src/components/label/label.rs @@ -164,17 +164,9 @@ impl LabelCommon for Label { self } - /// Make the label display in a single line mode - /// - /// # Examples - /// - /// ``` - /// use ui::prelude::*; - /// - /// let my_label = Label::new("Hello, World!").single_line(); - /// ``` fn single_line(mut self) -> Self { self.single_line = true; + self.base = self.base.single_line(); self } } @@ -186,8 +178,6 @@ impl RenderOnce for Label { } else { self.label }; - self.base - .child(target_label) - .when(self.single_line, |this| this.single_line()) + self.base.child(target_label) } } diff --git a/crates/ui/src/components/label/label_like.rs b/crates/ui/src/components/label/label_like.rs index 79052a16a6aec0..b1c3240f5a7287 100644 --- a/crates/ui/src/components/label/label_like.rs +++ b/crates/ui/src/components/label/label_like.rs @@ -50,7 +50,7 @@ pub trait LabelCommon { /// Sets the alpha property of the label, overwriting the alpha value of the color. fn alpha(self, alpha: f32) -> Self; - /// Sets the no_wrap property of the label. + /// Sets the label to render as a single line. fn single_line(self) -> Self; }