Theming
One global struct controls every color and radius.
Components never hardcode colors. They read them from the Theme global through cx.theme(). Change a token and every component follows.
Tokens
| background / foreground | The window surface and default text. |
| card / card_foreground | Raised surfaces such as Card and Alert. |
| primary / primary_foreground | The main action color, used by the default Button. |
| secondary / secondary_foreground | Lower-emphasis actions. |
| muted / muted_foreground | Subtle fills and secondary text. |
| accent / accent_foreground | Hover fills for ghost and outline controls. |
| destructive / destructive_foreground | Dangerous actions and errors. |
| border / input / ring | Outlines, form borders and the focus ring. |
| radius | The base corner radius. Cards use radius × 1.5. |
Light and dark
Theme::light() and Theme::dark() ship by default. Swap the global and refresh to change modes at runtime:
use gpui::App;
use crate::theme::{ActiveTheme, Theme};
fn toggle_dark_mode(cx: &mut App) {
let dark = cx.theme().background.l < 0.5;
cx.set_global(if dark { Theme::light() } else { Theme::dark() });
cx.refresh_windows();
}Your own theme
Start from a built-in theme and override only what you need:
impl Theme {
pub fn ember() -> Self {
Self {
primary: c(18.0, 88.0, 54.0),
primary_foreground: c(0.0, 0.0, 100.0),
radius: px(12.0),
..Self::light()
}
}
}