Relativize child layouts to their parent origin

Nathan Sobo created

Change summary

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(-)

Detailed changes

crates/gpui/playground/src/adapter.rs 🔗

@@ -47,7 +47,7 @@ impl<V: 'static> gpui::Element<V> for AdapterElement<V> {
         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());
     }

crates/gpui/playground/src/div.rs 🔗

@@ -72,7 +72,7 @@ impl<V: 'static> Element<V> for Div<V> {
         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();

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<V: 'static>: 'static {
 /// Used to make ElementState<V, E> into a trait object, so we can wrap it in AnyElement<V>.
 trait AnyStatefulElement<V> {
     fn layout(&mut self, view: &mut V, cx: &mut LayoutContext<V>) -> Result<LayoutId>;
-    fn paint(&mut self, view: &mut V, cx: &mut PaintContext<V>);
+    fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext<V>);
 }
 
 /// A wrapper around an element that stores its layout state.
@@ -91,13 +92,14 @@ impl<V, E: Element<V>> AnyStatefulElement<V> for StatefulElement<V, E> {
         result
     }
 
-    fn paint(&mut self, view: &mut V, cx: &mut PaintContext<V>) {
+    fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext<V>) {
         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<V> AnyElement<V> {
         self.0.layout(view, cx)
     }
 
-    pub fn paint(&mut self, view: &mut V, cx: &mut PaintContext<V>) {
-        self.0.paint(view, cx)
+    pub fn paint(&mut self, view: &mut V, parent_origin: Vector2F, cx: &mut PaintContext<V>) {
+        self.0.paint(view, parent_origin, cx)
     }
 }
 

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<V>,
             ) {
-                rendered_element.paint(view, cx);
+                rendered_element.paint(view, layout.bounds.origin(), cx);
             }
         }