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)]
  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
 83pub fn highlight_ranges(
 84    text: &str,
 85    indices: &[usize],
 86    style: HighlightStyle,
 87) -> Vec<(Range<usize>, HighlightStyle)> {
 88    let mut highlight_indices = indices.iter().copied().peekable();
 89    let mut highlights: Vec<(Range<usize>, HighlightStyle)> = Vec::new();
 90
 91    while let Some(start_ix) = highlight_indices.next() {
 92        let mut end_ix = start_ix;
 93
 94        loop {
 95            end_ix = end_ix + text[end_ix..].chars().next().unwrap().len_utf8();
 96            if let Some(&next_ix) = highlight_indices.peek() {
 97                if next_ix == end_ix {
 98                    end_ix = next_ix;
 99                    highlight_indices.next();
100                    continue;
101                }
102            }
103            break;
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}