Rename `cx.global_default_mut` to `cx.global_default`

Marshall Bowers created

Change summary

Cargo.lock                                  |  1 
crates/client2/src/client2.rs               |  2 
crates/feature_flags2/src/feature_flags2.rs |  4 +-
crates/gpui2/src/app.rs                     |  2 
crates/gpui2/src/interactive.rs             |  6 ++--
crates/language2/src/language_settings.rs   |  2 
crates/project2/src/project_settings.rs     |  2 
crates/settings2/src/settings_store.rs      | 22 +++++++++---------
crates/storybook2/Cargo.toml                |  1 
crates/storybook2/src/storybook2.rs         |  1 
crates/terminal2/src/terminal_settings.rs   |  2 
crates/theme2/src/registry.rs               | 27 +++++++++++-----------
crates/theme2/src/settings.rs               | 14 +++++-----
crates/ui2/src/theme.rs                     | 16 ++++++------
14 files changed, 52 insertions(+), 50 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -8080,6 +8080,7 @@ dependencies = [
  "smallvec",
  "strum",
  "theme",
+ "theme2",
  "ui2",
  "util",
 ]

crates/client2/src/client2.rs 🔗

@@ -379,7 +379,7 @@ impl settings2::Setting for TelemetrySettings {
     fn load(
         default_value: &Self::FileContent,
         user_values: &[&Self::FileContent],
-        _: &AppContext,
+        _: &mut AppContext,
     ) -> Result<Self> {
         Ok(Self {
             diagnostics: user_values.first().and_then(|v| v.diagnostics).unwrap_or(

crates/feature_flags2/src/feature_flags2.rs 🔗

@@ -52,13 +52,13 @@ pub trait FeatureFlagAppExt {
 
 impl FeatureFlagAppExt for AppContext {
     fn update_flags(&mut self, staff: bool, flags: Vec<String>) {
-        let feature_flags = self.default_global_mut::<FeatureFlags>();
+        let feature_flags = self.default_global::<FeatureFlags>();
         feature_flags.staff = staff;
         feature_flags.flags = flags;
     }
 
     fn set_staff(&mut self, staff: bool) {
-        let feature_flags = self.default_global_mut::<FeatureFlags>();
+        let feature_flags = self.default_global::<FeatureFlags>();
         feature_flags.staff = staff;
     }
 

crates/gpui2/src/app.rs 🔗

@@ -554,7 +554,7 @@ impl AppContext {
             .unwrap()
     }
 
-    pub fn default_global_mut<G: 'static + Default + Sync + Send>(&mut self) -> &mut G {
+    pub fn default_global<G: 'static + Default + Sync + Send>(&mut self) -> &mut G {
         let global_type = TypeId::of::<G>();
         self.push_effect(Effect::NotifyGlobalObservers { global_type });
         self.globals_by_type

crates/gpui2/src/interactive.rs 🔗

@@ -764,7 +764,7 @@ pub struct GroupBounds(HashMap<SharedString, SmallVec<[Bounds<Pixels>; 1]>>);
 
 impl GroupBounds {
     pub fn get(name: &SharedString, cx: &mut AppContext) -> Option<Bounds<Pixels>> {
-        cx.default_global_mut::<Self>()
+        cx.default_global::<Self>()
             .0
             .get(name)
             .and_then(|bounds_stack| bounds_stack.last())
@@ -772,7 +772,7 @@ impl GroupBounds {
     }
 
     pub fn push(name: SharedString, bounds: Bounds<Pixels>, cx: &mut AppContext) {
-        cx.default_global_mut::<Self>()
+        cx.default_global::<Self>()
             .0
             .entry(name)
             .or_default()
@@ -780,7 +780,7 @@ impl GroupBounds {
     }
 
     pub fn pop(name: &SharedString, cx: &mut AppContext) {
-        cx.default_global_mut::<Self>()
+        cx.default_global::<Self>()
             .0
             .get_mut(name)
             .unwrap()

crates/language2/src/language_settings.rs 🔗

@@ -262,7 +262,7 @@ impl settings2::Setting for AllLanguageSettings {
     fn load(
         default_value: &Self::FileContent,
         user_settings: &[&Self::FileContent],
-        _: &AppContext,
+        _: &mut AppContext,
     ) -> Result<Self> {
         // A default is provided for all settings.
         let mut defaults: LanguageSettings =

crates/project2/src/project_settings.rs 🔗

@@ -41,7 +41,7 @@ impl Setting for ProjectSettings {
     fn load(
         default_value: &Self::FileContent,
         user_values: &[&Self::FileContent],
-        _: &AppContext,
+        _: &mut AppContext,
     ) -> anyhow::Result<Self> {
         Self::load_via_json_merge(default_value, user_values)
     }

crates/settings2/src/settings_store.rs 🔗

@@ -35,7 +35,7 @@ pub trait Setting: 'static + Send + Sync {
     fn load(
         default_value: &Self::FileContent,
         user_values: &[&Self::FileContent],
-        cx: &AppContext,
+        cx: &mut AppContext,
     ) -> Result<Self>
     where
         Self: Sized;
@@ -121,7 +121,7 @@ trait AnySettingValue: 'static + Send + Sync {
         &self,
         default_value: &DeserializedSetting,
         custom: &[DeserializedSetting],
-        cx: &AppContext,
+        cx: &mut AppContext,
     ) -> Result<Box<dyn Any>>;
     fn value_for_path(&self, path: Option<(usize, &Path)>) -> &dyn Any;
     fn set_global_value(&mut self, value: Box<dyn Any>);
@@ -138,7 +138,7 @@ struct DeserializedSetting(Box<dyn Any>);
 
 impl SettingsStore {
     /// Add a new type of setting to the store.
-    pub fn register_setting<T: Setting>(&mut self, cx: &AppContext) {
+    pub fn register_setting<T: Setting>(&mut self, cx: &mut AppContext) {
         let setting_type_id = TypeId::of::<T>();
         let entry = self.setting_values.entry(setting_type_id);
         if matches!(entry, hash_map::Entry::Occupied(_)) {
@@ -205,7 +205,7 @@ impl SettingsStore {
     }
 
     #[cfg(any(test, feature = "test-support"))]
-    pub fn test(cx: &AppContext) -> Self {
+    pub fn test(cx: &mut AppContext) -> Self {
         let mut this = Self::default();
         this.set_default_settings(&crate::test_settings(), cx)
             .unwrap();
@@ -220,7 +220,7 @@ impl SettingsStore {
     #[cfg(any(test, feature = "test-support"))]
     pub fn update_user_settings<T: Setting>(
         &mut self,
-        cx: &AppContext,
+        cx: &mut AppContext,
         update: impl FnOnce(&mut T::FileContent),
     ) {
         let old_text = serde_json::to_string(&self.raw_user_settings).unwrap();
@@ -317,7 +317,7 @@ impl SettingsStore {
     pub fn set_default_settings(
         &mut self,
         default_settings_content: &str,
-        cx: &AppContext,
+        cx: &mut AppContext,
     ) -> Result<()> {
         let settings: serde_json::Value = parse_json_with_comments(default_settings_content)?;
         if settings.is_object() {
@@ -333,7 +333,7 @@ impl SettingsStore {
     pub fn set_user_settings(
         &mut self,
         user_settings_content: &str,
-        cx: &AppContext,
+        cx: &mut AppContext,
     ) -> Result<()> {
         let settings: serde_json::Value = parse_json_with_comments(user_settings_content)?;
         if settings.is_object() {
@@ -351,7 +351,7 @@ impl SettingsStore {
         root_id: usize,
         path: Arc<Path>,
         settings_content: Option<&str>,
-        cx: &AppContext,
+        cx: &mut AppContext,
     ) -> Result<()> {
         if let Some(content) = settings_content {
             self.raw_local_settings
@@ -364,7 +364,7 @@ impl SettingsStore {
     }
 
     /// Add or remove a set of local settings via a JSON string.
-    pub fn clear_local_settings(&mut self, root_id: usize, cx: &AppContext) -> Result<()> {
+    pub fn clear_local_settings(&mut self, root_id: usize, cx: &mut AppContext) -> Result<()> {
         self.raw_local_settings.retain(|k, _| k.0 != root_id);
         self.recompute_values(Some((root_id, "".as_ref())), cx)?;
         Ok(())
@@ -456,7 +456,7 @@ impl SettingsStore {
     fn recompute_values(
         &mut self,
         changed_local_path: Option<(usize, &Path)>,
-        cx: &AppContext,
+        cx: &mut AppContext,
     ) -> Result<()> {
         // Reload the global and local values for every setting.
         let mut user_settings_stack = Vec::<DeserializedSetting>::new();
@@ -557,7 +557,7 @@ impl<T: Setting> AnySettingValue for SettingValue<T> {
         &self,
         default_value: &DeserializedSetting,
         user_values: &[DeserializedSetting],
-        cx: &AppContext,
+        cx: &mut AppContext,
     ) -> Result<Box<dyn Any>> {
         let default_value = default_value.0.downcast_ref::<T::FileContent>().unwrap();
         let values: SmallVec<[&T::FileContent; 6]> = user_values

crates/storybook2/Cargo.toml 🔗

@@ -24,6 +24,7 @@ simplelog = "0.9"
 smallvec.workspace = true
 strum = { version = "0.25.0", features = ["derive"] }
 theme = { path = "../theme" }
+theme2 = { path = "../theme2" }
 ui = { package = "ui2", path = "../ui2", features = ["stories"] }
 util = { path = "../util" }
 

crates/storybook2/src/storybook2.rs 🔗

@@ -47,7 +47,6 @@ fn main() {
     let args = Args::parse();
 
     let story_selector = args.story.clone();
-
     let theme_name = args.theme.unwrap_or("One Dark".to_string());
     let theme = themes::load_theme(theme_name).unwrap();
 

crates/terminal2/src/terminal_settings.rs 🔗

@@ -106,7 +106,7 @@ impl settings2::Setting for TerminalSettings {
     fn load(
         default_value: &Self::FileContent,
         user_values: &[&Self::FileContent],
-        _: &AppContext,
+        _: &mut AppContext,
     ) -> anyhow::Result<Self> {
         Self::load_via_json_merge(default_value, user_values)
     }

crates/theme2/src/registry.rs 🔗

@@ -8,16 +8,6 @@ pub struct ThemeRegistry {
 }
 
 impl ThemeRegistry {
-    pub fn new() -> Self {
-        let mut this = Self {
-            themes: HashMap::default(),
-        };
-
-        this.insert_themes([one_dark()]);
-
-        this
-    }
-
     fn insert_themes(&mut self, themes: impl IntoIterator<Item = Theme>) {
         for theme in themes.into_iter() {
             self.themes
@@ -33,11 +23,22 @@ impl ThemeRegistry {
         self.themes.values().map(|theme| theme.metadata.clone())
     }
 
-    pub fn get(&self, name: impl Into<SharedString>) -> Result<Arc<Theme>> {
-        let name = name.into();
+    pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
         self.themes
-            .get(&name)
+            .get(name)
             .ok_or_else(|| anyhow!("theme not found: {}", name))
             .cloned()
     }
 }
+
+impl Default for ThemeRegistry {
+    fn default() -> Self {
+        let mut this = Self {
+            themes: HashMap::default(),
+        };
+
+        this.insert_themes([one_dark()]);
+
+        this
+    }
+}

crates/theme2/src/settings.rs 🔗

@@ -62,7 +62,7 @@ impl BufferLineHeight {
 impl ThemeSettings {
     pub fn buffer_font_size(&self, cx: &mut AppContext) -> Pixels {
         let font_size = *cx
-            .default_global_mut::<AdjustedBufferFontSize>()
+            .default_global::<AdjustedBufferFontSize>()
             .0
             .get_or_insert(self.buffer_font_size.into());
         font_size.max(MIN_FONT_SIZE)
@@ -74,7 +74,7 @@ impl ThemeSettings {
 }
 
 pub fn adjusted_font_size(size: Pixels, cx: &mut AppContext) -> Pixels {
-    if let Some(adjusted_size) = cx.default_global_mut::<AdjustedBufferFontSize>().0 {
+    if let Some(adjusted_size) = cx.default_global::<AdjustedBufferFontSize>().0 {
         let buffer_font_size = settings2::get::<ThemeSettings>(cx).buffer_font_size;
         let delta = adjusted_size - buffer_font_size;
         size + delta
@@ -87,7 +87,7 @@ pub fn adjusted_font_size(size: Pixels, cx: &mut AppContext) -> Pixels {
 pub fn adjust_font_size(cx: &mut AppContext, f: fn(&mut Pixels)) {
     let buffer_font_size = settings2::get::<ThemeSettings>(cx).buffer_font_size;
     let adjusted_size = cx
-        .default_global_mut::<AdjustedBufferFontSize>()
+        .default_global::<AdjustedBufferFontSize>()
         .0
         .get_or_insert(buffer_font_size);
     f(adjusted_size);
@@ -110,9 +110,9 @@ impl settings2::Setting for ThemeSettings {
     fn load(
         defaults: &Self::FileContent,
         user_values: &[&Self::FileContent],
-        cx: &AppContext,
+        cx: &mut AppContext,
     ) -> Result<Self> {
-        let themes = cx.global::<Arc<ThemeRegistry>>();
+        let themes = cx.default_global::<Arc<ThemeRegistry>>();
 
         let mut this = Self {
             buffer_font: Font {
@@ -123,7 +123,7 @@ impl settings2::Setting for ThemeSettings {
             },
             buffer_font_size: defaults.buffer_font_size.unwrap().into(),
             buffer_line_height: defaults.buffer_line_height.unwrap(),
-            theme: themes.get(defaults.theme.as_ref().unwrap().clone()).unwrap(),
+            theme: themes.get(defaults.theme.as_ref().unwrap()).unwrap(),
         };
 
         for value in user_values.into_iter().copied().cloned() {
@@ -135,7 +135,7 @@ impl settings2::Setting for ThemeSettings {
             }
 
             if let Some(value) = &value.theme {
-                if let Some(theme) = themes.get(value.clone()).log_err() {
+                if let Some(theme) = themes.get(value).log_err() {
                     this.theme = theme;
                 }
             }

crates/ui2/src/theme.rs 🔗

@@ -137,9 +137,9 @@ where
     E: Element,
     F: FnOnce(&mut ViewContext<E::ViewState>) -> E,
 {
-    cx.default_global_mut::<ThemeStack>().0.push(theme.clone());
+    cx.default_global::<ThemeStack>().0.push(theme.clone());
     let child = build_child(cx);
-    cx.default_global_mut::<ThemeStack>().0.pop();
+    cx.default_global::<ThemeStack>().0.pop();
     Themed { theme, child }
 }
 
@@ -178,11 +178,11 @@ where
         element_state: Option<Self::ElementState>,
         cx: &mut ViewContext<Self::ViewState>,
     ) -> Self::ElementState {
-        cx.default_global_mut::<ThemeStack>()
+        cx.default_global::<ThemeStack>()
             .0
             .push(self.theme.clone());
         let element_state = self.child.initialize(view_state, element_state, cx);
-        cx.default_global_mut::<ThemeStack>().0.pop();
+        cx.default_global::<ThemeStack>().0.pop();
         element_state
     }
 
@@ -195,11 +195,11 @@ where
     where
         Self: Sized,
     {
-        cx.default_global_mut::<ThemeStack>()
+        cx.default_global::<ThemeStack>()
             .0
             .push(self.theme.clone());
         let layout_id = self.child.layout(view_state, element_state, cx);
-        cx.default_global_mut::<ThemeStack>().0.pop();
+        cx.default_global::<ThemeStack>().0.pop();
         layout_id
     }
 
@@ -212,11 +212,11 @@ where
     ) where
         Self: Sized,
     {
-        cx.default_global_mut::<ThemeStack>()
+        cx.default_global::<ThemeStack>()
             .0
             .push(self.theme.clone());
         self.child.paint(bounds, view_state, frame_state, cx);
-        cx.default_global_mut::<ThemeStack>().0.pop();
+        cx.default_global::<ThemeStack>().0.pop();
     }
 }