2023-11-09 16:30:41 +00:00
|
|
|
use crate::{
|
|
|
|
AvailableSpace, BorrowWindow, Bounds, ElementId, LayoutId, Pixels, Point, Size, ViewContext,
|
2023-11-20 19:21:42 +00:00
|
|
|
WindowContext,
|
2023-11-09 16:30:41 +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-11-20 19:21:42 +00:00
|
|
|
use std::{any::Any, fmt::Debug};
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
pub trait Render: 'static + Sized {
|
|
|
|
type Element: Element + 'static;
|
2023-11-19 03:05:47 +00:00
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element;
|
2023-11-19 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
pub trait RenderOnce: Sized {
|
|
|
|
type Element: Element + 'static;
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-11-14 08:15:48 +00:00
|
|
|
fn element_id(&self) -> Option<ElementId>;
|
2023-10-10 18:41:28 +00:00
|
|
|
|
2023-11-19 03:05:47 +00:00
|
|
|
fn render_once(self) -> Self::Element;
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn render_into_any(self) -> AnyElement {
|
2023-11-19 03:05:47 +00:00
|
|
|
self.render_once().into_any()
|
|
|
|
}
|
2023-11-15 17:19:26 +00:00
|
|
|
|
|
|
|
fn draw<T, R>(
|
|
|
|
self,
|
|
|
|
origin: Point<Pixels>,
|
|
|
|
available_space: Size<T>,
|
2023-11-20 02:32:31 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-20 19:21:42 +00:00
|
|
|
f: impl FnOnce(&mut <Self::Element as Element>::State, &mut WindowContext) -> R,
|
2023-11-15 17:19:26 +00:00
|
|
|
) -> R
|
|
|
|
where
|
|
|
|
T: Clone + Default + Debug + Into<AvailableSpace>,
|
|
|
|
{
|
2023-11-19 04:51:47 +00:00
|
|
|
let element = self.render_once();
|
|
|
|
let element_id = element.element_id();
|
|
|
|
let element = DrawableElement {
|
|
|
|
element: Some(element),
|
|
|
|
phase: ElementDrawPhase::Start,
|
2023-11-15 17:19:26 +00:00
|
|
|
};
|
2023-11-20 19:21:42 +00:00
|
|
|
|
|
|
|
let frame_state =
|
|
|
|
DrawableElement::draw(element, origin, available_space.map(Into::into), cx);
|
2023-11-19 04:51:47 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:47 +00:00
|
|
|
fn map<U>(self, f: impl FnOnce(Self) -> U) -> U
|
|
|
|
where
|
|
|
|
Self: Sized,
|
2023-11-20 19:21:42 +00:00
|
|
|
U: RenderOnce,
|
2023-11-19 03:05:47 +00:00
|
|
|
{
|
|
|
|
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<T>(self, option: Option<T>, then: impl FnOnce(Self, T) -> Self) -> Self
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
self.map(|this| {
|
|
|
|
if let Some(value) = option {
|
|
|
|
then(this, value)
|
2023-11-15 17:19:26 +00:00
|
|
|
} else {
|
2023-11-19 03:05:47 +00:00
|
|
|
this
|
2023-11-15 17:19:26 +00:00
|
|
|
}
|
2023-11-19 03:05:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
pub trait Element: 'static + RenderOnce {
|
2023-11-18 06:32:55 +00:00
|
|
|
type State: 'static;
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
fn layout(
|
|
|
|
&mut self,
|
2023-11-20 19:21:42 +00:00
|
|
|
state: Option<Self::State>,
|
|
|
|
cx: &mut WindowContext,
|
2023-11-18 06:32:55 +00:00
|
|
|
) -> (LayoutId, Self::State);
|
2023-09-19 19:19:22 +00:00
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext);
|
2023-11-15 17:19:26 +00:00
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn into_any(self) -> AnyElement {
|
2023-11-18 03:05:37 +00:00
|
|
|
AnyElement::new(self)
|
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
pub trait Component: 'static {
|
|
|
|
type Rendered: RenderOnce;
|
2023-11-19 03:05:47 +00:00
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn render(self, cx: &mut WindowContext) -> Self::Rendered;
|
2023-11-19 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
pub struct CompositeElement<C> {
|
2023-11-19 03:05:47 +00:00
|
|
|
component: Option<C>,
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
pub struct CompositeElementState<C: Component> {
|
|
|
|
rendered_element: Option<<C::Rendered as RenderOnce>::Element>,
|
|
|
|
rendered_element_state: <<C::Rendered as RenderOnce>::Element as Element>::State,
|
2023-11-19 03:05:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
impl<C> CompositeElement<C> {
|
2023-11-19 03:05:47 +00:00
|
|
|
pub fn new(component: C) -> Self {
|
|
|
|
CompositeElement {
|
|
|
|
component: Some(component),
|
2023-11-15 17:19:26 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
impl<C: Component> Element for CompositeElement<C> {
|
|
|
|
type State = CompositeElementState<C>;
|
2023-11-19 03:05:47 +00:00
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&mut self,
|
|
|
|
state: Option<Self::State>,
|
2023-11-20 19:21:42 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-19 03:05:47 +00:00
|
|
|
) -> (LayoutId, Self::State) {
|
2023-11-20 19:21:42 +00:00
|
|
|
let mut element = self.component.take().unwrap().render(cx).render_once();
|
|
|
|
let (layout_id, state) = element.layout(state.map(|s| s.rendered_element_state), cx);
|
2023-11-19 03:05:47 +00:00
|
|
|
let state = CompositeElementState {
|
|
|
|
rendered_element: Some(element),
|
|
|
|
rendered_element_state: state,
|
|
|
|
};
|
|
|
|
(layout_id, state)
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
|
|
|
|
state
|
|
|
|
.rendered_element
|
|
|
|
.take()
|
|
|
|
.unwrap()
|
|
|
|
.paint(bounds, &mut state.rendered_element_state, cx);
|
2023-11-19 03:05:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
impl<C: Component> RenderOnce for CompositeElement<C> {
|
2023-11-19 03:05:47 +00:00
|
|
|
type Element = Self;
|
|
|
|
|
2023-11-19 04:51:47 +00:00
|
|
|
fn element_id(&self) -> Option<ElementId> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:47 +00:00
|
|
|
fn render_once(self) -> Self::Element {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-11-20 19:21:42 +00:00
|
|
|
pub trait ParentElement {
|
2023-11-20 02:32:31 +00:00
|
|
|
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]>;
|
2023-10-10 14:47:09 +00:00
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn child(mut self, child: impl RenderOnce) -> Self
|
2023-09-20 03:55:49 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-11-19 03:05:47 +00:00
|
|
|
self.children_mut().push(child.render_once().into_any());
|
2023-09-20 03:55:49 +00:00
|
|
|
self
|
|
|
|
}
|
2023-09-21 04:26:46 +00:00
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn children(mut self, children: impl IntoIterator<Item = impl RenderOnce>) -> Self
|
2023-09-21 04:26:46 +00:00
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-11-19 03:05:47 +00:00
|
|
|
self.children_mut().extend(
|
|
|
|
children
|
|
|
|
.into_iter()
|
|
|
|
.map(|child| child.render_once().into_any()),
|
|
|
|
);
|
2023-09-21 04:26:46 +00:00
|
|
|
self
|
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 02:32:31 +00:00
|
|
|
trait ElementObject {
|
2023-11-15 00:25:55 +00:00
|
|
|
fn element_id(&self) -> Option<ElementId>;
|
2023-11-20 19:21:42 +00:00
|
|
|
|
2023-11-20 02:32:31 +00:00
|
|
|
fn layout(&mut self, cx: &mut WindowContext) -> LayoutId;
|
2023-11-20 19:21:42 +00:00
|
|
|
|
2023-11-20 02:32:31 +00:00
|
|
|
fn paint(&mut self, cx: &mut WindowContext);
|
2023-11-20 19:21:42 +00:00
|
|
|
|
2023-11-09 17:43:26 +00:00
|
|
|
fn measure(
|
|
|
|
&mut self,
|
|
|
|
available_space: Size<AvailableSpace>,
|
2023-11-20 02:32:31 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-09 17:43:26 +00:00
|
|
|
) -> Size<Pixels>;
|
2023-11-20 19:21:42 +00:00
|
|
|
|
2023-11-09 17:43:26 +00:00
|
|
|
fn draw(
|
|
|
|
&mut self,
|
|
|
|
origin: Point<Pixels>,
|
|
|
|
available_space: Size<AvailableSpace>,
|
2023-11-20 02:32:31 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-09 17:43:26 +00:00
|
|
|
);
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
pub struct DrawableElement<E: Element> {
|
2023-11-18 06:32:55 +00:00
|
|
|
element: Option<E>,
|
|
|
|
phase: ElementDrawPhase<E::State>,
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
2023-11-19 04:51:47 +00:00
|
|
|
enum ElementDrawPhase<S> {
|
2023-09-19 19:19:22 +00:00
|
|
|
#[default]
|
2023-10-18 12:12:50 +00:00
|
|
|
Start,
|
2023-09-19 19:19:22 +00:00
|
|
|
LayoutRequested {
|
|
|
|
layout_id: LayoutId,
|
2023-11-19 04:51:47 +00:00
|
|
|
frame_state: Option<S>,
|
2023-09-19 19:19:22 +00:00
|
|
|
},
|
2023-11-09 17:43:26 +00:00
|
|
|
LayoutComputed {
|
|
|
|
layout_id: LayoutId,
|
|
|
|
available_space: Size<AvailableSpace>,
|
2023-11-19 04:51:47 +00:00
|
|
|
frame_state: Option<S>,
|
2023-11-15 17:19:26 +00:00
|
|
|
},
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-19 04:51:47 +00:00
|
|
|
/// A wrapper around an implementer of [Element] that allows it to be drawn in a window.
|
2023-11-20 19:21:42 +00:00
|
|
|
impl<E: Element> DrawableElement<E> {
|
2023-09-19 19:19:22 +00:00
|
|
|
fn new(element: E) -> Self {
|
2023-11-18 06:32:55 +00:00
|
|
|
DrawableElement {
|
|
|
|
element: Some(element),
|
|
|
|
phase: ElementDrawPhase::Start,
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-11 04:14:47 +00:00
|
|
|
|
2023-11-15 00:25:55 +00:00
|
|
|
fn element_id(&self) -> Option<ElementId> {
|
2023-11-18 07:03:23 +00:00
|
|
|
self.element.as_ref()?.element_id()
|
2023-11-15 00:25:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 02:32:31 +00:00
|
|
|
fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
|
2023-11-18 06:32:55 +00:00
|
|
|
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| {
|
2023-11-20 19:21:42 +00:00
|
|
|
self.element.as_mut().unwrap().layout(element_state, cx)
|
2023-11-18 06:32:55 +00:00
|
|
|
});
|
|
|
|
(layout_id, None)
|
|
|
|
} else {
|
2023-11-20 19:21:42 +00:00
|
|
|
let (layout_id, frame_state) = self.element.as_mut().unwrap().layout(None, cx);
|
2023-11-18 06:32:55 +00:00
|
|
|
(layout_id, Some(frame_state))
|
2023-10-11 04:14:47 +00:00
|
|
|
};
|
|
|
|
|
2023-11-18 06:32:55 +00:00
|
|
|
self.phase = ElementDrawPhase::LayoutRequested {
|
2023-09-19 19:19:22 +00:00
|
|
|
layout_id,
|
|
|
|
frame_state,
|
|
|
|
};
|
2023-10-11 04:14:47 +00:00
|
|
|
layout_id
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn paint(mut self, cx: &mut WindowContext) -> Option<E::State> {
|
2023-11-18 06:32:55 +00:00
|
|
|
match self.phase {
|
|
|
|
ElementDrawPhase::LayoutRequested {
|
2023-09-19 19:19:22 +00:00
|
|
|
layout_id,
|
2023-11-18 06:32:55 +00:00
|
|
|
frame_state,
|
2023-11-09 17:43:26 +00:00
|
|
|
}
|
2023-11-18 06:32:55 +00:00
|
|
|
| ElementDrawPhase::LayoutComputed {
|
2023-11-09 17:43:26 +00:00
|
|
|
layout_id,
|
2023-11-18 06:32:55 +00:00
|
|
|
frame_state,
|
2023-11-09 17:43:26 +00:00
|
|
|
..
|
2023-09-19 19:19:22 +00:00
|
|
|
} => {
|
2023-10-20 15:31:42 +00:00
|
|
|
let bounds = cx.layout_bounds(layout_id);
|
2023-11-18 06:32:55 +00:00
|
|
|
|
|
|
|
if let Some(mut frame_state) = frame_state {
|
|
|
|
self.element
|
|
|
|
.take()
|
|
|
|
.unwrap()
|
2023-11-20 19:21:42 +00:00
|
|
|
.paint(bounds, &mut frame_state, cx);
|
2023-11-18 06:32:55 +00:00
|
|
|
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| {
|
2023-10-18 12:12:50 +00:00
|
|
|
let mut element_state = element_state.unwrap();
|
2023-11-20 19:21:42 +00:00
|
|
|
self.element
|
|
|
|
.take()
|
|
|
|
.unwrap()
|
|
|
|
.paint(bounds, &mut element_state, cx);
|
2023-10-18 12:12:50 +00:00
|
|
|
((), element_state)
|
|
|
|
});
|
2023-11-18 06:32:55 +00:00
|
|
|
None
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 12:12:50 +00:00
|
|
|
_ => panic!("must call layout before paint"),
|
2023-11-18 06:32:55 +00:00
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
2023-11-09 17:43:26 +00:00
|
|
|
|
|
|
|
fn measure(
|
|
|
|
&mut self,
|
|
|
|
available_space: Size<AvailableSpace>,
|
2023-11-20 02:32:31 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-09 17:43:26 +00:00
|
|
|
) -> Size<Pixels> {
|
2023-11-18 06:32:55 +00:00
|
|
|
if matches!(&self.phase, ElementDrawPhase::Start) {
|
2023-11-20 02:32:31 +00:00
|
|
|
self.layout(cx);
|
2023-11-09 17:43:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let layout_id = match &mut self.phase {
|
2023-11-18 06:32:55 +00:00
|
|
|
ElementDrawPhase::LayoutRequested {
|
2023-11-09 17:43:26 +00:00
|
|
|
layout_id,
|
|
|
|
frame_state,
|
|
|
|
} => {
|
|
|
|
cx.compute_layout(*layout_id, available_space);
|
|
|
|
let layout_id = *layout_id;
|
2023-11-18 06:32:55 +00:00
|
|
|
self.phase = ElementDrawPhase::LayoutComputed {
|
2023-11-09 17:43:26 +00:00
|
|
|
layout_id,
|
|
|
|
available_space,
|
|
|
|
frame_state: frame_state.take(),
|
|
|
|
};
|
|
|
|
layout_id
|
|
|
|
}
|
2023-11-18 06:32:55 +00:00
|
|
|
ElementDrawPhase::LayoutComputed {
|
2023-11-09 17:43:26 +00:00
|
|
|
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(
|
2023-11-18 06:32:55 +00:00
|
|
|
mut self,
|
2023-11-15 21:11:19 +00:00
|
|
|
origin: Point<Pixels>,
|
2023-11-09 17:43:26 +00:00
|
|
|
available_space: Size<AvailableSpace>,
|
2023-11-20 02:32:31 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-18 06:32:55 +00:00
|
|
|
) -> Option<E::State> {
|
2023-11-20 02:32:31 +00:00
|
|
|
self.measure(available_space, cx);
|
|
|
|
cx.with_absolute_element_offset(origin, |cx| self.paint(cx))
|
2023-11-09 17:43:26 +00:00
|
|
|
}
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
// impl<V: 'static, E: Element> Element for DrawableElement<V, E> {
|
|
|
|
// type State = <E::Element as Element>::State;
|
2023-11-19 04:51:47 +00:00
|
|
|
|
|
|
|
// fn layout(
|
|
|
|
// &mut self,
|
|
|
|
// element_state: Option<Self::State>,
|
2023-11-20 19:21:42 +00:00
|
|
|
// cx: &mut WindowContext,
|
2023-11-19 04:51:47 +00:00
|
|
|
// ) -> (LayoutId, Self::State) {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fn paint(
|
|
|
|
// self,
|
|
|
|
// bounds: Bounds<Pixels>,
|
|
|
|
// element_state: &mut Self::State,
|
2023-11-20 19:21:42 +00:00
|
|
|
// cx: &mut WindowContext,
|
2023-11-19 04:51:47 +00:00
|
|
|
// ) {
|
|
|
|
// todo!()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
// impl<V: 'static, E: 'static + Element> RenderOnce for DrawableElement<V, E> {
|
2023-11-19 04:51:47 +00:00
|
|
|
// type Element = Self;
|
|
|
|
|
|
|
|
// fn element_id(&self) -> Option<ElementId> {
|
|
|
|
// self.element.as_ref()?.element_id()
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fn render_once(self) -> Self::Element {
|
|
|
|
// self
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
impl<E> ElementObject for Option<DrawableElement<E>>
|
2023-11-18 06:32:55 +00:00
|
|
|
where
|
2023-11-20 19:21:42 +00:00
|
|
|
E: Element,
|
2023-11-18 06:32:55 +00:00
|
|
|
E::State: 'static,
|
|
|
|
{
|
2023-11-18 07:03:23 +00:00
|
|
|
fn element_id(&self) -> Option<ElementId> {
|
|
|
|
self.as_ref().unwrap().element_id()
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
|
|
|
|
DrawableElement::layout(self.as_mut().unwrap(), cx)
|
2023-11-18 06:32:55 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn paint(&mut self, cx: &mut WindowContext) {
|
|
|
|
DrawableElement::paint(self.take().unwrap(), cx);
|
2023-11-18 06:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn measure(
|
|
|
|
&mut self,
|
|
|
|
available_space: Size<AvailableSpace>,
|
2023-11-20 19:21:42 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-18 06:32:55 +00:00
|
|
|
) -> Size<Pixels> {
|
2023-11-20 19:21:42 +00:00
|
|
|
DrawableElement::measure(self.as_mut().unwrap(), available_space, cx)
|
2023-11-18 06:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
&mut self,
|
|
|
|
origin: Point<Pixels>,
|
|
|
|
available_space: Size<AvailableSpace>,
|
2023-11-20 19:21:42 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-18 06:32:55 +00:00
|
|
|
) {
|
2023-11-20 19:21:42 +00:00
|
|
|
DrawableElement::draw(self.take().unwrap(), origin, available_space, cx);
|
2023-11-18 06:32:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 02:32:31 +00:00
|
|
|
pub struct AnyElement(Box<dyn ElementObject>);
|
2023-10-26 09:21:42 +00:00
|
|
|
|
2023-11-20 02:32:31 +00:00
|
|
|
impl AnyElement {
|
2023-10-24 12:20:01 +00:00
|
|
|
pub fn new<E>(element: E) -> Self
|
|
|
|
where
|
2023-11-20 02:32:31 +00:00
|
|
|
E: 'static + Element,
|
2023-11-18 06:32:55 +00:00
|
|
|
E::State: Any,
|
2023-10-24 12:20:01 +00:00
|
|
|
{
|
2023-11-20 19:21:42 +00:00
|
|
|
AnyElement(Box::new(Some(DrawableElement::new(element))) as Box<dyn ElementObject>)
|
2023-10-12 17:30:00 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 00:25:55 +00:00
|
|
|
pub fn element_id(&self) -> Option<ElementId> {
|
|
|
|
self.0.element_id()
|
|
|
|
}
|
|
|
|
|
2023-11-20 02:32:31 +00:00
|
|
|
pub fn layout(&mut self, cx: &mut WindowContext) -> LayoutId {
|
|
|
|
self.0.layout(cx)
|
2023-10-18 12:12:50 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
pub fn paint(mut self, cx: &mut WindowContext) {
|
2023-11-20 02:32:31 +00:00
|
|
|
self.0.paint(cx)
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
2023-11-09 16:30:41 +00:00
|
|
|
|
|
|
|
/// Initializes this element and performs layout within the given available space to determine its size.
|
|
|
|
pub fn measure(
|
|
|
|
&mut self,
|
|
|
|
available_space: Size<AvailableSpace>,
|
2023-11-20 02:32:31 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-09 16:30:41 +00:00
|
|
|
) -> Size<Pixels> {
|
2023-11-20 02:32:31 +00:00
|
|
|
self.0.measure(available_space, cx)
|
2023-11-09 16:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Initializes this element and performs layout in the available space, then paints it at the given origin.
|
|
|
|
pub fn draw(
|
2023-11-18 06:32:55 +00:00
|
|
|
mut self,
|
2023-11-09 16:30:41 +00:00
|
|
|
origin: Point<Pixels>,
|
|
|
|
available_space: Size<AvailableSpace>,
|
2023-11-20 02:32:31 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-09 16:30:41 +00:00
|
|
|
) {
|
2023-11-20 02:32:31 +00:00
|
|
|
self.0.draw(origin, available_space, cx)
|
2023-11-09 16:30:41 +00:00
|
|
|
}
|
2023-10-26 10:38:23 +00:00
|
|
|
|
2023-11-19 03:05:47 +00:00
|
|
|
/// Converts this `AnyElement` into a trait object that can be stored and manipulated.
|
2023-11-20 19:21:42 +00:00
|
|
|
pub fn into_any(self) -> AnyElement {
|
2023-11-19 03:05:47 +00:00
|
|
|
AnyElement::new(self)
|
2023-09-19 19:19:22 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-26 09:21:42 +00:00
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
impl Element for AnyElement {
|
2023-11-18 07:03:23 +00:00
|
|
|
type State = ();
|
2023-10-26 09:21:42 +00:00
|
|
|
|
2023-11-15 19:41:09 +00:00
|
|
|
fn layout(
|
2023-10-26 09:21:42 +00:00
|
|
|
&mut self,
|
2023-11-18 07:03:23 +00:00
|
|
|
_: Option<Self::State>,
|
2023-11-20 02:32:31 +00:00
|
|
|
cx: &mut WindowContext,
|
2023-11-18 07:03:23 +00:00
|
|
|
) -> (LayoutId, Self::State) {
|
2023-11-20 19:21:42 +00:00
|
|
|
let layout_id = self.layout(cx);
|
2023-11-18 07:03:23 +00:00
|
|
|
(layout_id, ())
|
2023-10-26 09:21:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
fn paint(self, _: Bounds<Pixels>, _: &mut Self::State, cx: &mut WindowContext) {
|
|
|
|
self.paint(cx);
|
2023-10-26 09:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
impl RenderOnce for AnyElement {
|
2023-11-19 03:05:47 +00:00
|
|
|
type Element = Self;
|
2023-11-02 15:39:40 +00:00
|
|
|
|
2023-11-19 04:51:47 +00:00
|
|
|
fn element_id(&self) -> Option<ElementId> {
|
|
|
|
AnyElement::element_id(self)
|
2023-10-26 09:21:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:05:47 +00:00
|
|
|
fn render_once(self) -> Self::Element {
|
2023-09-19 19:19:22 +00:00
|
|
|
self
|
2023-10-26 09:21:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
// impl<V, E, F> Element for Option<F>
|
2023-11-18 07:03:23 +00:00
|
|
|
// where
|
|
|
|
// V: 'static,
|
2023-11-20 19:21:42 +00:00
|
|
|
// E: Element,
|
|
|
|
// F: FnOnce(&mut V, &mut WindowContext<'_, V>) -> E + 'static,
|
2023-11-18 07:03:23 +00:00
|
|
|
// {
|
2023-11-20 19:21:42 +00:00
|
|
|
// type State = Option<AnyElement>;
|
2023-11-18 07:03:23 +00:00
|
|
|
|
|
|
|
// fn element_id(&self) -> Option<ElementId> {
|
|
|
|
// None
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fn layout(
|
|
|
|
// &mut self,
|
2023-11-19 03:05:47 +00:00
|
|
|
// _: Option<Self::State>,
|
2023-11-20 19:21:42 +00:00
|
|
|
// cx: &mut WindowContext,
|
2023-11-18 07:03:23 +00:00
|
|
|
// ) -> (LayoutId, Self::State) {
|
2023-11-19 03:05:47 +00:00
|
|
|
// let render = self.take().unwrap();
|
|
|
|
// let mut element = (render)(view_state, cx).into_any();
|
|
|
|
// let layout_id = element.layout(view_state, cx);
|
|
|
|
// (layout_id, Some(element))
|
2023-11-18 07:03:23 +00:00
|
|
|
// }
|
|
|
|
|
|
|
|
// fn paint(
|
|
|
|
// self,
|
2023-11-19 03:05:47 +00:00
|
|
|
// _bounds: Bounds<Pixels>,
|
|
|
|
// rendered_element: &mut Self::State,
|
2023-11-20 19:21:42 +00:00
|
|
|
// cx: &mut WindowContext,
|
2023-11-18 07:03:23 +00:00
|
|
|
// ) {
|
2023-11-19 03:05:47 +00:00
|
|
|
// rendered_element.take().unwrap().paint(view_state, cx);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2023-11-20 19:21:42 +00:00
|
|
|
// impl<V, E, F> RenderOnce for Option<F>
|
2023-11-19 03:05:47 +00:00
|
|
|
// where
|
|
|
|
// V: 'static,
|
2023-11-20 19:21:42 +00:00
|
|
|
// E: Element,
|
|
|
|
// F: FnOnce(&mut V, &mut WindowContext) -> E + 'static,
|
2023-11-19 03:05:47 +00:00
|
|
|
// {
|
|
|
|
// type Element = Self;
|
|
|
|
|
|
|
|
// fn render(self) -> Self::Element {
|
|
|
|
// self
|
2023-11-18 07:03:23 +00:00
|
|
|
// }
|
|
|
|
// }
|