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 {
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;
}

View File

@ -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()
}

View File

@ -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()

View File

@ -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 {

View File

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

View File

@ -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()

View File

@ -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();
}
}