Introduce new PaintContext and LayoutContext

This commit is contained in:
Nathan Sobo 2023-08-13 21:57:38 -06:00
parent 2d17e9685f
commit 625e4a1bd0
3 changed files with 31 additions and 20 deletions

View file

@ -1,3 +1,4 @@
use crate::element::LayoutContext;
use util::ResultExt; use util::ResultExt;
use crate::element::AnyElement; use crate::element::AnyElement;
@ -12,16 +13,22 @@ impl<V: 'static> gpui::Element<V> for Adapter<V> {
&mut self, &mut self,
constraint: gpui::SizeConstraint, constraint: gpui::SizeConstraint,
view: &mut V, view: &mut V,
cx: &mut gpui::LayoutContext<V>, legacy_cx: &mut gpui::LayoutContext<V>,
) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) { ) -> (gpui::geometry::vector::Vector2F, Self::LayoutState) {
cx.push_layout_engine(); legacy_cx.push_layout_engine();
if let Some(node) = self.0.layout(view, cx).log_err() { let node = self
cx.layout_engine() .0
.layout(view, &mut LayoutContext { legacy_cx })
.log_err();
if let Some(node) = node {
legacy_cx
.layout_engine()
.unwrap() .unwrap()
.compute_layout(node, constraint.max) .compute_layout(node, constraint.max)
.log_err(); .log_err();
} }
cx.pop_layout_engine(); legacy_cx.pop_layout_engine();
(constraint.max, ()) (constraint.max, ())
} }

View file

@ -1,14 +1,27 @@
use crate::style::{DefinedLength, Display, Overflow, Position, Style}; use crate::style::{DefinedLength, Display, Overflow, Position, Style};
use anyhow::Result; use anyhow::Result;
use gpui::{Layout, LayoutContext, PaintContext}; use derive_more::{Deref, DerefMut};
use gpui::{Layout, LayoutContext as LegacyLayoutContext, PaintContext as LegacyPaintContext};
use playground_macros::tailwind_lengths; use playground_macros::tailwind_lengths;
pub use taffy::tree::NodeId; pub use taffy::tree::NodeId;
#[derive(Deref, DerefMut)]
pub struct LayoutContext<'a, 'b, 'c, 'd, V> {
pub(crate) legacy_cx: &'d mut LegacyLayoutContext<'a, 'b, 'c, V>,
}
#[derive(Deref, DerefMut)]
pub struct PaintContext<'a, 'b, 'c, 'd, V> {
#[deref]
#[deref_mut]
pub(crate) legacy_cx: &'d mut LegacyPaintContext<'a, 'b, 'c, V>,
scene: &'d mut gpui::SceneBuilder,
}
pub trait Element<V> { pub trait Element<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>;
fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut gpui::PaintContext<V>) fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext<V>) -> Result<()>;
-> Result<()>;
/// Convert to a dynamically-typed element suitable for layout and paint. /// Convert to a dynamically-typed element suitable for layout and paint.
fn into_any(self) -> AnyElement<V> fn into_any(self) -> AnyElement<V>

View file

@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use gpui::{Layout, LayoutNodeId}; use gpui::{Layout, LayoutNodeId};
use crate::{ use crate::{
element::{AnyElement, Element}, element::{AnyElement, Element, LayoutContext, PaintContext},
style::Style, style::Style,
}; };
@ -23,11 +23,7 @@ impl<V: 'static> Element<V> for Frame<V> {
&mut self.style &mut self.style
} }
fn layout( fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<taffy::tree::NodeId> {
&mut self,
view: &mut V,
cx: &mut gpui::LayoutContext<V>,
) -> Result<taffy::tree::NodeId> {
let child_layout_node_ids = self let child_layout_node_ids = self
.children .children
.iter_mut() .iter_mut()
@ -40,12 +36,7 @@ impl<V: 'static> Element<V> for Frame<V> {
.add_node(self.style.to_taffy(rem_size), child_layout_node_ids) .add_node(self.style.to_taffy(rem_size), child_layout_node_ids)
} }
fn paint( fn paint(&mut self, layout: Layout, view: &mut V, cx: &mut PaintContext<V>) -> Result<()> {
&mut self,
layout: Layout,
view: &mut V,
cx: &mut gpui::PaintContext<V>,
) -> Result<()> {
for child in &mut self.children { for child in &mut self.children {
child.paint(view, cx)?; child.paint(view, cx)?;
} }