@@ -9,28 +9,39 @@ use crate::util::sys::Box;
9
9
///
10
10
/// This trait is automatically implemented for all types (including closures) that define a function with the appropriate type signature.
11
11
pub trait Measurable : Send + Sync {
12
+ /// A user-defined context which is passed to taffy when the `compute_layout` function is called, and which Taffy then passes
13
+ /// into measure functions when it calls them
14
+ type Context ;
15
+
12
16
/// Measure node
13
- fn measure ( & self , known_dimensions : Size < Option < f32 > > , available_space : Size < AvailableSpace > ) -> Size < f32 > ;
17
+ fn measure (
18
+ & self ,
19
+ known_dimensions : Size < Option < f32 > > ,
20
+ available_space : Size < AvailableSpace > ,
21
+ context : Self :: Context ,
22
+ ) -> Size < f32 > ;
14
23
}
15
24
16
25
/// A function that can be used to compute the intrinsic size of a node
17
- pub enum MeasureFunc {
26
+ pub enum MeasureFunc < Context = ( ) > {
18
27
/// Stores an unboxed function
19
- Raw ( fn ( Size < Option < f32 > > , Size < AvailableSpace > ) -> Size < f32 > ) ,
28
+ Raw ( fn ( Size < Option < f32 > > , Size < AvailableSpace > , context : Context ) -> Size < f32 > ) ,
20
29
21
30
/// Stores a boxed function
22
31
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
23
- Boxed ( Box < dyn Measurable > ) ,
32
+ Boxed ( Box < dyn Measurable < Context = Context > > ) ,
24
33
}
25
34
26
- impl Measurable for MeasureFunc {
35
+ impl < Context > Measurable for MeasureFunc < Context > {
36
+ type Context = Context ;
37
+
27
38
/// Call the measure function to measure to the node
28
39
#[ inline( always) ]
29
- fn measure ( & self , known_dimensions : Size < Option < f32 > > , available_space : Size < AvailableSpace > ) -> Size < f32 > {
40
+ fn measure ( & self , known_dimensions : Size < Option < f32 > > , available_space : Size < AvailableSpace > , context : Context ) -> Size < f32 > {
30
41
match self {
31
- Self :: Raw ( measure) => measure ( known_dimensions, available_space) ,
42
+ Self :: Raw ( measure) => measure ( known_dimensions, available_space, context ) ,
32
43
#[ cfg( any( feature = "std" , feature = "alloc" ) ) ]
33
- Self :: Boxed ( measurable) => measurable. measure ( known_dimensions, available_space) ,
44
+ Self :: Boxed ( measurable) => measurable. measure ( known_dimensions, available_space, context ) ,
34
45
}
35
46
}
36
47
}
0 commit comments