Compiling checkpoint

This commit is contained in:
Nathan Sobo 2023-08-15 09:33:59 -06:00
parent 0fe457020b
commit e7489bd4c9
2 changed files with 46 additions and 26 deletions

View file

@ -1,3 +1,5 @@
use std::{any::Any, sync::Arc};
use crate::{ use crate::{
adapter::Adapter, adapter::Adapter,
style::{DefinedLength, Display, Fill, Length, Overflow, Position, Style}, style::{DefinedLength, Display, Fill, Length, Overflow, Position, Style},
@ -24,8 +26,11 @@ pub struct PaintContext<'a, 'b, 'c, 'd, V> {
} }
pub trait Element<V: 'static>: 'static + Clone { pub trait Element<V: 'static>: 'static + Clone {
type Layout: 'static;
fn style_mut(&mut self) -> &mut Style; fn style_mut(&mut self) -> &mut Style;
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId>; fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>)
-> Result<(NodeId, Self::Layout)>;
fn paint(&mut self, layout: EngineLayout, view: &mut V, cx: &mut PaintContext<V>) fn paint(&mut self, layout: EngineLayout, view: &mut V, cx: &mut PaintContext<V>)
-> Result<()>; -> Result<()>;
@ -36,7 +41,7 @@ pub trait Element<V: 'static>: 'static + Clone {
{ {
AnyElement { AnyElement {
element: Box::new(self) as Box<dyn ElementObject<V>>, element: Box::new(self) as Box<dyn ElementObject<V>>,
layout_node_id: None, layout: None,
} }
} }
@ -261,7 +266,8 @@ pub trait Element<V: 'static>: 'static + Clone {
pub trait ElementObject<V> { pub trait ElementObject<V> {
fn style_mut(&mut self) -> &mut Style; fn style_mut(&mut self) -> &mut Style;
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId>; fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>)
-> Result<(NodeId, Arc<dyn Any>)>;
fn paint(&mut self, layout: EngineLayout, view: &mut V, cx: &mut PaintContext<V>) fn paint(&mut self, layout: EngineLayout, view: &mut V, cx: &mut PaintContext<V>)
-> Result<()>; -> Result<()>;
fn clone_object(&self) -> Box<dyn ElementObject<V>>; fn clone_object(&self) -> Box<dyn ElementObject<V>>;
@ -272,8 +278,14 @@ impl<V: 'static, E: Element<V>> ElementObject<V> for E {
self.style_mut() self.style_mut()
} }
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId> { fn layout(
self.layout(view, cx) &mut self,
view: &mut V,
cx: &mut LayoutContext<V>,
) -> Result<(NodeId, Arc<dyn Any>)> {
let (node_id, layout) = self.layout(view, cx)?;
let layout = Arc::new(layout) as Arc<dyn Any>;
Ok((node_id, layout))
} }
fn paint( fn paint(
@ -292,24 +304,18 @@ impl<V: 'static, E: Element<V>> ElementObject<V> for E {
pub struct AnyElement<V> { pub struct AnyElement<V> {
element: Box<dyn ElementObject<V>>, element: Box<dyn ElementObject<V>>,
layout_node_id: Option<NodeId>, layout: Option<(NodeId, Arc<dyn Any>)>,
} }
// enum LayoutState {
// None,
// Registered(NodeId, Box<dyn Any>),
// Computed(Layout, Box<dyn Any>),
// }
impl<V> AnyElement<V> { impl<V> AnyElement<V> {
pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId> { pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId> {
let layout_node_id = self.element.layout(view, cx)?; let (node_id, layout) = self.element.layout(view, cx)?;
self.layout_node_id = Some(layout_node_id); self.layout = Some((node_id, layout));
Ok(layout_node_id) Ok(node_id)
} }
pub fn paint(&mut self, view: &mut V, cx: &mut PaintContext<V>) -> Result<()> { pub fn paint(&mut self, view: &mut V, cx: &mut PaintContext<V>) -> Result<()> {
let layout_node_id = self.layout_node_id.expect("paint called before layout"); let (layout_node_id, layout) = self.layout.clone().expect("paint called before layout");
let layout = cx let layout = cx
.layout_engine() .layout_engine()
.unwrap() .unwrap()
@ -323,18 +329,24 @@ impl<V> Clone for AnyElement<V> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
element: self.element.clone_object(), element: self.element.clone_object(),
layout_node_id: self.layout_node_id, layout: self.layout.clone(),
} }
} }
} }
impl<V: 'static> Element<V> for AnyElement<V> { impl<V: 'static> Element<V> for AnyElement<V> {
type Layout = ();
fn style_mut(&mut self) -> &mut Style { fn style_mut(&mut self) -> &mut Style {
self.element.style_mut() self.element.style_mut()
} }
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<NodeId> { fn layout(
self.layout(view, cx) &mut self,
view: &mut V,
cx: &mut LayoutContext<V>,
) -> Result<(NodeId, Self::Layout)> {
Ok((self.layout(view, cx)?, ()))
} }
fn paint( fn paint(

View file

@ -1,10 +1,9 @@
use anyhow::{anyhow, Result};
use gpui::{EngineLayout, LayoutNodeId};
use crate::{ use crate::{
element::{AnyElement, Element, LayoutContext, PaintContext}, element::{AnyElement, Element, LayoutContext, NodeId, PaintContext},
style::Style, style::Style,
}; };
use anyhow::{anyhow, Result};
use gpui::{EngineLayout, LayoutNodeId};
pub struct Frame<V> { pub struct Frame<V> {
style: Style, style: Style,
@ -19,11 +18,17 @@ pub fn frame<V>() -> Frame<V> {
} }
impl<V: 'static> Element<V> for Frame<V> { impl<V: 'static> Element<V> for Frame<V> {
type Layout = ();
fn style_mut(&mut self) -> &mut Style { fn style_mut(&mut self) -> &mut Style {
&mut self.style &mut self.style
} }
fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<taffy::tree::NodeId> { fn layout(
&mut self,
view: &mut V,
cx: &mut LayoutContext<V>,
) -> Result<(NodeId, Self::Layout)> {
let child_layout_node_ids = self let child_layout_node_ids = self
.children .children
.iter_mut() .iter_mut()
@ -31,9 +36,12 @@ impl<V: 'static> Element<V> for Frame<V> {
.collect::<Result<Vec<LayoutNodeId>>>()?; .collect::<Result<Vec<LayoutNodeId>>>()?;
let rem_size = cx.rem_pixels(); let rem_size = cx.rem_pixels();
cx.layout_engine() let node_id = cx
.layout_engine()
.ok_or_else(|| anyhow!("no layout engine"))? .ok_or_else(|| anyhow!("no layout engine"))?
.add_node(self.style.to_taffy(rem_size), child_layout_node_ids) .add_node(self.style.to_taffy(rem_size), child_layout_node_ids)?;
Ok((node_id, ()))
} }
fn paint( fn paint(