diff --git a/Cargo.lock b/Cargo.lock index 1747eae2d25fb958fa096a748793a3b0f24ab02b..e196464aa9f14aacd5b56235e07f35a8b8a893c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3759,7 +3759,7 @@ dependencies = [ "smol", "sqlez", "sum_tree", - "taffy", + "taffy 0.3.11 (git+https://github.com/DioxusLabs/taffy?rev=4fb530bdd71609bb1d3f76c6a8bde1ba82805d5e)", "thiserror", "time", "tiny-skia", @@ -3824,7 +3824,7 @@ dependencies = [ "smol", "sqlez", "sum_tree", - "taffy", + "taffy 0.3.11 (git+https://github.com/DioxusLabs/taffy?rev=1876f72bee5e376023eaa518aa7b8a34c769bd1b)", "thiserror", "time", "tiny-skia", @@ -3859,6 +3859,12 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eec1c01eb1de97451ee0d60de7d81cf1e72aabefb021616027f3d1c3ec1c723c" +[[package]] +name = "grid" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df00eed8d1f0db937f6be10e46e8072b0671accb504cf0f959c5c52c679f5b9" + [[package]] name = "h2" version = "0.3.21" @@ -9053,13 +9059,24 @@ dependencies = [ "winx", ] +[[package]] +name = "taffy" +version = "0.3.11" +source = "git+https://github.com/DioxusLabs/taffy?rev=1876f72bee5e376023eaa518aa7b8a34c769bd1b#1876f72bee5e376023eaa518aa7b8a34c769bd1b" +dependencies = [ + "arrayvec 0.7.4", + "grid 0.11.0", + "num-traits", + "slotmap", +] + [[package]] name = "taffy" version = "0.3.11" source = "git+https://github.com/DioxusLabs/taffy?rev=4fb530bdd71609bb1d3f76c6a8bde1ba82805d5e#4fb530bdd71609bb1d3f76c6a8bde1ba82805d5e" dependencies = [ "arrayvec 0.7.4", - "grid", + "grid 0.10.0", "num-traits", "slotmap", ] diff --git a/crates/gpui2/Cargo.toml b/crates/gpui2/Cargo.toml index 1bec9d43dc34f8ebcd13b65ec962e67b16bf363c..afb5d3ea0ce7080830bbcb1eb86aad62b8f7432c 100644 --- a/crates/gpui2/Cargo.toml +++ b/crates/gpui2/Cargo.toml @@ -47,7 +47,7 @@ serde_derive.workspace = true serde_json.workspace = true smallvec.workspace = true smol.workspace = true -taffy = { git = "https://github.com/DioxusLabs/taffy", rev = "4fb530bdd71609bb1d3f76c6a8bde1ba82805d5e" } +taffy = { git = "https://github.com/DioxusLabs/taffy", rev = "1876f72bee5e376023eaa518aa7b8a34c769bd1b" } thiserror.workspace = true time.workspace = true tiny-skia = "0.5" diff --git a/crates/gpui2/src/taffy.rs b/crates/gpui2/src/taffy.rs index ea87f73872cd445ee37e530d973d5e0e054a76fd..5896011f96d6aaf952208abdab2a2240addb2876 100644 --- a/crates/gpui2/src/taffy.rs +++ b/crates/gpui2/src/taffy.rs @@ -5,12 +5,14 @@ use std::fmt::Debug; use taffy::{ geometry::{Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize}, style::AvailableSpace as TaffyAvailableSpace, - tree::{Measurable, MeasureFunc, NodeId}, + tree::NodeId, Taffy, }; +type Measureable = dyn Fn(Size>, Size) -> Size + Send + Sync; + pub struct TaffyLayoutEngine { - taffy: Taffy, + taffy: Taffy>, children_to_parents: HashMap, absolute_layout_bounds: HashMap>, computed_layouts: HashSet, @@ -70,9 +72,9 @@ impl TaffyLayoutEngine { ) -> LayoutId { let style = style.to_taffy(rem_size); - let measurable = Box::new(Measureable(measure)) as Box; + let measurable = Box::new(measure); self.taffy - .new_leaf_with_measure(style, MeasureFunc::Boxed(measurable)) + .new_leaf_with_context(style, measurable) .expect(EXPECT_MESSAGE) .into() } @@ -154,7 +156,22 @@ impl TaffyLayoutEngine { // let started_at = std::time::Instant::now(); self.taffy - .compute_layout(id.into(), available_space.into()) + .compute_layout_with_measure( + id.into(), + available_space.into(), + |known_dimensions, available_space, node_id, context| { + let Some(measure) = context else { + return taffy::geometry::Size::default(); + }; + + let known_dimensions = Size { + width: known_dimensions.width.map(Pixels), + height: known_dimensions.height.map(Pixels), + }; + + measure(known_dimensions, available_space.into()).into() + }, + ) .expect(EXPECT_MESSAGE); // println!("compute_layout took {:?}", started_at.elapsed()); } @@ -202,25 +219,6 @@ impl From for NodeId { } } -struct Measureable(F); - -impl taffy::tree::Measurable for Measureable -where - F: Fn(Size>, Size) -> Size + Send + Sync, -{ - fn measure( - &self, - known_dimensions: TaffySize>, - available_space: TaffySize, - ) -> TaffySize { - let known_dimensions: Size> = known_dimensions.into(); - let known_dimensions: Size> = known_dimensions.map(|d| d.map(Into::into)); - let available_space = available_space.into(); - let size = (self.0)(known_dimensions, available_space); - size.into() - } -} - trait ToTaffy { fn to_taffy(&self, rem_size: Pixels) -> Output; }