Parameterize theme2::init to allow loading just the base theme (#3345)

This PR adds a parameter to the `theme2::init` method to indicate what
the theme-loading behavior should be.

This allows us to indicate when we want to load all of the additional
built-in user themes (like in the Zed binary and in the storybook), and
when we don't want to load the user themes (like in tests).

We're using an enum over just a `bool` here for clarity at the call
site.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2023-11-16 13:03:30 -05:00 committed by GitHub
parent 2aa7c6f2b4
commit b559bfd80f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 36 additions and 17 deletions

View File

@ -224,7 +224,7 @@ impl TestServer {
});
cx.update(|cx| {
theme::init(cx);
theme::init(theme::LoadThemes::JustBase, cx);
Project::init(&client, cx);
client::init(&client, cx);
language::init(cx);

View File

@ -456,7 +456,7 @@ mod tests {
fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
cx.update(|cx| {
let app_state = AppState::test(cx);
theme::init(cx);
theme::init(theme::LoadThemes::JustBase, cx);
language::init(cx);
editor::init(cx);
workspace::init(app_state.clone(), cx);

View File

@ -1891,6 +1891,6 @@ mod tests {
fn init_test(cx: &mut AppContext) {
let store = SettingsStore::test(cx);
cx.set_global(store);
theme::init(cx);
theme::init(theme::LoadThemes::JustBase, cx);
}
}

View File

@ -8277,7 +8277,7 @@ pub(crate) fn init_test(cx: &mut TestAppContext, f: fn(&mut AllLanguageSettingsC
cx.update(|cx| {
let store = SettingsStore::test(cx);
cx.set_global(store);
theme::init(cx);
theme::init(theme::LoadThemes::JustBase, cx);
client::init_settings(cx);
language::init(cx);
Project::init_settings(cx);

View File

@ -3179,7 +3179,7 @@ all hints should be invalidated and requeried for all of its visible excerpts"
cx.update(|cx| {
let settings_store = SettingsStore::test(cx);
cx.set_global(settings_store);
theme::init(cx);
theme::init(theme::LoadThemes::JustBase, cx);
client::init_settings(cx);
language::init(cx);
Project::init_settings(cx);

View File

@ -1763,7 +1763,7 @@ mod tests {
fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
cx.update(|cx| {
let state = AppState::test(cx);
theme::init(cx);
theme::init(theme::LoadThemes::JustBase, cx);
language::init(cx);
super::init(cx);
editor::init(cx);

View File

@ -2785,7 +2785,7 @@ mod tests {
let settings_store = SettingsStore::test(cx);
cx.set_global(settings_store);
init_settings(cx);
theme::init(cx);
theme::init(theme::LoadThemes::JustBase, cx);
language::init(cx);
editor::init_settings(cx);
crate::init((), cx);
@ -2798,7 +2798,7 @@ mod tests {
fn init_test_with_editor(cx: &mut TestAppContext) {
cx.update(|cx| {
let app_state = AppState::test(cx);
theme::init(cx);
theme::init(theme::LoadThemes::JustBase, cx);
init_settings(cx);
language::init(cx);
editor::init(cx);

View File

@ -60,7 +60,7 @@ fn main() {
.unwrap();
cx.set_global(store);
theme2::init(cx);
theme2::init(theme2::LoadThemes::All, cx);
let selector =
story_selector.unwrap_or(StorySelector::Component(ComponentStory::Workspace));

View File

@ -1126,7 +1126,7 @@ mod tests {
pub async fn init_test(cx: &mut TestAppContext) -> (Model<Project>, View<Workspace>) {
let params = cx.update(AppState::test);
cx.update(|cx| {
theme::init(cx);
theme::init(theme::LoadThemes::JustBase, cx);
Project::init_settings(cx);
language::init(cx);
});

View File

@ -100,6 +100,11 @@ impl ThemeRegistry {
.ok_or_else(|| anyhow!("theme not found: {}", name))
.cloned()
}
pub fn load_user_themes(&mut self) {
#[cfg(not(feature = "importing-themes"))]
self.insert_user_theme_familes(crate::all_user_themes());
}
}
impl Default for ThemeRegistry {
@ -110,9 +115,6 @@ impl Default for ThemeRegistry {
this.insert_theme_families([one_family()]);
#[cfg(not(feature = "importing-themes"))]
this.insert_user_theme_familes(crate::all_user_themes());
this
}
}

View File

@ -117,7 +117,7 @@ impl settings::Settings for ThemeSettings {
user_values: &[&Self::FileContent],
cx: &mut AppContext,
) -> Result<Self> {
let themes = cx.default_global::<Arc<ThemeRegistry>>();
let themes = cx.default_global::<ThemeRegistry>();
let mut this = Self {
ui_font_size: defaults.ui_font_size.unwrap_or(16.).into(),

View File

@ -31,8 +31,25 @@ pub enum Appearance {
Dark,
}
pub fn init(cx: &mut AppContext) {
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum LoadThemes {
/// Only load the base theme.
///
/// No user themes will be loaded.
JustBase,
/// Load all of the built-in themes.
All,
}
pub fn init(themes_to_load: LoadThemes, cx: &mut AppContext) {
cx.set_global(ThemeRegistry::default());
match themes_to_load {
LoadThemes::JustBase => (),
LoadThemes::All => cx.global_mut::<ThemeRegistry>().load_user_themes(),
}
ThemeSettings::register(cx);
}

View File

@ -355,7 +355,7 @@ impl AppState {
let user_store = cx.build_model(|cx| UserStore::new(client.clone(), http_client, cx));
let workspace_store = cx.build_model(|cx| WorkspaceStore::new(client.clone(), cx));
theme2::init(cx);
theme2::init(theme2::LoadThemes::JustBase, cx);
client2::init(&client, cx);
crate::init_settings(cx);

View File

@ -140,7 +140,7 @@ fn main() {
cx.set_global(client.clone());
theme::init(cx);
theme::init(theme::LoadThemes::All, cx);
project::Project::init(&client, cx);
client::init(&client, cx);
command_palette::init(cx);