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