Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add YGLayoutGetMargin #335

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions csharp/Facebook.Yoga/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ public static extern void YGNodeSetBaselineFunc(
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern float YGNodeLayoutGetHeight(YGNodeHandle node);

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern float YGNodeLayoutGetMargin(YGNodeHandle node, YogaEdge edge);

[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern float YGNodeLayoutGetPadding(YGNodeHandle node, YogaEdge edge);

Expand Down
48 changes: 48 additions & 0 deletions csharp/Facebook.Yoga/YogaNode.Spacing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,54 @@ public float BorderWidth
}
}

public float LayoutMarginLeft
{
get
{
return Native.YGNodeLayoutGetMargin(_ygNode, YogaEdge.Left);
}
}

public float LayoutMarginTop
{
get
{
return Native.YGNodeLayoutGetMargin(_ygNode, YogaEdge.Top);
}
}

public float LayoutMarginRight
{
get
{
return Native.YGNodeLayoutGetMargin(_ygNode, YogaEdge.Right);
}
}

public float LayoutMarginBottom
{
get
{
return Native.YGNodeLayoutGetMargin(_ygNode, YogaEdge.Bottom);
}
}

public float LayoutMarginStart
{
get
{
return Native.YGNodeLayoutGetMargin(_ygNode, YogaEdge.Start);
}
}

public float LayoutMarginEnd
{
get
{
return Native.YGNodeLayoutGetMargin(_ygNode, YogaEdge.End);
}
}

public float LayoutPaddingLeft
{
get
Expand Down
6 changes: 6 additions & 0 deletions csharp/Facebook.Yoga/YogaNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ public void SetPosition(YogaEdge edge, YogaValue value)
}
}

