Avoid doing string manipulation on render for single line label (#23227)

Michael Sloan created

Release Notes:

- N/A

Change summary

crates/ui/src/components/label/label.rs | 11 ++---------
1 file changed, 2 insertions(+), 9 deletions(-)

Detailed changes

crates/ui/src/components/label/label.rs 🔗

@@ -36,7 +36,6 @@ use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle};
 pub struct Label {
     base: LabelLike,
     label: SharedString,
-    single_line: bool,
 }
 
 impl Label {
@@ -53,7 +52,6 @@ impl Label {
         Self {
             base: LabelLike::new(),
             label: label.into(),
-            single_line: false,
         }
     }
 }
@@ -170,7 +168,7 @@ impl LabelCommon for Label {
     }
 
     fn single_line(mut self) -> Self {
-        self.single_line = true;
+        self.label = SharedString::from(self.label.replace('\n', "␤"));
         self.base = self.base.single_line();
         self
     }
@@ -178,11 +176,6 @@ impl LabelCommon for Label {
 
 impl RenderOnce for Label {
     fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
-        let target_label = if self.single_line {
-            SharedString::from(self.label.replace('\n', "␤"))
-        } else {
-            self.label
-        };
-        self.base.child(target_label)
+        self.base.child(self.label)
     }
 }