diff --git a/Cargo.lock b/Cargo.lock index fa1f43f4b4..1562c4a7b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7920,6 +7920,7 @@ version = "0.1.0" dependencies = [ "anyhow", "color", + "derive_more", "fs", "gpui", "indexmap 1.9.3", diff --git a/crates/theme/Cargo.toml b/crates/theme/Cargo.toml index 9c061b226f..a4b177fb9b 100644 --- a/crates/theme/Cargo.toml +++ b/crates/theme/Cargo.toml @@ -21,6 +21,7 @@ doctest = false [dependencies] anyhow.workspace = true +derive_more.workspace = true fs = { path = "../fs" } gpui = { path = "../gpui" } indexmap = { version = "1.6.2", features = ["serde"] } diff --git a/crates/theme/src/registry.rs b/crates/theme/src/registry.rs index 1fbee2449e..c44e7c11dd 100644 --- a/crates/theme/src/registry.rs +++ b/crates/theme/src/registry.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::sync::Arc; use anyhow::{anyhow, Context, Result}; +use derive_more::{Deref, DerefMut}; use gpui::{AppContext, AssetSource, HighlightStyle, SharedString}; use refineable::Refineable; use util::ResultExt; @@ -18,6 +19,20 @@ pub struct ThemeMeta { pub appearance: Appearance, } +/// The global [`ThemeRegistry`]. +/// +/// This newtype exists for obtaining a unique [`TypeId`](std::any::TypeId) when +/// inserting the [`ThemeRegistry`] into the context as a global. +/// +/// This should not be exposed outside of this module. +#[derive(Default, Deref, DerefMut)] +struct GlobalThemeRegistry(ThemeRegistry); + +/// Initializes the theme registry. +pub fn init(assets: Box, cx: &mut AppContext) { + cx.set_global(GlobalThemeRegistry(ThemeRegistry::new(assets))); +} + pub struct ThemeRegistry { assets: Box, themes: HashMap>, @@ -26,19 +41,19 @@ pub struct ThemeRegistry { impl ThemeRegistry { /// Returns the global [`ThemeRegistry`]. pub fn global(cx: &AppContext) -> &Self { - cx.global::() + cx.global::() } /// Returns a mutable reference to the global [`ThemeRegistry`]. pub fn global_mut(cx: &mut AppContext) -> &mut Self { - cx.global_mut::() + cx.global_mut::() } /// Returns a mutable reference to the global [`ThemeRegistry`]. /// /// Inserts a default [`ThemeRegistry`] if one does not yet exist. pub fn default_global(cx: &mut AppContext) -> &mut Self { - cx.default_global::() + cx.default_global::() } pub fn new(assets: Box) -> Self { diff --git a/crates/theme/src/theme.rs b/crates/theme/src/theme.rs index 2686ffe62d..bb25133210 100644 --- a/crates/theme/src/theme.rs +++ b/crates/theme/src/theme.rs @@ -60,7 +60,7 @@ pub fn init(themes_to_load: LoadThemes, cx: &mut AppContext) { LoadThemes::JustBase => (Box::new(()) as Box, false), LoadThemes::All(assets) => (assets, true), }; - cx.set_global(ThemeRegistry::new(assets)); + registry::init(assets, cx); if load_user_themes { ThemeRegistry::global_mut(cx).load_user_themes();