Use taffy to retrieve the parent for a given layout node
This commit is contained in:
parent
53564fb269
commit
5904bcf1c2
3 changed files with 23 additions and 32 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -3091,9 +3091,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "grid"
|
name = "grid"
|
||||||
version = "0.11.0"
|
version = "0.13.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1df00eed8d1f0db937f6be10e46e8072b0671accb504cf0f959c5c52c679f5b9"
|
checksum = "d196ffc1627db18a531359249b2bf8416178d84b729f3cebeb278f285fb9b58c"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "h2"
|
name = "h2"
|
||||||
|
@ -7665,11 +7665,12 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "taffy"
|
name = "taffy"
|
||||||
version = "0.3.11"
|
version = "0.3.11"
|
||||||
source = "git+https://github.com/DioxusLabs/taffy?rev=1876f72bee5e376023eaa518aa7b8a34c769bd1b#1876f72bee5e376023eaa518aa7b8a34c769bd1b"
|
source = "git+https://github.com/zed-industries/taffy?rev=5e6c2d23e70e9f2156911d11050cb686362ba277#5e6c2d23e70e9f2156911d11050cb686362ba277"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arrayvec 0.7.4",
|
"arrayvec 0.7.4",
|
||||||
"grid",
|
"grid",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
"serde",
|
||||||
"slotmap",
|
"slotmap",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ serde_derive.workspace = true
|
||||||
serde_json.workspace = true
|
serde_json.workspace = true
|
||||||
smallvec.workspace = true
|
smallvec.workspace = true
|
||||||
smol.workspace = true
|
smol.workspace = true
|
||||||
taffy = { git = "https://github.com/DioxusLabs/taffy", rev = "1876f72bee5e376023eaa518aa7b8a34c769bd1b" }
|
taffy = { git = "https://github.com/zed-industries/taffy", rev = "5e6c2d23e70e9f2156911d11050cb686362ba277" }
|
||||||
thiserror.workspace = true
|
thiserror.workspace = true
|
||||||
time.workspace = true
|
time.workspace = true
|
||||||
tiny-skia = "0.5"
|
tiny-skia = "0.5"
|
||||||
|
|
|
@ -6,15 +6,12 @@ use collections::{FxHashMap, FxHashSet};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use taffy::{
|
use taffy::{
|
||||||
geometry::{Point as TaffyPoint, Rect as TaffyRect, Size as TaffySize},
|
AvailableSpace as TaffyAvailableSpace, NodeId, Point as TaffyPoint, Rect as TaffyRect,
|
||||||
style::AvailableSpace as TaffyAvailableSpace,
|
Size as TaffySize, TaffyTree, TraversePartialTree,
|
||||||
tree::NodeId,
|
|
||||||
Taffy,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct TaffyLayoutEngine {
|
pub struct TaffyLayoutEngine {
|
||||||
taffy: Taffy,
|
tree: TaffyTree,
|
||||||
children_to_parents: FxHashMap<LayoutId, LayoutId>,
|
|
||||||
absolute_layout_bounds: FxHashMap<LayoutId, Bounds<Pixels>>,
|
absolute_layout_bounds: FxHashMap<LayoutId, Bounds<Pixels>>,
|
||||||
computed_layouts: FxHashSet<LayoutId>,
|
computed_layouts: FxHashSet<LayoutId>,
|
||||||
nodes_to_measure: FxHashMap<
|
nodes_to_measure: FxHashMap<
|
||||||
|
@ -34,8 +31,7 @@ static EXPECT_MESSAGE: &str = "we should avoid taffy layout errors by constructi
|
||||||
impl TaffyLayoutEngine {
|
impl TaffyLayoutEngine {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
TaffyLayoutEngine {
|
TaffyLayoutEngine {
|
||||||
taffy: Taffy::new(),
|
tree: TaffyTree::new(),
|
||||||
children_to_parents: FxHashMap::default(),
|
|
||||||
absolute_layout_bounds: FxHashMap::default(),
|
absolute_layout_bounds: FxHashMap::default(),
|
||||||
computed_layouts: FxHashSet::default(),
|
computed_layouts: FxHashSet::default(),
|
||||||
nodes_to_measure: FxHashMap::default(),
|
nodes_to_measure: FxHashMap::default(),
|
||||||
|
@ -43,8 +39,7 @@ impl TaffyLayoutEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
self.taffy.clear();
|
self.tree.clear();
|
||||||
self.children_to_parents.clear();
|
|
||||||
self.absolute_layout_bounds.clear();
|
self.absolute_layout_bounds.clear();
|
||||||
self.computed_layouts.clear();
|
self.computed_layouts.clear();
|
||||||
self.nodes_to_measure.clear();
|
self.nodes_to_measure.clear();
|
||||||
|
@ -58,18 +53,13 @@ impl TaffyLayoutEngine {
|
||||||
) -> LayoutId {
|
) -> LayoutId {
|
||||||
let style = style.to_taffy(rem_size);
|
let style = style.to_taffy(rem_size);
|
||||||
if children.is_empty() {
|
if children.is_empty() {
|
||||||
self.taffy.new_leaf(style).expect(EXPECT_MESSAGE).into()
|
self.tree.new_leaf(style).expect(EXPECT_MESSAGE).into()
|
||||||
} else {
|
} else {
|
||||||
let parent_id = self
|
self.tree
|
||||||
.taffy
|
|
||||||
// This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
|
// This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId.
|
||||||
.new_with_children(style, unsafe { std::mem::transmute(children) })
|
.new_with_children(style, unsafe { std::mem::transmute(children) })
|
||||||
.expect(EXPECT_MESSAGE)
|
.expect(EXPECT_MESSAGE)
|
||||||
.into();
|
.into()
|
||||||
for child_id in children {
|
|
||||||
self.children_to_parents.insert(*child_id, parent_id);
|
|
||||||
}
|
|
||||||
parent_id
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +73,7 @@ impl TaffyLayoutEngine {
|
||||||
let style = style.to_taffy(rem_size);
|
let style = style.to_taffy(rem_size);
|
||||||
|
|
||||||
let layout_id = self
|
let layout_id = self
|
||||||
.taffy
|
.tree
|
||||||
.new_leaf_with_context(style, ())
|
.new_leaf_with_context(style, ())
|
||||||
.expect(EXPECT_MESSAGE)
|
.expect(EXPECT_MESSAGE)
|
||||||
.into();
|
.into();
|
||||||
|
@ -96,7 +86,7 @@ impl TaffyLayoutEngine {
|
||||||
fn count_all_children(&self, parent: LayoutId) -> anyhow::Result<u32> {
|
fn count_all_children(&self, parent: LayoutId) -> anyhow::Result<u32> {
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
|
|
||||||
for child in self.taffy.children(parent.0)? {
|
for child in self.tree.children(parent.0)? {
|
||||||
// Count this child.
|
// Count this child.
|
||||||
count += 1;
|
count += 1;
|
||||||
|
|
||||||
|
@ -112,12 +102,12 @@ impl TaffyLayoutEngine {
|
||||||
fn max_depth(&self, depth: u32, parent: LayoutId) -> anyhow::Result<u32> {
|
fn max_depth(&self, depth: u32, parent: LayoutId) -> anyhow::Result<u32> {
|
||||||
println!(
|
println!(
|
||||||
"{parent:?} at depth {depth} has {} children",
|
"{parent:?} at depth {depth} has {} children",
|
||||||
self.taffy.child_count(parent.0)?
|
self.tree.child_count(parent.0)
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut max_child_depth = 0;
|
let mut max_child_depth = 0;
|
||||||
|
|
||||||
for child in self.taffy.children(parent.0)? {
|
for child in self.tree.children(parent.0)? {
|
||||||
max_child_depth = std::cmp::max(max_child_depth, self.max_depth(0, LayoutId(child))?);
|
max_child_depth = std::cmp::max(max_child_depth, self.max_depth(0, LayoutId(child))?);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +119,7 @@ impl TaffyLayoutEngine {
|
||||||
fn get_edges(&self, parent: LayoutId) -> anyhow::Result<Vec<(LayoutId, LayoutId)>> {
|
fn get_edges(&self, parent: LayoutId) -> anyhow::Result<Vec<(LayoutId, LayoutId)>> {
|
||||||
let mut edges = Vec::new();
|
let mut edges = Vec::new();
|
||||||
|
|
||||||
for child in self.taffy.children(parent.0)? {
|
for child in self.tree.children(parent.0)? {
|
||||||
edges.push((parent, LayoutId(child)));
|
edges.push((parent, LayoutId(child)));
|
||||||
|
|
||||||
edges.extend(self.get_edges(LayoutId(child))?);
|
edges.extend(self.get_edges(LayoutId(child))?);
|
||||||
|
@ -162,7 +152,7 @@ impl TaffyLayoutEngine {
|
||||||
while let Some(id) = stack.pop() {
|
while let Some(id) = stack.pop() {
|
||||||
self.absolute_layout_bounds.remove(&id);
|
self.absolute_layout_bounds.remove(&id);
|
||||||
stack.extend(
|
stack.extend(
|
||||||
self.taffy
|
self.tree
|
||||||
.children(id.into())
|
.children(id.into())
|
||||||
.expect(EXPECT_MESSAGE)
|
.expect(EXPECT_MESSAGE)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -172,7 +162,7 @@ impl TaffyLayoutEngine {
|
||||||
}
|
}
|
||||||
|
|
||||||
// let started_at = std::time::Instant::now();
|
// let started_at = std::time::Instant::now();
|
||||||
self.taffy
|
self.tree
|
||||||
.compute_layout_with_measure(
|
.compute_layout_with_measure(
|
||||||
id.into(),
|
id.into(),
|
||||||
available_space.into(),
|
available_space.into(),
|
||||||
|
@ -199,14 +189,14 @@ impl TaffyLayoutEngine {
|
||||||
return layout;
|
return layout;
|
||||||
}
|
}
|
||||||
|
|
||||||
let layout = self.taffy.layout(id.into()).expect(EXPECT_MESSAGE);
|
let layout = self.tree.layout(id.into()).expect(EXPECT_MESSAGE);
|
||||||
let mut bounds = Bounds {
|
let mut bounds = Bounds {
|
||||||
origin: layout.location.into(),
|
origin: layout.location.into(),
|
||||||
size: layout.size.into(),
|
size: layout.size.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(parent_id) = self.children_to_parents.get(&id).copied() {
|
if let Some(parent_id) = self.tree.parent(id.0) {
|
||||||
let parent_bounds = self.layout_bounds(parent_id);
|
let parent_bounds = self.layout_bounds(parent_id.into());
|
||||||
bounds.origin += parent_bounds.origin;
|
bounds.origin += parent_bounds.origin;
|
||||||
}
|
}
|
||||||
self.absolute_layout_bounds.insert(id, bounds);
|
self.absolute_layout_bounds.insert(id, bounds);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue