Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Rename `cx.global_default_mut` to `cx.global_default`
Marshall Bowersd6bd000aa8db
diff --git a/Cargo.lock b/Cargo.lockindex 9c36a708d61..feae40d2198 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -8080,6 +8080,7 @@ dependencies = ["smallvec","strum","theme",+ "theme2","ui2","util",]diff --git a/crates/client2/src/client2.rs b/crates/client2/src/client2.rsindex 1029b1d3946..92b0fbfe57f 100644--- a/crates/client2/src/client2.rs+++ b/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(diff --git a/crates/feature_flags2/src/feature_flags2.rs b/crates/feature_flags2/src/feature_flags2.rsindex cc672d85caf..7b1c0dd4d71 100644--- a/crates/feature_flags2/src/feature_flags2.rs+++ b/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;}diff --git a/crates/gpui2/src/app.rs b/crates/gpui2/src/app.rsindex bb5a16a4dc8..6019ec0167e 100644--- a/crates/gpui2/src/app.rs+++ b/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_typediff --git a/crates/gpui2/src/interactive.rs b/crates/gpui2/src/interactive.rsindex 652ff7af0c4..ac23c915f42 100644--- a/crates/gpui2/src/interactive.rs+++ b/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()diff --git a/crates/language2/src/language_settings.rs b/crates/language2/src/language_settings.rsindex ce50f588441..26af1763530 100644--- a/crates/language2/src/language_settings.rs+++ b/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 =diff --git a/crates/project2/src/project_settings.rs b/crates/project2/src/project_settings.rsindex 155a976540c..c739d96a6fe 100644--- a/crates/project2/src/project_settings.rs+++ b/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)}diff --git a/crates/settings2/src/settings_store.rs b/crates/settings2/src/settings_store.rsindex af60fb9978c..bd3c96de69a 100644--- a/crates/settings2/src/settings_store.rs+++ b/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>whereSelf: 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_valuesdiff --git a/crates/storybook2/Cargo.toml b/crates/storybook2/Cargo.tomlindex e5bd0aea02f..76c814cd772 100644--- a/crates/storybook2/Cargo.toml+++ b/crates/storybook2/Cargo.toml@@ -24,6 +24,7 @@ simplelog = "0.9"smallvec.workspace = truestrum = { version = "0.25.0", features = ["derive"] }theme = { path = "../theme" }+theme2 = { path = "../theme2" }ui = { package = "ui2", path = "../ui2", features = ["stories"] }util = { path = "../util" }diff --git a/crates/storybook2/src/storybook2.rs b/crates/storybook2/src/storybook2.rsindex 02337cbf05e..0d85119af30 100644--- a/crates/storybook2/src/storybook2.rs+++ b/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();diff --git a/crates/terminal2/src/terminal_settings.rs b/crates/terminal2/src/terminal_settings.rsindex 867c3070baa..208c480f103 100644--- a/crates/terminal2/src/terminal_settings.rs+++ b/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)}diff --git a/crates/theme2/src/registry.rs b/crates/theme2/src/registry.rsindex 0dc4f1b9da2..bf46fa661fe 100644--- a/crates/theme2/src/registry.rs+++ b/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+ }+}diff --git a/crates/theme2/src/settings.rs b/crates/theme2/src/settings.rsindex 5251f291074..b658465ec57 100644--- a/crates/theme2/src/settings.rs+++ b/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;}}diff --git a/crates/ui2/src/theme.rs b/crates/ui2/src/theme.rsindex a95ce77cf6f..d37aa2a2bcd 100644--- a/crates/ui2/src/theme.rs+++ b/crates/ui2/src/theme.rs@@ -137,9 +137,9 @@ whereE: 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 @@ whereelement_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 @@ wherewhereSelf: 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) whereSelf: 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();}}