Change the type of aspect Ratio to YGFloatOptional

Reviewed By: emilsjolander

Differential Revision: D7302651

fbshipit-source-id: 53e3b4c9627207a379f927b1f3485e36a9c70601
This commit is contained in:
Pritesh Nandgaonkar 2018-04-03 14:56:29 -07:00 committed by Facebook Github Bot
parent e5a4d59244
commit bcd12f1e87
4 changed files with 47 additions and 30 deletions

View File

@ -95,7 +95,7 @@ void YogaStylableProps::apply(const RawProps &rawProps) {
YOGA_STYLE_PREFIXED_EDGE_PROPERTY(padding) YOGA_STYLE_PREFIXED_EDGE_PROPERTY(padding)
YOGA_STYLE_PREFIXED_EDGE_PROPERTY(border) 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(minSize, minDimensions, , stringFromYogaStyleDimensions)
YOGA_STYLE_PROPS_ADD_TO_SET(maxSize, maxDimensions, , stringFromYogaStyleDimensions) YOGA_STYLE_PROPS_ADD_TO_SET(maxSize, maxDimensions, , stringFromYogaStyleDimensions)
YOGA_STYLE_PROPS_ADD_TO_SET(aspectRatio, aspectRatio, , folly::to<std::string>) YOGA_STYLE_PROPS_ADD_TO_SET(aspectRatio, aspectRatio, , stringFromYogaStyleOptionalFloat)
return list; return list;
} }

View File

