accents.rs

 1use gpui::Hsla;
 2use serde_derive::Deserialize;
 3
 4use crate::{AccentContent, default::default_dark_theme, try_parse_color};
 5
 6/// A collection of colors that are used to color indent aware lines in the editor.
 7#[derive(Clone, Debug, Deserialize, PartialEq)]
 8pub struct AccentColors(pub Vec<Hsla>);
 9
10impl Default for AccentColors {
11    /// Don't use this!
12    /// We have to have a default to be `[refineable::Refinable]`.
13    /// TODO "Find a way to not need this for Refinable"
14    fn default() -> Self {
15        default_dark_theme().accents().clone()
16    }
17}
18
19impl AccentColors {
20    /// Returns the color for the given index.
21    pub fn color_for_index(&self, index: u32) -> Hsla {
22        self.0[index as usize % self.0.len()]
23    }
24
25    /// Merges the given accent colors into this [`AccentColors`] instance.
26    pub fn merge(&mut self, accent_colors: &[AccentContent]) {
27        if accent_colors.is_empty() {
28            return;
29        }
30
31        let colors = accent_colors
32            .iter()
33            .filter_map(|accent_color| {
34                accent_color
35                    .0
36                    .as_ref()
37                    .and_then(|color| try_parse_color(color).ok())
38            })
39            .collect::<Vec<_>>();
40
41        if !colors.is_empty() {
42            self.0 = colors;
43        }
44    }
45}