pub use crate::ViewContext; use anyhow::Result; use gpui::geometry::vector::Vector2F; pub use gpui::{Layout, LayoutId}; use smallvec::SmallVec; pub trait Element: 'static + IntoElement { type PaintState; fn layout( &mut self, view: &mut V, cx: &mut ViewContext, ) -> Result<(LayoutId, Self::PaintState)> where Self: Sized; fn paint( &mut self, view: &mut V, parent_origin: Vector2F, layout: &Layout, state: &mut Self::PaintState, cx: &mut ViewContext, ) where Self: Sized; fn into_any(self) -> AnyElement where Self: 'static + Sized, { AnyElement(Box::new(StatefulElement { element: self, phase: ElementPhase::Init, })) } /// Applies a given function `then` to the current element if `condition` is true. /// This function is used to conditionally modify the element based on a given condition. /// If `condition` is false, it just returns the current element as it is. /// /// # Parameters /// - `self`: The current element /// - `condition`: The boolean condition based on which `then` is applied to the element. /// - `then`: A function that takes in the current element and returns a possibly modified element. /// /// # Return /// It returns the potentially modified element. fn when(mut self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self where Self: Sized, { if condition { self = then(self); } self } } /// 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 ViewContext) -> Result; fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext); } /// 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 ViewContext) -> 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 ViewContext) { self.phase = match std::mem::take(&mut self.phase) { ElementPhase::PostLayout { layout_id, mut paint_state, } => match cx.computed_layout(layout_id) { Ok(layout) => { self.element .paint(view, parent_origin, &layout, &mut paint_state, cx); ElementPhase::PostPaint { layout, paint_state, } } Err(error) => ElementPhase::Error(error.to_string()), }, ElementPhase::PostPaint { layout, mut paint_state, } => { self.element .paint(view, parent_origin, &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 ViewContext) -> Result { self.0.layout(view, cx) } pub fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut ViewContext) { 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 } // HACK: This is a temporary hack to get children working for the purposes // of building UI on top of the current version of gpui2. // // We'll (hopefully) be moving away from this in the future. fn children_any(mut self, children: I) -> Self where I: IntoIterator>, Self: Sized, { self.children_mut().extend(children.into_iter()); self } // HACK: This is a temporary hack to get children working for the purposes // of building UI on top of the current version of gpui2. // // We'll (hopefully) be moving away from this in the future. fn child_any(mut self, children: AnyElement) -> Self where Self: Sized, { self.children_mut().push(children); self } } pub trait IntoElement { type Element: Element; fn into_element(self) -> Self::Element; }