@ -50,7 +50,7 @@ YGStyle::YGStyle()
dimensions(kYGDefaultDimensionValuesAutoUnit), dimensions(kYGDefaultDimensionValuesAutoUnit),
minDimensions(kYGDefaultDimensionValuesUnit), minDimensions(kYGDefaultDimensionValuesUnit),
maxDimensions(kYGDefaultDimensionValuesUnit), maxDimensions(kYGDefaultDimensionValuesUnit),
aspectRatio(YGUndefined) {} aspectRatio(YGFloatOptional()) {}
// Yoga specific properties, not compatible with flexbox specification // Yoga specific properties, not compatible with flexbox specification
bool YGStyle::operator==(const YGStyle& style) { bool YGStyle::operator==(const YGStyle& style) {
@ -91,10 +91,9 @@ bool YGStyle::operator==(const YGStyle& style) {
flexShrink.getValue() == style.flexShrink.getValue(); flexShrink.getValue() == style.flexShrink.getValue();
} }
if (!(YGFloatIsUndefined(aspectRatio) && if (!(aspectRatio.isUndefined() && style.aspectRatio.isUndefined())) {
YGFloatIsUndefined(style.aspectRatio))) { areNonFloatValuesEqual = areNonFloatValuesEqual &&
areNonFloatValuesEqual = aspectRatio.getValue() == style.aspectRatio.getValue();
areNonFloatValuesEqual && aspectRatio == style.aspectRatio;
} }
return areNonFloatValuesEqual; return areNonFloatValuesEqual;

View File

@ -32,7 +32,7 @@ struct YGStyle {
std::array<YGValue, 2> dimensions; std::array<YGValue, 2> dimensions;
std::array<YGValue, 2> minDimensions; std::array<YGValue, 2> minDimensions;
std::array<YGValue, 2> maxDimensions; std::array<YGValue, 2> maxDimensions;
float aspectRatio; YGFloatOptional aspectRatio;
YGStyle(); YGStyle();
// Yoga specific properties, not compatible with flexbox specification // Yoga specific properties, not compatible with flexbox specification

View File

@ -930,16 +930,30 @@ float YGNodeStyleGetBorder(const YGNodeRef node, const YGEdge edge) {
return node->getStyle().border[edge].value; 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, Width, width, dimensions[YGDimensionWidth]);
YG_NODE_STYLE_PROPERTY_UNIT_AUTO_IMPL(YGValue, Height, height, dimensions[YGDimensionHeight]); 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, MinWidth, minWidth, minDimensions[YGDimensionWidth]);
YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MinHeight, minHeight, minDimensions[YGDimensionHeight]); 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, MaxWidth, maxWidth, maxDimensions[YGDimensionWidth]);
YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MaxHeight, maxHeight, maxDimensions[YGDimensionHeight]); 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, Left, position[YGEdgeLeft]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop]); YG_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight]); 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) { if (!isMainAxisRow && childWidthMeasureMode == YGMeasureModeExactly) {
childHeight = marginColumn + childHeight = marginColumn +
(childWidth - marginRow) / child->getStyle().aspectRatio; (childWidth - marginRow) / child->getStyle().aspectRatio.getValue();
childHeightMeasureMode = YGMeasureModeExactly; childHeightMeasureMode = YGMeasureModeExactly;
} else if (isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) { } else if (isMainAxisRow && childHeightMeasureMode == YGMeasureModeExactly) {
childWidth = marginRow + childWidth = marginRow +
(childHeight - marginColumn) * child->getStyle().aspectRatio; (childHeight - marginColumn) *
child->getStyle().aspectRatio.getValue();
childWidthMeasureMode = YGMeasureModeExactly; childWidthMeasureMode = YGMeasureModeExactly;
} }
} }
@ -1300,8 +1315,9 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
if (!isMainAxisRow && !isRowStyleDimDefined && hasExactWidth && childWidthStretch) { if (!isMainAxisRow && !isRowStyleDimDefined && hasExactWidth && childWidthStretch) {
childWidth = width; childWidth = width;
childWidthMeasureMode = YGMeasureModeExactly; childWidthMeasureMode = YGMeasureModeExactly;
if (!YGFloatIsUndefined(child->getStyle().aspectRatio)) { if (!child->getStyle().aspectRatio.isUndefined()) {
childHeight = (childWidth - marginRow) / child->getStyle().aspectRatio; childHeight =
(childWidth - marginRow) / child->getStyle().aspectRatio.getValue();
childHeightMeasureMode = YGMeasureModeExactly; childHeightMeasureMode = YGMeasureModeExactly;
} }
} }
@ -1313,9 +1329,9 @@ static void YGNodeComputeFlexBasisForChild(const YGNodeRef node,
childHeight = height; childHeight = height;
childHeightMeasureMode = YGMeasureModeExactly; childHeightMeasureMode = YGMeasureModeExactly;
if (!YGFloatIsUndefined(child->getStyle().aspectRatio)) { if (!child->getStyle().aspectRatio.isUndefined()) {
childWidth = childWidth = (childHeight - marginColumn) *
(childHeight - marginColumn) * child->getStyle().aspectRatio; child->getStyle().aspectRatio.getValue();
childWidthMeasureMode = YGMeasureModeExactly; 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 // 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. // calculation. One dimension being the anchor and the other being flexible.
if (YGFloatIsUndefined(childWidth) ^ YGFloatIsUndefined(childHeight)) { if (YGFloatIsUndefined(childWidth) ^ YGFloatIsUndefined(childHeight)) {
if (!YGFloatIsUndefined(child->getStyle().aspectRatio)) { if (!child->getStyle().aspectRatio.isUndefined()) {
if (YGFloatIsUndefined(childWidth)) { if (YGFloatIsUndefined(childWidth)) {
childWidth = marginRow + childWidth = marginRow +
(childHeight - marginColumn) * child->getStyle().aspectRatio; (childHeight - marginColumn) *
child->getStyle().aspectRatio.getValue();
} else if (YGFloatIsUndefined(childHeight)) { } else if (YGFloatIsUndefined(childHeight)) {
childHeight = marginColumn + childHeight = marginColumn +
(childWidth - marginRow) / child->getStyle().aspectRatio; (childWidth - marginRow) / child->getStyle().aspectRatio.getValue();
} }
} }
} }
@ -1997,11 +2014,11 @@ static float YGDistributeFreeSpaceSecondPass(
YGMeasureMode childCrossMeasureMode; YGMeasureMode childCrossMeasureMode;
YGMeasureMode childMainMeasureMode = YGMeasureModeExactly; YGMeasureMode childMainMeasureMode = YGMeasureModeExactly;
if (!YGFloatIsUndefined(currentRelativeChild->getStyle().aspectRatio)) { if (!currentRelativeChild->getStyle().aspectRatio.isUndefined()) {
childCrossSize = isMainAxisRow ? (childMainSize - marginMain) / childCrossSize = isMainAxisRow ? (childMainSize - marginMain) /
currentRelativeChild->getStyle().aspectRatio currentRelativeChild->getStyle().aspectRatio.getValue()
: (childMainSize - marginMain) * : (childMainSize - marginMain) *
currentRelativeChild->getStyle().aspectRatio; currentRelativeChild->getStyle().aspectRatio.getValue();
childCrossMeasureMode = YGMeasureModeExactly; childCrossMeasureMode = YGMeasureModeExactly;
childCrossSize += marginCross; childCrossSize += marginCross;
@ -2889,11 +2906,12 @@ static void YGNodelayoutImpl(const YGNodeRef node,
float childMainSize = float childMainSize =
child->getLayout().measuredDimensions[dim[mainAxis]]; child->getLayout().measuredDimensions[dim[mainAxis]];
float childCrossSize = float childCrossSize =
!YGFloatIsUndefined(child->getStyle().aspectRatio) !child->getStyle().aspectRatio.isUndefined()
? ((child->getMarginForAxis(crossAxis, availableInnerWidth) + ? ((child->getMarginForAxis(crossAxis, availableInnerWidth) +
(isMainAxisRow (isMainAxisRow ? childMainSize /
? childMainSize / child->getStyle().aspectRatio child->getStyle().aspectRatio.getValue()
: childMainSize * child->getStyle().aspectRatio))) : childMainSize *
child->getStyle().aspectRatio.getValue())))
: collectedFlexItemsValues.crossDim; : collectedFlexItemsValues.crossDim;
childMainSize += childMainSize +=