[Obsolete("use LayoutMargin properties")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't add an Obsolete property :p all these will be removed soon enough.

public float GetLayoutMargin(YogaEdge edge)
{
return Native.YGNodeLayoutGetMargin(_ygNode, edge);
}

[Obsolete("use LayoutPadding properties")]
public float GetLayoutPadding(YogaEdge edge)
{
Expand Down
28 changes: 28 additions & 0 deletions java/com/facebook/yoga/YogaNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public static boolean isExperimentalFeatureEnabled(YogaExperimentalFeature featu
@DoNotStrip
private float mLeft = YogaConstants.UNDEFINED;
@DoNotStrip
private float mMarginLeft = 0;
@DoNotStrip
private float mMarginTop = 0;
@DoNotStrip
private float mMarginRight = 0;
@DoNotStrip
private float mMarginBottom = 0;
@DoNotStrip
private float mPaddingLeft = 0;
@DoNotStrip
private float mPaddingTop = 0;
Expand Down Expand Up @@ -573,6 +581,26 @@ public float getLayoutHeight() {
return mHeight;
}

@Override
public float getLayoutMargin(YogaEdge edge) {
switch (edge) {
case LEFT:
return mMarginLeft;
case TOP:
return mMarginTop;
case RIGHT:
return mMarginRight;
case BOTTOM:
return mMarginBottom;
case START:
return getLayoutDirection() == YogaDirection.RTL ? mMarginRight : mMarginLeft;
case END:
return getLayoutDirection() == YogaDirection.RTL ? mMarginLeft : mMarginRight;
default:
throw new IllegalArgumentException("Cannot get layout margins of multi-edge shorthands");
}
}

@Override
public float getLayoutPadding(YogaEdge edge) {
switch (edge) {
Expand Down
1 change: 1 addition & 0 deletions java/com/facebook/yoga/YogaNodeAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public interface YogaNodeAPI<YogaNodeType extends YogaNodeAPI> {
float getLayoutY();
float getLayoutWidth();
float getLayoutHeight();
float getLayoutMargin(YogaEdge edge);
float getLayoutPadding(YogaEdge edge);
YogaDirection getLayoutDirection();
YogaOverflow getOverflow();
Expand Down
10 changes: 10 additions & 0 deletions java/jni/YGJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ static void YGTransferLayoutOutputsRecursive(YGNodeRef root) {
static auto leftField = obj->getClass()->getField<jfloat>("mLeft");
static auto topField = obj->getClass()->getField<jfloat>("mTop");

static auto marginLeftField = obj->getClass()->getField<jfloat>("mMarginLeft");
static auto marginTopField = obj->getClass()->getField<jfloat>("mMarginTop");
static auto marginRightField = obj->getClass()->getField<jfloat>("mMarginRight");
static auto marginBottomField = obj->getClass()->getField<jfloat>("mMarginBottom");

static auto paddingLeftField = obj->getClass()->getField<jfloat>("mPaddingLeft");
static auto paddingTopField = obj->getClass()->getField<jfloat>("mPaddingTop");
static auto paddingRightField = obj->getClass()->getField<jfloat>("mPaddingRight");
Expand All @@ -40,6 +45,11 @@ static void YGTransferLayoutOutputsRecursive(YGNodeRef root) {
obj->setFieldValue(leftField, YGNodeLayoutGetLeft(root));
obj->setFieldValue(topField, YGNodeLayoutGetTop(root));

obj->setFieldValue(marginLeftField, YGNodeLayoutGetMargin(root, YGEdgeLeft));
obj->setFieldValue(marginTopField, YGNodeLayoutGetMargin(root, YGEdgeTop));
obj->setFieldValue(marginRightField, YGNodeLayoutGetMargin(root, YGEdgeRight));
obj->setFieldValue(marginBottomField, YGNodeLayoutGetMargin(root, YGEdgeBottom));

obj->setFieldValue(paddingLeftField, YGNodeLayoutGetPadding(root, YGEdgeLeft));
obj->setFieldValue(paddingTopField, YGNodeLayoutGetPadding(root, YGEdgeTop));
obj->setFieldValue(paddingRightField, YGNodeLayoutGetPadding(root, YGEdgeRight));
Expand Down
16 changes: 8 additions & 8 deletions javascript/build/Release/nbind.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions javascript/sources/Node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,13 @@ Layout Node::getComputedLayout(void) const

return layout;
}

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

double Node::getComputedPadding(int edge) const
{
return YGNodeLayoutGetPadding(m_node, static_cast<YGEdge>(edge));
}
3 changes: 3 additions & 0 deletions javascript/sources/Node.hh
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ class Node {

Layout getComputedLayout(void) const;

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

private:

YGNodeRef m_node;
Expand Down
3 changes: 3 additions & 0 deletions javascript/sources/nbind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,7 @@ NBIND_CLASS(Node)
method(getComputedHeight);

method(getComputedLayout);

method(getComputedMargin);
method(getComputedPadding);
}
33 changes: 33 additions & 0 deletions javascript/tests/Facebook.Yoga/YGComputedMarginTest.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("margin_start", function () {
var root = Yoga.Node.create();
root.setWidth(100);
root.setHeight(100);
root.setMargin(Yoga.EDGE_START, `10%`);

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

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

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

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

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

(typeof gc !== "undefined") && gc();
console.assert(0 === Yoga.getInstanceCount(), "0 === Yoga.getInstanceCount() (" + Yoga.getInstanceCount() + ")");
});
33 changes: 33 additions & 0 deletions javascript/tests/Facebook.Yoga/YGComputedPaddingTest.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("padding_start", function () {
var root = Yoga.Node.create();
root.setWidth(100);
root.setHeight(100);
root.setPadding(Yoga.EDGE_START, `10%`);

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

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

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

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

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

(typeof gc !== "undefined") && gc();
console.assert(0 === Yoga.getInstanceCount(), "0 === Yoga.getInstanceCount() (" + Yoga.getInstanceCount() + ")");
});
30 changes: 30 additions & 0 deletions tests/YGComputedMarginTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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.
*/

#include <yoga/Yoga.h>
#include <gtest/gtest.h>

TEST(YogaTest, computed_layout_margin) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
YGNodeStyleSetMarginPercent(root, YGEdgeStart, 10);

YGNodeCalculateLayout(root, 100, 100, YGDirectionLTR);

ASSERT_FLOAT_EQ(10, YGNodeLayoutGetMargin(root, YGEdgeLeft));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetMargin(root, YGEdgeRight));

YGNodeCalculateLayout(root, 100, 100, YGDirectionRTL);

ASSERT_FLOAT_EQ(0, YGNodeLayoutGetMargin(root, YGEdgeLeft));
ASSERT_FLOAT_EQ(10, YGNodeLayoutGetMargin(root, YGEdgeRight));

YGNodeFreeRecursive(root);
}
64 changes: 43 additions & 21 deletions yoga/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/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