Updated the implementation of leading padding

Summary: Changed the arguments for the getter of leading padding to avoid copies. Added an assetion in getter of leading padding, as padding would always be defined even in the case when the user has not explicitly defined the value. In these cases it would take the default value of 0. So changing the type of `getLayoutPadding` to `YGFloatOptional`, doesn't make sense.

Reviewed By: emilsjolander

Differential Revision: D7336690

fbshipit-source-id: b2a2f010026f26fc2cc9fb35ad921da8f7017c9f
This commit is contained in:
Pritesh Nandgaonkar
2018-04-04 07:55:30 -07:00
committed by Facebook Github Bot
parent a3642541d0
commit 3e322e60e4
5 changed files with 34 additions and 14 deletions

View File

@@ -57,3 +57,12 @@ float YGFloatSanitize(const float& val) {
float YGUnwrapFloatOptional(const YGFloatOptional& op) {
return op.isUndefined() ? YGUndefined : op.getValue();
}
YGFloatOptional YGFloatOptionalMax(
const YGFloatOptional& op1,
const YGFloatOptional& op2) {
if (!op1.isUndefined() && !op2.isUndefined()) {
return op1.getValue() > op2.getValue() ? op1 : op2;
}
return op1.isUndefined() ? op2 : op1;
}

View File

@@ -65,6 +65,10 @@ bool YGFloatsEqual(const float a, const float b);
// compiler flag.
float YGFloatMax(const float a, const float b);
YGFloatOptional YGFloatOptionalMax(
const YGFloatOptional& op1,
const YGFloatOptional& op2);
// We need custom min function, since we want that, if one argument is
// YGUndefined then the min funtion should return the other argument as the min
// value. We wouldn't have needed a custom min function if YGUndefined was NAN

View File

@@ -654,21 +654,21 @@ float YGNode::getTrailingBorder(const YGFlexDirection flexDirection) const {
return YGFloatMax(computedEdgeValue, 0.0f);
}
float YGNode::getLeadingPadding(
const YGFlexDirection axis,
const float widthSize) const {
YGFloatOptional YGNode::getLeadingPadding(
const YGFlexDirection& axis,
const float& widthSize) const {
const YGFloatOptional& paddingEdgeStart =
YGResolveValue(style_.padding[YGEdgeStart], widthSize);
if (YGFlexDirectionIsRow(axis) &&
style_.padding[YGEdgeStart].unit != YGUnitUndefined &&
!YGResolveValue(style_.padding[YGEdgeStart], widthSize).isUndefined() &&
YGUnwrapFloatOptional(
YGResolveValue(style_.padding[YGEdgeStart], widthSize)) > 0.0f) {
return YGUnwrapFloatOptional(YGResolveValue(style_.padding[YGEdgeStart], widthSize));
!paddingEdgeStart.isUndefined() && paddingEdgeStart.getValue() > 0.0f) {
return paddingEdgeStart;
}
float resolvedValue = YGUnwrapFloatOptional(YGResolveValue(
YGFloatOptional resolvedValue = YGResolveValue(
*YGComputedEdgeValue(style_.padding, leading[axis], &YGValueZero),
widthSize));
return YGFloatMax(resolvedValue, 0.0f);
widthSize);
return YGFloatOptionalMax(resolvedValue, YGFloatOptional(0.0f));
}
float YGNode::getTrailingPadding(
@@ -692,7 +692,8 @@ float YGNode::getTrailingPadding(
float YGNode::getLeadingPaddingAndBorder(
const YGFlexDirection axis,
const float widthSize) const {
return getLeadingPadding(axis, widthSize) + getLeadingBorder(axis);
return YGUnwrapFloatOptional(getLeadingPadding(axis, widthSize)) +
getLeadingBorder(axis);
}
float YGNode::getTrailingPaddingAndBorder(

View File

@@ -93,7 +93,9 @@ struct YGNode {
float getTrailingMargin(const YGFlexDirection axis, const float widthSize) const;
float getLeadingBorder(const YGFlexDirection flexDirection) const;
float getTrailingBorder(const YGFlexDirection flexDirection) const;
float getLeadingPadding(const YGFlexDirection axis, const float widthSize) const;
YGFloatOptional getLeadingPadding(
const YGFlexDirection& axis,
const float& widthSize) const;
float getTrailingPadding(const YGFlexDirection axis, const float widthSize) const;
float getLeadingPaddingAndBorder(
const YGFlexDirection axis,

View File

@@ -2573,11 +2573,15 @@ static void YGNodelayoutImpl(const YGNodeRef node,
node->getTrailingBorder(flexColumnDirection), YGEdgeBottom);
node->setLayoutPadding(
node->getLeadingPadding(flexRowDirection, ownerWidth), YGEdgeStart);
YGUnwrapFloatOptional(
node->getLeadingPadding(flexRowDirection, ownerWidth)),
YGEdgeStart);
node->setLayoutPadding(
node->getTrailingPadding(flexRowDirection, ownerWidth), YGEdgeEnd);
node->setLayoutPadding(
node->getLeadingPadding(flexColumnDirection, ownerWidth), YGEdgeTop);
YGUnwrapFloatOptional(
node->getLeadingPadding(flexColumnDirection, ownerWidth)),
YGEdgeTop);
node->setLayoutPadding(
node->getTrailingPadding(flexColumnDirection, ownerWidth), YGEdgeBottom);