Rework third-party themes (#3254)

This PR reworks the way we define our third-party themes to make them
work as overlays on top of a base theme.

We introduce the concept of a `UserThemeFamily` that contains
`UserTheme`s. Rather than being an entire theme definition on their own,
a `UserTheme` just contains optional overrides for the values in a
`Theme`.

When resolving a `UserTheme`, we apply it on top of the base theme. Any
values not overridden in the `UserTheme` will fall back to the `Theme`
defaults.

Right now we are just using `UserTheme` to model third-party themes that
we distribute with the Zed binary. However, this same structure can also
be used to import arbitrary user themes (such as from a theme registry,
or even a theme blob from the settings file).

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2023-11-07 17:40:07 +01:00 committed by GitHub
parent 9582a6f317
commit 0d95410634
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 1389 additions and 6000 deletions

View file

@ -1,7 +1,14 @@
use crate::{zed_pro_family, Theme, ThemeFamily};
use std::collections::HashMap;
use std::sync::Arc;
use anyhow::{anyhow, Result};
use gpui::SharedString;
use std::{collections::HashMap, sync::Arc};
use refineable::Refineable;
use crate::{
zed_pro_family, Appearance, GitStatusColors, PlayerColors, StatusColors, SyntaxTheme,
SystemColors, Theme, ThemeColors, ThemeFamily, ThemeStyles, UserTheme, UserThemeFamily,
};
pub struct ThemeRegistry {
themes: HashMap<SharedString, Arc<Theme>>,
@ -20,6 +27,37 @@ impl ThemeRegistry {
}
}
fn insert_user_theme_familes(&mut self, families: impl IntoIterator<Item = UserThemeFamily>) {
for family in families.into_iter() {
self.insert_user_themes(family.themes);
}
}
fn insert_user_themes(&mut self, themes: impl IntoIterator<Item = UserTheme>) {
self.insert_themes(themes.into_iter().map(|user_theme| {
let mut theme_colors = match user_theme.appearance {
Appearance::Light => ThemeColors::default_light(),
Appearance::Dark => ThemeColors::default_dark(),
};
theme_colors.refine(&user_theme.styles.colors);
Theme {
id: uuid::Uuid::new_v4().to_string(),
name: user_theme.name.into(),
appearance: user_theme.appearance,
styles: ThemeStyles {
system: SystemColors::default(),
colors: theme_colors,
status: StatusColors::default(),
git: GitStatusColors::default(),
player: PlayerColors::default(),
syntax: SyntaxTheme::default_dark(),
},
}
}));
}
pub fn list_names(&self, _staff: bool) -> impl Iterator<Item = SharedString> + '_ {
self.themes.keys().cloned()
}
@ -43,7 +81,7 @@ impl Default for ThemeRegistry {
};
this.insert_theme_families([zed_pro_family()]);
this.insert_theme_families(crate::all_imported_themes());
this.insert_user_theme_familes(crate::all_imported_themes());
this
}