Checkpoint

This commit is contained in:
Antonio Scandurra 2023-10-22 18:25:24 +02:00
parent db6a3e1783
commit ce75be91e1
9 changed files with 111 additions and 244 deletions

View file

@ -1,8 +1,6 @@
use std::ops::Deref;
use gpui2::{
rems, AbsoluteLength, AnyElement, BorrowAppContext, Bounds, LayoutId, Pixels, WindowContext,
};
use gpui2::{rems, AbsoluteLength, WindowContext};
use crate::prelude::*;
@ -72,78 +70,3 @@ impl Default for FakeSettings {
}
impl FakeSettings {}
pub fn with_settings<E, F>(
settings: FakeSettings,
cx: &mut ViewContext<E::ViewState>,
build_child: F,
) -> WithSettings<E>
where
E: Element,
F: FnOnce(&mut ViewContext<E::ViewState>) -> E,
{
let child = cx.with_global(settings.clone(), |cx| build_child(cx));
WithSettings { settings, child }
}
pub struct WithSettings<E> {
pub(crate) settings: FakeSettings,
pub(crate) child: E,
}
impl<E> IntoAnyElement<E::ViewState> for WithSettings<E>
where
E: Element,
{
fn into_any(self) -> AnyElement<E::ViewState> {
AnyElement::new(self)
}
}
impl<E: Element> Element for WithSettings<E> {
type ViewState = E::ViewState;
type ElementState = E::ElementState;
fn id(&self) -> Option<gpui2::ElementId> {
None
}
fn initialize(
&mut self,
view_state: &mut Self::ViewState,
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
) -> 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<E::ViewState>,
) -> 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<Pixels>,
view_state: &mut Self::ViewState,
frame_state: &mut Self::ElementState,
cx: &mut ViewContext<Self::ViewState>,
) where
Self: Sized,
{
cx.with_global(self.settings.clone(), |cx| {
self.child.paint(bounds, view_state, frame_state, cx);
});
}
}

View file

@ -1,13 +1,12 @@
use gpui2::{
AnyElement, Bounds, Element, Hsla, IntoAnyElement, LayoutId, Pixels, Result, ViewContext,
WindowContext,
};
use serde::{de::Visitor, Deserialize, Deserializer};
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use gpui2::{
AnyElement, BorrowAppContext, Bounds, Element, Hsla, IntoAnyElement, LayoutId, Pixels, Result,
ViewContext, WindowContext,
};
use serde::{de::Visitor, Deserialize, Deserializer};
#[derive(Deserialize, Clone, Default, Debug)]
pub struct Theme {
pub name: String,
@ -138,7 +137,9 @@ where
E: Element,
F: FnOnce(&mut ViewContext<E::ViewState>) -> E,
{
let child = cx.with_global(theme.clone(), |cx| build_child(cx));
cx.default_global::<ThemeStack>().0.push(theme.clone());
let child = build_child(cx);
cx.default_global::<ThemeStack>().0.pop();
Themed { theme, child }
}
@ -156,6 +157,9 @@ where
}
}
#[derive(Default)]
struct ThemeStack(Vec<Theme>);
impl<E: Element> Element for Themed<E> {
type ViewState = E::ViewState;
type ElementState = E::ElementState;
@ -170,9 +174,10 @@ impl<E: Element> Element for Themed<E> {
element_state: Option<Self::ElementState>,
cx: &mut ViewContext<Self::ViewState>,
) -> Self::ElementState {
cx.with_global(self.theme.clone(), |cx| {
self.child.initialize(view_state, element_state, cx)
})
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(
@ -184,9 +189,10 @@ impl<E: Element> Element for Themed<E> {
where
Self: Sized,
{
cx.with_global(self.theme.clone(), |cx| {
self.child.layout(view_state, element_state, cx)
})
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(
@ -198,9 +204,9 @@ impl<E: Element> Element for Themed<E> {
) where
Self: Sized,
{
cx.with_global(self.theme.clone(), |cx| {
self.child.paint(bounds, view_state, frame_state, cx);
});
cx.default_global::<ThemeStack>().0.push(self.theme.clone());
self.child.paint(bounds, view_state, frame_state, cx);
cx.default_global::<ThemeStack>().0.pop();
}
}