2023-10-10 18:41:28 +00:00
|
|
|
use crate::{Bounds, Identified, LayoutId, Pixels, Point, Result, ViewContext};
|
|
|
|
use derive_more::{Deref, DerefMut};
|
2023-09-21 19:46:31 +00:00
|
|
|
pub(crate) use smallvec::SmallVec;
|
2023-10-10 18:41:28 +00:00
|
|
|
use util::arc_cow::ArcCow;
|
2023-09-19 19:19:22 +00:00
|
|
|
|
|
|
|
pub trait Element: 'static {
|
|
|
|
type State;
|
|
|
|
type FrameState;
|
|
|
|
|
2023-10-10 18:41:28 +00:00
|
|
|
fn element_id(&self) -> Option<ElementId> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-09-29 23:41:31 +00:00
|
|
|
fn layout(
|
2023-09-19 19:19:22 +00:00
|
|
|
&mut self,
|
|
|
|
state: &mut Self::State,
|
2023-09-29 23:41:31 +00:00
|
|
|
cx: &mut ViewContext<Self::State>,
|
2023-09-19 19:19:22 +00:00
|
|
|
) -> Result<(LayoutId, Self::FrameState)>;
|
|
|
|
|
2023-09-29 23:41:31 +00:00
|
|
|
fn paint(
|
2023-09-19 19:19:22 +00:00
|
|
|
&mut self,
|
2023-10-06 03:02:26 +00:00
|
|
|
bounds: Bounds<Pixels>,
|
2023-09-19 19:19:22 +00:00
|
|
|
state: &mut Self::State,
|
|
|
|
frame_state: &mut Self::FrameState,
|
2023-09-29 23:41:31 +00:00
|
|
|
cx: &mut ViewContext<Self::State>,
|
2023-09-19 19:19:22 +00:00
|
|
|
) -> Result<()>;
|
2023-10-10 17:48:32 +00:00
|
|
|
|
2023-10-10 18:41:28 +00:00
|
|
|
fn id(self, id: ElementId) -> Identified<Self>
|
2023-10-10 17:48:32 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-10-10 18:41:28 +00:00
|
|
|
Identified { element: self, id }
|
2023-10-10 17:48:32 +00:00
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 18:42:44 +00:00
|
|
|
pub trait StatefulElement: Element {
|
|
|
|
fn element_id(&self) -> ElementId {
|
|
|
|
Element::element_id(self).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 18:41:28 +00:00
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
|
|
|
pub struct ElementId(ArcCow<'static, [u8]>);
|
|
|
|
|
|
|
|
#[derive(Deref, DerefMut, Default, Clone, Debug)]
|
|
|
|
pub(crate) struct GlobalElementId(SmallVec<[ElementId; 8]>);
|
|
|
|
|
2023-10-10 14:47:09 +00:00
|
|
|
pub trait ParentElement {
|
|
|
|
type State;
|
|
|
|
|
|
|
|
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<Self::State>; 2]>;
|
2023-09-20 03:55:49 +00:00
|
|
|
|
2023-10-10 14:47:09 +00:00
|
|
|
fn child(mut self, child: impl IntoAnyElement<Self::State>) -> Self
|
2023-09-20 03:55:49 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
self.children_mut().push(child.into_any());
|
|
|
|
self
|
|
|
|
}
|
2023-09-21 04:26:46 +00:00
|
|
|
|
2023-10-10 14:47:09 +00:00
|
|
|
fn children(mut self, iter: impl IntoIterator<Item = impl IntoAnyElement<Self::State>>) -> Self
|
2023-09-21 04:26:46 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
self.children_mut()
|
|
|
|
.extend(iter.into_iter().map(|item| item.into_any()));
|
|
|
|
self
|
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trait ElementObject<S> {
|
2023-09-29 23:41:31 +00:00
|
|
|
fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> Result<LayoutId>;
|
|
|
|
fn paint(
|
2023-09-19 19:19:22 +00:00
|
|
|
&mut self,
|
|
|
|
state: &mut S,
|
2023-09-21 18:18:09 +00:00
|
|
|
offset: Option<Point<Pixels>>,
|
2023-09-29 23:41:31 +00:00
|
|
|
cx: &mut ViewContext<S>,
|
2023-09-19 19:19:22 +00:00
|
|
|
) -> Result<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct RenderedElement<E: Element> {
|
|
|
|
element: E,
|
|
|
|
phase: ElementRenderPhase<E::FrameState>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
enum ElementRenderPhase<S> {
|
|
|
|
#[default]
|
|
|
|
Rendered,
|
|
|
|
LayoutRequested {
|
|
|
|
layout_id: LayoutId,
|
|
|
|
frame_state: S,
|
|
|
|
},
|
|
|
|
Painted {
|
2023-10-06 03:02:26 +00:00
|
|
|
bounds: Bounds<Pixels>,
|
2023-09-19 19:19:22 +00:00
|
|
|
frame_state: S,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal struct that wraps an element to store Layout and FrameState after the element is rendered.
|
|
|
|
/// It's allocated as a trait object to erase the element type and wrapped in AnyElement<E::State> for
|
|
|
|
/// improved usability.
|
|
|
|
impl<E: Element> RenderedElement<E> {
|
|
|
|
fn new(element: E) -> Self {
|
|
|
|
RenderedElement {
|
|
|
|
element,
|
|
|
|
phase: ElementRenderPhase::Rendered,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: Element> ElementObject<E::State> for RenderedElement<E> {
|
|
|
|
fn layout(&mut self, state: &mut E::State, cx: &mut ViewContext<E::State>) -> Result<LayoutId> {
|
|
|
|
let (layout_id, frame_state) = self.element.layout(state, cx)?;
|
|
|
|
self.phase = ElementRenderPhase::LayoutRequested {
|
|
|
|
layout_id,
|
|
|
|
frame_state,
|
|
|
|
};
|
|
|
|
Ok(layout_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
|
|
|
state: &mut E::State,
|
2023-09-21 18:18:09 +00:00
|
|
|
offset: Option<Point<Pixels>>,
|
2023-09-19 19:19:22 +00:00
|
|
|
cx: &mut ViewContext<E::State>,
|
|
|
|
) -> Result<()> {
|
|
|
|
self.phase = match std::mem::take(&mut self.phase) {
|
|
|
|
ElementRenderPhase::Rendered => panic!("must call layout before paint"),
|
|
|
|
|
|
|
|
ElementRenderPhase::LayoutRequested {
|
|
|
|
layout_id,
|
|
|
|
mut frame_state,
|
|
|
|
} => {
|
2023-10-06 03:02:26 +00:00
|
|
|
let mut bounds = cx.layout_bounds(layout_id)?.clone();
|
|
|
|
offset.map(|offset| bounds.origin += offset);
|
|
|
|
self.element.paint(bounds, state, &mut frame_state, cx)?;
|
2023-09-19 19:19:22 +00:00
|
|
|
ElementRenderPhase::Painted {
|
2023-10-06 03:02:26 +00:00
|
|
|
bounds,
|
2023-09-19 19:19:22 +00:00
|
|
|
frame_state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ElementRenderPhase::Painted {
|
2023-10-06 03:02:26 +00:00
|
|
|
bounds,
|
2023-09-19 19:19:22 +00:00
|
|
|
mut frame_state,
|
|
|
|
} => {
|
|
|
|
self.element
|
2023-10-06 03:02:26 +00:00
|
|
|
.paint(bounds.clone(), state, &mut frame_state, cx)?;
|
2023-09-19 19:19:22 +00:00
|
|
|
ElementRenderPhase::Painted {
|
2023-10-06 03:02:26 +00:00
|
|
|
bounds,
|
2023-09-19 19:19:22 +00:00
|
|
|
frame_state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AnyElement<S>(Box<dyn ElementObject<S>>);
|
|
|
|
|
|
|
|
impl<S> AnyElement<S> {
|
2023-09-29 23:41:31 +00:00
|
|
|
pub fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> Result<LayoutId> {
|
2023-09-19 19:19:22 +00:00
|
|
|
self.0.layout(state, cx)
|
|
|
|
}
|
|
|
|
|
2023-09-29 23:41:31 +00:00
|
|
|
pub fn paint(
|
2023-09-19 19:19:22 +00:00
|
|
|
&mut self,
|
|
|
|
state: &mut S,
|
2023-09-21 18:18:09 +00:00
|
|
|
offset: Option<Point<Pixels>>,
|
2023-09-29 23:41:31 +00:00
|
|
|
cx: &mut ViewContext<S>,
|
2023-09-19 19:19:22 +00:00
|
|
|
) -> Result<()> {
|
2023-09-21 18:18:09 +00:00
|
|
|
self.0.paint(state, offset, cx)
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait IntoAnyElement<S> {
|
|
|
|
fn into_any(self) -> AnyElement<S>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E: Element> IntoAnyElement<E::State> for E {
|
|
|
|
fn into_any(self) -> AnyElement<E::State> {
|
|
|
|
AnyElement(Box::new(RenderedElement::new(self)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S> IntoAnyElement<S> for AnyElement<S> {
|
|
|
|
fn into_any(self) -> AnyElement<S> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|