highlighted_label.rs

  1use std::ops::Range;
  2
  3use gpui::{FontWeight, HighlightStyle, StyledText};
  4
  5use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle};
  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, strikethrough: bool) -> Self {
 48        self.base = self.base.strikethrough(strikethrough);
 49        self
 50    }
 51
 52    fn italic(mut self, italic: bool) -> Self {
 53        self.base = self.base.italic(italic);
 54        self
 55    }
 56}
 57
 58pub fn highlight_ranges(
 59    text: &str,
 60    indices: &Vec<usize>,
 61    style: HighlightStyle,
 62) -> Vec<(Range<usize>, HighlightStyle)> {
 63    let mut highlight_indices = indices.iter().copied().peekable();
 64    let mut highlights: Vec<(Range<usize>, HighlightStyle)> = Vec::new();
 65
 66    while let Some(start_ix) = highlight_indices.next() {
 67        let mut end_ix = start_ix;
 68
 69        loop {
 70            end_ix = end_ix + text[end_ix..].chars().next().unwrap().len_utf8();
 71            if let Some(&next_ix) = highlight_indices.peek() {
 72                if next_ix == end_ix {
 73                    end_ix = next_ix;
 74                    highlight_indices.next();
 75                    continue;
 76                }
 77            }
 78            break;
 79        }
 80
 81        highlights.push((start_ix..end_ix, style));
 82    }
 83
 84    highlights
 85}
 86
 87impl RenderOnce for HighlightedLabel {
 88    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
 89        let highlight_color = cx.theme().colors().text_accent;
 90
 91        let highlights = highlight_ranges(
 92            &self.label,
 93            &self.highlight_indices,
 94            HighlightStyle {
 95                color: Some(highlight_color),
 96                ..Default::default()
 97            },
 98        );
 99
100        let mut text_style = cx.text_style();
101        text_style.color = self.base.color.color(cx);
102
103        self.base
104            .child(StyledText::new(self.label).with_highlights(&text_style, highlights))
105    }
106}