gpui: Store measure functions as context of taffy nodes (#18732)
Taffy maintains a mapping of NodeId <-> Context anyways (and does the lookup), so it's redundant for us to store it separately. Tl;dr: we get rid of one map and one map lookup per layout request. Release Notes: - N/A
This commit is contained in:
parent
37ded190cf
commit
bafd7ed000
1 changed files with 13 additions and 14 deletions
|
@ -15,12 +15,13 @@ use taffy::{
|
||||||
type NodeMeasureFn =
|
type NodeMeasureFn =
|
||||||
Box<dyn FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>>;
|
Box<dyn FnMut(Size<Option<Pixels>>, Size<AvailableSpace>, &mut WindowContext) -> Size<Pixels>>;
|
||||||
|
|
||||||
|
struct NodeContext {
|
||||||
|
measure: NodeMeasureFn,
|
||||||
|
}
|
||||||
pub struct TaffyLayoutEngine {
|
pub struct TaffyLayoutEngine {
|
||||||
taffy: TaffyTree<()>,
|
taffy: TaffyTree<NodeContext>,
|
||||||
styles: FxHashMap<LayoutId, Style>,
|
|
||||||
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<LayoutId, NodeMeasureFn>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const EXPECT_MESSAGE: &str = "we should avoid taffy layout errors by construction if possible";
|
const EXPECT_MESSAGE: &str = "we should avoid taffy layout errors by construction if possible";
|
||||||
|
@ -29,10 +30,8 @@ impl TaffyLayoutEngine {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
TaffyLayoutEngine {
|
TaffyLayoutEngine {
|
||||||
taffy: TaffyTree::new(),
|
taffy: TaffyTree::new(),
|
||||||
styles: 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(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,8 +39,6 @@ impl TaffyLayoutEngine {
|
||||||
self.taffy.clear();
|
self.taffy.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.styles.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request_layout(
|
pub fn request_layout(
|
||||||
|
@ -67,7 +64,6 @@ impl TaffyLayoutEngine {
|
||||||
.into();
|
.into();
|
||||||
parent_id
|
parent_id
|
||||||
};
|
};
|
||||||
self.styles.insert(layout_id, style);
|
|
||||||
layout_id
|
layout_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,11 +78,14 @@ impl TaffyLayoutEngine {
|
||||||
|
|
||||||
let layout_id = self
|
let layout_id = self
|
||||||
.taffy
|
.taffy
|
||||||
.new_leaf_with_context(taffy_style, ())
|
.new_leaf_with_context(
|
||||||
|
taffy_style,
|
||||||
|
NodeContext {
|
||||||
|
measure: Box::new(measure),
|
||||||
|
},
|
||||||
|
)
|
||||||
.expect(EXPECT_MESSAGE)
|
.expect(EXPECT_MESSAGE)
|
||||||
.into();
|
.into();
|
||||||
self.nodes_to_measure.insert(layout_id, Box::new(measure));
|
|
||||||
self.styles.insert(layout_id, style);
|
|
||||||
layout_id
|
layout_id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -175,8 +174,8 @@ impl TaffyLayoutEngine {
|
||||||
.compute_layout_with_measure(
|
.compute_layout_with_measure(
|
||||||
id.into(),
|
id.into(),
|
||||||
available_space.into(),
|
available_space.into(),
|
||||||
|known_dimensions, available_space, node_id, _context, _style| {
|
|known_dimensions, available_space, _node_id, node_context, _style| {
|
||||||
let Some(measure) = self.nodes_to_measure.get_mut(&node_id.into()) else {
|
let Some(node_context) = node_context else {
|
||||||
return taffy::geometry::Size::default();
|
return taffy::geometry::Size::default();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -185,7 +184,7 @@ impl TaffyLayoutEngine {
|
||||||
height: known_dimensions.height.map(Pixels),
|
height: known_dimensions.height.map(Pixels),
|
||||||
};
|
};
|
||||||
|
|
||||||
measure(known_dimensions, available_space.into(), cx).into()
|
(node_context.measure)(known_dimensions, available_space.into(), cx).into()
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.expect(EXPECT_MESSAGE);
|
.expect(EXPECT_MESSAGE);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue