@@ -7,7 +7,7 @@ use crate::compute::taffy_tree::{compute_layout, measure_node_size, perform_node
7
7
use crate :: geometry:: { Line , Size } ;
8
8
use crate :: prelude:: LayoutTree ;
9
9
use crate :: style:: { AvailableSpace , Style } ;
10
- use crate :: tree:: { Layout , MeasureFunc , NodeData , NodeId , SizeBaselinesAndMargins , SizingMode } ;
10
+ use crate :: tree:: { Layout , Measurable , MeasureFunc , NodeData , NodeId , SizeBaselinesAndMargins , SizingMode } ;
11
11
use crate :: util:: sys:: { new_vec_with_capacity, ChildrenVec , Vec } ;
12
12
13
13
use super :: { TaffyError , TaffyResult } ;
@@ -25,12 +25,15 @@ impl Default for TaffyConfig {
25
25
}
26
26
27
27
/// A tree of UI nodes suitable for UI layout
28
- pub struct Taffy {
28
+ pub struct Taffy < Measure = MeasureFunc < ( ) > >
29
+ where
30
+ Measure : Measurable ,
31
+ {
29
32
/// The [`NodeData`] for each node stored in this tree
30
33
pub ( crate ) nodes : SlotMap < DefaultKey , NodeData > ,
31
34
32
35
/// Functions/closures that compute the intrinsic size of leaf nodes
33
- pub ( crate ) measure_funcs : SparseSecondaryMap < DefaultKey , MeasureFunc > ,
36
+ pub ( crate ) measure_funcs : SparseSecondaryMap < DefaultKey , MeasureFunc < Measure :: Context > > ,
34
37
35
38
/// The children of each node
36
39
///
@@ -44,6 +47,9 @@ pub struct Taffy {
44
47
45
48
/// Layout mode configuration
46
49
pub ( crate ) config : TaffyConfig ,
50
+
51
+ /// Used to store the context during layout. Cleared before returning from `compute_layout`.
52
+ pub ( crate ) context : Option < Measure :: Context > ,
47
53
}
48
54
49
55
impl Default for Taffy {
@@ -62,8 +68,8 @@ impl<'a> Iterator for TaffyChildIter<'a> {
62
68
}
63
69
}
64
70
65
- impl LayoutTree for Taffy {
66
- type ChildIter < ' a > = TaffyChildIter < ' a > ;
71
+ impl < Measure : Measurable > LayoutTree for Taffy < Measure > {
72
+ type ChildIter < ' a > = TaffyChildIter < ' a > where Measure : ' a ;
67
73
68
74
#[ inline( always) ]
69
75
fn children ( & self , node : NodeId ) -> Self :: ChildIter < ' _ > {
@@ -139,29 +145,7 @@ impl LayoutTree for Taffy {
139
145
}
140
146
141
147
#[ allow( clippy:: iter_cloned_collect) ] // due to no-std support, we need to use `iter_cloned` instead of `collect`
142
- impl Taffy {
143
- /// Creates a new [`Taffy`]
144
- ///
145
- /// The default capacity of a [`Taffy`] is 16 nodes.
146
- #[ must_use]
147
- pub fn new ( ) -> Self {
148
- Self :: with_capacity ( 16 )
149
- }
150
-
151
- /// Creates a new [`Taffy`] that can store `capacity` nodes before reallocation
152
- #[ must_use]
153
- pub fn with_capacity ( capacity : usize ) -> Self {
154
- Self {
155
- // TODO: make this method const upstream,
156
- // so constructors here can be const
157
- nodes : SlotMap :: with_capacity ( capacity) ,
158
- children : SlotMap :: with_capacity ( capacity) ,
159
- parents : SlotMap :: with_capacity ( capacity) ,
160
- measure_funcs : SparseSecondaryMap :: with_capacity ( capacity) ,
161
- config : TaffyConfig :: default ( ) ,
162
- }
163
- }
164
-
148
+ impl < Measure : Measurable > Taffy < Measure > {
165
149
/// Enable rounding of layout values. Rounding is enabled by default.
166
150
pub fn enable_rounding ( & mut self ) {
167
151
self . config . use_rounding = true ;
@@ -184,7 +168,11 @@ impl Taffy {
184
168
/// Creates and adds a new unattached leaf node to the tree, and returns the node of the new node
185
169
///
186
170
/// Creates and adds a new leaf node with a supplied [`MeasureFunc`]
187
- pub fn new_leaf_with_measure ( & mut self , layout : Style , measure : MeasureFunc ) -> TaffyResult < NodeId > {
171
+ pub fn new_leaf_with_measure (
172
+ & mut self ,
173
+ layout : Style ,
174
+ measure : MeasureFunc < Measure :: Context > ,
175
+ ) -> TaffyResult < NodeId > {
188
176
let mut data = NodeData :: new ( layout) ;
189
177
data. needs_measure = true ;
190
178
@@ -237,7 +225,7 @@ impl Taffy {
237
225
}
238
226
239
227
/// Sets the [`MeasureFunc`] of the associated node
240
- pub fn set_measure ( & mut self , node : NodeId , measure : Option < MeasureFunc > ) -> TaffyResult < ( ) > {
228
+ pub fn set_measure ( & mut self , node : NodeId , measure : Option < MeasureFunc < Measure :: Context > > ) -> TaffyResult < ( ) > {
241
229
let key = node. into ( ) ;
242
230
if let Some ( measure) = measure {
243
231
self . nodes [ key] . needs_measure = true ;
@@ -404,9 +392,47 @@ impl Taffy {
404
392
Ok ( self . nodes [ node. into ( ) ] . cache . is_empty ( ) )
405
393
}
406
394
395
+ /// Updates the stored layout of the provided `node` and its children
396
+ pub fn compute_layout_with_context (
397
+ & mut self ,
398
+ node : NodeId ,
399
+ available_space : Size < AvailableSpace > ,
400
+ context : Measure :: Context ,
401
+ ) -> Result < ( ) , TaffyError > {
402
+ self . context = Some ( context) ;
403
+ let result = compute_layout ( self , node, available_space) ;
404
+ self . context = None ;
405
+ result
406
+ }
407
+ }
408
+
409
+ impl Taffy < MeasureFunc < ( ) > > {
410
+ /// Creates a new [`Taffy`]
411
+ ///
412
+ /// The default capacity of a [`Taffy`] is 16 nodes.
413
+ #[ must_use]
414
+ pub fn new ( ) -> Self {
415
+ Self :: with_capacity ( 16 )
416
+ }
417
+
418
+ /// Creates a new [`Taffy`] that can store `capacity` nodes before reallocation
419
+ #[ must_use]
420
+ pub fn with_capacity ( capacity : usize ) -> Self {
421
+ Taffy {
422
+ // TODO: make this method const upstream,
423
+ // so constructors here can be const
424
+ nodes : SlotMap :: with_capacity ( capacity) ,
425
+ children : SlotMap :: with_capacity ( capacity) ,
426
+ parents : SlotMap :: with_capacity ( capacity) ,
427
+ measure_funcs : SparseSecondaryMap :: with_capacity ( capacity) ,
428
+ config : TaffyConfig :: default ( ) ,
429
+ context : None ,
430
+ }
431
+ }
432
+
407
433
/// Updates the stored layout of the provided `node` and its children
408
434
pub fn compute_layout ( & mut self , node : NodeId , available_space : Size < AvailableSpace > ) -> Result < ( ) , TaffyError > {
409
- compute_layout ( self , node, available_space)
435
+ self . compute_layout_with_context ( node, available_space, ( ) )
410
436
}
411
437
}
412
438
0 commit comments