highlighted_label.rs

  1use std::ops::Range;
  2
  3use gpui::{FontWeight, HighlightStyle, StyledText};
  4
  5use crate::{LabelCommon, LabelLike, LabelSize, LineHeightStyle, prelude::*};
  6
  7#[derive(IntoElement, RegisterComponent)]
  8pub struct HighlightedLabel {
  9    base: LabelLike,
 10    label: SharedString,
 11    highlight_indices: Vec<usize>,
 12}
 13
 14impl HighlightedLabel {
 15    /// Constructs a label with the given characters highlighted.
 16    /// Characters are identified by UTF-8 byte position.
 17    pub fn new(label: impl Into<SharedString>, highlight_indices: Vec<usize>) -> Self {
 18        Self {
 19            base: LabelLike::new(),
 20            label: label.into(),
 21            highlight_indices,
 22        }
 23    }
 24}
 25
 26impl LabelCommon for HighlightedLabel {
 27    fn size(mut self, size: LabelSize) -> Self {
 28        self.base = self.base.size(size);
 29        self
 30    }
 31
 32    fn weight(mut self, weight: FontWeight) -> Self {
 33        self.base = self.base.weight(weight);
 34        self
 35    }
 36
 37    fn line_height_style(mut self, line_height_style: LineHeightStyle) -> Self {
 38        self.base = self.base.line_height_style(line_height_style);
 39        self
 40    }
 41
 42    fn color(mut self, color: Color) -> Self {
 43        self.base = self.base.color(color);
 44        self
 45    }
 46
 47    fn strikethrough(mut self) -> Self {
 48        self.base = self.base.strikethrough();
 49        self
 50    }
 51
 52    fn italic(mut self) -> Self {
 53        self.base = self.base.italic();
 54        self
 55    }
 56
 57    fn alpha(mut self, alpha: f32) -> Self {
 58        self.base = self.base.alpha(alpha);
 59        self
 60    }
 61
 62    fn underline(mut self) -> Self {
 63        self.base = self.base.underline();
 64        self
 65    }
 66
 67    fn truncate(mut self) -> Self {
 68        self.base = self.base.truncate();
 69        self
 70    }
 71
 72    fn single_line(mut self) -> Self {
 73        self.base = self.base.single_line();
 74        self
 75    }
 76
 77    fn buffer_font(mut self, cx: &App) -> Self {
 78        self.base = self.base.buffer_font(cx);
 79        self
 80    }
 81
 82    fn inline_code(mut self, cx: &App) -> Self {
 83        self.base = self.base.inline_code(cx);
 84        self
 85    }
 86}
 87
 88pub fn highlight_ranges(
 89    text: &str,
 90    indices: &[usize],
 91    style: HighlightStyle,
 92) -> Vec<(Range<usize>, HighlightStyle)> {
 93    let mut highlight_indices = indices.iter().copied().peekable();
 94    let mut highlights: Vec<(Range<usize>, HighlightStyle)> = Vec::new();
 95
 96    while let Some(start_ix) = highlight_indices.next() {
 97        let mut end_ix = start_ix;
 98
 99        loop {
100            end_ix = end_ix + text[end_ix..].chars().next().unwrap().len_utf8();
101            if let Some(&next_ix) = highlight_indices.peek()
102                && next_ix == end_ix {
103                    end_ix = next_ix;
104                    highlight_indices.next();
105                    continue;
106                }
107            break;
108        }
109
110        highlights.push((start_ix..end_ix, style));
111    }
112
113    highlights
114}
115
116impl RenderOnce for HighlightedLabel {
117    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
118        let highlight_color = cx.theme().colors().text_accent;
119
120        let highlights = highlight_ranges(
121            &self.label,
122            &self.highlight_indices,
123            HighlightStyle {
124                color: Some(highlight_color),
125                ..Default::default()
126            },
127        );
128
129        let mut text_style = window.text_style();
130        text_style.color = self.base.color.color(cx);
131
132        self.base
133            .child(StyledText::new(self.label).with_default_highlights(&text_style, highlights))
134    }
135}
136
137impl Component for HighlightedLabel {
138    fn scope() -> ComponentScope {
139        ComponentScope::Typography
140    }
141
142    fn name() -> &'static str {
143        "HighlightedLabel"
144    }
145
146    fn description() -> Option<&'static str> {
147        Some("A label with highlighted characters based on specified indices.")
148    }
149
150    fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
151        Some(
152            v_flex()
153                .gap_6()
154                .children(vec![
155                    example_group_with_title(
156                        "Basic Usage",
157                        vec![
158                            single_example(
159                                "Default",
160                                HighlightedLabel::new("Highlighted Text", vec![0, 1, 2, 3]).into_any_element(),
161                            ),
162                            single_example(
163                                "Custom Color",
164                                HighlightedLabel::new("Colored Highlight", vec![0, 1, 7, 8, 9])
165                                    .color(Color::Accent)
166                                    .into_any_element(),
167                            ),
168                        ],
169                    ),
170                    example_group_with_title(
171                        "Styles",
172                        vec![
173                            single_example(
174                                "Bold",
175                                HighlightedLabel::new("Bold Highlight", vec![0, 1, 2, 3])
176                                    .weight(FontWeight::BOLD)
177                                    .into_any_element(),
178                            ),
179                            single_example(
180                                "Italic",
181                                HighlightedLabel::new("Italic Highlight", vec![0, 1, 6, 7, 8])
182                                    .italic()
183                                    .into_any_element(),
184                            ),
185                            single_example(
186                                "Underline",
187                                HighlightedLabel::new("Underlined Highlight", vec![0, 1, 10, 11, 12])
188                                    .underline()
189                                    .into_any_element(),
190                            ),
191                        ],
192                    ),
193                    example_group_with_title(
194                        "Sizes",
195                        vec![
196                            single_example(
197                                "Small",
198                                HighlightedLabel::new("Small Highlight", vec![0, 1, 5, 6, 7])
199                                    .size(LabelSize::Small)
200                                    .into_any_element(),
201                            ),
202                            single_example(
203                                "Large",
204                                HighlightedLabel::new("Large Highlight", vec![0, 1, 5, 6, 7])
205                                    .size(LabelSize::Large)
206                                    .into_any_element(),
207                            ),
208                        ],
209                    ),
210                    example_group_with_title(
211                        "Special Cases",
212                        vec![
213                            single_example(
214                                "Single Line",
215                                HighlightedLabel::new("Single Line Highlight\nWith Newline", vec![0, 1, 7, 8, 9])
216                                    .single_line()
217                                    .into_any_element(),
218                            ),
219                            single_example(
220                                "Truncate",
221                                HighlightedLabel::new("This is a very long text that should be truncated with highlights", vec![0, 1, 2, 3, 4, 5])
222                                    .truncate()
223                                    .into_any_element(),
224                            ),
225                        ],
226                    ),
227                ])
228                .into_any_element()
229        )
230    }
231}