Checkpoint

Nathan Sobo created

Change summary

crates/storybook/src/gpui3/app.rs    |  4 ++--
crates/storybook/src/gpui3/mod.rs    |  1 +
crates/storybook/src/gpui3/taffy.rs  | 25 ++++++++++++++++---------
crates/storybook/src/gpui3/window.rs | 28 +++++++++++-----------------
4 files changed, 30 insertions(+), 28 deletions(-)

Detailed changes

crates/storybook/src/gpui3/app.rs 🔗

@@ -11,7 +11,7 @@ pub struct AppContext {
     pub(crate) entities: SlotMap<EntityId, Option<Box<dyn Any>>>,
     pub(crate) windows: SlotMap<WindowId, Option<Window>>,
     // We recycle this memory across layout requests.
-    pub(crate) child_layout_buffer: Vec<LayoutId>,
+    pub(crate) layout_id_buffer: Vec<LayoutId>,
 }
 
 impl AppContext {
@@ -19,7 +19,7 @@ impl AppContext {
         AppContext {
             entities: SlotMap::with_key(),
             windows: SlotMap::with_key(),
-            child_layout_buffer: Default::default(),
+            layout_id_buffer: Default::default(),
         }
     }
 

crates/storybook/src/gpui3/mod.rs 🔗

@@ -15,6 +15,7 @@ pub use element::*;
 pub use elements::*;
 pub use geometry::*;
 pub use style::*;
+pub use taffy::LayoutId;
 use taffy::TaffyLayoutEngine;
 pub use window::*;
 

crates/storybook/src/gpui3/taffy.rs 🔗

@@ -1,26 +1,33 @@
 use super::{
-    AbsoluteLength, Bounds, DefiniteLength, Edges, Layout, LayoutEngine, LayoutId, Length, Pixels,
-    Point, Result, Size, Style,
+    AbsoluteLength, Bounds, DefiniteLength, Edges, Layout, Length, Pixels, Point, Result, Size,
+    Style,
 };
 use gpui2::taffy::{self, Taffy};
 use std::fmt::Debug;
 
-pub use gpui2::taffy::tree::NodeId;
-
+pub use gpui2::taffy::tree::NodeId as LayoutId;
 pub struct TaffyLayoutEngine(Taffy);
 
 impl TaffyLayoutEngine {
     pub fn new() -> Self {
         TaffyLayoutEngine(Taffy::new())
     }
-}
 
-impl LayoutEngine for TaffyLayoutEngine {
-    fn request_layout(&mut self, style: Style, children: &[LayoutId]) -> Result<LayoutId> {
-        todo!()
+    pub fn request_layout(
+        &mut self,
+        style: Style,
+        rem_size: Pixels,
+        children: &[LayoutId],
+    ) -> Result<LayoutId> {
+        let style = style.to_taffy(rem_size);
+        if children.is_empty() {
+            Ok(self.0.new_leaf(style)?)
+        } else {
+            Ok(self.0.new_with_children(style, children)?)
+        }
     }
 
-    fn layout(&mut self, id: LayoutId) -> Result<Layout> {
+    pub fn layout(&mut self, id: LayoutId) -> Result<Layout> {
         todo!()
     }
 }

crates/storybook/src/gpui3/window.rs 🔗

@@ -1,4 +1,7 @@
-use super::{px, AppContext, Bounds, Context, EntityId, Handle, Pixels, Style, TaffyLayoutEngine};
+use super::{
+    px, taffy::LayoutId, AppContext, Bounds, Context, EntityId, Handle, Pixels, Style,
+    TaffyLayoutEngine,
+};
 use anyhow::Result;
 use derive_more::{Deref, DerefMut};
 use gpui2::Reference;
@@ -9,7 +12,7 @@ pub struct AnyWindow {}
 pub struct Window {
     id: WindowId,
     rem_size: Pixels,
-    layout_engine: Box<dyn LayoutEngine>,
+    layout_engine: TaffyLayoutEngine,
     pub(crate) root_view: Option<Box<dyn Any>>,
 }
 
@@ -17,7 +20,7 @@ impl Window {
     pub fn new(id: WindowId) -> Window {
         Window {
             id,
-            layout_engine: Box::new(TaffyLayoutEngine::new()),
+            layout_engine: TaffyLayoutEngine::new(),
             rem_size: px(16.),
             root_view: None,
         }
@@ -52,11 +55,13 @@ impl<'a, 'w> WindowContext<'a, 'w> {
         style: Style,
         children: impl IntoIterator<Item = LayoutId>,
     ) -> Result<LayoutId> {
-        self.app.child_layout_buffer.clear();
-        self.app.child_layout_buffer.extend(children.into_iter());
+        self.app.layout_id_buffer.clear();
+        self.app.layout_id_buffer.extend(children.into_iter());
+        let rem_size = self.rem_size();
+
         self.window
             .layout_engine
-            .request_layout(style, &self.app.child_layout_buffer)
+            .request_layout(style, rem_size, &self.app.layout_id_buffer)
     }
 
     pub fn layout(&mut self, layout_id: LayoutId) -> Result<Layout> {
@@ -216,14 +221,3 @@ pub struct Layout {
     pub order: u32,
     pub bounds: Bounds<Pixels>,
 }
-
-#[derive(Copy, Clone)]
-pub struct LayoutId(slotmap::DefaultKey);
-
-pub trait LayoutEngine {
-    /// Register a new node on which to perform layout.
-    fn request_layout(&mut self, style: Style, children: &[LayoutId]) -> Result<LayoutId>;
-
-    /// Get the layout for the given id.
-    fn layout(&mut self, id: LayoutId) -> Result<Layout>;
-}