2023-10-11 18:47:43 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use crate::{
|
2023-10-12 03:34:08 +00:00
|
|
|
BorrowWindow, Bounds, Clickable, ElementId, Group, LayoutId, MouseDownEvent, MouseUpEvent,
|
|
|
|
Pixels, Point, SharedString, ViewContext,
|
2023-10-11 18:47:43 +00:00
|
|
|
};
|
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-09-19 19:19:22 +00:00
|
|
|
|
2023-10-12 17:30:00 +00:00
|
|
|
pub trait Element: 'static + Send + Sync + IntoAnyElement<Self::ViewState> {
|
2023-10-11 04:14:47 +00:00
|
|
|
type ViewState: 'static + Send + Sync;
|
|
|
|
type ElementState: 'static + Send + Sync;
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-10-11 17:40:42 +00:00
|
|
|
fn element_id(&self) -> Option<ElementId>;
|
2023-10-10 18:41:28 +00:00
|
|
|
|
2023-09-29 23:41:31 +00:00
|
|
|
fn layout(
|
2023-09-19 19:19:22 +00:00
|
|
|
&mut self,
|
2023-10-11 04:14:47 +00:00
|
|
|
state: &mut Self::ViewState,
|
|
|
|
element_state: Option<Self::ElementState>,
|
|
|
|
cx: &mut ViewContext<Self::ViewState>,
|
|
|
|
) -> (LayoutId, Self::ElementState);
|
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-11 04:14:47 +00:00
|
|
|
state: &mut Self::ViewState,
|
|
|
|
element_state: &mut Self::ElementState,
|
|
|
|
cx: &mut ViewContext<Self::ViewState>,
|
|
|
|
);
|
2023-10-10 17:48:32 +00:00
|
|
|
|
2023-10-12 03:34:08 +00:00
|
|
|
fn group(self, name: impl Into<SharedString>) -> Group<Self>
|
2023-10-10 17:48:32 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-10-12 03:34:08 +00:00
|
|
|
Group::new(name.into(), self)
|
2023-10-10 17:48:32 +00:00
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-11 18:47:43 +00:00
|
|
|
pub trait IdentifiedElement: Element {
|
2023-10-10 18:42:44 +00:00
|
|
|
fn element_id(&self) -> ElementId {
|
|
|
|
Element::element_id(self).unwrap()
|
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-10-11 18:47:43 +00:00
|
|
|
fn on_click(
|
|
|
|
self,
|
|
|
|
listener: impl Fn(
|
|
|
|
&mut Self::ViewState,
|
|
|
|
(&MouseDownEvent, &MouseUpEvent),
|
|
|
|
&mut ViewContext<Self::ViewState>,
|
|
|
|
) + Send
|
|
|
|
+ Sync
|
|
|
|
+ 'static,
|
|
|
|
) -> Clickable<Self>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
Clickable::new(self, Arc::from(listener))
|
|
|
|
}
|
2023-10-10 18:42:44 +00:00
|
|
|
}
|
2023-10-10 18:41:28 +00:00
|
|
|
|
2023-10-11 04:14:47 +00:00
|
|
|
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
|
2023-10-10 18:41:28 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-10-11 04:14:47 +00:00
|
|
|
trait ElementObject<S>: 'static + Send + Sync {
|
|
|
|
fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> LayoutId;
|
|
|
|
fn paint(&mut self, state: &mut S, offset: Option<Point<Pixels>>, cx: &mut ViewContext<S>);
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct RenderedElement<E: Element> {
|
|
|
|
element: E,
|
2023-10-11 04:14:47 +00:00
|
|
|
phase: ElementRenderPhase<E::ElementState>,
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
enum ElementRenderPhase<S> {
|
|
|
|
#[default]
|
|
|
|
Rendered,
|
|
|
|
LayoutRequested {
|
|
|
|
layout_id: LayoutId,
|
2023-10-11 04:14:47 +00:00
|
|
|
frame_state: Option<S>,
|
2023-09-19 19:19:22 +00:00
|
|
|
},
|
|
|
|
Painted {
|
2023-10-06 03:02:26 +00:00
|
|
|
bounds: Bounds<Pixels>,
|
2023-10-11 04:14:47 +00:00
|
|
|
frame_state: Option<S>,
|
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.
|
|
|
|
impl<E: Element> RenderedElement<E> {
|
|
|
|
fn new(element: E) -> Self {
|
|
|
|
RenderedElement {
|
|
|
|
element,
|
|
|
|
phase: ElementRenderPhase::Rendered,
|
|
|
|
}
|
|
|
|
}
|
2023-10-11 04:14:47 +00:00
|
|
|
|
|
|
|
fn paint_with_element_state(
|
|
|
|
&mut self,
|
|
|
|
bounds: Bounds<Pixels>,
|
|
|
|
view_state: &mut E::ViewState,
|
|
|
|
frame_state: &mut Option<E::ElementState>,
|
|
|
|
cx: &mut ViewContext<E::ViewState>,
|
|
|
|
) {
|
|
|
|
if let Some(id) = self.element.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-11 04:14:47 +00:00
|
|
|
impl<E, S> ElementObject<E::ViewState> for RenderedElement<E>
|
|
|
|
where
|
|
|
|
E: Element<ElementState = S>,
|
|
|
|
S: 'static + Send + Sync,
|
|
|
|
{
|
|
|
|
fn layout(&mut self, state: &mut E::ViewState, cx: &mut ViewContext<E::ViewState>) -> LayoutId {
|
|
|
|
let (layout_id, frame_state) = if let Some(id) = self.element.element_id() {
|
|
|
|
let layout_id = cx.with_element_state(id, |element_state, cx| {
|
|
|
|
self.element.layout(state, element_state, cx)
|
|
|
|
});
|
|
|
|
(layout_id, None)
|
|
|
|
} else {
|
|
|
|
let (layout_id, frame_state) = self.element.layout(state, None, cx);
|
|
|
|
(layout_id, Some(frame_state))
|
|
|
|
};
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn paint(
|
|
|
|
&mut self,
|
2023-10-11 04:14:47 +00:00
|
|
|
view_state: &mut E::ViewState,
|
2023-09-21 18:18:09 +00:00
|
|
|
offset: Option<Point<Pixels>>,
|
2023-10-11 04:14:47 +00:00
|
|
|
cx: &mut ViewContext<E::ViewState>,
|
|
|
|
) {
|
2023-09-19 19:19:22 +00:00
|
|
|
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-11 04:14:47 +00:00
|
|
|
let mut bounds = cx.layout_bounds(layout_id);
|
2023-10-06 03:02:26 +00:00
|
|
|
offset.map(|offset| bounds.origin += offset);
|
2023-10-11 04:14:47 +00:00
|
|
|
self.paint_with_element_state(bounds, view_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,
|
|
|
|
} => {
|
2023-10-11 04:14:47 +00:00
|
|
|
self.paint_with_element_state(bounds, view_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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AnyElement<S>(Box<dyn ElementObject<S>>);
|
|
|
|
|
2023-10-12 17:30:00 +00:00
|
|
|
impl<S: 'static + Send + Sync> AnyElement<S> {
|
|
|
|
pub fn new<E: Element<ViewState = S>>(element: E) -> Self {
|
|
|
|
AnyElement(Box::new(RenderedElement::new(element)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 04:14:47 +00:00
|
|
|
impl<S: 'static + Send + Sync> AnyElement<S> {
|
|
|
|
pub fn layout(&mut self, state: &mut S, cx: &mut ViewContext<S>) -> LayoutId {
|
2023-09-19 19:19:22 +00:00
|
|
|
self.0.layout(state, cx)
|
|
|
|
}
|
|
|
|
|
2023-10-11 04:14:47 +00:00
|
|
|
pub fn paint(&mut self, state: &mut S, offset: Option<Point<Pixels>>, cx: &mut ViewContext<S>) {
|
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<S> IntoAnyElement<S> for AnyElement<S> {
|
|
|
|
fn into_any(self) -> AnyElement<S> {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|