Use SharedString for Palette and PaletteItems

This commit is contained in:
Marshall Bowers 2023-10-18 14:03:13 -04:00
parent 8890636a56
commit 856d23626f
2 changed files with 37 additions and 32 deletions

View file

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

View file

@ -21,12 +21,12 @@ impl<S: 'static + Send + Sync + Clone> RecentProjects<S> {
div().child( div().child(
Palette::new(self.scroll_state.clone()) Palette::new(self.scroll_state.clone())
.items(vec![ .items(vec![
PaletteItem::new("zed").sublabel("~/projects/zed"), PaletteItem::new("zed").sublabel(SharedString::from("~/projects/zed")),
PaletteItem::new("saga").sublabel("~/projects/saga"), PaletteItem::new("saga").sublabel(SharedString::from("~/projects/saga")),
PaletteItem::new("journal").sublabel("~/journal"), PaletteItem::new("journal").sublabel(SharedString::from("~/journal")),
PaletteItem::new("dotfiles").sublabel("~/dotfiles"), PaletteItem::new("dotfiles").sublabel(SharedString::from("~/dotfiles")),
PaletteItem::new("zed.dev").sublabel("~/projects/zed.dev"), PaletteItem::new("zed.dev").sublabel(SharedString::from("~/projects/zed.dev")),
PaletteItem::new("laminar").sublabel("~/projects/laminar"), PaletteItem::new("laminar").sublabel(SharedString::from("~/projects/laminar")),
]) ])
.placeholder("Recent Projects...") .placeholder("Recent Projects...")
.empty_string("No matches") .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) Story::container(cx)
.child(Story::title_for::<_, RecentProjects<S>>(cx)) .child(Story::title_for::<_, RecentProjects<S>>(cx))
.child(Story::label(cx, "Default")) .child(Story::label(cx, "Default"))