diff --git a/crates/storybook2/src/storybook2.rs b/crates/storybook2/src/storybook2.rs index 32bc45ae5844fa5d259087bafa1f10789ce3cd16..4beca32ca18377c948d44ac8d84b65801b237a88 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 fd7693fd08209ab92199959c99422aea47122c92..05df5b49566c9c4d9f97b6d0bbd70e4c483df904 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); + }); + } +}