Fix runtime errors

This commit is contained in:
Nathan Sobo 2023-10-23 11:34:35 +02:00
parent 5247f217fd
commit da8919002f
7 changed files with 31 additions and 20 deletions

View file

@ -51,13 +51,13 @@ pub trait FeatureFlagAppExt {
impl FeatureFlagAppExt for AppContext { impl FeatureFlagAppExt for AppContext {
fn update_flags(&mut self, staff: bool, flags: Vec<String>) { 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.staff = staff;
feature_flags.flags = flags; feature_flags.flags = flags;
} }
fn set_staff(&mut self, staff: bool) { 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; feature_flags.staff = staff;
} }

View file

@ -510,14 +510,12 @@ impl AppContext {
.unwrap() .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>(); let global_type = TypeId::of::<G>();
self.push_effect(Effect::NotifyGlobalObservers { global_type }); self.push_effect(Effect::NotifyGlobalObservers { global_type });
self.globals_by_type self.globals_by_type
.insert(global_type, Box::new(G::default())); .entry(global_type)
self.globals_by_type .or_insert_with(|| Box::new(G::default()))
.get_mut(&global_type)
.unwrap()
.downcast_mut::<G>() .downcast_mut::<G>()
.unwrap() .unwrap()
} }

View file

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

View file

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

View file

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

View file

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

View file

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