From 625e4a1bd007ea3a1c743c275fe994d49b76a35e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sun, 13 Aug 2023 21:57:38 -0600 Subject: [PATCH] Introduce new PaintContext and LayoutContext --- crates/gpui/playground/src/adapter.rs | 17 ++++++++++++----- crates/gpui/playground/src/element.rs | 19 ++++++++++++++++--- crates/gpui/playground/src/frame.rs | 15 +++------------ 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/crates/gpui/playground/src/adapter.rs b/crates/gpui/playground/src/adapter.rs index a37a5a6261a83b7c6c1b1c27e7fcfccb9fb68baa..0743a817f7bcf279e7cf50b0a63f46075c6324f8 100644 --- a/crates/gpui/playground/src/adapter.rs +++ b/crates/gpui/playground/src/adapter.rs @@ -1,3 +1,4 @@ +use crate::element::LayoutContext; use util::ResultExt; use crate::element::AnyElement; @@ -12,16 +13,22 @@ impl gpui::Element for Adapter { &mut self, constraint: gpui::SizeConstraint, view: &mut V, - cx: &mut gpui::LayoutContext, + legacy_cx: &mut gpui::LayoutContext, ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { - cx.push_layout_engine(); - if let Some(node) = self.0.layout(view, cx).log_err() { - cx.layout_engine() + legacy_cx.push_layout_engine(); + let node = self + .0 + .layout(view, &mut LayoutContext { legacy_cx }) + .log_err(); + + if let Some(node) = node { + legacy_cx + .layout_engine() .unwrap() .compute_layout(node, constraint.max) .log_err(); } - cx.pop_layout_engine(); + legacy_cx.pop_layout_engine(); (constraint.max, ()) } diff --git a/crates/gpui/playground/src/element.rs b/crates/gpui/playground/src/element.rs index 9ed6926366899cc686aae7aefa12aa1b0af5fda1..2355ff64f4a4e12d0967b58d61d1418b29a0c3ae 100644 --- a/crates/gpui/playground/src/element.rs +++ b/crates/gpui/playground/src/element.rs @@ -1,14 +1,27 @@ use crate::style::{DefinedLength, Display, Overflow, Position, Style}; use anyhow::Result; -use gpui::{Layout, LayoutContext, PaintContext}; +use derive_more::{Deref, DerefMut}; +use gpui::{Layout, LayoutContext as LegacyLayoutContext, PaintContext as LegacyPaintContext}; use playground_macros::tailwind_lengths; pub use taffy::tree::NodeId; +#[derive(Deref, DerefMut)] +pub struct LayoutContext<'a, 'b, 'c, 'd, V> { + pub(crate) legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, V>, +} + +#[derive(Deref, DerefMut)] +pub struct PaintContext<'a, 'b, 'c, 'd, V> { + #[deref] + #[deref_mut] + pub(crate) legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, V>, + scene: &'d mut gpui::SceneBuilder, +} + pub trait Element { fn style_mut(&mut self) -> &mut Style; fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result; - fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut gpui::PaintContext) - -> Result<()>; + fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext) -> Result<()>; /// Convert to a dynamically-typed element suitable for layout and paint. fn into_any(self) -> AnyElement diff --git a/crates/gpui/playground/src/frame.rs b/crates/gpui/playground/src/frame.rs index 30ec69dc2065126c55052aba8b59ff70de473568..98b71fdda1a310ee536a5e66a68f92b8e0c4f673 100644 --- a/crates/gpui/playground/src/frame.rs +++ b/crates/gpui/playground/src/frame.rs @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result}; use gpui::{Layout, LayoutNodeId}; use crate::{ - element::{AnyElement, Element}, + element::{AnyElement, Element, LayoutContext, PaintContext}, style::Style, }; @@ -23,11 +23,7 @@ impl Element for Frame { &mut self.style } - fn layout( - &mut self, - view: &mut V, - cx: &mut gpui::LayoutContext, - ) -> Result { + fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { let child_layout_node_ids = self .children .iter_mut() @@ -40,12 +36,7 @@ impl Element for Frame { .add_node(self.style.to_taffy(rem_size), child_layout_node_ids) } - fn paint( - &mut self, - layout: Layout, - view: &mut V, - cx: &mut gpui::PaintContext, - ) -> Result<()> { + fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext) -> Result<()> { for child in &mut self.children { child.paint(view, cx)?; }