use crate::{ AvailableSpace, BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, Size, ViewContext, }; use derive_more::{Deref, DerefMut}; pub(crate) use smallvec::SmallVec; use std::{any::Any, fmt::Debug}; pub trait Element: 'static + Sized { type State: 'static; fn element_id(&self) -> Option; fn layout( &mut self, view_state: &mut V, element_state: Option, cx: &mut ViewContext, ) -> (LayoutId, Self::State); fn paint( self, bounds: Bounds, view_state: &mut V, element_state: &mut Self::State, cx: &mut ViewContext, ); fn into_any(self) -> AnyElement { AnyElement::new(self) } fn draw( self, origin: Point, available_space: Size, view_state: &mut V, cx: &mut ViewContext, f: impl FnOnce(&mut Self::State, &mut ViewContext) -> R, ) -> R where T: Clone + Default + Debug + Into, { let element_id = self.element_id(); let element = DrawableElement { element: Some(self), phase: ElementDrawPhase::Start, }; let frame_state = element.draw(origin, available_space.map(Into::into), view_state, cx); if let Some(mut frame_state) = frame_state { f(&mut frame_state, cx) } else { cx.with_element_state(element_id.unwrap(), |element_state, cx| { let mut element_state = element_state.unwrap(); let result = f(&mut element_state, cx); (result, element_state) }) } } } #[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)] pub struct GlobalElementId(SmallVec<[ElementId; 32]>); pub trait ParentComponent { fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>; fn child(mut self, child: impl Component) -> Self where Self: Sized, { self.children_mut().push(child.render()); self } fn children(mut self, iter: impl IntoIterator>) -> Self where Self: Sized, { self.children_mut() .extend(iter.into_iter().map(|item| item.render())); self } } trait ElementObject { fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext) -> LayoutId; fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext); fn measure( &mut self, available_space: Size, view_state: &mut V, cx: &mut ViewContext, ) -> Size; fn draw( &mut self, origin: Point, available_space: Size, view_state: &mut V, cx: &mut ViewContext, ); } pub struct DrawableElement> { element: Option, phase: ElementDrawPhase, } #[derive(Default)] enum ElementDrawPhase { #[default] Start, LayoutRequested { layout_id: LayoutId, frame_state: Option, }, LayoutComputed { layout_id: LayoutId, available_space: Size, frame_state: Option, }, } /// Internal struct that wraps an element to store Layout and ElementState after the element is rendered. /// It's allocated as a trait object to erase the element type and wrapped in AnyElement for /// improved usability. impl> DrawableElement { fn new(element: E) -> Self { DrawableElement { element: Some(element), phase: ElementDrawPhase::Start, } } fn layout(&mut self, state: &mut V, cx: &mut ViewContext) -> LayoutId { let (layout_id, frame_state) = if let Some(id) = self.element.as_ref().unwrap().element_id() { let layout_id = cx.with_element_state(id, |element_state, cx| { self.element .as_mut() .unwrap() .layout(state, element_state, cx) }); (layout_id, None) } else { let (layout_id, frame_state) = self.element.as_mut().unwrap().layout(state, None, cx); (layout_id, Some(frame_state)) }; self.phase = ElementDrawPhase::LayoutRequested { layout_id, frame_state, }; layout_id } fn paint(mut self, view_state: &mut V, cx: &mut ViewContext) -> Option { match self.phase { ElementDrawPhase::LayoutRequested { layout_id, frame_state, } | ElementDrawPhase::LayoutComputed { layout_id, frame_state, .. } => { let bounds = cx.layout_bounds(layout_id); if let Some(mut frame_state) = frame_state { self.element .take() .unwrap() .paint(bounds, view_state, &mut frame_state, cx); Some(frame_state) } else { let element_id = self .element .as_ref() .unwrap() .element_id() .expect("if we don't have frame state, we should have element state"); cx.with_element_state(element_id, |element_state, cx| { let mut element_state = element_state.unwrap(); self.element.take().unwrap().paint( bounds, view_state, &mut element_state, cx, ); ((), element_state) }); None } } _ => panic!("must call layout before paint"), } } fn measure( &mut self, available_space: Size, view_state: &mut V, cx: &mut ViewContext, ) -> Size { if matches!(&self.phase, ElementDrawPhase::Start) { self.layout(view_state, cx); } let layout_id = match &mut self.phase { ElementDrawPhase::LayoutRequested { layout_id, frame_state, } => { cx.compute_layout(*layout_id, available_space); let layout_id = *layout_id; self.phase = ElementDrawPhase::LayoutComputed { layout_id, available_space, frame_state: frame_state.take(), }; layout_id } ElementDrawPhase::LayoutComputed { layout_id, available_space: prev_available_space, .. } => { if available_space != *prev_available_space { cx.compute_layout(*layout_id, available_space); *prev_available_space = available_space; } *layout_id } _ => panic!("cannot measure after painting"), }; cx.layout_bounds(layout_id).size } fn draw( mut self, origin: Point, available_space: Size, view_state: &mut V, cx: &mut ViewContext, ) -> Option { self.measure(available_space, view_state, cx); cx.with_absolute_element_offset(origin, |cx| self.paint(view_state, cx)) } } impl ElementObject for Option> where E: Element, E::State: 'static, { fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext) -> LayoutId { DrawableElement::layout(self.as_mut().unwrap(), view_state, cx) } fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext) { DrawableElement::paint(self.take().unwrap(), view_state, cx); } fn measure( &mut self, available_space: Size, view_state: &mut V, cx: &mut ViewContext, ) -> Size { DrawableElement::measure(self.as_mut().unwrap(), available_space, view_state, cx) } fn draw( &mut self, origin: Point, available_space: Size, view_state: &mut V, cx: &mut ViewContext, ) { DrawableElement::draw( self.take().unwrap(), origin, available_space, view_state, cx, ); } } pub struct AnyElement(Box>); impl AnyElement { pub fn new(element: E) -> Self where V: 'static, E: 'static + Element, E::State: Any, { AnyElement(Box::new(Some(DrawableElement::new(element))) as Box>) } pub fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext) -> LayoutId { self.0.layout(view_state, cx) } pub fn paint(mut self, view_state: &mut V, cx: &mut ViewContext) { self.0.paint(view_state, cx) } /// Initializes this element and performs layout within the given available space to determine its size. pub fn measure( &mut self, available_space: Size, view_state: &mut V, cx: &mut ViewContext, ) -> Size { self.0.measure(available_space, view_state, cx) } /// Initializes this element and performs layout in the available space, then paints it at the given origin. pub fn draw( mut self, origin: Point, available_space: Size, view_state: &mut V, cx: &mut ViewContext, ) { self.0.draw(origin, available_space, view_state, cx) } } pub trait Component { fn render(self) -> AnyElement; fn map(self, f: impl FnOnce(Self) -> U) -> U where Self: Sized, U: Component, { f(self) } fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self where Self: Sized, { self.map(|this| if condition { then(this) } else { this }) } fn when_some(self, option: Option, then: impl FnOnce(Self, T) -> Self) -> Self where Self: Sized, { self.map(|this| { if let Some(value) = option { then(this, value) } else { this } }) } } impl Component for AnyElement { fn render(self) -> AnyElement { self } } impl Element for Option where V: 'static, E: 'static + Component, F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static, { type State = Option>; fn element_id(&self) -> Option { None } fn layout( &mut self, view_state: &mut V, _: Option, cx: &mut ViewContext, ) -> (LayoutId, Self::State) { let render = self.take().unwrap(); let mut rendered_element = (render)(view_state, cx).render(); let layout_id = rendered_element.layout(view_state, cx); (layout_id, Some(rendered_element)) } fn paint( self, _bounds: Bounds, view_state: &mut V, rendered_element: &mut Self::State, cx: &mut ViewContext, ) { rendered_element.take().unwrap().paint(view_state, cx); } } impl Component for Option where V: 'static, E: 'static + Component, F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static, { fn render(self) -> AnyElement { AnyElement::new(self) } } impl Component for F where V: 'static, E: 'static + Component, F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static, { fn render(self) -> AnyElement { AnyElement::new(Some(self)) } }