gpui: Add Global marker trait (#7095)

This should prevent a class of bugs where one queries the wrong type of
global, which results in oddities at runtime.

Release Notes:

- N/A

---------

Co-authored-by: Marshall <marshall@zed.dev>
Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
Piotr Osiewicz 2024-01-30 20:08:20 +01:00 committed by GitHub
parent 7bfa584eb6
commit e6ebe7974d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
59 changed files with 449 additions and 237 deletions

View file

@ -6,7 +6,7 @@ use anyhow::{anyhow, Context, Result};
use derive_more::{Deref, DerefMut};
use fs::Fs;
use futures::StreamExt;
use gpui::{AppContext, AssetSource, HighlightStyle, SharedString};
use gpui::{AppContext, AssetSource, Global, HighlightStyle, SharedString};
use parking_lot::RwLock;
use refineable::Refineable;
use util::ResultExt;
@ -32,10 +32,7 @@ pub struct ThemeMeta {
#[derive(Default, Deref, DerefMut)]
struct GlobalThemeRegistry(Arc<ThemeRegistry>);
/// Initializes the theme registry.
pub fn init(assets: Box<dyn AssetSource>, cx: &mut AppContext) {
cx.set_global(GlobalThemeRegistry(Arc::new(ThemeRegistry::new(assets))));
}
impl Global for GlobalThemeRegistry {}
struct ThemeRegistryState {
themes: HashMap<SharedString, Arc<Theme>>,
@ -59,6 +56,11 @@ impl ThemeRegistry {
cx.default_global::<GlobalThemeRegistry>().0.clone()
}
/// Sets the global [`ThemeRegistry`].
pub(crate) fn set_global(assets: Box<dyn AssetSource>, cx: &mut AppContext) {
cx.set_global(GlobalThemeRegistry(Arc::new(ThemeRegistry::new(assets))));
}
pub fn new(assets: Box<dyn AssetSource>) -> Self {
let registry = Self {
state: RwLock::new(ThemeRegistryState {

View file

@ -2,7 +2,8 @@ use crate::one_themes::one_dark;
use crate::{SyntaxTheme, Theme, ThemeRegistry, ThemeStyleContent};
use anyhow::Result;
use gpui::{
px, AppContext, Font, FontFeatures, FontStyle, FontWeight, Pixels, Subscription, ViewContext,
px, AppContext, Font, FontFeatures, FontStyle, FontWeight, Global, Pixels, Subscription,
ViewContext,
};
use refineable::Refineable;
use schemars::{
@ -34,6 +35,8 @@ pub struct ThemeSettings {
#[derive(Default)]
pub(crate) struct AdjustedBufferFontSize(Pixels);
impl Global for AdjustedBufferFontSize {}
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct ThemeSettingsContent {
#[serde(default)]

View file

@ -60,7 +60,7 @@ pub fn init(themes_to_load: LoadThemes, cx: &mut AppContext) {
LoadThemes::JustBase => (Box::new(()) as Box<dyn AssetSource>, false),
LoadThemes::All(assets) => (assets, true),
};
registry::init(assets, cx);
ThemeRegistry::set_global(assets, cx);
if load_user_themes {
ThemeRegistry::global(cx).load_bundled_themes();