@@ -85,6 +85,9 @@ typedef struct CSSStyle {
8585 float dimensions [2 ];
8686 float minDimensions [2 ];
8787 float maxDimensions [2 ];
88+
89+ // Yoga specific properties, not compatible with flexbox specification
90+ float aspectRatio ;
8891} CSSStyle ;
8992
9093typedef struct CSSNode {
@@ -269,6 +272,8 @@ void CSSNodeInit(const CSSNodeRef node) {
269272 node -> style .border [edge ] = CSSUndefined ;
270273 }
271274
275+ node -> style .aspectRatio = CSSUndefined ;
276+
272277 node -> layout .dimensions [CSSDimensionWidth ] = CSSUndefined ;
273278 node -> layout .dimensions [CSSDimensionHeight ] = CSSUndefined ;
274279
@@ -459,6 +464,9 @@ CSS_NODE_STYLE_PROPERTY_IMPL(float, MinHeight, minHeight, minDimensions[CSSDimen
459464CSS_NODE_STYLE_PROPERTY_IMPL (float , MaxWidth , maxWidth , maxDimensions [CSSDimensionWidth ]);
460465CSS_NODE_STYLE_PROPERTY_IMPL (float , MaxHeight , maxHeight , maxDimensions [CSSDimensionHeight ]);
461466
467+ // Yoga specific properties, not compatible with flexbox specification
468+ CSS_NODE_STYLE_PROPERTY_IMPL (float , AspectRatio , aspectRatio , aspectRatio );
469+
462470CSS_NODE_LAYOUT_PROPERTY_IMPL (float , Left , position [CSSEdgeLeft ]);
463471CSS_NODE_LAYOUT_PROPERTY_IMPL (float , Top , position [CSSEdgeTop ]);
464472CSS_NODE_LAYOUT_PROPERTY_IMPL (float , Right , position [CSSEdgeRight ]);
@@ -1032,6 +1040,20 @@ static void computeChildFlexBasis(const CSSNodeRef node,
10321040 childHeightMeasureMode = CSSMeasureModeExactly ;
10331041 }
10341042
1043+ if (!CSSValueIsUndefined (child -> style .aspectRatio )) {
1044+ if (!isMainAxisRow && childWidthMeasureMode == CSSMeasureModeExactly ) {
1045+ child -> layout .computedFlexBasis =
1046+ fmaxf (childWidth * child -> style .aspectRatio ,
1047+ getPaddingAndBorderAxis (child , CSSFlexDirectionColumn ));
1048+ return ;
1049+ } else if (isMainAxisRow && childHeightMeasureMode == CSSMeasureModeExactly ) {
1050+ child -> layout .computedFlexBasis =
1051+ fmaxf (childHeight * child -> style .aspectRatio ,
1052+ getPaddingAndBorderAxis (child , CSSFlexDirectionRow ));
1053+ return ;
1054+ }
1055+ }
1056+
10351057 constrainMaxSizeForMode (child -> style .maxDimensions [CSSDimensionWidth ],
10361058 & childWidthMeasureMode ,
10371059 & childWidth );
@@ -1108,6 +1130,20 @@ static void absoluteLayoutChild(const CSSNodeRef node,
11081130 }
11091131 }
11101132
1133+ // Exactly one dimension needs to be defined for us to be able to do aspect ratio
1134+ // calculation. One dimension being the anchor and the other being flexible.
1135+ if (CSSValueIsUndefined (childWidth ) ^ CSSValueIsUndefined (childHeight )) {
1136+ if (!CSSValueIsUndefined (child -> style .aspectRatio )) {
1137+ if (CSSValueIsUndefined (childWidth )) {
1138+ childWidth = fmaxf (childHeight * child -> style .aspectRatio ,
1139+ getPaddingAndBorderAxis (child , CSSFlexDirectionColumn ));
1140+ } else if (CSSValueIsUndefined (childHeight )) {
1141+ childHeight = fmaxf (childWidth * child -> style .aspectRatio ,
1142+ getPaddingAndBorderAxis (child , CSSFlexDirectionRow ));
1143+ }
1144+ }
1145+ }
1146+
11111147 // If we're still missing one or the other dimension, measure the content.
11121148 if (CSSValueIsUndefined (childWidth ) || CSSValueIsUndefined (childHeight )) {
11131149 childWidthMeasureMode =
@@ -1774,6 +1810,19 @@ static void layoutNodeImpl(const CSSNodeRef node,
17741810 }
17751811 }
17761812
1813+ if (!CSSValueIsUndefined (currentRelativeChild -> style .aspectRatio )) {
1814+ if (isMainAxisRow && childHeightMeasureMode != CSSMeasureModeExactly ) {
1815+ childHeight =
1816+ fmaxf (childWidth * currentRelativeChild -> style .aspectRatio ,
1817+ getPaddingAndBorderAxis (currentRelativeChild , CSSFlexDirectionColumn ));
1818+ childHeightMeasureMode = CSSMeasureModeExactly ;
1819+ } else if (!isMainAxisRow && childWidthMeasureMode != CSSMeasureModeExactly ) {
1820+ childWidth = fmaxf (childHeight * currentRelativeChild -> style .aspectRatio ,
1821+ getPaddingAndBorderAxis (currentRelativeChild , CSSFlexDirectionRow ));
1822+ childWidthMeasureMode = CSSMeasureModeExactly ;
1823+ }
1824+ }
1825+
17771826 constrainMaxSizeForMode (currentRelativeChild -> style .maxDimensions [CSSDimensionWidth ],
17781827 & childWidthMeasureMode ,
17791828 & childWidth );
0 commit comments