Change the type of flex to YGFloatOptional

Reviewed By: emilsjolander

Differential Revision: D7211327

fbshipit-source-id: 0d979b6ba00317317b98bbc6e63979c7f1feb2da
This commit is contained in:
Pritesh Nandgaonkar 2018-03-14 04:17:09 -07:00 committed by Facebook Github Bot
parent 9f7e70363a
commit 5b3d59598a
8 changed files with 61 additions and 16 deletions

View File

@ -57,3 +57,9 @@ float YGFloatSanitize(const float& val) {
float YGUnwrapFloatOptional(const YGFloatOptional& op) {
return op.isUndefined ? YGUndefined : op.value;
}
bool YGFloatOptionalFloatEquals(
const YGFloatOptional& optional,
const float& val) {
return YGUnwrapFloatOptional(optional) == val;
}

View File

@ -94,6 +94,12 @@ float YGFloatSanitize(const float& val);
// TODO: Get rid off this function
float YGUnwrapFloatOptional(const YGFloatOptional& op);
// This function returns true if val and optional both are undefined or if val
// and optional.val is true, otherwise its false.
bool YGFloatOptionalFloatEquals(
const YGFloatOptional& optional,
const float& val);
YGFlexDirection YGFlexDirectionCross(
const YGFlexDirection flexDirection,
const YGDirection direction);

View File

@ -500,7 +500,7 @@ YGValue YGNode::resolveFlexBasisPtr() const {
if (flexBasis.unit != YGUnitAuto && flexBasis.unit != YGUnitUndefined) {
return flexBasis;
}
if (!YGFloatIsUndefined(style_.flex) && style_.flex > 0.0f) {
if (!style_.flex.isUndefined && style_.flex.value > 0.0f) {
return config_->useWebDefaults ? YGValueAuto : YGValueZero;
}
return YGValueAuto;
@ -595,8 +595,8 @@ float YGNode::resolveFlexGrow() {
if (!YGFloatIsUndefined(style_.flexGrow)) {
return style_.flexGrow;
}
if (!YGFloatIsUndefined(style_.flex) && style_.flex > 0.0f) {
return style_.flex;
if (!style_.flex.isUndefined && style_.flex.value > 0.0f) {
return style_.flex.value;
}
return kDefaultFlexGrow;
}
@ -608,9 +608,9 @@ float YGNode::resolveFlexShrink() {
if (!YGFloatIsUndefined(style_.flexShrink)) {
return style_.flexShrink;
}
if (!config_->useWebDefaults && !YGFloatIsUndefined(style_.flex) &&
style_.flex < 0.0f) {
return -style_.flex;
if (!config_->useWebDefaults && !style_.flex.isUndefined &&
style_.flex.value < 0.0f) {
return -style_.flex.value;
}
return config_->useWebDefaults ? kWebDefaultFlexShrink : kDefaultFlexShrink;
}

View File

@ -39,6 +39,15 @@ static void appendFormatedString(string* str, const char* fmt, ...) {
str->append(result);
}
static void appendFloatOptionalIfDefined(
string* base,
const string key,
const YGFloatOptional num) {
if (!num.isUndefined) {
appendFormatedString(base, "%s: %g; ", key.c_str(), num.value);
}
}
static void
appendFloatIfNotUndefined(string* base, const string key, const float num) {
if (!YGFloatIsUndefined(num)) {
@ -155,7 +164,7 @@ void YGNodeToString(
appendFloatIfNotUndefined(str, "flex-grow", node->getStyle().flexGrow);
appendFloatIfNotUndefined(str, "flex-shrink", node->getStyle().flexShrink);
appendNumberIfNotAuto(str, "flex-basis", node->getStyle().flexBasis);
appendFloatIfNotUndefined(str, "flex", node->getStyle().flex);
appendFloatOptionalIfDefined(str, "flex", node->getStyle().flex);
if (node->getStyle().flexWrap != YGNode().getStyle().flexWrap) {
appendFormatedString(

View File

@ -7,6 +7,9 @@
#include "YGStyle.h"
#define YGFloatOptionalUndefined \
{ true, 0 }
const YGValue kYGValueUndefined = {0, YGUnitUndefined};
const YGValue kYGValueAuto = {YGUndefined, YGUnitAuto};
@ -39,7 +42,7 @@ YGStyle::YGStyle()
flexWrap(YGWrapNoWrap),
overflow(YGOverflowVisible),
display(YGDisplayFlex),
flex(YGUndefined),
flex(YGFloatOptionalUndefined),
flexGrow(YGUndefined),
flexShrink(YGUndefined),
flexBasis(kYGValueAuto),
@ -69,8 +72,11 @@ bool YGStyle::operator==(const YGStyle& style) {
YGValueArrayEqual(minDimensions, style.minDimensions) &&
YGValueArrayEqual(maxDimensions, style.maxDimensions);
if (!(YGFloatIsUndefined(flex) && YGFloatIsUndefined(style.flex))) {
areNonFloatValuesEqual = areNonFloatValuesEqual && flex == style.flex;
areNonFloatValuesEqual =
areNonFloatValuesEqual && flex.isUndefined == style.flex.isUndefined;
if (areNonFloatValuesEqual && !flex.isUndefined && !style.flex.isUndefined) {
areNonFloatValuesEqual =
areNonFloatValuesEqual && flex.value == style.flex.value;
}
if (!(YGFloatIsUndefined(flexGrow) && YGFloatIsUndefined(style.flexGrow))) {

View File

@ -20,7 +20,7 @@ struct YGStyle {
YGWrap flexWrap;
YGOverflow overflow;
YGDisplay display;
float flex;
YGFloatOptional flex;
float flexGrow;
float flexShrink;
YGValue flexBasis;

View File

@ -752,7 +752,26 @@ YG_NODE_STYLE_PROPERTY_IMPL(YGWrap, FlexWrap, flexWrap, flexWrap);
YG_NODE_STYLE_PROPERTY_IMPL(YGOverflow, Overflow, overflow, overflow);
YG_NODE_STYLE_PROPERTY_IMPL(YGDisplay, Display, display, display);
YG_NODE_STYLE_PROPERTY_IMPL(float, Flex, flex, flex);
// TODO(T26792433): Change the API to accept YGFloatOptional.
void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) {
if (!YGFloatOptionalFloatEquals(node->getStyle().flex, flex)) {
YGStyle style = node->getStyle();
if (YGFloatIsUndefined(flex)) {
style.flex = {true, 0};
} else {
style.flex = {false, flex};
}
node->setStyle(style);
node->markDirtyAndPropogate();
}
}
// TODO(T26792433): Change the API to accept YGFloatOptional.
float YGNodeStyleGetFlex(const YGNodeRef node) {
return node->getStyle().flex.isUndefined ? YGUndefined
: node->getStyle().flex.value;
}
YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexGrow, flexGrow, flexGrow);
YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexShrink, flexShrink, flexShrink);
YG_NODE_STYLE_PROPERTY_UNIT_AUTO_IMPL(YGValue, FlexBasis, flexBasis, flexBasis);
@ -762,7 +781,7 @@ YG_NODE_STYLE_EDGE_PROPERTY_UNIT_IMPL(YGValue, Margin, margin, margin);
YG_NODE_STYLE_EDGE_PROPERTY_UNIT_AUTO_IMPL(YGValue, Margin, margin);
YG_NODE_STYLE_EDGE_PROPERTY_UNIT_IMPL(YGValue, Padding, padding, padding);
// TODO: Change the API to accept YGFloatOptional.
// TODO(T26792433): Change the API to accept YGFloatOptional.
void YGNodeStyleSetBorder(
const YGNodeRef node,
const YGEdge edge,
@ -783,8 +802,8 @@ void YGNodeStyleSetBorder(
float YGNodeStyleGetBorder(const YGNodeRef node, const YGEdge edge) {
if (node->getStyle().border[edge].unit == YGUnitUndefined) {
// TODO: Rather than returning YGUndefined, change the api to return
// YGFloatOptional.
// TODO(T26792433): Rather than returning YGUndefined, change the api to
// return YGFloatOptional.
return YGUndefined;
}

View File

@ -195,7 +195,6 @@ YG_NODE_STYLE_PROPERTY(YGPositionType, PositionType, positionType);
YG_NODE_STYLE_PROPERTY(YGWrap, FlexWrap, flexWrap);
YG_NODE_STYLE_PROPERTY(YGOverflow, Overflow, overflow);
YG_NODE_STYLE_PROPERTY(YGDisplay, Display, display);
YG_NODE_STYLE_PROPERTY(float, Flex, flex);
YG_NODE_STYLE_PROPERTY(float, FlexGrow, flexGrow);
YG_NODE_STYLE_PROPERTY(float, FlexShrink, flexShrink);