Skip to content

Commit

Permalink
Add YGLayoutGetMargin
Browse files Browse the repository at this point in the history
Summary:
Fix #326. I'll open another PR once this one gets accepted to add support for `YGLayoutGetBorder` 👌
Closes facebook/yoga#335

Reviewed By: gkassabli

Differential Revision: D4409399

Pulled By: emilsjolander

fbshipit-source-id: 8153f6701cab60b55a485f6d2e0b9f7767481090
  • Loading branch information
arcanis authored and facebook-github-bot committed Jan 15, 2017
1 parent fdd3288 commit 2ea5457
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
64 changes: 43 additions & 21 deletions Yoga.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef struct YGCachedMeasurement {
typedef struct YGLayout {
float position[4];
float dimensions[2];
float margin[6];
float padding[6];
YGDirection direction;

Expand Down Expand Up @@ -530,6 +531,29 @@ void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) {
return node->layout.instanceName; \
}

#define YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(type, name, instanceName) \
type YGNodeLayoutGet##name(const YGNodeRef node, const YGEdge edge) { \
YG_ASSERT(edge <= YGEdgeEnd, "Cannot get layout properties of multi-edge shorthands"); \
\
if (edge == YGEdgeLeft) { \
if (node->layout.direction == YGDirectionRTL) { \
return node->layout.instanceName[YGEdgeEnd]; \
} else { \
return node->layout.instanceName[YGEdgeStart]; \
} \
} \
\
if (edge == YGEdgeRight) { \
if (node->layout.direction == YGDirectionRTL) { \
return node->layout.instanceName[YGEdgeStart]; \
} else { \
return node->layout.instanceName[YGEdgeEnd]; \
} \
} \
\
return node->layout.instanceName[edge]; \
}

YG_NODE_PROPERTY_IMPL(void *, Context, context, context);
YG_NODE_PROPERTY_IMPL(YGPrintFunc, PrintFunc, printFunc, print);
YG_NODE_PROPERTY_IMPL(bool, HasNewLayout, hasNewLayout, hasNewLayout);
Expand Down Expand Up @@ -571,27 +595,8 @@ YG_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[YGDimensionWidth]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight]);
YG_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction);

float YGNodeLayoutGetPadding(const YGNodeRef node, const YGEdge edge) {
YG_ASSERT(edge <= YGEdgeEnd, "Cannot get layout paddings of multi-edge shorthands");

if (edge == YGEdgeLeft) {
if (node->layout.direction == YGDirectionRTL) {
return node->layout.padding[YGEdgeEnd];
} else {
return node->layout.padding[YGEdgeStart];
}
}

if (edge == YGEdgeRight) {
if (node->layout.direction == YGDirectionRTL) {
return node->layout.padding[YGEdgeStart];
} else {
return node->layout.padding[YGEdgeEnd];
}
}

return node->layout.padding[edge];
}
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Margin, margin);
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding);

uint32_t gCurrentGenerationCount = 0;

Expand Down Expand Up @@ -1742,6 +1747,23 @@ static void YGNodelayoutImpl(const YGNodeRef node,
const YGDirection direction = YGNodeResolveDirection(node, parentDirection);
node->layout.direction = direction;

node->layout.margin[YGEdgeStart] =
YGNodeLeadingMargin(node,
YGFlexDirectionResolve(YGFlexDirectionRow, direction),
parentWidth);
node->layout.margin[YGEdgeEnd] =
YGNodeTrailingMargin(node,
YGFlexDirectionResolve(YGFlexDirectionRow, direction),
parentWidth);
node->layout.margin[YGEdgeTop] =
YGNodeLeadingMargin(node,
YGFlexDirectionResolve(YGFlexDirectionColumn, direction),
parentWidth);
node->layout.margin[YGEdgeBottom] =
YGNodeTrailingMargin(node,
YGFlexDirectionResolve(YGFlexDirectionColumn, direction),
parentWidth);

node->layout.padding[YGEdgeStart] =
YGNodeLeadingPadding(node,
YGFlexDirectionResolve(YGFlexDirectionRow, direction),
Expand Down
1 change: 1 addition & 0 deletions Yoga.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ YG_NODE_LAYOUT_PROPERTY(YGDirection, Direction);
// pixel values then the returned value will be the same as YGNodeStyleGetPadding. However if
// padding was set using a percentage value then the returned value is the computed value used
// during layout.
WIN_EXPORT float YGNodeLayoutGetMargin(const YGNodeRef node, const YGEdge edge);
WIN_EXPORT float YGNodeLayoutGetPadding(const YGNodeRef node, const YGEdge edge);

WIN_EXPORT void YGSetLogger(YGLogger logger);
Expand Down

0 comments on commit 2ea5457

Please sign in to comment.