pub use crate::layout_context::LayoutContext; pub use crate::paint_context::PaintContext; use anyhow::Result; use gpui::geometry::vector::Vector2F; pub use gpui::{Layout, LayoutId}; use smallvec::SmallVec; pub trait Element: 'static { type PaintState; fn layout( &mut self, view: &mut V, cx: &mut LayoutContext, ) -> Result<(LayoutId, Self::PaintState)> where Self: Sized; fn paint( &mut self, view: &mut V, layout: &Layout, state: &mut Self::PaintState, cx: &mut PaintContext, ) where Self: Sized; fn into_any(self) -> AnyElement where Self: 'static + Sized, { AnyElement(Box::new(StatefulElement { element: self, phase: ElementPhase::Init, })) } } /// Used to make ElementState into a trait object, so we can wrap it in AnyElement. trait AnyStatefulElement { fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result; fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext); } /// A wrapper around an element that stores its layout state. struct StatefulElement> { element: E, phase: ElementPhase, } enum ElementPhase> { Init, PostLayout { layout_id: LayoutId, paint_state: E::PaintState, }, #[allow(dead_code)] PostPaint { layout: Layout, paint_state: E::PaintState, }, Error(String), } impl> std::fmt::Debug for ElementPhase { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { ElementPhase::Init => write!(f, "Init"), ElementPhase::PostLayout { layout_id, .. } => { write!(f, "PostLayout with layout id: {:?}", layout_id) } ElementPhase::PostPaint { layout, .. } => { write!(f, "PostPaint with layout: {:?}", layout) } ElementPhase::Error(err) => write!(f, "Error: {}", err), } } } impl> Default for ElementPhase { fn default() -> Self { Self::Init } } /// We blanket-implement the object-safe ElementStateObject interface to make ElementStates into trait objects impl> AnyStatefulElement for StatefulElement { fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { let result; self.phase = match self.element.layout(view, cx) { Ok((layout_id, paint_state)) => { result = Ok(layout_id); ElementPhase::PostLayout { layout_id, paint_state, } } Err(error) => { let message = error.to_string(); result = Err(error); ElementPhase::Error(message) } }; result } fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext) { self.phase = match std::mem::take(&mut self.phase) { ElementPhase::PostLayout { layout_id, mut paint_state, } => match cx.computed_layout(layout_id) { Ok(mut layout) => { layout.bounds = layout.bounds + parent_origin; self.element.paint(view, &layout, &mut paint_state, cx); ElementPhase::PostPaint { layout, paint_state, } } Err(error) => ElementPhase::Error(error.to_string()), }, ElementPhase::PostPaint { mut layout, mut paint_state, } => { layout.bounds = layout.bounds + parent_origin; self.element.paint(view, &layout, &mut paint_state, cx); ElementPhase::PostPaint { layout, paint_state, } } phase @ ElementPhase::Error(_) => phase, phase @ _ => { panic!("invalid element phase to call paint: {:?}", phase); } }; } } /// A dynamic element. pub struct AnyElement(Box>); impl AnyElement { pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result { self.0.layout(view, cx) } pub fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext) { self.0.paint(view, parent_origin, cx) } } pub trait ParentElement { fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>; fn child(mut self, child: impl IntoElement) -> Self where Self: Sized, { self.children_mut().push(child.into_element().into_any()); self } fn children(mut self, children: I) -> Self where I: IntoIterator, E: IntoElement, Self: Sized, { self.children_mut().extend( children .into_iter() .map(|child| child.into_element().into_any()), ); self } } pub trait IntoElement { type Element: Element; fn into_element(self) -> Self::Element; }