common.rs

  1use crate::{Hsla, Rgba, WindowAppearance, rgb};
  2
  3/// The appearance of the base GPUI colors, used to style GPUI elements
  4///
  5/// Varies based on the system's current [`WindowAppearance`].
  6#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
  7pub enum DefaultThemeAppearance {
  8    /// Use the set of colors for light appearances.
  9    #[default]
 10    Light,
 11    /// Use the set of colors for dark appearances.
 12    Dark,
 13}
 14
 15impl From<WindowAppearance> for DefaultThemeAppearance {
 16    fn from(appearance: WindowAppearance) -> Self {
 17        match appearance {
 18            WindowAppearance::Light | WindowAppearance::VibrantLight => Self::Light,
 19            WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::Dark,
 20        }
 21    }
 22}
 23
 24/// Returns the default colors for the given appearance.
 25pub fn colors(appearance: DefaultThemeAppearance) -> DefaultColors {
 26    match appearance {
 27        DefaultThemeAppearance::Light => DefaultColors::light(),
 28        DefaultThemeAppearance::Dark => DefaultColors::dark(),
 29    }
 30}
 31
 32/// A collection of colors.
 33#[derive(Debug, Clone, Copy, PartialEq)]
 34pub struct DefaultColors {
 35    text: Rgba,
 36    selected_text: Rgba,
 37    background: Rgba,
 38    disabled: Rgba,
 39    selected: Rgba,
 40    border: Rgba,
 41    separator: Rgba,
 42    container: Rgba,
 43}
 44
 45impl DefaultColors {
 46    /// Returns the default dark colors.
 47    pub fn dark() -> Self {
 48        Self {
 49            text: rgb(0xffffff),
 50            selected_text: rgb(0xffffff),
 51            disabled: rgb(0x565656),
 52            selected: rgb(0x2457ca),
 53            background: rgb(0x222222),
 54            border: rgb(0x000000),
 55            separator: rgb(0xd9d9d9),
 56            container: rgb(0x262626),
 57        }
 58    }
 59
 60    /// Returns the default light colors.
 61    pub fn light() -> Self {
 62        Self {
 63            text: rgb(0x252525),
 64            selected_text: rgb(0xffffff),
 65            background: rgb(0xffffff),
 66            disabled: rgb(0xb0b0b0),
 67            selected: rgb(0x2a63d9),
 68            border: rgb(0xd9d9d9),
 69            separator: rgb(0xe6e6e6),
 70            container: rgb(0xf4f5f5),
 71        }
 72    }
 73}
 74
 75/// A default GPUI color.
 76#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::EnumIter)]
 77pub enum DefaultColor {
 78    /// Text color
 79    Text,
 80    /// Selected text color
 81    SelectedText,
 82    /// Background color
 83    Background,
 84    /// Disabled color
 85    Disabled,
 86    /// Selected color
 87    Selected,
 88    /// Border color
 89    Border,
 90    /// Separator color
 91    Separator,
 92    /// Container color
 93    Container,
 94}
 95
 96impl DefaultColor {
 97    /// Returns the RGBA color for the given color type.
 98    pub fn color(&self, colors: &DefaultColors) -> Rgba {
 99        match self {
100            DefaultColor::Text => colors.text,
101            DefaultColor::SelectedText => colors.selected_text,
102            DefaultColor::Background => colors.background,
103            DefaultColor::Disabled => colors.disabled,
104            DefaultColor::Selected => colors.selected,
105            DefaultColor::Border => colors.border,
106            DefaultColor::Separator => colors.separator,
107            DefaultColor::Container => colors.container,
108        }
109    }
110
111    /// Returns the HSLA color for the given color type.
112    pub fn hsla(&self, colors: &DefaultColors) -> Hsla {
113        self.color(colors).into()
114    }
115}