From 8dad3ad8eafb086f306253d5ded065a75cf9c3df Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Wed, 18 Oct 2023 14:19:26 -0400 Subject: [PATCH] Use `SharedString` for `List`s --- crates/ui2/src/components/context_menu.rs | 6 ++-- crates/ui2/src/components/list.rs | 36 ++++++++++++----------- crates/ui2/src/elements/avatar.rs | 6 ++-- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/crates/ui2/src/components/context_menu.rs b/crates/ui2/src/components/context_menu.rs index cd7f70576b033f2318b7dd55177080cf9f3c0852..b3ed884e64f42baf68e0835c4e16b2b1628b5b8a 100644 --- a/crates/ui2/src/components/context_menu.rs +++ b/crates/ui2/src/components/context_menu.rs @@ -3,7 +3,7 @@ use crate::{theme, v_stack, Label, List, ListEntry, ListItem, ListSeparator, Lis #[derive(Clone)] pub enum ContextMenuItem { - Header(&'static str), + Header(SharedString), Entry(Label), Separator, } @@ -19,8 +19,8 @@ impl ContextMenuItem { } } - pub fn header(label: &'static str) -> Self { - Self::Header(label) + pub fn header(label: impl Into) -> Self { + Self::Header(label.into()) } pub fn separator() -> Self { diff --git a/crates/ui2/src/components/list.rs b/crates/ui2/src/components/list.rs index d4aba32f36544ea30cd91d8b9a532033106c4dcd..28d4e59d0cc611db608447471843bab2010e734c 100644 --- a/crates/ui2/src/components/list.rs +++ b/crates/ui2/src/components/list.rs @@ -20,7 +20,7 @@ pub enum ListItemVariant { #[derive(Element, Clone)] pub struct ListHeader { state_type: PhantomData, - label: &'static str, + label: SharedString, left_icon: Option, variant: ListItemVariant, state: InteractionState, @@ -28,10 +28,10 @@ pub struct ListHeader { } impl ListHeader { - pub fn new(label: &'static str) -> Self { + pub fn new(label: impl Into) -> Self { Self { state_type: PhantomData, - label, + label: label.into(), left_icon: None, variant: ListItemVariant::default(), state: InteractionState::default(), @@ -131,7 +131,7 @@ impl ListHeader { .size(IconSize::Small) })) .child( - Label::new(self.label) + Label::new(self.label.clone()) .color(LabelColor::Muted) .size(LabelSize::Small), ), @@ -144,16 +144,16 @@ impl ListHeader { #[derive(Element, Clone)] pub struct ListSubHeader { state_type: PhantomData, - label: &'static str, + label: SharedString, left_icon: Option, variant: ListItemVariant, } impl ListSubHeader { - pub fn new(label: &'static str) -> Self { + pub fn new(label: impl Into) -> Self { Self { state_type: PhantomData, - label, + label: label.into(), left_icon: None, variant: ListItemVariant::default(), } @@ -189,7 +189,7 @@ impl ListSubHeader { .size(IconSize::Small) })) .child( - Label::new(self.label) + Label::new(self.label.clone()) .color(LabelColor::Muted) .size(LabelSize::Small), ), @@ -201,7 +201,7 @@ impl ListSubHeader { #[derive(Clone)] pub enum LeftContent { Icon(Icon), - Avatar(&'static str), + Avatar(SharedString), } #[derive(Default, PartialEq, Copy, Clone)] @@ -309,8 +309,8 @@ impl ListEntry { self } - pub fn set_left_avatar(mut self, left_avatar: &'static str) -> Self { - self.left_content = Some(LeftContent::Avatar(left_avatar)); + pub fn set_left_avatar(mut self, left_avatar: impl Into) -> Self { + self.left_content = Some(LeftContent::Avatar(left_avatar.into())); self } @@ -378,7 +378,7 @@ impl ListEntry { let system_color = SystemColor::new(); let color = ThemeColor::new(cx); - let left_content = match self.left_content { + let left_content = match self.left_content.clone() { Some(LeftContent::Icon(i)) => Some( h_stack().child( IconElement::new(i) @@ -452,7 +452,7 @@ impl ListSeparator { #[derive(Element)] pub struct List { items: Vec>, - empty_message: &'static str, + empty_message: SharedString, header: Option>, toggleable: Toggleable, } @@ -461,14 +461,14 @@ impl List { pub fn new(items: Vec>) -> Self { Self { items, - empty_message: "No items", + empty_message: "No items".into(), header: None, toggleable: Toggleable::default(), } } - pub fn empty_message(mut self, empty_message: &'static str) -> Self { - self.empty_message = empty_message; + pub fn empty_message(mut self, empty_message: impl Into) -> Self { + self.empty_message = empty_message.into(); self } @@ -491,7 +491,9 @@ impl List { let list_content = match (self.items.is_empty(), is_toggled) { (_, false) => div(), (false, _) => div().children(self.items.iter().cloned()), - (true, _) => div().child(Label::new(self.empty_message).color(LabelColor::Muted)), + (true, _) => { + div().child(Label::new(self.empty_message.clone()).color(LabelColor::Muted)) + } }; v_stack() diff --git a/crates/ui2/src/elements/avatar.rs b/crates/ui2/src/elements/avatar.rs index fa86d5f7c4586bd84db2df602b8e387d37e66e94..314e001bdeb472e3f315c37a3489346fc8b891c1 100644 --- a/crates/ui2/src/elements/avatar.rs +++ b/crates/ui2/src/elements/avatar.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use gpui3::{img, ArcCow}; +use gpui3::img; use crate::prelude::*; use crate::theme::theme; @@ -8,12 +8,12 @@ use crate::theme::theme; #[derive(Element, Clone)] pub struct Avatar { state_type: PhantomData, - src: ArcCow<'static, str>, + src: SharedString, shape: Shape, } impl Avatar { - pub fn new(src: impl Into>) -> Self { + pub fn new(src: impl Into) -> Self { Self { state_type: PhantomData, src: src.into(),