@@ -9,10 +9,11 @@ use crate::dom::Dom;
99use crate :: event:: EventResponse ;
1010use crate :: event:: { EventInterest , WidgetEvent } ;
1111use crate :: geometry:: { Constraints , FlexFit } ;
12+ use crate :: id:: WidgetId ;
1213use crate :: input:: { InputState , NavDirection } ;
1314use crate :: layout:: LayoutDom ;
1415use crate :: paint:: PaintDom ;
15- use crate :: { Flow , WidgetId } ;
16+ use crate :: types :: { Axis , Flow } ;
1617
1718/// Trait that's automatically implemented for all widget props.
1819///
@@ -38,6 +39,26 @@ impl<'dom> LayoutContext<'dom> {
3839 self . layout
3940 . calculate ( self . dom , self . input , widget, constraints)
4041 }
42+
43+ /// Calculates the intrinsic size for the given widget on the given axis.
44+ pub fn intrinsic_size ( & self , widget : WidgetId , axis : Axis ) -> f32 {
45+ self . layout . intrinsic_size ( self . dom , widget, axis)
46+ }
47+ }
48+
49+ /// Information available to a widget during the layout phase.
50+ #[ non_exhaustive]
51+ #[ allow( missing_docs) ]
52+ pub struct IntrinsicSizeContext < ' dom > {
53+ pub dom : & ' dom Dom ,
54+ pub layout : & ' dom LayoutDom ,
55+ }
56+
57+ impl < ' dom > IntrinsicSizeContext < ' dom > {
58+ /// Calculates the intrinsic size for the given widget on the given axis.
59+ pub fn intrinsic_size ( & self , widget : WidgetId , axis : Axis ) -> f32 {
60+ self . layout . intrinsic_size ( self . dom , widget, axis)
61+ }
4162}
4263
4364/// Information available to a widget during the paint phase.
@@ -134,6 +155,21 @@ pub trait Widget: 'static + fmt::Debug {
134155 constraints. constrain_min ( size)
135156 }
136157
158+ /// Tells the intrinsic size on one axis of the object, which is its size
159+ /// along that axis if the widget were given unbounded constraints on the
160+ /// other axis.
161+ fn intrinsic_size ( & self , ctx : IntrinsicSizeContext < ' _ > , axis : Axis ) -> f32 {
162+ let node = ctx. dom . get_current ( ) ;
163+ let mut size: f32 = 0.0 ;
164+
165+ for & child in & node. children {
166+ let child_size = ctx. intrinsic_size ( child, axis) ;
167+ size = size. max ( child_size) ;
168+ }
169+
170+ size
171+ }
172+
137173 /// Paint the widget based on its current state.
138174 ///
139175 /// The default implementation will paint all of the widget's children.
@@ -180,6 +216,9 @@ pub trait ErasedWidget: Any + fmt::Debug {
180216 /// See [`Widget::layout`].
181217 fn layout ( & self , ctx : LayoutContext < ' _ > , constraints : Constraints ) -> Vec2 ;
182218
219+ /// See [`Widget::intrinsic_size`].
220+ fn intrinsic_size ( & self , ctx : IntrinsicSizeContext < ' _ > , axis : Axis ) -> f32 ;
221+
183222 /// See [`Widget::flex`].
184223 fn flex ( & self ) -> ( u32 , FlexFit ) ;
185224
@@ -207,6 +246,10 @@ where
207246 <T as Widget >:: layout ( self , ctx, constraints)
208247 }
209248
249+ fn intrinsic_size ( & self , ctx : IntrinsicSizeContext < ' _ > , axis : Axis ) -> f32 {
250+ <T as Widget >:: intrinsic_size ( self , ctx, axis)
251+ }
252+
210253 fn flex ( & self ) -> ( u32 , FlexFit ) {
211254 <T as Widget >:: flex ( self )
212255 }
0 commit comments