diff --git a/crates/ui2/src/elements/label.rs b/crates/ui2/src/elements/label.rs index accd9e2be5571d6a52b3f68d9d1fa522467ed641..e66f2538314e7d4cd626488139f9afc33004caaa 100644 --- a/crates/ui2/src/elements/label.rs +++ b/crates/ui2/src/elements/label.rs @@ -51,7 +51,6 @@ pub struct Label { label: SharedString, color: LabelColor, size: LabelSize, - highlight_indices: Vec, strikethrough: bool, } @@ -62,7 +61,6 @@ impl Label { label: label.into(), color: LabelColor::Default, size: LabelSize::Default, - highlight_indices: Vec::new(), strikethrough: false, } } @@ -77,8 +75,60 @@ impl Label { self } - pub fn with_highlights(mut self, indices: Vec) -> Self { - self.highlight_indices = indices; + pub fn set_strikethrough(mut self, strikethrough: bool) -> Self { + self.strikethrough = strikethrough; + self + } + + fn render(&mut self, _view: &mut S, cx: &mut ViewContext) -> impl Element { + let theme = theme(cx); + + div() + .when(self.strikethrough, |this| { + this.relative().child( + div() + .absolute() + .top_px() + .my_auto() + .w_full() + .h_px() + .bg(LabelColor::Hidden.hsla(cx)), + ) + }) + .text_color(self.color.hsla(cx)) + .child(self.label.clone()) + } +} + +#[derive(Element, Clone)] +pub struct HighlightedLabel { + state_type: PhantomData, + label: SharedString, + color: LabelColor, + size: LabelSize, + highlight_indices: Vec, + strikethrough: bool, +} + +impl HighlightedLabel { + pub fn new(label: impl Into, highlight_indices: Vec) -> Self { + Self { + state_type: PhantomData, + label: label.into(), + color: LabelColor::Default, + size: LabelSize::Default, + highlight_indices, + strikethrough: false, + } + } + + pub fn color(mut self, color: LabelColor) -> Self { + self.color = color; + self + } + + pub fn size(mut self, size: LabelSize) -> Self { + self.size = size; self } @@ -192,7 +242,10 @@ mod stories { .child(Story::label(cx, "Default")) .child(Label::new("Hello, world!")) .child(Story::label(cx, "Highlighted")) - .child(Label::new("Hello, world!").with_highlights(vec![0, 1, 2, 7, 8, 12])) + .child(HighlightedLabel::new( + "Hello, world!", + vec![0, 1, 2, 7, 8, 12], + )) } } }