Fix runtime errors

Nathan Sobo created

Change summary

crates/feature_flags2/src/feature_flags2.rs |  4 ++--
crates/gpui2/src/app.rs                     |  8 +++-----
crates/gpui2/src/interactive.rs             |  6 +++---
crates/storybook2/src/storybook2.rs         |  3 +++
crates/ui2/src/lib.rs                       |  2 +-
crates/ui2/src/settings.rs                  |  6 +++++-
crates/ui2/src/theme.rs                     | 22 ++++++++++++++--------
7 files changed, 31 insertions(+), 20 deletions(-)

Detailed changes

crates/feature_flags2/src/feature_flags2.rs 🔗

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

crates/gpui2/src/app.rs 🔗

@@ -510,14 +510,12 @@ impl AppContext {
             .unwrap()
     }
 
-    pub fn default_global<G: 'static + Default + Sync + Send>(&mut self) -> &mut G {
+    pub fn default_global_mut<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
-            .insert(global_type, Box::new(G::default()));
-        self.globals_by_type
-            .get_mut(&global_type)
-            .unwrap()
+            .entry(global_type)
+            .or_insert_with(|| Box::new(G::default()))
             .downcast_mut::<G>()
             .unwrap()
     }

crates/gpui2/src/interactive.rs 🔗

@@ -603,7 +603,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::<Self>()
+        cx.default_global_mut::<Self>()
             .0
             .get(name)
             .and_then(|bounds_stack| bounds_stack.last())
@@ -611,7 +611,7 @@ impl GroupBounds {
     }
 
     pub fn push(name: SharedString, bounds: Bounds<Pixels>, cx: &mut AppContext) {
-        cx.default_global::<Self>()
+        cx.default_global_mut::<Self>()
             .0
             .entry(name)
             .or_default()
@@ -619,7 +619,7 @@ impl GroupBounds {
     }
 
     pub fn pop(name: &SharedString, cx: &mut AppContext) {
-        cx.default_global::<GroupBounds>()
+        cx.default_global_mut::<Self>()
             .0
             .get_mut(name)
             .unwrap()

crates/storybook2/src/storybook2.rs 🔗

@@ -56,6 +56,9 @@ fn main() {
         let selector =
             story_selector.unwrap_or(StorySelector::Component(ComponentStory::Workspace));
 
+        cx.set_global(theme.clone());
+        ui::settings::init(cx);
+
         let window = cx.open_window(
             WindowOptions {
                 bounds: WindowBounds::Fixed(Bounds {

crates/ui2/src/lib.rs 🔗

@@ -4,7 +4,7 @@ mod components;
 mod element_ext;
 mod elements;
 pub mod prelude;
-mod settings;
+pub mod settings;
 mod static_data;
 mod theme;
 

crates/ui2/src/settings.rs 🔗

@@ -1,9 +1,13 @@
 use std::ops::Deref;
 
-use gpui2::{rems, AbsoluteLength, WindowContext};
+use gpui2::{rems, AbsoluteLength, AppContext, WindowContext};
 
 use crate::prelude::*;
 
+pub fn init(cx: &mut AppContext) {
+    cx.set_global(FakeSettings::default());
+}
+
 /// Returns the user settings.
 pub fn user_settings(cx: &WindowContext) -> FakeSettings {
     cx.global::<FakeSettings>().clone()

crates/ui2/src/theme.rs 🔗

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