#![deny(missing_docs)]
mod default_colors;
mod fallback_themes;
mod font_family_cache;
mod icon_theme;
mod icon_theme_schema;
mod registry;
mod scale;
mod schema;
mod styles;
mod theme_settings_provider;
mod ui_density;
use std::sync::Arc;
use gpui::BorrowAppContext;
use gpui::Global;
use gpui::{
App, AssetSource, Hsla, Pixels, SharedString, WindowAppearance, WindowBackgroundAppearance, px,
};
use serde::Deserialize;
pub use crate::default_colors::*;
pub use crate::fallback_themes::{apply_status_color_defaults, apply_theme_color_defaults};
pub use crate::font_family_cache::*;
pub use crate::icon_theme::*;
pub use crate::icon_theme_schema::*;
pub use crate::registry::*;
pub use crate::scale::*;
pub use crate::schema::*;
pub use crate::styles::*;
pub use crate::theme_settings_provider::*;
pub use crate::ui_density::*;
pub const DEFAULT_DARK_THEME: &str = "One Dark";
pub const CLIENT_SIDE_DECORATION_ROUNDING: Pixels = px(10.0);
pub const CLIENT_SIDE_DECORATION_SHADOW: Pixels = px(10.0);
#[derive(Debug, PartialEq, Clone, Copy, Deserialize)]
pub enum Appearance {
Light,
Dark,
}
impl Appearance {
pub fn is_light(&self) -> bool {
match self {
Self::Light => true,
Self::Dark => false,
}
}
}
impl From<WindowAppearance> for Appearance {
fn from(value: WindowAppearance) -> Self {
match value {
WindowAppearance::Dark | WindowAppearance::VibrantDark => Self::Dark,
WindowAppearance::Light | WindowAppearance::VibrantLight => Self::Light,
}
}
}
pub enum LoadThemes {
JustBase,
All(Box<dyn AssetSource>),
}
pub fn init(themes_to_load: LoadThemes, cx: &mut App) {
SystemAppearance::init(cx);
let assets = match themes_to_load {
LoadThemes::JustBase => Box::new(()) as Box<dyn AssetSource>,
LoadThemes::All(assets) => assets,
};
ThemeRegistry::set_global(assets, cx);
FontFamilyCache::init_global(cx);
let themes = ThemeRegistry::default_global(cx);
let theme = themes.get(DEFAULT_DARK_THEME).unwrap_or_else(|_| {
themes
.list()
.into_iter()
.next()
.map(|m| themes.get(&m.name).unwrap())
.unwrap()
});
let icon_theme = themes.default_icon_theme().unwrap();
cx.set_global(GlobalTheme { theme, icon_theme });
}
pub trait ActiveTheme {
fn theme(&self) -> &Arc<Theme>;
}
impl ActiveTheme for App {
fn theme(&self) -> &Arc<Theme> {
GlobalTheme::theme(self)
}
}
#[derive(Debug, Clone, Copy)]
pub struct SystemAppearance(pub Appearance);
impl std::ops::Deref for SystemAppearance {
type Target = Appearance;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Default for SystemAppearance {
fn default() -> Self {
Self(Appearance::Dark)
}
}
#[derive(Default)]
struct GlobalSystemAppearance(SystemAppearance);
impl std::ops::DerefMut for GlobalSystemAppearance {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl std::ops::Deref for GlobalSystemAppearance {
type Target = SystemAppearance;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Global for GlobalSystemAppearance {}
impl SystemAppearance {
pub fn init(cx: &mut App) {
*cx.default_global::<GlobalSystemAppearance>() =
GlobalSystemAppearance(SystemAppearance(cx.window_appearance().into()));
}
pub fn global(cx: &App) -> Self {
cx.global::<GlobalSystemAppearance>().0
}
pub fn global_mut(cx: &mut App) -> &mut Self {
cx.global_mut::<GlobalSystemAppearance>()
}
}
pub struct ThemeFamily {
pub id: String,
pub name: SharedString,
pub author: SharedString,
pub themes: Vec<Theme>,
pub scales: ColorScales,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Theme {
pub id: String,
pub name: SharedString,
pub appearance: Appearance,
pub styles: ThemeStyles,
}
impl Theme {
#[inline(always)]
pub fn system(&self) -> &SystemColors {
&self.styles.system
}
#[inline(always)]
pub fn accents(&self) -> &AccentColors {
&self.styles.accents
}
#[inline(always)]
pub fn players(&self) -> &PlayerColors {
&self.styles.player
}
#[inline(always)]
pub fn colors(&self) -> &ThemeColors {
&self.styles.colors
}
#[inline(always)]
pub fn syntax(&self) -> &Arc<SyntaxTheme> {
&self.styles.syntax
}
#[inline(always)]
pub fn status(&self) -> &StatusColors {
&self.styles.status
}
#[inline(always)]
pub fn appearance(&self) -> Appearance {
self.appearance
}
#[inline(always)]
pub fn window_background_appearance(&self) -> WindowBackgroundAppearance {
self.styles.window_background_appearance
}
pub fn darken(&self, color: Hsla, light_amount: f32, dark_amount: f32) -> Hsla {
let amount = match self.appearance {
Appearance::Light => light_amount,
Appearance::Dark => dark_amount,
};
let mut hsla = color;
hsla.l = (hsla.l - amount).max(0.0);
hsla
}
}
pub fn deserialize_icon_theme(bytes: &[u8]) -> anyhow::Result<IconThemeFamilyContent> {
let icon_theme_family: IconThemeFamilyContent = serde_json_lenient::from_slice(bytes)?;
Ok(icon_theme_family)
}
pub struct GlobalTheme {
theme: Arc<Theme>,
icon_theme: Arc<IconTheme>,
}
impl Global for GlobalTheme {}
impl GlobalTheme {
pub fn new(theme: Arc<Theme>, icon_theme: Arc<IconTheme>) -> Self {
Self { theme, icon_theme }
}
pub fn update_theme(cx: &mut App, theme: Arc<Theme>) {
cx.update_global::<Self, _>(|this, _| this.theme = theme);
}
pub fn update_icon_theme(cx: &mut App, icon_theme: Arc<IconTheme>) {
cx.update_global::<Self, _>(|this, _| this.icon_theme = icon_theme);
}
pub fn theme(cx: &App) -> &Arc<Theme> {
&cx.global::<Self>().theme
}
pub fn icon_theme(cx: &App) -> &Arc<IconTheme> {
&cx.global::<Self>().icon_theme
}
}
pub fn set_appearance(appearance: Appearance, cx: &mut App) {
*SystemAppearance::global_mut(cx) = SystemAppearance(appearance);
let theme = match appearance {
Appearance::Light => crate::fallback_themes::default_light(),
Appearance::Dark => crate::fallback_themes::default_dark(),
};
GlobalTheme::update_theme(cx, Arc::new(theme));
}