From 47ad3f63cf26051729ab505bc738dc98310e009b Mon Sep 17 00:00:00 2001 From: Pritesh Nandgaonkar Date: Wed, 14 Mar 2018 04:17:05 -0700 Subject: [PATCH] Remove the usage of YGUndefined in the default values of Border in YGStyle Summary: Remove the usage of YGUndefined in the default values of Border in YGStyle. In the getter of Border function, YGUndefined is used just to keep it logically consistent. The proper solution would be to change the api in `Yoga.h` to accept `YGFloatOptional`, but that would require to change lot of the code in client of this library. Will make a separate diff for that. Reviewed By: emilsjolander Differential Revision: D7195115 fbshipit-source-id: e635cf55ac94d8a90caef6cafce281579da2cbfc --- yoga/Utils.cpp | 4 ++++ yoga/Utils.h | 3 +++ yoga/YGStyle.cpp | 2 +- yoga/Yoga.cpp | 30 +++++++++++++++++++++++++++++- yoga/Yoga.h | 5 +++++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/yoga/Utils.cpp b/yoga/Utils.cpp index 41284572..5ca834c8 100644 --- a/yoga/Utils.cpp +++ b/yoga/Utils.cpp @@ -49,3 +49,7 @@ bool YGFloatsEqual(const float a, const float b) { } return YGFloatIsUndefined(a) && YGFloatIsUndefined(b); } + +float YGFloatSanitize(const float& val) { + return YGFloatIsUndefined(val) ? 0 : val; +} diff --git a/yoga/Utils.h b/yoga/Utils.h index 6ebeb86f..44cfd300 100644 --- a/yoga/Utils.h +++ b/yoga/Utils.h @@ -86,6 +86,9 @@ bool YGFloatArrayEqual( return areEqual; } +// This function returns 0 if YGFloatIsUndefined(val) is true and val otherwise +float YGFloatSanitize(const float& val); + YGFlexDirection YGFlexDirectionCross( const YGFlexDirection flexDirection, const YGDirection direction); diff --git a/yoga/YGStyle.cpp b/yoga/YGStyle.cpp index f3bc16fe..c4ccf777 100644 --- a/yoga/YGStyle.cpp +++ b/yoga/YGStyle.cpp @@ -7,7 +7,7 @@ #include "YGStyle.h" -const YGValue kYGValueUndefined = {YGUndefined, YGUnitUndefined}; +const YGValue kYGValueUndefined = {0, YGUnitUndefined}; const YGValue kYGValueAuto = {YGUndefined, YGUnitAuto}; diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 41f89e87..61ceee77 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -760,7 +760,35 @@ YG_NODE_STYLE_EDGE_PROPERTY_UNIT_IMPL(YGValue, Position, position, position); 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); -YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Border, border, border); + +// TODO: Change the API to accept YGFloatOptional. +void YGNodeStyleSetBorder( + const YGNodeRef node, + const YGEdge edge, + const float border) { + YGValue value = { + .value = YGFloatSanitize(border), + .unit = YGFloatIsUndefined(border) ? YGUnitUndefined : YGUnitPoint, + }; + if ((node->getStyle().border[edge].value != value.value && + value.unit != YGUnitUndefined) || + node->getStyle().border[edge].unit != value.unit) { + YGStyle style = node->getStyle(); + style.border[edge] = value; + node->setStyle(style); + node->markDirtyAndPropogate(); + } +} + +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. + return YGUndefined; + } + + return node->getStyle().border[edge].value; +} YG_NODE_STYLE_PROPERTY_UNIT_AUTO_IMPL(YGValue, Width, width, dimensions[YGDimensionWidth]); YG_NODE_STYLE_PROPERTY_UNIT_AUTO_IMPL(YGValue, Height, height, dimensions[YGDimensionHeight]); diff --git a/yoga/Yoga.h b/yoga/Yoga.h index a632917a..56fe95ff 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -42,6 +42,11 @@ typedef struct YGValue { YGUnit unit; } YGValue; +struct YGFloatOptional { + bool isUndefined; + float value; +}; + extern const YGValue YGValueUndefined; extern const YGValue YGValueAuto;