Use `SharedString` for `Palette` and `PaletteItem`s

Marshall Bowers created

Change summary

crates/ui2/src/components/palette.rs         | 51 +++++++++++----------
crates/ui2/src/components/recent_projects.rs | 18 ++++---
2 files changed, 37 insertions(+), 32 deletions(-)

Detailed changes

crates/ui2/src/components/palette.rs 🔗

@@ -8,8 +8,8 @@ use crate::{h_stack, v_stack, Keybinding, Label, LabelColor};
 pub struct Palette<S: 'static + Send + Sync + Clone> {
     state_type: PhantomData<S>,
     scroll_state: ScrollState,
-    input_placeholder: &'static str,
-    empty_string: &'static str,
+    input_placeholder: SharedString,
+    empty_string: SharedString,
     items: Vec<PaletteItem<S>>,
     default_order: OrderMethod,
 }
@@ -19,8 +19,8 @@ impl<S: 'static + Send + Sync + Clone> Palette<S> {
         Self {
             state_type: PhantomData,
             scroll_state,
-            input_placeholder: "Find something...",
-            empty_string: "No items found.",
+            input_placeholder: "Find something...".into(),
+            empty_string: "No items found.".into(),
             items: vec![],
             default_order: OrderMethod::default(),
         }
@@ -31,13 +31,13 @@ impl<S: 'static + Send + Sync + Clone> Palette<S> {
         self
     }
 
-    pub fn placeholder(mut self, input_placeholder: &'static str) -> Self {
-        self.input_placeholder = input_placeholder;
+    pub fn placeholder(mut self, input_placeholder: impl Into<SharedString>) -> Self {
+        self.input_placeholder = input_placeholder.into();
         self
     }
 
-    pub fn empty_string(mut self, empty_string: &'static str) -> Self {
-        self.empty_string = empty_string;
+    pub fn empty_string(mut self, empty_string: impl Into<SharedString>) -> Self {
+        self.empty_string = empty_string.into();
         self
     }
 
@@ -59,11 +59,9 @@ impl<S: 'static + Send + Sync + Clone> Palette<S> {
             .child(
                 v_stack()
                     .gap_px()
-                    .child(v_stack().py_0p5().px_1().child(
-                        div().px_2().py_0p5().child(
-                            Label::new(self.input_placeholder).color(LabelColor::Placeholder),
-                        ),
-                    ))
+                    .child(v_stack().py_0p5().px_1().child(div().px_2().py_0p5().child(
+                        Label::new(self.input_placeholder.clone()).color(LabelColor::Placeholder),
+                    )))
                     .child(div().h_px().w_full().bg(theme.lowest.base.default.border))
                     .child(
                         v_stack()
@@ -74,9 +72,12 @@ impl<S: 'static + Send + Sync + Clone> Palette<S> {
                             .overflow_y_scroll(self.scroll_state.clone())
                             .children(
                                 vec![if self.items.is_empty() {
-                                    Some(h_stack().justify_between().px_2().py_1().child(
-                                        Label::new(self.empty_string).color(LabelColor::Muted),
-                                    ))
+                                    Some(
+                                        h_stack().justify_between().px_2().py_1().child(
+                                            Label::new(self.empty_string.clone())
+                                                .color(LabelColor::Muted),
+                                        ),
+                                    )
                                 } else {
                                     None
                                 }]
@@ -101,26 +102,26 @@ impl<S: 'static + Send + Sync + Clone> Palette<S> {
 
 #[derive(Element, Clone)]
 pub struct PaletteItem<S: 'static + Send + Sync + Clone> {
-    pub label: &'static str,
-    pub sublabel: Option<&'static str>,
+    pub label: SharedString,
+    pub sublabel: Option<SharedString>,
     pub keybinding: Option<Keybinding<S>>,
 }
 
 impl<S: 'static + Send + Sync + Clone> PaletteItem<S> {
-    pub fn new(label: &'static str) -> Self {
+    pub fn new(label: impl Into<SharedString>) -> Self {
         Self {
-            label,
+            label: label.into(),
             sublabel: None,
             keybinding: None,
         }
     }
 
-    pub fn label(mut self, label: &'static str) -> Self {
-        self.label = label;
+    pub fn label(mut self, label: impl Into<SharedString>) -> Self {
+        self.label = label.into();
         self
     }
 
-    pub fn sublabel<L: Into<Option<&'static str>>>(mut self, sublabel: L) -> Self {
+    pub fn sublabel(mut self, sublabel: impl Into<Option<SharedString>>) -> Self {
         self.sublabel = sublabel.into();
         self
     }
@@ -143,8 +144,8 @@ impl<S: 'static + Send + Sync + Clone> PaletteItem<S> {
             .justify_between()
             .child(
                 v_stack()
-                    .child(Label::new(self.label))
-                    .children(self.sublabel.map(|sublabel| Label::new(sublabel))),
+                    .child(Label::new(self.label.clone()))
+                    .children(self.sublabel.clone().map(|sublabel| Label::new(sublabel))),
             )
             .children(self.keybinding.clone())
     }

crates/ui2/src/components/recent_projects.rs 🔗

@@ -21,12 +21,12 @@ impl<S: 'static + Send + Sync + Clone> RecentProjects<S> {
         div().child(
             Palette::new(self.scroll_state.clone())
                 .items(vec![
-                    PaletteItem::new("zed").sublabel("~/projects/zed"),
-                    PaletteItem::new("saga").sublabel("~/projects/saga"),
-                    PaletteItem::new("journal").sublabel("~/journal"),
-                    PaletteItem::new("dotfiles").sublabel("~/dotfiles"),
-                    PaletteItem::new("zed.dev").sublabel("~/projects/zed.dev"),
-                    PaletteItem::new("laminar").sublabel("~/projects/laminar"),
+                    PaletteItem::new("zed").sublabel(SharedString::from("~/projects/zed")),
+                    PaletteItem::new("saga").sublabel(SharedString::from("~/projects/saga")),
+                    PaletteItem::new("journal").sublabel(SharedString::from("~/journal")),
+                    PaletteItem::new("dotfiles").sublabel(SharedString::from("~/dotfiles")),
+                    PaletteItem::new("zed.dev").sublabel(SharedString::from("~/projects/zed.dev")),
+                    PaletteItem::new("laminar").sublabel(SharedString::from("~/projects/laminar")),
                 ])
                 .placeholder("Recent Projects...")
                 .empty_string("No matches")
@@ -56,7 +56,11 @@ mod stories {
             }
         }
 
-        fn render(&mut self, _view: &mut S, cx: &mut ViewContext<S>) -> impl Element<ViewState = S> {
+        fn render(
+            &mut self,
+            _view: &mut S,
+            cx: &mut ViewContext<S>,
+        ) -> impl Element<ViewState = S> {
             Story::container(cx)
                 .child(Story::title_for::<_, RecentProjects<S>>(cx))
                 .child(Story::label(cx, "Default"))