From 85f35497b6d6737a7da786b59c477d699e6a2e89 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sun, 13 Aug 2023 22:39:06 -0600 Subject: [PATCH] Compiling checkpoint --- crates/gpui/playground/src/adapter.rs | 43 ++++++++++++++++++-------- crates/gpui/playground/src/element.rs | 44 +++++++++++++++++++++++---- crates/gpui/playground/src/frame.rs | 9 ++++++ 3 files changed, 77 insertions(+), 19 deletions(-) diff --git a/crates/gpui/playground/src/adapter.rs b/crates/gpui/playground/src/adapter.rs index 5ce4fd3b60d7da3d105b4a69691c9d1223327c6c..44d830be8f4f013aca7f863ad6f6e31f5bab8f85 100644 --- a/crates/gpui/playground/src/adapter.rs +++ b/crates/gpui/playground/src/adapter.rs @@ -4,22 +4,26 @@ use util::ResultExt; use crate::element::AnyElement; -struct Adapter(AnyElement); +#[derive(Clone)] +pub struct Adapter { + view: V, + element: AnyElement, +} -impl gpui::Element for Adapter { +impl gpui::Element> for Adapter { type LayoutState = (); type PaintState = (); fn layout( &mut self, constraint: gpui::SizeConstraint, - view: &mut V, - legacy_cx: &mut gpui::LayoutContext, + view: &mut Self, + legacy_cx: &mut gpui::LayoutContext, ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { legacy_cx.push_layout_engine(); let node = self - .0 - .layout(view, &mut LayoutContext { legacy_cx }) + .element + .layout(&mut self.view, &mut LayoutContext { legacy_cx }) .log_err(); if let Some(node) = node { @@ -37,11 +41,11 @@ impl gpui::Element for Adapter { bounds: RectF, visible_bounds: RectF, layout: &mut (), - view: &mut V, - legacy_cx: &mut gpui::PaintContext, + adapter: &mut Self, + legacy_cx: &mut gpui::PaintContext, ) -> Self::PaintState { let mut cx = PaintContext { legacy_cx, scene }; - self.0.paint(view, &mut cx).log_err(); + self.element.paint(&mut adapter.view, &mut cx).log_err(); } fn rect_for_text_range( @@ -51,8 +55,8 @@ impl gpui::Element for Adapter { visible_bounds: RectF, layout: &Self::LayoutState, paint: &Self::PaintState, - view: &V, - cx: &gpui::ViewContext, + view: &Adapter, + cx: &gpui::ViewContext>, ) -> Option { todo!() } @@ -62,9 +66,22 @@ impl gpui::Element for Adapter { bounds: RectF, layout: &Self::LayoutState, paint: &Self::PaintState, - view: &V, - cx: &gpui::ViewContext, + view: &Adapter, + cx: &gpui::ViewContext>, ) -> gpui::serde_json::Value { todo!() } } + +impl gpui::Entity for Adapter { + type Event = (); +} + +impl gpui::View for Adapter +where + V: Clone, +{ + fn render(&mut self, cx: &mut gpui::ViewContext<'_, '_, Self>) -> gpui::AnyElement { + gpui::Element::into_any(self.clone()) + } +} diff --git a/crates/gpui/playground/src/element.rs b/crates/gpui/playground/src/element.rs index dee1b92acefb5faee8b6aecc4522e7234540b532..8b5fdf2d7c52a4c7072bfda02a7b52740cc3fd39 100644 --- a/crates/gpui/playground/src/element.rs +++ b/crates/gpui/playground/src/element.rs @@ -1,4 +1,7 @@ -use crate::style::{DefinedLength, Display, Overflow, Position, Style}; +use crate::{ + adapter::Adapter, + style::{DefinedLength, Display, Overflow, Position, Style}, +}; use anyhow::Result; use derive_more::{Deref, DerefMut}; use gpui::{Layout, LayoutContext as LegacyLayoutContext, PaintContext as LegacyPaintContext}; @@ -7,18 +10,18 @@ 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>, + pub(crate) legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, Adapter>, } #[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>, + pub(crate) legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, Adapter>, pub(crate) scene: &'d mut gpui::SceneBuilder, } -pub trait Element { +pub trait Element: 'static + Clone { 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 PaintContext) -> Result<()>; @@ -29,7 +32,7 @@ pub trait Element { Self: 'static + Sized, { AnyElement { - element: Box::new(self) as Box>, + element: Box::new(self) as Box>, layout_node_id: None, } } @@ -196,8 +199,28 @@ pub trait Element { } } +pub trait ElementObject { + fn clone_object(&self) -> Box>; + fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result; + fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext) -> Result<()>; +} + +impl> ElementObject for E { + fn clone_object(&self) -> Box> { + Box::new(Clone::clone(self)) + } + + fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { + self.layout(view, cx) + } + + fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext) -> Result<()> { + self.paint(layout, view, cx) + } +} + pub struct AnyElement { - element: Box>, + element: Box>, layout_node_id: Option, } @@ -218,3 +241,12 @@ impl AnyElement { self.element.paint(layout, view, cx) } } + +impl Clone for AnyElement { + fn clone(&self) -> Self { + Self { + element: self.element.clone_object(), + layout_node_id: self.layout_node_id, + } + } +} diff --git a/crates/gpui/playground/src/frame.rs b/crates/gpui/playground/src/frame.rs index 98b71fdda1a310ee536a5e66a68f92b8e0c4f673..fb74151a1856224da282abc31e8073112e3b123a 100644 --- a/crates/gpui/playground/src/frame.rs +++ b/crates/gpui/playground/src/frame.rs @@ -43,3 +43,12 @@ impl Element for Frame { Ok(()) } } + +impl Clone for Frame { + fn clone(&self) -> Self { + Self { + style: self.style.clone(), + children: self.children.clone(), + } + } +}