From e398e9e9e0764a683bb733501c4e1789a1c12438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Thu, 12 Jan 2017 11:17:40 +0100 Subject: [PATCH] Add layout margin functions to the java interface --- java/com/facebook/yoga/YogaNode.java | 28 +++++++++++++++++++++++++ java/com/facebook/yoga/YogaNodeAPI.java | 1 + java/jni/YGJNI.cpp | 10 +++++++++ 3 files changed, 39 insertions(+) diff --git a/java/com/facebook/yoga/YogaNode.java b/java/com/facebook/yoga/YogaNode.java index 432c204017..cc325c923b 100644 --- a/java/com/facebook/yoga/YogaNode.java +++ b/java/com/facebook/yoga/YogaNode.java @@ -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; @@ -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) { diff --git a/java/com/facebook/yoga/YogaNodeAPI.java b/java/com/facebook/yoga/YogaNodeAPI.java index 6d782c4525..b4a43b70d6 100644 --- a/java/com/facebook/yoga/YogaNodeAPI.java +++ b/java/com/facebook/yoga/YogaNodeAPI.java @@ -82,6 +82,7 @@ public interface YogaNodeAPI { float getLayoutY(); float getLayoutWidth(); float getLayoutHeight(); + float getLayoutMargin(YogaEdge edge); float getLayoutPadding(YogaEdge edge); YogaDirection getLayoutDirection(); YogaOverflow getOverflow(); diff --git a/java/jni/YGJNI.cpp b/java/jni/YGJNI.cpp index 54610dbada..05d1c20e06 100644 --- a/java/jni/YGJNI.cpp +++ b/java/jni/YGJNI.cpp @@ -30,6 +30,11 @@ static void YGTransferLayoutOutputsRecursive(YGNodeRef root) { static auto leftField = obj->getClass()->getField("mLeft"); static auto topField = obj->getClass()->getField("mTop"); + static auto marginLeftField = obj->getClass()->getField("mMarginLeft"); + static auto marginTopField = obj->getClass()->getField("mMarginTop"); + static auto marginRightField = obj->getClass()->getField("mMarginRight"); + static auto marginBottomField = obj->getClass()->getField("mMarginBottom"); + static auto paddingLeftField = obj->getClass()->getField("mPaddingLeft"); static auto paddingTopField = obj->getClass()->getField("mPaddingTop"); static auto paddingRightField = obj->getClass()->getField("mPaddingRight"); @@ -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));