From 9371754942d16ea00e08c00513e52a7a196f0f46 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 28 Aug 2023 15:13:59 -0600 Subject: [PATCH] Provide themes to subtrees via context Co-Authored-By: Mikayla Maki --- crates/gpui/playground/src/div.rs | 3 +- crates/gpui/playground/src/element.rs | 15 +++- crates/gpui/playground/src/paint_context.rs | 19 ----- crates/gpui/playground/src/playground.rs | 80 ++++++++++++++------- crates/gpui/playground/src/style.rs | 32 +++++++++ crates/gpui/playground/src/themes.rs | 63 +++++++++++++++- crates/gpui/src/app.rs | 27 +------ crates/gpui/src/app/window.rs | 41 ++++++++++- 8 files changed, 206 insertions(+), 74 deletions(-) diff --git a/crates/gpui/playground/src/div.rs b/crates/gpui/playground/src/div.rs index ca71ede6d4..79d539b221 100644 --- a/crates/gpui/playground/src/div.rs +++ b/crates/gpui/playground/src/div.rs @@ -65,7 +65,8 @@ impl Element for Div { { let style = &self.computed_style(); let pop_text_style = style.text_style().map_or(false, |style| { - cx.push_text_style(cx.text_style().clone().refined(&style)); + let style = cx.text_style().clone().refined(&style); + cx.push_text_style(style); true }); style.paint_background(layout.bounds, cx); diff --git a/crates/gpui/playground/src/element.rs b/crates/gpui/playground/src/element.rs index 741ba68317..2ebe07f5fd 100644 --- a/crates/gpui/playground/src/element.rs +++ b/crates/gpui/playground/src/element.rs @@ -1,5 +1,8 @@ +use std::marker::PhantomData; + pub use crate::layout_context::LayoutContext; pub use crate::paint_context::PaintContext; +use crate::themes::{Theme, Themed}; use anyhow::Result; use gpui::geometry::vector::Vector2F; pub use gpui::{Layout, LayoutId}; @@ -34,6 +37,17 @@ pub trait Element: 'static { phase: ElementPhase::Init, })) } + + fn themed(self, theme: Theme) -> Themed + where + Self: Sized, + { + crate::themes::Themed { + child: self, + theme, + view_type: PhantomData, + } + } } /// Used to make ElementState into a trait object, so we can wrap it in AnyElement. @@ -85,7 +99,6 @@ impl> AnyStatefulElement for StatefulElement { ElementPhase::Error(message) } }; - result } diff --git a/crates/gpui/playground/src/paint_context.rs b/crates/gpui/playground/src/paint_context.rs index 9cd59143cf..0760b13cd3 100644 --- a/crates/gpui/playground/src/paint_context.rs +++ b/crates/gpui/playground/src/paint_context.rs @@ -3,7 +3,6 @@ use derive_more::{Deref, DerefMut}; pub use gpui::taffy::tree::NodeId; use gpui::{ scene::EventHandler, EventContext, Layout, LayoutId, PaintContext as LegacyPaintContext, - RenderContext, ViewContext, }; use std::{any::TypeId, rc::Rc}; @@ -15,24 +14,6 @@ pub struct PaintContext<'a, 'b, 'c, 'd, V> { pub(crate) scene: &'d mut gpui::SceneBuilder, } -impl<'a, 'b, V> RenderContext<'a, 'b, V> for PaintContext<'a, 'b, '_, '_, V> { - fn text_style(&self) -> gpui::fonts::TextStyle { - self.legacy_cx.text_style() - } - - fn push_text_style(&mut self, style: gpui::fonts::TextStyle) { - self.legacy_cx.push_text_style(style) - } - - fn pop_text_style(&mut self) { - self.legacy_cx.pop_text_style() - } - - fn as_view_context(&mut self) -> &mut ViewContext<'a, 'b, V> { - &mut self.view_context - } -} - impl<'a, 'b, 'c, 'd, V: 'static> PaintContext<'a, 'b, 'c, 'd, V> { pub fn new( legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, V>, diff --git a/crates/gpui/playground/src/playground.rs b/crates/gpui/playground/src/playground.rs index 58b9f9438f..59c9113fd4 100644 --- a/crates/gpui/playground/src/playground.rs +++ b/crates/gpui/playground/src/playground.rs @@ -1,16 +1,15 @@ #![allow(dead_code, unused_variables)] -use crate::{ - color::black, element::ParentElement, style::StyleHelpers, themes::rose_pine::RosePinePalette, -}; -use element::Element; +use crate::{element::ParentElement, style::StyleHelpers}; +use element::{Element, IntoElement}; use gpui::{ geometry::{pixels, rect::RectF, vector::vec2f}, platform::WindowOptions, ViewContext, }; use log::LevelFilter; +use playground_macros::Element; use simplelog::SimpleLogger; -use themes::{rose_pine, ThemeColors}; +use themes::{current_theme, rose_pine, Theme, ThemeColors}; use view::view; mod adapter; @@ -41,30 +40,63 @@ fn main() { center: true, ..Default::default() }, - |_| view(|cx| workspace(&rose_pine::moon(), cx)), + |_| { + view(|cx| { + playground(Theme { + colors: rose_pine::dawn(), + }) + }) + }, ); cx.platform().activate(true); }); } -fn playground(theme: &ThemeColors) -> impl Element { - use div::div; - let p = RosePinePalette::dawn(); - - div() - .text_color(black()) - .h_full() - .w_full() - .fill(p.rose) - .child(div().fill(p.pine).child(div().fill(p.love).w_6().h_3())) - .child(div().fill(p.gold).child(div().fill(p.iris).w_3().h_3())) +fn playground(theme: Theme) -> impl Element { + workspace().themed(theme) } -fn workspace(theme: &ThemeColors, cx: &mut ViewContext) -> impl Element { - use div::div; - // one line change1! - div() - .full() - .fill(theme.base(0.5)) - .child(div().h(pixels(cx.titlebar_height())).fill(theme.base(0.))) +fn workspace() -> impl Element { + WorkspaceElement +} + +use crate as playground; +#[derive(Element)] +struct WorkspaceElement; + +impl WorkspaceElement { + fn render(&mut self, _: &mut V, cx: &mut ViewContext) -> impl Element { + use div::div; + let theme = &cx.theme::().colors; + // one line change1! + div() + .full() + .flex() + .flex_col() + .fill(theme.base(0.5)) + .child(self.title_bar(cx)) + .child(self.stage(cx)) + .child(self.status_bar(cx)) + } + + fn title_bar(&mut self, cx: &mut ViewContext) -> impl IntoElement { + use div::div; + + let theme = ¤t_theme(cx).colors; + div().h(pixels(cx.titlebar_height())).fill(theme.base(0.)) + } + + fn status_bar(&mut self, cx: &mut ViewContext) -> impl IntoElement { + use div::div; + + let theme = ¤t_theme(cx).colors; + div().h(pixels(cx.titlebar_height())).fill(theme.base(0.)) + } + + fn stage(&mut self, cx: &mut ViewContext) -> impl IntoElement { + use div::div; + + let theme = ¤t_theme(cx).colors; + div().flex_grow() + } } diff --git a/crates/gpui/playground/src/style.rs b/crates/gpui/playground/src/style.rs index f01f269aa6..d2f639fdc2 100644 --- a/crates/gpui/playground/src/style.rs +++ b/crates/gpui/playground/src/style.rs @@ -327,6 +327,38 @@ pub trait StyleHelpers: Styleable