Checkpoint

This commit is contained in:
Nathan Sobo 2023-08-21 16:14:59 -06:00
parent f4d8763d2b
commit ff7b25c538
6 changed files with 146 additions and 133 deletions

View file

@ -44,7 +44,7 @@ impl<V: 'static, D> Layout<V, D> {
}
}
pub trait Element<V> {
pub trait Element<V: 'static>: 'static {
type Layout;
fn layout(
@ -63,30 +63,33 @@ pub trait Element<V> {
) where
Self: Sized;
fn into_any(mut self) -> AnyElement<V>
fn into_any(self) -> AnyElement<V>
where
Self: Sized,
Self: 'static + Sized,
{
AnyElement(Box::new(ElementWithLayout {
AnyElement(Box::new(ElementState {
element: self,
layout: None,
}))
}
}
trait ElementTraitObject<V> {
/// Used to make ElementState<V, E> into a trait object, so we can wrap it in AnyElement<V>.
trait ElementStateObject<V> {
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<LayoutId>;
fn paint(&mut self, view: &mut V, layout_id: LayoutId, cx: &mut PaintContext<V>);
}
struct ElementWithLayout<V, E: Element<V>> {
/// A wrapper around an element that stores its layout state.
struct ElementState<V: 'static, E: Element<V>> {
element: E,
layout: Option<Layout<V, E::Layout>>,
}
impl<V, E: Element<V>> ElementTraitObject<V> for ElementWithLayout<V, E> {
/// We blanket-implement the object-safe ElementStateObject interface to make ElementStates into trait objects
impl<V, E: Element<V>> ElementStateObject<V> for ElementState<V, E> {
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<LayoutId> {
let layout = Element::layout(self, view, cx)?;
let layout = self.element.layout(view, cx)?;
let layout_id = layout.id;
self.layout = Some(layout);
Ok(layout_id)
@ -94,23 +97,24 @@ impl<V, E: Element<V>> ElementTraitObject<V> for ElementWithLayout<V, E> {
fn paint(&mut self, view: &mut V, layout_id: LayoutId, cx: &mut PaintContext<V>) {
let layout = self.layout.as_mut().expect("paint called before layout");
Element::paint(self, view, layout, cx);
self.element.paint(view, layout, cx)
}
}
pub struct AnyElement<V>(Box<dyn ElementTraitObject<V>>);
/// A dynamic element.
pub struct AnyElement<V>(Box<dyn ElementStateObject<V>>);
impl<V> AnyElement<V> {
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<LayoutId> {
pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<LayoutId> {
self.0.layout(view, cx)
}
fn paint(&mut self, view: &mut V, layout_id: LayoutId, cx: &mut PaintContext<V>) {
pub fn paint(&mut self, view: &mut V, layout_id: LayoutId, cx: &mut PaintContext<V>) {
self.0.paint(view, layout_id, cx)
}
}
pub trait ParentElement<V> {
pub trait ParentElement<V: 'static> {
fn children_mut(&mut self) -> &mut Vec<AnyElement<V>>;
fn child(mut self, child: impl IntoElement<V>) -> Self
@ -136,7 +140,7 @@ pub trait ParentElement<V> {
}
}
pub trait IntoElement<V> {
pub trait IntoElement<V: 'static> {
type Element: Element<V>;
fn into_element(self) -> Self::Element;