use crate::AppExt as _;
use gpui::{App, Global};
use crate::{
Appearance, paint,
theme::{Theme, layout},
};
impl Theme {
pub fn install(appearance: Appearance, cx: &mut App) {
let mut theme = match cx.try_global::<Palette>() {
Some(palette) => (palette.0)(appearance),
None => Self::for_appearance(appearance),
};
let brand = cx.brand();
brand.apply(&mut theme);
layout::set_base_radius(brand.radius);
Self::install_custom(theme, cx);
}
pub fn install_custom(theme: Theme, cx: &mut App) {
paint::set_current_appearance(theme.appearance);
paint::bump_generation();
cx.set_global(theme);
cx.refresh_windows();
}
pub fn of(cx: &App) -> &Theme {
cx.global::<Theme>()
}
}
struct Palette(Box<dyn Fn(Appearance) -> Theme>);
impl Global for Palette {}
pub(crate) fn set_palette(build: impl Fn(Appearance) -> Theme + 'static, cx: &mut App) {
cx.set_global(Palette(Box::new(build)));
}