From 5bb780073e1f3281316886ba7f9735806ac62e79 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 25 Aug 2023 23:22:53 -0600 Subject: [PATCH] Relativize child layouts to their parent origin --- crates/gpui/playground/src/adapter.rs | 2 +- crates/gpui/playground/src/div.rs | 2 +- crates/gpui/playground/src/element.rs | 12 +++++++----- crates/gpui/playground_macros/src/derive_element.rs | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/crates/gpui/playground/src/adapter.rs b/crates/gpui/playground/src/adapter.rs index 7a5a0399a8..eff2356c94 100644 --- a/crates/gpui/playground/src/adapter.rs +++ b/crates/gpui/playground/src/adapter.rs @@ -47,7 +47,7 @@ impl gpui::Element for AdapterElement { let (layout_engine, layout_id) = layout_data.take().unwrap(); legacy_cx.push_layout_engine(layout_engine); let mut cx = PaintContext::new(legacy_cx, scene); - self.0.paint(view, &mut cx); + self.0.paint(view, bounds.origin(), &mut cx); *layout_data = legacy_cx.pop_layout_engine().zip(Some(layout_id)); debug_assert!(layout_data.is_some()); } diff --git a/crates/gpui/playground/src/div.rs b/crates/gpui/playground/src/div.rs index 75ac5d965b..ca71ede6d4 100644 --- a/crates/gpui/playground/src/div.rs +++ b/crates/gpui/playground/src/div.rs @@ -72,7 +72,7 @@ impl Element for Div { self.interaction_handlers() .paint(layout.order, layout.bounds, cx); for child in &mut self.children { - child.paint(view, cx); + child.paint(view, layout.bounds.origin(), cx); } if pop_text_style { cx.pop_text_style(); diff --git a/crates/gpui/playground/src/element.rs b/crates/gpui/playground/src/element.rs index c39ee7e196..6091e17989 100644 --- a/crates/gpui/playground/src/element.rs +++ b/crates/gpui/playground/src/element.rs @@ -1,6 +1,7 @@ pub use crate::layout_context::LayoutContext; pub use crate::paint_context::PaintContext; use anyhow::Result; +use gpui::geometry::vector::Vector2F; pub use gpui::{Layout, LayoutId}; use smallvec::SmallVec; @@ -38,7 +39,7 @@ pub trait Element: 'static { /// Used to make ElementState into a trait object, so we can wrap it in AnyElement. trait AnyStatefulElement { fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> Result; - fn paint(&mut self, view: &mut V, cx: &mut PaintContext); + fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext); } /// A wrapper around an element that stores its layout state. @@ -91,13 +92,14 @@ impl> AnyStatefulElement for StatefulElement { result } - fn paint(&mut self, view: &mut V, cx: &mut PaintContext) { + fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext) { self.phase = match std::mem::take(&mut self.phase) { ElementPhase::PostLayout { layout_id, mut paint_state, } => match cx.computed_layout(layout_id) { - Ok(layout) => { + Ok(mut layout) => { + layout.bounds = layout.bounds + parent_origin; self.element.paint(view, &layout, &mut paint_state, cx); ElementPhase::PostPaint { layout, @@ -120,8 +122,8 @@ impl AnyElement { self.0.layout(view, cx) } - pub fn paint(&mut self, view: &mut V, cx: &mut PaintContext) { - self.0.paint(view, cx) + pub fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext) { + self.0.paint(view, parent_origin, cx) } } diff --git a/crates/gpui/playground_macros/src/derive_element.rs b/crates/gpui/playground_macros/src/derive_element.rs index 482f6ccaf4..180d19227d 100644 --- a/crates/gpui/playground_macros/src/derive_element.rs +++ b/crates/gpui/playground_macros/src/derive_element.rs @@ -81,7 +81,7 @@ pub fn derive_element(input: TokenStream) -> TokenStream { rendered_element: &mut Self::PaintState, cx: &mut playground::element::PaintContext, ) { - rendered_element.paint(view, cx); + rendered_element.paint(view, layout.bounds.origin(), cx); } }