2023-09-22 14:33:51 +00:00
|
|
|
use super::{Layout, LayoutId, Pixels, Point, Result, ViewContext};
|
2023-09-21 19:46:31 +00:00
|
|
|
pub(crate) use smallvec::SmallVec;
|
2023-09-19 19:19:22 +00:00
|
|
|
|
|
|
|
pub trait Element: 'static {
|
|
|
|
type State;
|
|
|
|
type FrameState;
|
|
|
|
|
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,
|
|
|
|
layout: Layout,
|
|
|
|
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<()>;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait ParentElement<S> {
|
2023-09-20 03:55:49 +00:00
|
|
|
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<S>; 2]>;
|
|
|
|
|
|
|
|
fn child(mut self, child: impl IntoAnyElement<S>) -> Self
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
self.children_mut().push(child.into_any());
|
|
|
|
self
|
|
|
|
}
|
2023-09-21 04:26:46 +00:00
|
|
|
|
|
|
|
fn children(mut self, iter: impl IntoIterator<Item = impl IntoAnyElement<S>>) -> Self
|
|
|
|
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 {
|
|
|
|
layout: Layout,
|
|
|
|
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-09-21 18:18:09 +00:00
|
|
|
let mut layout = cx.layout(layout_id)?.clone();
|
|
|
|
offset.map(|offset| layout.bounds.origin += offset);
|
2023-09-19 19:19:22 +00:00
|
|
|
self.element
|
|
|
|
.paint(layout.clone(), state, &mut frame_state, cx)?;
|
|
|
|
ElementRenderPhase::Painted {
|
|
|
|
layout,
|
|
|
|
frame_state,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ElementRenderPhase::Painted {
|
|
|
|
layout,
|
|
|
|
mut frame_state,
|
|
|
|
} => {
|
|
|
|
self.element
|
|
|
|
.paint(layout.clone(), state, &mut frame_state, cx)?;
|
|
|
|
ElementRenderPhase::Painted {
|
|
|
|
layout,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|