Add a newtype wrapper around the global ThemeRegistry
(#7025)
This PR adds a newtype wrapper around the global `ThemeRegistry`. This allows us to limit where the `ThemeRegistry` can be accessed directly via `cx.global` et al., without going through the dedicated methods on the `ThemeRegistry`. Release Notes: - N/A
This commit is contained in:
parent
7656096814
commit
e69e6f5586
4 changed files with 21 additions and 4 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -7920,6 +7920,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"color",
|
"color",
|
||||||
|
"derive_more",
|
||||||
"fs",
|
"fs",
|
||||||
"gpui",
|
"gpui",
|
||||||
"indexmap 1.9.3",
|
"indexmap 1.9.3",
|
||||||
|
|
|
@ -21,6 +21,7 @@ doctest = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
|
derive_more.workspace = true
|
||||||
fs = { path = "../fs" }
|
fs = { path = "../fs" }
|
||||||
gpui = { path = "../gpui" }
|
gpui = { path = "../gpui" }
|
||||||
indexmap = { version = "1.6.2", features = ["serde"] }
|
indexmap = { version = "1.6.2", features = ["serde"] }
|
||||||
|
|
|
@ -2,6 +2,7 @@ use std::collections::HashMap;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
use derive_more::{Deref, DerefMut};
|
||||||
use gpui::{AppContext, AssetSource, HighlightStyle, SharedString};
|
use gpui::{AppContext, AssetSource, HighlightStyle, SharedString};
|
||||||
use refineable::Refineable;
|
use refineable::Refineable;
|
||||||
use util::ResultExt;
|
use util::ResultExt;
|
||||||
|
@ -18,6 +19,20 @@ pub struct ThemeMeta {
|
||||||
pub appearance: Appearance,
|
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<dyn AssetSource>, cx: &mut AppContext) {
|
||||||
|
cx.set_global(GlobalThemeRegistry(ThemeRegistry::new(assets)));
|
||||||
|
}
|
||||||
|
|
||||||
pub struct ThemeRegistry {
|
pub struct ThemeRegistry {
|
||||||
assets: Box<dyn AssetSource>,
|
assets: Box<dyn AssetSource>,
|
||||||
themes: HashMap<SharedString, Arc<Theme>>,
|
themes: HashMap<SharedString, Arc<Theme>>,
|
||||||
|
@ -26,19 +41,19 @@ pub struct ThemeRegistry {
|
||||||
impl ThemeRegistry {
|
impl ThemeRegistry {
|
||||||
/// Returns the global [`ThemeRegistry`].
|
/// Returns the global [`ThemeRegistry`].
|
||||||
pub fn global(cx: &AppContext) -> &Self {
|
pub fn global(cx: &AppContext) -> &Self {
|
||||||
cx.global::<ThemeRegistry>()
|
cx.global::<GlobalThemeRegistry>()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the global [`ThemeRegistry`].
|
/// Returns a mutable reference to the global [`ThemeRegistry`].
|
||||||
pub fn global_mut(cx: &mut AppContext) -> &mut Self {
|
pub fn global_mut(cx: &mut AppContext) -> &mut Self {
|
||||||
cx.global_mut::<ThemeRegistry>()
|
cx.global_mut::<GlobalThemeRegistry>()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the global [`ThemeRegistry`].
|
/// Returns a mutable reference to the global [`ThemeRegistry`].
|
||||||
///
|
///
|
||||||
/// Inserts a default [`ThemeRegistry`] if one does not yet exist.
|
/// Inserts a default [`ThemeRegistry`] if one does not yet exist.
|
||||||
pub fn default_global(cx: &mut AppContext) -> &mut Self {
|
pub fn default_global(cx: &mut AppContext) -> &mut Self {
|
||||||
cx.default_global::<ThemeRegistry>()
|
cx.default_global::<GlobalThemeRegistry>()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(assets: Box<dyn AssetSource>) -> Self {
|
pub fn new(assets: Box<dyn AssetSource>) -> Self {
|
||||||
|
|
|
@ -60,7 +60,7 @@ pub fn init(themes_to_load: LoadThemes, cx: &mut AppContext) {
|
||||||
LoadThemes::JustBase => (Box::new(()) as Box<dyn AssetSource>, false),
|
LoadThemes::JustBase => (Box::new(()) as Box<dyn AssetSource>, false),
|
||||||
LoadThemes::All(assets) => (assets, true),
|
LoadThemes::All(assets) => (assets, true),
|
||||||
};
|
};
|
||||||
cx.set_global(ThemeRegistry::new(assets));
|
registry::init(assets, cx);
|
||||||
|
|
||||||
if load_user_themes {
|
if load_user_themes {
|
||||||
ThemeRegistry::global_mut(cx).load_user_themes();
|
ThemeRegistry::global_mut(cx).load_user_themes();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue