1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//! Design tokens and the active theme.
//!
//! The token names mirror shadcn/ui's CSS variables (`--background`, `--primary`,
//! `--muted-foreground`, …) so anyone who has themed shadcn/ui already knows them.
//! The theme lives in a GPUI [`Global`]; read it anywhere with [`ActiveTheme::theme`].
mod presets;
use gpui::{px, App, Global, Hsla, Pixels, SharedString, Window};
pub use presets::ThemePreset;
/// Light or dark appearance.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum ThemeMode {
/// Dark text on light surfaces.
#[default]
Light,
/// Light text on dark surfaces.
Dark,
}
impl ThemeMode {
/// `true` for [`ThemeMode::Dark`].
#[must_use]
pub fn is_dark(self) -> bool {
self == ThemeMode::Dark
}
/// The other mode.
#[must_use]
pub fn toggled(self) -> Self {
match self {
ThemeMode::Light => ThemeMode::Dark,
ThemeMode::Dark => ThemeMode::Light,
}
}
}
/// Color tokens, one per shadcn/ui CSS variable.
///
/// Every `*_foreground` token is the text color meant to sit on the token it is
/// named after; each pair is checked for at least 4.5:1 contrast in the tests.
#[derive(Clone, Debug, PartialEq)]
pub struct ThemeColors {
/// App background.
pub background: Hsla,
/// Default text.
pub foreground: Hsla,
/// Card surface.
pub card: Hsla,
/// Text on cards.
pub card_foreground: Hsla,
/// Floating surfaces: dialogs, tooltips' counterpart, menus.
pub popover: Hsla,
/// Text in popovers and menus.
pub popover_foreground: Hsla,
/// Main call to action.
pub primary: Hsla,
/// Text on the primary color.
pub primary_foreground: Hsla,
/// Low-emphasis filled surfaces.
pub secondary: Hsla,
/// Text on the secondary color.
pub secondary_foreground: Hsla,
/// Subdued backgrounds and secondary text.
pub muted: Hsla,
/// Secondary text: descriptions, placeholders.
pub muted_foreground: Hsla,
/// Hover and selected backgrounds.
pub accent: Hsla,
/// Text on the accent color.
pub accent_foreground: Hsla,
/// Destructive actions and errors.
pub destructive: Hsla,
/// Text on the destructive color.
pub destructive_foreground: Hsla,
/// Error text drawn directly on `background` or `card` (alerts, field errors).
pub destructive_text: Hsla,
/// Hairlines and dividers.
pub border: Hsla,
/// Form control borders.
pub input: Hsla,
/// Focus ring.
pub ring: Hsla,
/// Scrim behind modal dialogs.
pub overlay: Hsla,
}
/// The complete theme: colors plus shape and type tokens.
#[derive(Clone, Debug, PartialEq)]
pub struct Theme {
/// Human-readable name, for example "Neutral" or "Rok".
pub name: SharedString,
/// Light or dark.
pub mode: ThemeMode,
/// The color tokens.
pub colors: ThemeColors,
/// Base corner radius (shadcn's `--radius`). Components derive smaller and
/// larger radii from it, like `rounded-md` = radius − 2px.
pub radius: Pixels,
/// UI font. `.SystemUIFont` resolves to the platform's UI font.
pub font_family: SharedString,
/// Code and keyboard-shortcut font.
pub monospace_font_family: SharedString,
/// Base text size (shadcn's `text-sm` is used for most controls).
pub font_size: Pixels,
/// Which preset produced this theme, so mode toggles keep the brand.
pub preset: ThemePreset,
}
impl Global for Theme {}
impl Theme {
/// Build a theme from a preset and a mode.
#[must_use]
pub fn from_preset(preset: ThemePreset, mode: ThemeMode) -> Self {
presets::build_theme(preset, mode)
}
/// The theme currently installed in the app.
///
/// Panics if [`crate::init`] was not called.
pub fn global(cx: &App) -> &Theme {
cx.global::<Theme>()
}
/// Install `theme` and redraw every window.
pub fn set_global(theme: Theme, cx: &mut App) {
crate::sx::set_theme_snapshot(&theme);
cx.set_global(theme);
cx.refresh_windows();
}
/// Switch between light and dark, keeping the current preset and fonts.
pub fn toggle_mode(cx: &mut App) {
let current = Theme::global(cx);
let next = current.rebuilt(current.preset, current.mode.toggled());
Theme::set_global(next, cx);
}
/// Switch preset, keeping the current mode and fonts.
pub fn change_preset(preset: ThemePreset, cx: &mut App) {
let current = Theme::global(cx);
let next = current.rebuilt(preset, current.mode);
Theme::set_global(next, cx);
}
/// Use `family` for all UI text, for example a font registered with
/// [`crate::fonts`]. Preset and mode changes keep it.
///
/// ```no_run
/// # use rok_ui::prelude::*;
/// # fn example(cx: &mut App) -> gpui::Result<()> {
/// rok_ui::fonts::CAIRO.register(cx)?;
/// Theme::set_font_family(rok_ui::fonts::CAIRO.family(), cx);
/// # Ok(())
/// # }
/// ```
pub fn set_font_family(family: impl Into<SharedString>, cx: &mut App) {
let mut theme = Theme::global(cx).clone();
theme.font_family = family.into();
Theme::set_global(theme, cx);
}
/// The `preset` theme in `mode`, carrying over this theme's fonts.
fn rebuilt(&self, preset: ThemePreset, mode: ThemeMode) -> Theme {
Theme {
font_family: self.font_family.clone(),
monospace_font_family: self.monospace_font_family.clone(),
font_size: self.font_size,
..Theme::from_preset(preset, mode)
}
}
/// Follow the operating system's light or dark appearance.
pub fn sync_with_system_appearance(window: &Window, cx: &mut App) {
let mode = match window.appearance() {
gpui::WindowAppearance::Dark | gpui::WindowAppearance::VibrantDark => ThemeMode::Dark,
gpui::WindowAppearance::Light | gpui::WindowAppearance::VibrantLight => {
ThemeMode::Light
}
};
let current = Theme::global(cx);
let next = current.rebuilt(current.preset, mode);
Theme::set_global(next, cx);
}
/// `rounded-sm`: radius − 4px, never below zero.
#[must_use]
pub fn radius_small(&self) -> Pixels {
(self.radius - px(4.)).max(px(0.))
}
/// `rounded-md`: radius − 2px, never below zero.
#[must_use]
pub fn radius_medium(&self) -> Pixels {
(self.radius - px(2.)).max(px(0.))
}
/// `rounded-lg`: the base radius.
#[must_use]
pub fn radius_large(&self) -> Pixels {
self.radius
}
/// `rounded-xl`: radius + 4px (cards, dialogs).
#[must_use]
pub fn radius_extra_large(&self) -> Pixels {
self.radius + px(4.)
}
}
impl Default for Theme {
fn default() -> Self {
Theme::from_preset(ThemePreset::default(), ThemeMode::Light)
}
}
/// Read the active theme from any GPUI context: `cx.theme().colors.primary`.
pub trait ActiveTheme {
/// The active theme.
fn theme(&self) -> &Theme;
}
impl ActiveTheme for App {
fn theme(&self) -> &Theme {
Theme::global(self)
}
}
/// Install the default theme if none is set. Called by [`crate::init`].
pub fn init(cx: &mut App) {
if !cx.has_global::<Theme>() {
cx.set_global(Theme::default());
}
crate::sx::set_theme_snapshot(Theme::global(cx));
}