From bcd12f1e87634343efe238691a535eabfcc21260 Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Tue, 3 Apr 2018 14:56:29 -0700 Subject: [PATCH] Change the type of aspect Ratio to YGFloatOptional Reviewed By: emilsjolander Differential Revision: D7302651 fbshipit-source-id: 53e3b4c9627207a379f927b1f3485e36a9c70601 --- .../fabric/view/yoga/YogaStylableProps.cpp | 4 +- ReactCommon/yoga/yoga/YGStyle.cpp | 9 ++- ReactCommon/yoga/yoga/YGStyle.h | 2 +- ReactCommon/yoga/yoga/Yoga.cpp | 62 ++++++++++++------- 4 files changed, 47 insertions(+), 30 deletions(-) diff --git a/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp b/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp index 24a2ca568..c13e3462d 100644 --- a/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp +++ b/ReactCommon/fabric/view/yoga/YogaStylableProps.cpp @@ -95,7 +95,7 @@ void YogaStylableProps::apply(const RawProps &rawProps) { YOGA_STYLE_PREFIXED_EDGE_PROPERTY(padding) YOGA_STYLE_PREFIXED_EDGE_PROPERTY(border) - YOGA_STYLE_SIMPLE_FLOAT_PROPERTY(aspectRatio) + YOGA_STYLE_OPTIONAL_FLOAT_PROPERTY(aspectRatio) } } @@ -139,7 +139,7 @@ SharedDebugStringConvertibleList YogaStylableProps::getDebugProps() const { YOGA_STYLE_PROPS_ADD_TO_SET(minSize, minDimensions, , stringFromYogaStyleDimensions) YOGA_STYLE_PROPS_ADD_TO_SET(maxSize, maxDimensions, , stringFromYogaStyleDimensions) - YOGA_STYLE_PROPS_ADD_TO_SET(aspectRatio, aspectRatio, , folly::to) + YOGA_STYLE_PROPS_ADD_TO_SET(aspectRatio, aspectRatio, , stringFromYogaStyleOptionalFloat) return list; } diff --git a/ReactCommon/yoga/yoga/YGStyle.cpp b/ReactCommon/yoga/yoga/YGStyle.cpp index 66e4d04cc..7664dcf8e 100644 --- a/ReactCommon/yoga/yoga/YGStyle.cpp +++ b/ReactCommon/yoga/yoga/YGStyle.cpp @@ -50,7 +50,7 @@ YGStyle::YGStyle() dimensions(kYGDefaultDimensionValuesAutoUnit), minDimensions(kYGDefaultDimensionValuesUnit), maxDimensions(kYGDefaultDimensionValuesUnit), - aspectRatio(YGUndefined) {} + aspectRatio(YGFloatOptional()) {} // Yoga specific properties, not compatible with flexbox specification bool YGStyle::operator==(const YGStyle& style) { @@ -91,10 +91,9 @@ bool YGStyle::operator==(const YGStyle& style) { flexShrink.getValue() == style.flexShrink.getValue(); } - if (!(YGFloatIsUndefined(aspectRatio) && - YGFloatIsUndefined(style.aspectRatio))) { - areNonFloatValuesEqual = - areNonFloatValuesEqual && aspectRatio == style.aspectRatio; + if (!(aspectRatio.isUndefined() && style.aspectRatio.isUndefined())) { + areNonFloatValuesEqual = areNonFloatValuesEqual && + aspectRatio.getValue() == style.aspectRatio.getValue(); } return areNonFloatValuesEqual; diff --git a/ReactCommon/yoga/yoga/YGStyle.h b/ReactCommon/yoga/yoga/YGStyle.h index 7a7f519a5..f0f97bd8f 100644 --- a/ReactCommon/yoga/yoga/YGStyle.h +++ b/ReactCommon/yoga/yoga/YGStyle.h @@ -32,7 +32,7 @@ struct YGStyle { std::array dimensions; std::array minDimensions; std::array maxDimensions; - float aspectRatio; + YGFloatOptional aspectRatio; YGStyle(); // Yoga specific properties, not compatible with flexbox specification diff --git a/ReactCommon/yoga/yoga/Yoga.cpp b/ReactCommon/yoga/yoga/Yoga.cpp index b13b28e1c..f00133e2f 100644 --- a/ReactCommon/yoga/yoga/Yoga.cpp +++ b/ReactCommon/yoga/yoga/Yoga.cpp @@ -930,16 +930,30 @@ float YGNodeStyleGetBorder(const YGNodeRef node, const YGEdge edge) { return node->getStyle().border[edge].value; } +// Yoga specific properties, not compatible with flexbox specification + +// TODO(T26792433): Change the API to accept YGFloatOptional. +float YGNodeStyleGetAspectRatio(const YGNodeRef node) { + const YGFloatOptional op = node->getStyle().aspectRatio; + return op.isUndefined() ? YGUndefined : op.getValue(); +} + +// TODO(T26792433): Change the API to accept YGFloatOptional. +void YGNodeStyleSetAspectRatio(const YGNodeRef node, const float aspectRatio) { + if (!YGFloatOptionalFloatEquals(node->getStyle().aspectRatio, aspectRatio)) { + YGStyle style = node->getStyle(); + style.aspectRatio = YGFloatOptional(aspectRatio); + node->setStyle(style); + node->markDirtyAndPropogate(); + } +} + YG_NODE_STYLE_PROPERTY_UNIT_AUTO_IMPL(YGValue, Width, width, dimensions[YGDimensionWidth]); YG_NODE_STYLE_PROPERTY_UNIT_AUTO_IMPL(YGValue, Height, height, dimensions[YGDimensionHeight]); YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MinWidth, minWidth, minDimensions[YGDimensionWidth]); YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MinHeight, minHeight, minDimensions[YGDimensionHeight]); YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MaxWidth, maxWidth, maxDimensions[YGDimensionWidth]); YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MaxHeight, maxHeight, maxDimensions[YGDimensionHeight]); - -// Yoga specific properties, not compatible with flexbox specification -YG_NODE_STYLE_PROPERTY_IMPL(float, AspectRatio, aspectRatio, aspectRatio); - YG_NODE_LAYOUT_PROPERTY_IMPL(float, Left, position[YGEdgeLeft]); YG_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop]); YG_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight]); @@ -1278,14 +1292,15 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node, } } - if (!YGFloatIsUndefined(child->getStyle().aspectRatio)) { + if (!child->getStyle().aspectRatio.isUndefined()) { if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) { childHeight = marginColumn + - (childWidth - marginRow) / child->getStyle().aspectRatio; + (childWidth - marginRow) / child->getStyle().aspectRatio.getValue(); childHeightMeasureMode = YGMeasureModeExactly; } else if (isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) { childWidth = marginRow + - (childHeight - marginColumn) * child->getStyle().aspectRatio; + (childHeight - marginColumn) * + child->getStyle().aspectRatio.getValue(); childWidthMeasureMode = YGMeasureModeExactly; } } @@ -1300,8 +1315,9 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node, if (!isMainAxisRow && !isRowStyleDimDefined && hasExactWidth && childWidthStretch) { childWidth = width; childWidthMeasureMode = YGMeasureModeExactly; - if (!YGFloatIsUndefined(child->getStyle().aspectRatio)) { - childHeight = (childWidth - marginRow) / child->getStyle().aspectRatio; + if (!child->getStyle().aspectRatio.isUndefined()) { + childHeight = + (childWidth - marginRow) / child->getStyle().aspectRatio.getValue(); childHeightMeasureMode = YGMeasureModeExactly; } } @@ -1313,9 +1329,9 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node, childHeight = height; childHeightMeasureMode = YGMeasureModeExactly; - if (!YGFloatIsUndefined(child->getStyle().aspectRatio)) { - childWidth = - (childHeight - marginColumn) * child->getStyle().aspectRatio; + if (!child->getStyle().aspectRatio.isUndefined()) { + childWidth = (childHeight - marginColumn) * + child->getStyle().aspectRatio.getValue(); childWidthMeasureMode = YGMeasureModeExactly; } } @@ -1411,13 +1427,14 @@ static void YGNodeAbsoluteLayoutChild(const YGNodeRef node, // Exactly one dimension needs to be defined for us to be able to do aspect ratio // calculation. One dimension being the anchor and the other being flexible. if (YGFloatIsUndefined(childWidth) ^ YGFloatIsUndefined(childHeight)) { - if (!YGFloatIsUndefined(child->getStyle().aspectRatio)) { + if (!child->getStyle().aspectRatio.isUndefined()) { if (YGFloatIsUndefined(childWidth)) { childWidth = marginRow + - (childHeight - marginColumn) * child->getStyle().aspectRatio; + (childHeight - marginColumn) * + child->getStyle().aspectRatio.getValue(); } else if (YGFloatIsUndefined(childHeight)) { childHeight = marginColumn + - (childWidth - marginRow) / child->getStyle().aspectRatio; + (childWidth - marginRow) / child->getStyle().aspectRatio.getValue(); } } } @@ -1997,11 +2014,11 @@ static float YGDistributeFreeSpaceSecondPass( YGMeasureMode childCrossMeasureMode; YGMeasureMode childMainMeasureMode = YGMeasureModeExactly; - if (!YGFloatIsUndefined(currentRelativeChild->getStyle().aspectRatio)) { + if (!currentRelativeChild->getStyle().aspectRatio.isUndefined()) { childCrossSize = isMainAxisRow ? (childMainSize - marginMain) / - currentRelativeChild->getStyle().aspectRatio + currentRelativeChild->getStyle().aspectRatio.getValue() : (childMainSize - marginMain) * - currentRelativeChild->getStyle().aspectRatio; + currentRelativeChild->getStyle().aspectRatio.getValue(); childCrossMeasureMode = YGMeasureModeExactly; childCrossSize += marginCross; @@ -2889,11 +2906,12 @@ static void YGNodelayoutImpl(const YGNodeRef node, float childMainSize = child->getLayout().measuredDimensions[dim[mainAxis]]; float childCrossSize = - !YGFloatIsUndefined(child->getStyle().aspectRatio) + !child->getStyle().aspectRatio.isUndefined() ? ((child->getMarginForAxis(crossAxis, availableInnerWidth) + - (isMainAxisRow - ? childMainSize / child->getStyle().aspectRatio - : childMainSize * child->getStyle().aspectRatio))) + (isMainAxisRow ? childMainSize / + child->getStyle().aspectRatio.getValue() + : childMainSize * + child->getStyle().aspectRatio.getValue()))) : collectedFlexItemsValues.crossDim; childMainSize +=