diff --git a/crates/gpui/playground/src/element.rs b/crates/gpui/playground/src/element.rs index d5f86885ce..bf080b8770 100644 --- a/crates/gpui/playground/src/element.rs +++ b/crates/gpui/playground/src/element.rs @@ -1,3 +1,5 @@ +use std::{any::Any, sync::Arc}; + use crate::{ adapter::Adapter, style::{DefinedLength, Display, Fill, Length, Overflow, Position, Style}, @@ -24,8 +26,11 @@ pub struct PaintContext<'a, 'b, 'c, 'd, V> { } pub trait Element: 'static + Clone { + type Layout: 'static; + fn style_mut(&mut self) -> &mut Style; - fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result; + fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) + -> Result<(NodeId, Self::Layout)>; fn paint(&mut self, layout: EngineLayout, view: &mut V, cx: &mut PaintContext) -> Result<()>; @@ -36,7 +41,7 @@ pub trait Element: 'static + Clone { { AnyElement { element: Box::new(self) as Box>, - layout_node_id: None, + layout: None, } } @@ -261,7 +266,8 @@ pub trait Element: 'static + Clone { pub trait ElementObject { fn style_mut(&mut self) -> &mut Style; - fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result; + fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) + -> Result<(NodeId, Arc)>; fn paint(&mut self, layout: EngineLayout, view: &mut V, cx: &mut PaintContext) -> Result<()>; fn clone_object(&self) -> Box>; @@ -272,8 +278,14 @@ impl> ElementObject for E { self.style_mut() } - fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { - self.layout(view, cx) + fn layout( + &mut self, + view: &mut V, + cx: &mut LayoutContext, + ) -> Result<(NodeId, Arc)> { + let (node_id, layout) = self.layout(view, cx)?; + let layout = Arc::new(layout) as Arc; + Ok((node_id, layout)) } fn paint( @@ -292,24 +304,18 @@ impl> ElementObject for E { pub struct AnyElement { element: Box>, - layout_node_id: Option, + layout: Option<(NodeId, Arc)>, } -// enum LayoutState { -// None, -// Registered(NodeId, Box), -// Computed(Layout, Box), -// } - impl AnyElement { pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { - let layout_node_id = self.element.layout(view, cx)?; - self.layout_node_id = Some(layout_node_id); - Ok(layout_node_id) + let (node_id, layout) = self.element.layout(view, cx)?; + self.layout = Some((node_id, layout)); + Ok(node_id) } pub fn paint(&mut self, view: &mut V, cx: &mut PaintContext) -> Result<()> { - let layout_node_id = self.layout_node_id.expect("paint called before layout"); + let (layout_node_id, layout) = self.layout.clone().expect("paint called before layout"); let layout = cx .layout_engine() .unwrap() @@ -323,18 +329,24 @@ impl Clone for AnyElement { fn clone(&self) -> Self { Self { element: self.element.clone_object(), - layout_node_id: self.layout_node_id, + layout: self.layout.clone(), } } } impl Element for AnyElement { + type Layout = (); + fn style_mut(&mut self) -> &mut Style { self.element.style_mut() } - fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { - self.layout(view, cx) + fn layout( + &mut self, + view: &mut V, + cx: &mut LayoutContext, + ) -> Result<(NodeId, Self::Layout)> { + Ok((self.layout(view, cx)?, ())) } fn paint( diff --git a/crates/gpui/playground/src/frame.rs b/crates/gpui/playground/src/frame.rs index ef9be80c8e..b38c415d86 100644 --- a/crates/gpui/playground/src/frame.rs +++ b/crates/gpui/playground/src/frame.rs @@ -1,10 +1,9 @@ -use anyhow::{anyhow, Result}; -use gpui::{EngineLayout, LayoutNodeId}; - use crate::{ - element::{AnyElement, Element, LayoutContext, PaintContext}, + element::{AnyElement, Element, LayoutContext, NodeId, PaintContext}, style::Style, }; +use anyhow::{anyhow, Result}; +use gpui::{EngineLayout, LayoutNodeId}; pub struct Frame { style: Style, @@ -19,11 +18,17 @@ pub fn frame() -> Frame { } impl Element for Frame { + type Layout = (); + fn style_mut(&mut self) -> &mut Style { &mut self.style } - fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { + fn layout( + &mut self, + view: &mut V, + cx: &mut LayoutContext, + ) -> Result<(NodeId, Self::Layout)> { let child_layout_node_ids = self .children .iter_mut() @@ -31,9 +36,12 @@ impl Element for Frame { .collect::>>()?; let rem_size = cx.rem_pixels(); - cx.layout_engine() + let node_id = cx + .layout_engine() .ok_or_else(|| anyhow!("no layout engine"))? - .add_node(self.style.to_taffy(rem_size), child_layout_node_ids) + .add_node(self.style.to_taffy(rem_size), child_layout_node_ids)?; + + Ok((node_id, ())) } fn paint(