From b30bb56483f24841900ba1a6e0c9717137df4bdb Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 14 Sep 2023 16:14:42 -0600 Subject: [PATCH] Checkpoint --- 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(-) diff --git a/crates/storybook/src/gpui3/app.rs b/crates/storybook/src/gpui3/app.rs index 8d501341d018b58ad87e0788587c690f188a3592..92e498e6adc95955e1593c74910070011ad1d351 100644 --- a/crates/storybook/src/gpui3/app.rs +++ b/crates/storybook/src/gpui3/app.rs @@ -11,7 +11,7 @@ pub struct AppContext { pub(crate) entities: SlotMap>>, pub(crate) windows: SlotMap>, // We recycle this memory across layout requests. - pub(crate) child_layout_buffer: Vec, + pub(crate) layout_id_buffer: Vec, } 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(), } } diff --git a/crates/storybook/src/gpui3/mod.rs b/crates/storybook/src/gpui3/mod.rs index cbe23ca355484c7ce3120992e394573cf79d9bb5..7349ed1e05a9785738de064b07a41828b937a4b2 100644 --- a/crates/storybook/src/gpui3/mod.rs +++ b/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::*; diff --git a/crates/storybook/src/gpui3/taffy.rs b/crates/storybook/src/gpui3/taffy.rs index 9fe8adefe7997193cad8f21610d0bd6cedb1e193..b11cbd08ff754be918e41f9035bace0b4a946b7a 100644 --- a/crates/storybook/src/gpui3/taffy.rs +++ b/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 { - todo!() + pub fn request_layout( + &mut self, + style: Style, + rem_size: Pixels, + children: &[LayoutId], + ) -> Result { + 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 { + pub fn layout(&mut self, id: LayoutId) -> Result { todo!() } } diff --git a/crates/storybook/src/gpui3/window.rs b/crates/storybook/src/gpui3/window.rs index feabe41aeb5b1b473b00fdec8104f18944dea90c..3b0f561643f7b1bd6c54d71503a1d25647e8d0bb 100644 --- a/crates/storybook/src/gpui3/window.rs +++ b/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, + layout_engine: TaffyLayoutEngine, pub(crate) root_view: Option>, } @@ -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, ) -> Result { - 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 { @@ -216,14 +221,3 @@ pub struct Layout { pub order: u32, pub bounds: Bounds, } - -#[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; - - /// Get the layout for the given id. - fn layout(&mut self, id: LayoutId) -> Result; -}