Skip to content

Commit

Permalink
Add YGLayoutGetBorder, counterpart of YGLayoutGetPadding
Browse files Browse the repository at this point in the history
Summary:
Followup of #335, fix #326. This commit add the `YGLayoutGetBorder(node, edge)` function, which correctly takes RTL/LTR into account when resolving `EDGE_START` & `EDGE_END`.
Closes #344

Reviewed By: dshahidehpour

Differential Revision: D4459950

Pulled By: emilsjolander

fbshipit-source-id: b57eb7a5b1c181a364913c3200a3794a2b7b31a6
  • Loading branch information
arcanis authored and facebook-github-bot committed Jan 26, 2017
1 parent 7c09244 commit db732ce
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 35 deletions.
12 changes: 6 additions & 6 deletions javascript/build/Release/nbind.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions javascript/sources/Node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,11 @@ double Node::getComputedMargin(int edge) const
return YGNodeLayoutGetMargin(m_node, static_cast<YGEdge>(edge));
}

double Node::getComputedBorder(int edge) const
{
return YGNodeLayoutGetBorder(m_node, static_cast<YGEdge>(edge));
}

double Node::getComputedPadding(int edge) const
{
return YGNodeLayoutGetPadding(m_node, static_cast<YGEdge>(edge));
Expand Down
1 change: 1 addition & 0 deletions javascript/sources/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class Node {
Layout getComputedLayout(void) const;

double getComputedMargin(int edge) const;
double getComputedBorder(int edge) const;
double getComputedPadding(int edge) const;

private:
Expand Down
1 change: 1 addition & 0 deletions javascript/sources/nbind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,6 @@ NBIND_CLASS(Node)
method(getComputedLayout);

method(getComputedMargin);
method(getComputedBorder);
method(getComputedPadding);
}
33 changes: 33 additions & 0 deletions javascript/tests/Facebook.Yoga/YGComputedBorderTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

var Yoga = Yoga || require("../../sources/entry-" + process.env.TEST_ENTRY);

it("border_start", function () {
var root = Yoga.Node.create();
root.setWidth(100);
root.setHeight(100);
root.setBorder(Yoga.EDGE_START, 10);

root.calculateLayout(100, 100, Yoga.DIRECTION_LTR);

console.assert(10 === root.getComputedBorder(Yoga.EDGE_LEFT), "10 === root.getComputedBorder(Yoga.EDGE_LEFT)");
console.assert(0 === root.getComputedBorder(Yoga.EDGE_RIGHT), "0 === root.getComputedBorder(Yoga.EDGE_RIGHT)");

root.calculateLayout(100, 100, Yoga.DIRECTION_RTL);

console.assert(0 === root.getComputedBorder(Yoga.EDGE_LEFT), "0 === root.getComputedBorder(Yoga.EDGE_LEFT)");
console.assert(10 === root.getComputedBorder(Yoga.EDGE_RIGHT), "10 === root.getComputedBorder(Yoga.EDGE_RIGHT)");

if (typeof root !== "undefined")
root.freeRecursive();

(typeof gc !== "undefined") && gc();
console.assert(0 === Yoga.getInstanceCount(), "0 === Yoga.getInstanceCount() (" + Yoga.getInstanceCount() + ")");
});
46 changes: 22 additions & 24 deletions yoga/Yoga.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef struct YGLayout {
float position[4];
float dimensions[2];
float margin[6];
float border[6];
float padding[6];
YGDirection direction;

Expand Down Expand Up @@ -596,6 +597,7 @@ YG_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight]);
YG_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction);

YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Margin, margin);
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Border, border);
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding);

uint32_t gCurrentGenerationCount = 0;
Expand Down Expand Up @@ -1764,39 +1766,35 @@ static void YGNodelayoutImpl(const YGNodeRef node,
const YGDirection direction = YGNodeResolveDirection(node, parentDirection);
node->layout.direction = direction;

const YGFlexDirection flexRowDirection = YGFlexDirectionResolve(YGFlexDirectionRow, direction);
const YGFlexDirection flexColumnDirection = YGFlexDirectionResolve(YGFlexDirectionColumn, direction);

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

node->layout.border[YGEdgeStart] =
YGNodeLeadingBorder(node, flexRowDirection);
node->layout.border[YGEdgeEnd] =
YGNodeTrailingBorder(node, flexRowDirection);
node->layout.border[YGEdgeTop] =
YGNodeLeadingBorder(node, flexColumnDirection);
node->layout.border[YGEdgeBottom] =
YGNodeTrailingBorder(node, flexColumnDirection);

node->layout.padding[YGEdgeStart] =
YGNodeLeadingPadding(node,
YGFlexDirectionResolve(YGFlexDirectionRow, direction),
parentWidth);
YGNodeLeadingPadding(node, flexRowDirection, parentWidth);
node->layout.padding[YGEdgeEnd] =
YGNodeTrailingPadding(node,
YGFlexDirectionResolve(YGFlexDirectionRow, direction),
parentWidth);
YGNodeTrailingPadding(node, flexRowDirection, parentWidth);
node->layout.padding[YGEdgeTop] =
YGNodeLeadingPadding(node,
YGFlexDirectionResolve(YGFlexDirectionColumn, direction),
parentWidth);
YGNodeLeadingPadding(node, flexColumnDirection, parentWidth);
node->layout.padding[YGEdgeBottom] =
YGNodeTrailingPadding(node,
YGFlexDirectionResolve(YGFlexDirectionColumn, direction),
parentWidth);
YGNodeTrailingPadding(node, flexColumnDirection, parentWidth);

if (node->measure) {
YGNodeWithMeasureFuncSetMeasuredDimensions(
Expand Down
14 changes: 9 additions & 5 deletions yoga/Yoga.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ WIN_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode
#define YG_NODE_LAYOUT_PROPERTY(type, name) \
WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node);

#define YG_NODE_LAYOUT_EDGE_PROPERTY(type, name) \
WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node, const YGEdge edge);

YG_NODE_PROPERTY(void *, Context, context);
YG_NODE_PROPERTY(YGMeasureFunc, MeasureFunc, measureFunc);
YG_NODE_PROPERTY(YGBaselineFunc, BaselineFunc, baselineFunc)
Expand Down Expand Up @@ -195,12 +198,13 @@ YG_NODE_LAYOUT_PROPERTY(float, Width);
YG_NODE_LAYOUT_PROPERTY(float, Height);
YG_NODE_LAYOUT_PROPERTY(YGDirection, Direction);

// Get the computed padding for this node after performing layout. If padding was set using
// 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
// Get the computed values for these nodes after performing layout. If they were set using
// pixel values then the returned value will be the same as YGNodeStyleGetXXX. However if
// they were 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);
YG_NODE_LAYOUT_EDGE_PROPERTY(float, Margin);
YG_NODE_LAYOUT_EDGE_PROPERTY(float, Border);
YG_NODE_LAYOUT_EDGE_PROPERTY(float, Padding);

WIN_EXPORT void YGSetLogger(YGLogger logger);
WIN_EXPORT void YGLog(YGLogLevel level, const char *message, ...);
Expand Down

0 comments on commit db732ce

Please sign in to comment.