Skip to content

Commit 862559b

Browse files
committed
Add Context to Measurable trait
1 parent 5443996 commit 862559b

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/tree/measure_func.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,39 @@ use crate::util::sys::Box;
99
///
1010
/// This trait is automatically implemented for all types (including closures) that define a function with the appropriate type signature.
1111
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+
1216
/// 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>;
1423
}
1524

1625
/// A function that can be used to compute the intrinsic size of a node
17-
pub enum MeasureFunc {
26+
pub enum MeasureFunc<Context = ()> {
1827
/// 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>),
2029

2130
/// Stores a boxed function
2231
#[cfg(any(feature = "std", feature = "alloc"))]
23-
Boxed(Box<dyn Measurable>),
32+
Boxed(Box<dyn Measurable<Context = Context>>),
2433
}
2534

26-
impl Measurable for MeasureFunc {
35+
impl<Context> Measurable for MeasureFunc<Context> {
36+
type Context = Context;
37+
2738
/// Call the measure function to measure to the node
2839
#[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> {
3041
match self {
31-
Self::Raw(measure) => measure(known_dimensions, available_space),
42+
Self::Raw(measure) => measure(known_dimensions, available_space, context),
3243
#[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),
3445
}
3546
}
3647
}

0 commit comments

Comments
 (0)