From a1f7a97ff5c3c53f8eafb52830bea1c38383523e Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Thu, 19 Oct 2023 13:02:32 -0400 Subject: [PATCH] Pull the settings from the global state --- crates/storybook2/src/storybook2.rs | 4 +- crates/ui2/src/settings.rs | 84 +++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/crates/storybook2/src/storybook2.rs b/crates/storybook2/src/storybook2.rs index 32bc45ae58..4beca32ca1 100644 --- a/crates/storybook2/src/storybook2.rs +++ b/crates/storybook2/src/storybook2.rs @@ -17,7 +17,7 @@ use log::LevelFilter; use simplelog::SimpleLogger; use story_selector::ComponentStory; use ui::prelude::*; -use ui::{themed, FakeSettings}; +use ui::{themed, with_settings, FakeSettings}; use crate::assets::Assets; use crate::story_selector::StorySelector; @@ -100,7 +100,7 @@ impl StoryWrapper { } fn render(&mut self, cx: &mut ViewContext) -> impl Element { - cx.with_global(self.settings.clone(), |cx| { + with_settings(self.settings.clone(), cx, |cx| { themed(self.theme.clone(), cx, |cx| { div() .flex() diff --git a/crates/ui2/src/settings.rs b/crates/ui2/src/settings.rs index fd7693fd08..05df5b4956 100644 --- a/crates/ui2/src/settings.rs +++ b/crates/ui2/src/settings.rs @@ -1,13 +1,14 @@ use std::ops::Deref; -use gpui3::{rems, AbsoluteLength, WindowContext}; +use gpui3::{ + rems, AbsoluteLength, AnyElement, BorrowAppContext, Bounds, LayoutId, Pixels, WindowContext, +}; -use crate::DisclosureControlStyle; +use crate::prelude::*; /// Returns the user settings. pub fn user_settings(cx: &WindowContext) -> FakeSettings { - // cx.global::().clone() - FakeSettings::default() + cx.global::().clone() } #[derive(Clone)] @@ -67,3 +68,78 @@ impl Default for FakeSettings { } impl FakeSettings {} + +pub fn with_settings( + settings: FakeSettings, + cx: &mut ViewContext, + build_child: F, +) -> WithSettings +where + E: Element, + F: FnOnce(&mut ViewContext) -> E, +{ + let child = cx.with_global(theme.clone(), |cx| build_child(cx)); + WithSettings { settings, child } +} + +pub struct WithSettings { + pub(crate) settings: FakeSettings, + pub(crate) child: E, +} + +impl IntoAnyElement for WithSettings +where + E: Element, +{ + fn into_any(self) -> AnyElement { + AnyElement::new(self) + } +} + +impl Element for WithSettings { + type ViewState = E::ViewState; + type ElementState = E::ElementState; + + fn id(&self) -> Option { + None + } + + fn initialize( + &mut self, + view_state: &mut Self::ViewState, + element_state: Option, + cx: &mut ViewContext, + ) -> Self::ElementState { + cx.with_global(self.settings.clone(), |cx| { + self.child.initialize(view_state, element_state, cx) + }) + } + + fn layout( + &mut self, + view_state: &mut E::ViewState, + element_state: &mut Self::ElementState, + cx: &mut ViewContext, + ) -> LayoutId + where + Self: Sized, + { + cx.with_global(self.settings.clone(), |cx| { + self.child.layout(view_state, element_state, cx) + }) + } + + fn paint( + &mut self, + bounds: Bounds, + view_state: &mut Self::ViewState, + frame_state: &mut Self::ElementState, + cx: &mut ViewContext, + ) where + Self: Sized, + { + cx.with_global(self.settings.clone(), |cx| { + self.child.paint(bounds, view_state, frame_state, cx); + }); + } +}