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 += text[end_ix..].chars().next().map_or(0, |c| c.len_utf8());
101 if highlight_indices.next_if(|&ix| ix == end_ix).is_none() {
102 break;
103 }
104 }
105
106 highlights.push((start_ix..end_ix, style));
107 }
108
109 highlights
110}
111
112impl RenderOnce for HighlightedLabel {
113 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
114 let highlight_color = cx.theme().colors().text_accent;
115
116 let highlights = highlight_ranges(
117 &self.label,
118 &self.highlight_indices,
119 HighlightStyle {
120 color: Some(highlight_color),
121 ..Default::default()
122 },
123 );
124
125 let mut text_style = window.text_style();
126 text_style.color = self.base.color.color(cx);
127
128 self.base
129 .child(StyledText::new(self.label).with_default_highlights(&text_style, highlights))
130 }
131}
132
133impl Component for HighlightedLabel {
134 fn scope() -> ComponentScope {
135 ComponentScope::Typography
136 }
137
138 fn name() -> &'static str {
139 "HighlightedLabel"
140 }
141
142 fn description() -> Option<&'static str> {
143 Some("A label with highlighted characters based on specified indices.")
144 }
145
146 fn preview(_window: &mut Window, _cx: &mut App) -> Option<AnyElement> {
147 Some(
148 v_flex()
149 .gap_6()
150 .children(vec![
151 example_group_with_title(
152 "Basic Usage",
153 vec![
154 single_example(
155 "Default",
156 HighlightedLabel::new("Highlighted Text", vec![0, 1, 2, 3]).into_any_element(),
157 ),
158 single_example(
159 "Custom Color",
160 HighlightedLabel::new("Colored Highlight", vec![0, 1, 7, 8, 9])
161 .color(Color::Accent)
162 .into_any_element(),
163 ),
164 ],
165 ),
166 example_group_with_title(
167 "Styles",
168 vec![
169 single_example(
170 "Bold",
171 HighlightedLabel::new("Bold Highlight", vec![0, 1, 2, 3])
172 .weight(FontWeight::BOLD)
173 .into_any_element(),
174 ),
175 single_example(
176 "Italic",
177 HighlightedLabel::new("Italic Highlight", vec![0, 1, 6, 7, 8])
178 .italic()
179 .into_any_element(),
180 ),
181 single_example(
182 "Underline",
183 HighlightedLabel::new("Underlined Highlight", vec![0, 1, 10, 11, 12])
184 .underline()
185 .into_any_element(),
186 ),
187 ],
188 ),
189 example_group_with_title(
190 "Sizes",
191 vec![
192 single_example(
193 "Small",
194 HighlightedLabel::new("Small Highlight", vec![0, 1, 5, 6, 7])
195 .size(LabelSize::Small)
196 .into_any_element(),
197 ),
198 single_example(
199 "Large",
200 HighlightedLabel::new("Large Highlight", vec![0, 1, 5, 6, 7])
201 .size(LabelSize::Large)
202 .into_any_element(),
203 ),
204 ],
205 ),
206 example_group_with_title(
207 "Special Cases",
208 vec![
209 single_example(
210 "Single Line",
211 HighlightedLabel::new("Single Line Highlight\nWith Newline", vec![0, 1, 7, 8, 9])
212 .single_line()
213 .into_any_element(),
214 ),
215 single_example(
216 "Truncate",
217 HighlightedLabel::new("This is a very long text that should be truncated with highlights", vec![0, 1, 2, 3, 4, 5])
218 .truncate()
219 .into_any_element(),
220 ),
221 ],
222 ),
223 ])
224 .into_any_element()
225 )
226 }
227}