Remove themed wrapper

This commit is contained in:
Marshall Bowers 2023-10-30 14:28:25 -04:00
parent b31a004def
commit 7b4e699d0e
3 changed files with 11 additions and 101 deletions

View file

@ -18,7 +18,7 @@ use settings2::{default_settings, Settings, SettingsStore};
use simplelog::SimpleLogger; use simplelog::SimpleLogger;
use story_selector::ComponentStory; use story_selector::ComponentStory;
use theme2::{ThemeRegistry, ThemeSettings}; use theme2::{ThemeRegistry, ThemeSettings};
use ui::{prelude::*, themed}; use ui::prelude::*;
use crate::assets::Assets; use crate::assets::Assets;
use crate::story_selector::StorySelector; use crate::story_selector::StorySelector;
@ -86,7 +86,7 @@ fn main() {
}, },
move |cx| { move |cx| {
cx.build_view( cx.build_view(
|cx| StoryWrapper::new(selector.story(cx), theme), |cx| StoryWrapper::new(selector.story(cx)),
StoryWrapper::render, StoryWrapper::render,
) )
}, },
@ -99,22 +99,19 @@ fn main() {
#[derive(Clone)] #[derive(Clone)]
pub struct StoryWrapper { pub struct StoryWrapper {
story: AnyView, story: AnyView,
theme: Theme,
} }
impl StoryWrapper { impl StoryWrapper {
pub(crate) fn new(story: AnyView, theme: Theme) -> Self { pub(crate) fn new(story: AnyView) -> Self {
Self { story, theme } Self { story }
} }
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Component<Self> { fn render(&mut self, cx: &mut ViewContext<Self>) -> impl Component<Self> {
themed(self.theme.clone(), cx, |cx| {
div() div()
.flex() .flex()
.flex_col() .flex_col()
.size_full() .size_full()
.child(self.story.clone()) .child(self.story.clone())
})
} }
} }

View file

@ -5,7 +5,7 @@ pub use gpui2::{
pub use crate::elevation::*; pub use crate::elevation::*;
use crate::settings::user_settings; use crate::settings::user_settings;
pub use crate::{old_theme, theme, ButtonVariant, Theme}; pub use crate::{old_theme, theme, ButtonVariant};
use gpui2::{rems, Hsla, Rems}; use gpui2::{rems, Hsla, Rems};
use strum::EnumIter; use strum::EnumIter;

View file

@ -1,7 +1,4 @@
use gpui2::{ use gpui2::{AppContext, Hsla, Result, WindowContext};
AnyElement, AppContext, Bounds, Component, Element, Hsla, LayoutId, Pixels, Result,
ViewContext, WindowContext,
};
use serde::{de::Visitor, Deserialize, Deserializer}; use serde::{de::Visitor, Deserialize, Deserializer};
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
@ -132,90 +129,6 @@ where
deserializer.deserialize_map(SyntaxVisitor) deserializer.deserialize_map(SyntaxVisitor)
} }
pub fn themed<V, E, F>(theme: Theme, cx: &mut ViewContext<V>, build_child: F) -> Themed<E>
where
V: 'static,
E: Element<V>,
F: FnOnce(&mut ViewContext<V>) -> E,
{
cx.default_global::<ThemeStack>().0.push(theme.clone());
let child = build_child(cx);
cx.default_global::<ThemeStack>().0.pop();
Themed { theme, child }
}
pub struct Themed<E> {
pub(crate) theme: Theme,
pub(crate) child: E,
}
impl<V, E> Component<V> for Themed<E>
where
V: 'static,
E: 'static + Element<V> + Send,
E::ElementState: Send,
{
fn render(self) -> AnyElement<V> {
AnyElement::new(self)
}
}
#[derive(Default)]
struct ThemeStack(Vec<Theme>);
impl<V, E: 'static + Element<V> + Send> Element<V> for Themed<E>
where
V: 'static,
E::ElementState: Send,
{
type ElementState = E::ElementState;
fn id(&self) -> Option<gpui2::ElementId> {
None
}
fn initialize(
&mut self,
view_state: &mut V,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<V>,
) -> Self::ElementState {
cx.default_global::<ThemeStack>().0.push(self.theme.clone());
let element_state = self.child.initialize(view_state, element_state, cx);
cx.default_global::<ThemeStack>().0.pop();
element_state
}
fn layout(
&mut self,
view_state: &mut V,
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) -> LayoutId
where
Self: Sized,
{
cx.default_global::<ThemeStack>().0.push(self.theme.clone());
let layout_id = self.child.layout(view_state, element_state, cx);
cx.default_global::<ThemeStack>().0.pop();
layout_id
}
fn paint(
&mut self,
bounds: Bounds<Pixels>,
view_state: &mut V,
frame_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
) where
Self: Sized,
{
cx.default_global::<ThemeStack>().0.push(self.theme.clone());
self.child.paint(bounds, view_state, frame_state, cx);
cx.default_global::<ThemeStack>().0.pop();
}
}
pub fn old_theme(cx: &WindowContext) -> Arc<Theme> { pub fn old_theme(cx: &WindowContext) -> Arc<Theme> {
Arc::new(cx.global::<Theme>().clone()) Arc::new(cx.global::<Theme>().clone())
} }