fc_ui/capabilities/foundation/theme/
theme.rs1use gpui::*;
2
3use crate::capabilities::foundation::theme::tokens::ThemeTokens;
4
5#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum ThemeVariant {
8 Light,
10 Dark,
12 MidnightBlue,
14 ForestGrove,
16 SunsetAmber,
18 OceanBreeze,
20 Dracula,
22 Nord,
24 MonokaiPro,
26 TokyoNight,
28 CatppuccinMocha,
30 RosePine,
32 CoralReef,
34 LavenderDreams,
36 MintFresh,
38 PeachyKeen,
40 SkyBlue,
42 CherryBlossom,
44}
45
46impl ThemeVariant {
47 pub fn display_name(&self) -> &'static str {
48 match self {
49 Self::Light => "Light",
50 Self::Dark => "Dark",
51 Self::MidnightBlue => "Midnight Blue",
52 Self::ForestGrove => "Forest Grove",
53 Self::SunsetAmber => "Sunset Amber",
54 Self::OceanBreeze => "Ocean Breeze",
55 Self::Dracula => "Dracula",
56 Self::Nord => "Nord",
57 Self::MonokaiPro => "Monokai Pro",
58 Self::TokyoNight => "Tokyo Night",
59 Self::CatppuccinMocha => "Catppuccin Mocha",
60 Self::RosePine => "Rose Pine",
61 Self::CoralReef => "Coral Reef",
62 Self::LavenderDreams => "Lavender Dreams",
63 Self::MintFresh => "Mint Fresh",
64 Self::PeachyKeen => "Peachy Keen",
65 Self::SkyBlue => "Sky Blue",
66 Self::CherryBlossom => "Cherry Blossom",
67 }
68 }
69}
70
71#[derive(Clone, Debug)]
73pub struct Theme {
74 pub variant: ThemeVariant,
75 pub tokens: ThemeTokens,
76}
77
78impl Theme {
79 pub fn light() -> Self {
80 Self {
81 variant: ThemeVariant::Light,
82 tokens: ThemeTokens::light(),
83 }
84 }
85 pub fn dark() -> Self {
86 Self {
87 variant: ThemeVariant::Dark,
88 tokens: ThemeTokens::dark(),
89 }
90 }
91 pub fn midnight_blue() -> Self {
92 Self {
93 variant: ThemeVariant::MidnightBlue,
94 tokens: ThemeTokens::midnight_blue(),
95 }
96 }
97 pub fn forest_grove() -> Self {
98 Self {
99 variant: ThemeVariant::ForestGrove,
100 tokens: ThemeTokens::forest_grove(),
101 }
102 }
103 pub fn sunset_amber() -> Self {
104 Self {
105 variant: ThemeVariant::SunsetAmber,
106 tokens: ThemeTokens::sunset_amber(),
107 }
108 }
109 pub fn ocean_breeze() -> Self {
110 Self {
111 variant: ThemeVariant::OceanBreeze,
112 tokens: ThemeTokens::ocean_breeze(),
113 }
114 }
115 pub fn dracula() -> Self {
116 Self {
117 variant: ThemeVariant::Dracula,
118 tokens: ThemeTokens::dracula(),
119 }
120 }
121 pub fn nord() -> Self {
122 Self {
123 variant: ThemeVariant::Nord,
124 tokens: ThemeTokens::nord(),
125 }
126 }
127 pub fn monokai_pro() -> Self {
128 Self {
129 variant: ThemeVariant::MonokaiPro,
130 tokens: ThemeTokens::monokai_pro(),
131 }
132 }
133 pub fn tokyo_night() -> Self {
134 Self {
135 variant: ThemeVariant::TokyoNight,
136 tokens: ThemeTokens::tokyo_night(),
137 }
138 }
139 pub fn catppuccin_mocha() -> Self {
140 Self {
141 variant: ThemeVariant::CatppuccinMocha,
142 tokens: ThemeTokens::catppuccin_mocha(),
143 }
144 }
145 pub fn rose_pine() -> Self {
146 Self {
147 variant: ThemeVariant::RosePine,
148 tokens: ThemeTokens::rose_pine(),
149 }
150 }
151 pub fn coral_reef() -> Self {
152 Self {
153 variant: ThemeVariant::CoralReef,
154 tokens: ThemeTokens::coral_reef(),
155 }
156 }
157 pub fn lavender_dreams() -> Self {
158 Self {
159 variant: ThemeVariant::LavenderDreams,
160 tokens: ThemeTokens::lavender_dreams(),
161 }
162 }
163 pub fn mint_fresh() -> Self {
164 Self {
165 variant: ThemeVariant::MintFresh,
166 tokens: ThemeTokens::mint_fresh(),
167 }
168 }
169 pub fn peachy_keen() -> Self {
170 Self {
171 variant: ThemeVariant::PeachyKeen,
172 tokens: ThemeTokens::peachy_keen(),
173 }
174 }
175 pub fn sky_blue() -> Self {
176 Self {
177 variant: ThemeVariant::SkyBlue,
178 tokens: ThemeTokens::sky_blue(),
179 }
180 }
181 pub fn cherry_blossom() -> Self {
182 Self {
183 variant: ThemeVariant::CherryBlossom,
184 tokens: ThemeTokens::cherry_blossom(),
185 }
186 }
187
188 pub fn all() -> Vec<Theme> {
189 vec![
190 Self::dark(),
191 Self::light(),
192 Self::midnight_blue(),
193 Self::forest_grove(),
194 Self::sunset_amber(),
195 Self::ocean_breeze(),
196 Self::dracula(),
197 Self::nord(),
198 Self::monokai_pro(),
199 Self::tokyo_night(),
200 Self::catppuccin_mocha(),
201 Self::rose_pine(),
202 Self::coral_reef(),
203 Self::lavender_dreams(),
204 Self::mint_fresh(),
205 Self::peachy_keen(),
206 Self::sky_blue(),
207 Self::cherry_blossom(),
208 ]
209 }
210}
211
212struct ThemeState(Theme);
213
214impl Global for ThemeState {}
215
216pub fn install_theme(cx: &mut App, theme: Theme) {
221 cx.set_global(ThemeState(theme));
222 cx.refresh_windows();
223}
224
225pub fn use_theme(cx: &App) -> Theme {
228 cx.try_global::<ThemeState>()
229 .map(|state| state.0.clone())
230 .unwrap_or_else(Theme::dark)
231}
232
233#[cfg(test)]
234mod tests {
235 use super::{install_theme, use_theme, Theme, ThemeVariant};
236 use gpui::TestApp;
237
238 #[gpui::test]
239 fn themes_are_isolated_between_apps() {
240 let mut first = TestApp::new();
241 let mut second = TestApp::new();
242
243 first.update(|cx| install_theme(cx, Theme::light()));
244 second.update(|cx| install_theme(cx, Theme::dark()));
245
246 assert_eq!(first.read(|cx| use_theme(cx).variant), ThemeVariant::Light);
247 assert_eq!(second.read(|cx| use_theme(cx).variant), ThemeVariant::Dark);
248 }
249}