2023-10-20 15:31:42 +00:00
|
|
|
use crate::{BorrowWindow, Bounds, ElementId, LayoutId, Pixels, ViewContext};
|
2023-10-10 18:41:28 +00:00
|
|
|
use derive_more::{Deref, DerefMut};
|
2023-09-21 19:46:31 +00:00
|
|
|
pub(crate) use smallvec::SmallVec;
|
2023-10-24 12:20:01 +00:00
|
|
|
use std::{any::Any, mem};
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-10-26 09:31:47 +00:00
|
|
|
pub trait Element<V: 'static> {
|
2023-11-01 18:31:23 +00:00
|
|
|
type ElementState: 'static;
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-10-17 09:08:48 +00:00
|
|
|
fn id(&self) -> Option<ElementId>;
|
2023-10-10 18:41:28 +00:00
|
|
|
|
2023-10-26 08:46:02 +00:00
|
|
|
/// Called to initialize this element for the current frame. If this
|
|
|
|
/// element had state in a previous frame, it will be passed in for the 3rd argument.
|
2023-10-18 12:12:50 +00:00
|
|
|
fn initialize(
|
2023-09-19 19:19:22 +00:00
|
|
|
&mut self,
|
2023-10-26 07:45:26 +00:00
|
|
|
view_state: &mut V,
|
2023-10-11 04:14:47 +00:00
|
|
|
element_state: Option<Self::ElementState>,
|
2023-10-26 07:45:26 +00:00
|
|
|
cx: &mut ViewContext<V>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> Self::ElementState;
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
2023-10-26 07:45:26 +00:00
|
|
|
view_state: &mut V,
|
2023-10-18 12:12:50 +00:00
|
|
|
element_state: &mut Self::ElementState,
|
2023-10-26 07:45:26 +00:00
|
|
|
cx: &mut ViewContext<V>,
|
2023-10-18 12:12:50 +00:00
|
|
|
) -> LayoutId;
|
2023-09-19 19:19:22 +00:00
|
|
|
|
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-10-26 07:45:26 +00:00
|
|
|
view_state: &mut V,
|
2023-10-11 04:14:47 +00:00
|
|
|
element_state: &mut Self::ElementState,
|
2023-10-26 07:45:26 +00:00
|
|
|
cx: &mut ViewContext<V>,
|
2023-10-11 04:14:47 +00:00
|
|
|
);
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 04:14:47 +00:00
|
|
|
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
|
2023-10-19 17:43:28 +00:00
|
|
|
pub struct GlobalElementId(SmallVec<[ElementId; 32]>);
|
2023-10-10 18:41:28 +00:00
|
|
|
|
2023-10-26 08:46:02 +00:00
|
|
|
pub trait ParentElement<V: 'static> {
|
2023-10-26 07:45:26 +00:00
|
|
|
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]>;
|
2023-10-10 14:47:09 +00:00
|
|
|
|
2023-10-26 10:46:52 +00:00
|
|
|
fn child(mut self, child: impl Component<V>) -> Self
|
2023-09-20 03:55:49 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-10-26 10:46:52 +00:00
|
|
|
self.children_mut().push(child.render());
|
2023-09-20 03:55:49 +00:00
|
|
|
self
|
|
|
|
}
|
2023-09-21 04:26:46 +00:00
|
|
|
|
2023-10-26 10:46:52 +00:00
|
|
|
fn children(mut self, iter: impl IntoIterator<Item = impl Component<V>>) -> Self
|
2023-09-21 04:26:46 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
self.children_mut()
|
2023-10-26 10:46:52 +00:00
|
|
|
.extend(iter.into_iter().map(|item| item.render()));
|
2023-09-21 04:26:46 +00:00
|
|
|
self
|
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
trait ElementObject<V> {
|
2023-10-18 12:12:50 +00:00
|
|
|
fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>);
|
|
|
|
fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId;
|
2023-10-20 15:31:42 +00:00
|
|
|
fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>);
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 07:51:33 +00:00
|
|
|
struct RenderedElement<V: 'static, E: Element<V>> {
|
2023-09-19 19:19:22 +00:00
|
|
|
element: E,
|
2023-10-11 04:14:47 +00:00
|
|
|
phase: ElementRenderPhase<E::ElementState>,
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2023-10-18 12:12:50 +00:00
|
|
|
enum ElementRenderPhase<V> {
|
2023-09-19 19:19:22 +00:00
|
|
|
#[default]
|
2023-10-18 12:12:50 +00:00
|
|
|
Start,
|
|
|
|
Initialized {
|
|
|
|
frame_state: Option<V>,
|
|
|
|
},
|
2023-09-19 19:19:22 +00:00
|
|
|
LayoutRequested {
|
|
|
|
layout_id: LayoutId,
|
2023-10-18 12:12:50 +00:00
|
|
|
frame_state: Option<V>,
|
2023-09-19 19:19:22 +00:00
|
|
|
},
|
2023-10-18 12:12:50 +00:00
|
|
|
Painted,
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 04:14:47 +00:00
|
|
|
/// Internal struct that wraps an element to store Layout and ElementState after the element is rendered.
|
2023-09-19 19:19:22 +00:00
|
|
|
/// It's allocated as a trait object to erase the element type and wrapped in AnyElement<E::State> for
|
|
|
|
/// improved usability.
|
2023-10-26 07:45:26 +00:00
|
|
|
impl<V, E: Element<V>> RenderedElement<V, E> {
|
2023-09-19 19:19:22 +00:00
|
|
|
fn new(element: E) -> Self {
|
|
|
|
RenderedElement {
|
|
|
|
element,
|
2023-10-18 12:12:50 +00:00
|
|
|
phase: ElementRenderPhase::Start,
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-18 12:12:50 +00:00
|
|
|
}
|
2023-10-11 04:14:47 +00:00
|
|
|
|
2023-10-26 07:45:26 +00:00
|
|
|
impl<V, E> ElementObject<V> for RenderedElement<V, E>
|
2023-10-18 12:12:50 +00:00
|
|
|
where
|
2023-10-26 07:45:26 +00:00
|
|
|
E: Element<V>,
|
2023-11-01 18:31:23 +00:00
|
|
|
E::ElementState: 'static,
|
2023-10-18 12:12:50 +00:00
|
|
|
{
|
2023-10-26 07:45:26 +00:00
|
|
|
fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
|
2023-10-18 12:12:50 +00:00
|
|
|
let frame_state = if let Some(id) = self.element.id() {
|
2023-10-11 04:14:47 +00:00
|
|
|
cx.with_element_state(id, |element_state, cx| {
|
2023-10-18 12:12:50 +00:00
|
|
|
let element_state = self.element.initialize(view_state, element_state, cx);
|
2023-10-11 04:14:47 +00:00
|
|
|
((), element_state)
|
|
|
|
});
|
2023-10-18 12:12:50 +00:00
|
|
|
None
|
2023-10-11 04:14:47 +00:00
|
|
|
} else {
|
2023-10-18 12:12:50 +00:00
|
|
|
let frame_state = self.element.initialize(view_state, None, cx);
|
|
|
|
Some(frame_state)
|
|
|
|
};
|
|
|
|
|
|
|
|
self.phase = ElementRenderPhase::Initialized { frame_state };
|
2023-10-11 04:14:47 +00:00
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-10-26 07:45:26 +00:00
|
|
|
fn layout(&mut self, state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
|
2023-10-18 12:12:50 +00:00
|
|
|
let layout_id;
|
|
|
|
let mut frame_state;
|
|
|
|
match mem::take(&mut self.phase) {
|
|
|
|
ElementRenderPhase::Initialized {
|
|
|
|
frame_state: initial_frame_state,
|
|
|
|
} => {
|
|
|
|
frame_state = initial_frame_state;
|
|
|
|
if let Some(id) = self.element.id() {
|
|
|
|
layout_id = cx.with_element_state(id, |element_state, cx| {
|
|
|
|
let mut element_state = element_state.unwrap();
|
|
|
|
let layout_id = self.element.layout(state, &mut element_state, cx);
|
|
|
|
(layout_id, element_state)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
layout_id = self
|
|
|
|
.element
|
|
|
|
.layout(state, frame_state.as_mut().unwrap(), cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => panic!("must call initialize before layout"),
|
2023-10-11 04:14:47 +00:00
|
|
|
};
|
|
|
|
|
2023-09-19 19:19:22 +00:00
|
|
|
self.phase = ElementRenderPhase::LayoutRequested {
|
|
|
|
layout_id,
|
|
|
|
frame_state,
|
|
|
|
};
|
2023-10-11 04:14:47 +00:00
|
|
|
layout_id
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 07:45:26 +00:00
|
|
|
fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
|
2023-10-18 12:12:50 +00:00
|
|
|
self.phase = match mem::take(&mut self.phase) {
|
2023-09-19 19:19:22 +00:00
|
|
|
ElementRenderPhase::LayoutRequested {
|
|
|
|
layout_id,
|
|
|
|
mut frame_state,
|
|
|
|
} => {
|
2023-10-20 15:31:42 +00:00
|
|
|
let bounds = cx.layout_bounds(layout_id);
|
2023-10-18 12:12:50 +00:00
|
|
|
if let Some(id) = self.element.id() {
|
|
|
|
cx.with_element_state(id, |element_state, cx| {
|
|
|
|
let mut element_state = element_state.unwrap();
|
|
|
|
self.element
|
|
|
|
.paint(bounds, view_state, &mut element_state, cx);
|
|
|
|
((), element_state)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
self.element
|
|
|
|
.paint(bounds, view_state, frame_state.as_mut().unwrap(), cx);
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
2023-10-18 12:12:50 +00:00
|
|
|
ElementRenderPhase::Painted
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
_ => panic!("must call layout before paint"),
|
2023-09-19 19:19:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:31:23 +00:00
|
|
|
pub struct AnyElement<V>(Box<dyn ElementObject<V>>);
|
2023-10-26 09:21:42 +00:00
|
|
|
|
2023-10-24 12:20:01 +00:00
|
|
|
impl<V> AnyElement<V> {
|
|
|
|
pub fn new<E>(element: E) -> Self
|
|
|
|
where
|
2023-10-26 07:51:33 +00:00
|
|
|
V: 'static,
|
2023-11-01 18:31:23 +00:00
|
|
|
E: 'static + Element<V>,
|
|
|
|
E::ElementState: Any,
|
2023-10-24 12:20:01 +00:00
|
|
|
{
|
2023-10-12 17:30:00 +00:00
|
|
|
AnyElement(Box::new(RenderedElement::new(element)))
|
|
|
|
}
|
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
pub fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
|
|
|
|
self.0.initialize(view_state, cx);
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
pub fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) -> LayoutId {
|
|
|
|
self.0.layout(view_state, cx)
|
|
|
|
}
|
|
|
|
|
2023-10-20 15:31:42 +00:00
|
|
|
pub fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext<V>) {
|
|
|
|
self.0.paint(view_state, cx)
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:46:52 +00:00
|
|
|
pub trait Component<V> {
|
|
|
|
fn render(self) -> AnyElement<V>;
|
2023-10-26 10:38:23 +00:00
|
|
|
|
|
|
|
fn when(mut self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
if condition {
|
|
|
|
self = then(self);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 10:46:52 +00:00
|
|
|
impl<V> Component<V> for AnyElement<V> {
|
|
|
|
fn render(self) -> AnyElement<V> {
|
2023-09-19 19:19:22 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
2023-10-26 09:21:42 +00:00
|
|
|
|
|
|
|
impl<V, E, F> Element<V> for Option<F>
|
|
|
|
where
|
|
|
|
V: 'static,
|
2023-11-01 18:59:49 +00:00
|
|
|
E: 'static + Component<V>,
|
2023-11-02 08:41:49 +00:00
|
|
|
F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static,
|
2023-10-26 09:21:42 +00:00
|
|
|
{
|
|
|
|
type ElementState = AnyElement<V>;
|
|
|
|
|
|
|
|
fn id(&self) -> Option<ElementId> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn initialize(
|
|
|
|
&mut self,
|
|
|
|
view_state: &mut V,
|
|
|
|
_rendered_element: Option<Self::ElementState>,
|
|
|
|
cx: &mut ViewContext<V>,
|
|
|
|
) -> Self::ElementState {
|
|
|
|
let render = self.take().unwrap();
|
2023-10-26 13:22:48 +00:00
|
|
|
let mut rendered_element = (render)(view_state, cx).render();
|
|
|
|
rendered_element.initialize(view_state, cx);
|
|
|
|
rendered_element
|
2023-10-26 09:21:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
view_state: &mut V,
|
|
|
|
rendered_element: &mut Self::ElementState,
|
|
|
|
cx: &mut ViewContext<V>,
|
|
|
|
) -> LayoutId {
|
|
|
|
rendered_element.layout(view_state, cx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
|
|
|
_bounds: Bounds<Pixels>,
|
|
|
|
view_state: &mut V,
|
|
|
|
rendered_element: &mut Self::ElementState,
|
|
|
|
cx: &mut ViewContext<V>,
|
|
|
|
) {
|
|
|
|
rendered_element.paint(view_state, cx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:46:52 +00:00
|
|
|
impl<V, E, F> Component<V> for Option<F>
|
2023-10-26 09:21:42 +00:00
|
|
|
where
|
|
|
|
V: 'static,
|
2023-11-01 18:59:49 +00:00
|
|
|
E: 'static + Component<V>,
|
2023-11-02 08:41:49 +00:00
|
|
|
F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static,
|
2023-10-26 09:21:42 +00:00
|
|
|
{
|
2023-10-26 10:46:52 +00:00
|
|
|
fn render(self) -> AnyElement<V> {
|
2023-10-26 09:21:42 +00:00
|
|
|
AnyElement::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-26 10:46:52 +00:00
|
|
|
impl<V, E, F> Component<V> for F
|
2023-10-26 09:21:42 +00:00
|
|
|
where
|
|
|
|
V: 'static,
|
2023-11-01 18:59:49 +00:00
|
|
|
E: 'static + Component<V>,
|
2023-11-02 08:41:49 +00:00
|
|
|
F: FnOnce(&mut V, &mut ViewContext<'_, V>) -> E + 'static,
|
2023-10-26 09:21:42 +00:00
|
|
|
{
|
2023-10-26 10:46:52 +00:00
|
|
|
fn render(self) -> AnyElement<V> {
|
2023-10-26 09:21:42 +00:00
|
|
|
AnyElement::new(Some(self))
|
|
|
|
}
|
|
|
|
}
|