From 4e0c8dcea99ad6a71210760539ddfcc6fef3a94a Mon Sep 17 00:00:00 2001 From: Julia Date: Tue, 16 Jan 2024 10:44:24 -0500 Subject: [PATCH] Revert "Use taffy to retrieve the parent for a given layout node" This reverts commit 5904bcf1c20638d63b244a1b2b038ec9a664ba1c. Co-Authored-By: Antonio Scandurra --- Cargo.lock | 7 +++--- crates/gpui/Cargo.toml | 2 +- crates/gpui/src/taffy.rs | 46 ++++++++++++++++++++++++---------------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 090980ac3e..7b1fe5144c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3116,9 +3116,9 @@ dependencies = [ [[package]] name = "grid" -version = "0.13.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d196ffc1627db18a531359249b2bf8416178d84b729f3cebeb278f285fb9b58c" +checksum = "1df00eed8d1f0db937f6be10e46e8072b0671accb504cf0f959c5c52c679f5b9" [[package]] name = "h2" @@ -7703,12 +7703,11 @@ dependencies = [ [[package]] name = "taffy" version = "0.3.11" -source = "git+https://github.com/zed-industries/taffy?rev=5e6c2d23e70e9f2156911d11050cb686362ba277#5e6c2d23e70e9f2156911d11050cb686362ba277" +source = "git+https://github.com/DioxusLabs/taffy?rev=1876f72bee5e376023eaa518aa7b8a34c769bd1b#1876f72bee5e376023eaa518aa7b8a34c769bd1b" dependencies = [ "arrayvec 0.7.4", "grid", "num-traits", - "serde", "slotmap", ] diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index 333f5a1649..ee75492873 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -46,7 +46,7 @@ serde_derive.workspace = true serde_json.workspace = true smallvec.workspace = true smol.workspace = true -taffy = { git = "https://github.com/zed-industries/taffy", rev = "5e6c2d23e70e9f2156911d11050cb686362ba277" } +taffy = { git = "https://github.com/DioxusLabs/taffy", rev = "1876f72bee5e376023eaa518aa7b8a34c769bd1b" } thiserror.workspace = true time.workspace = true tiny-skia = "0.5" diff --git a/crates/gpui/src/taffy.rs b/crates/gpui/src/taffy.rs index 40098b090b..26d5a2e69e 100644 --- a/crates/gpui/src/taffy.rs +++ b/crates/gpui/src/taffy.rs @@ -6,13 +6,16 @@ use collections::{FxHashMap, FxHashSet}; use smallvec::SmallVec; use std::fmt::Debug; use taffy::{ - AvailableSpace as TaffyAvailableSpace, NodeId, Point as TaffyPoint, Rect as TaffyRect, - Size as TaffySize, TaffyTree, TraversePartialTree, + geometry::{Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize}, + style::AvailableSpace as TaffyAvailableSpace, + tree::NodeId, + Taffy, }; pub struct TaffyLayoutEngine { - tree: TaffyTree, + taffy: Taffy, styles: FxHashMap, + children_to_parents: FxHashMap, absolute_layout_bounds: FxHashMap>, computed_layouts: FxHashSet, nodes_to_measure: FxHashMap< @@ -32,8 +35,9 @@ static EXPECT_MESSAGE: &str = "we should avoid taffy layout errors by constructi impl TaffyLayoutEngine { pub fn new() -> Self { TaffyLayoutEngine { - tree: TaffyTree::new(), + taffy: Taffy::new(), styles: FxHashMap::default(), + children_to_parents: FxHashMap::default(), absolute_layout_bounds: FxHashMap::default(), computed_layouts: FxHashSet::default(), nodes_to_measure: FxHashMap::default(), @@ -41,7 +45,8 @@ impl TaffyLayoutEngine { } pub fn clear(&mut self) { - self.tree.clear(); + self.taffy.clear(); + self.children_to_parents.clear(); self.absolute_layout_bounds.clear(); self.computed_layouts.clear(); self.nodes_to_measure.clear(); @@ -60,16 +65,21 @@ impl TaffyLayoutEngine { ) -> LayoutId { let taffy_style = style.to_taffy(rem_size); let layout_id = if children.is_empty() { - self.tree + self.taffy .new_leaf(taffy_style) .expect(EXPECT_MESSAGE) .into() } else { - self.tree + let parent_id = self + .taffy // This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId. .new_with_children(taffy_style, unsafe { std::mem::transmute(children) }) .expect(EXPECT_MESSAGE) - .into() + .into(); + for child_id in children { + self.children_to_parents.insert(*child_id, parent_id); + } + parent_id }; self.styles.insert(layout_id, style.clone()); layout_id @@ -86,7 +96,7 @@ impl TaffyLayoutEngine { let taffy_style = style.to_taffy(rem_size); let layout_id = self - .tree + .taffy .new_leaf_with_context(taffy_style, ()) .expect(EXPECT_MESSAGE) .into(); @@ -100,7 +110,7 @@ impl TaffyLayoutEngine { fn count_all_children(&self, parent: LayoutId) -> anyhow::Result { let mut count = 0; - for child in self.tree.children(parent.0)? { + for child in self.taffy.children(parent.0)? { // Count this child. count += 1; @@ -116,12 +126,12 @@ impl TaffyLayoutEngine { fn max_depth(&self, depth: u32, parent: LayoutId) -> anyhow::Result { println!( "{parent:?} at depth {depth} has {} children", - self.tree.child_count(parent.0) + self.taffy.child_count(parent.0)? ); let mut max_child_depth = 0; - for child in self.tree.children(parent.0)? { + for child in self.taffy.children(parent.0)? { max_child_depth = std::cmp::max(max_child_depth, self.max_depth(0, LayoutId(child))?); } @@ -133,7 +143,7 @@ impl TaffyLayoutEngine { fn get_edges(&self, parent: LayoutId) -> anyhow::Result> { let mut edges = Vec::new(); - for child in self.tree.children(parent.0)? { + for child in self.taffy.children(parent.0)? { edges.push((parent, LayoutId(child))); edges.extend(self.get_edges(LayoutId(child))?); @@ -166,7 +176,7 @@ impl TaffyLayoutEngine { while let Some(id) = stack.pop() { self.absolute_layout_bounds.remove(&id); stack.extend( - self.tree + self.taffy .children(id.into()) .expect(EXPECT_MESSAGE) .into_iter() @@ -176,7 +186,7 @@ impl TaffyLayoutEngine { } // let started_at = std::time::Instant::now(); - self.tree + self.taffy .compute_layout_with_measure( id.into(), available_space.into(), @@ -203,14 +213,14 @@ impl TaffyLayoutEngine { return layout; } - let layout = self.tree.layout(id.into()).expect(EXPECT_MESSAGE); + let layout = self.taffy.layout(id.into()).expect(EXPECT_MESSAGE); let mut bounds = Bounds { origin: layout.location.into(), size: layout.size.into(), }; - if let Some(parent_id) = self.tree.parent(id.0) { - let parent_bounds = self.layout_bounds(parent_id.into()); + if let Some(parent_id) = self.children_to_parents.get(&id).copied() { + let parent_bounds = self.layout_bounds(parent_id); bounds.origin += parent_bounds.origin; } self.absolute_layout_bounds.insert(id, bounds);