Skip to main content

gpui/
colors.rs

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