From d44ebab006be1114cc88890a563edb7b862ba0a3 Mon Sep 17 00:00:00 2001 From: Lukas Woehrl Date: Fri, 23 Dec 2016 22:33:06 +0100 Subject: [PATCH] changed the csharp interop code to use new functions --- csharp/Facebook.Yoga/Native.cs | 50 +++++++++--- csharp/Facebook.Yoga/YogaConstants.cs | 2 +- csharp/Facebook.Yoga/YogaNode.cs | 94 +++++++++++++++++++--- csharp/Facebook.Yoga/YogaUnit.cs | 1 + csharp/Facebook.Yoga/YogaValue.cs | 25 +++--- csharp/tests/Facebook.Yoga/YogaNodeTest.cs | 2 +- yoga/Yoga.c | 14 ++-- 7 files changed, 144 insertions(+), 44 deletions(-) diff --git a/csharp/Facebook.Yoga/Native.cs b/csharp/Facebook.Yoga/Native.cs index a1cc0437..0f8d58cd 100644 --- a/csharp/Facebook.Yoga/Native.cs +++ b/csharp/Facebook.Yoga/Native.cs @@ -194,43 +194,64 @@ namespace Facebook.Yoga public static extern float YGNodeStyleGetFlexShrink(YGNodeHandle node); [DllImport(DllName)] - public static extern void YGNodeStyleSetFlexBasis(YGNodeHandle node, YogaValue flexBasis); + public static extern void YGNodeStyleSetFlexBasis(YGNodeHandle node, float flexBasis); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetFlexBasisPercent(YGNodeHandle node, float flexBasis); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetFlexBasis(YGNodeHandle node); [DllImport(DllName)] - public static extern void YGNodeStyleSetWidth(YGNodeHandle node, YogaValue width); + public static extern void YGNodeStyleSetWidth(YGNodeHandle node, float width); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetWidthPercent(YGNodeHandle node, float width); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetWidth(YGNodeHandle node); [DllImport(DllName)] - public static extern void YGNodeStyleSetHeight(YGNodeHandle node, YogaValue height); + public static extern void YGNodeStyleSetHeight(YGNodeHandle node, float height); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetHeightPercent(YGNodeHandle node, float height); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetHeight(YGNodeHandle node); [DllImport(DllName)] - public static extern void YGNodeStyleSetMinWidth(YGNodeHandle node, YogaValue minWidth); + public static extern void YGNodeStyleSetMinWidth(YGNodeHandle node, float minWidth); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetMinWidthPercent(YGNodeHandle node, float minWidth); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetMinWidth(YGNodeHandle node); [DllImport(DllName)] - public static extern void YGNodeStyleSetMinHeight(YGNodeHandle node, YogaValue minHeight); + public static extern void YGNodeStyleSetMinHeight(YGNodeHandle node, float minHeight); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetMinHeightPercent(YGNodeHandle node, float minHeight); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetMinHeight(YGNodeHandle node); [DllImport(DllName)] - public static extern void YGNodeStyleSetMaxWidth(YGNodeHandle node, YogaValue maxWidth); + public static extern void YGNodeStyleSetMaxWidth(YGNodeHandle node, float maxWidth); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetMaxWidthPercent(YGNodeHandle node, float maxWidth); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetMaxWidth(YGNodeHandle node); [DllImport(DllName)] - public static extern void YGNodeStyleSetMaxHeight(YGNodeHandle node, YogaValue maxHeight); + public static extern void YGNodeStyleSetMaxHeight(YGNodeHandle node, float maxHeight); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetMaxHeightPercent(YGNodeHandle node, float maxHeight); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetMaxHeight(YGNodeHandle node); @@ -246,19 +267,28 @@ namespace Facebook.Yoga #region YG_NODE_STYLE_EDGE_PROPERTY [DllImport(DllName)] - public static extern void YGNodeStyleSetPosition(YGNodeHandle node, YogaEdge edge, YogaValue position); + public static extern void YGNodeStyleSetPosition(YGNodeHandle node, YogaEdge edge, float position); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetPositionPercent(YGNodeHandle node, YogaEdge edge, float position); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetPosition(YGNodeHandle node, YogaEdge edge); [DllImport(DllName)] - public static extern void YGNodeStyleSetMargin(YGNodeHandle node, YogaEdge edge, YogaValue margin); + public static extern void YGNodeStyleSetMargin(YGNodeHandle node, YogaEdge edge, float margin); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetMarginPercent(YGNodeHandle node, YogaEdge edge, float margin); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetMargin(YGNodeHandle node, YogaEdge edge); [DllImport(DllName)] - public static extern void YGNodeStyleSetPadding(YGNodeHandle node, YogaEdge edge, YogaValue padding); + public static extern void YGNodeStyleSetPadding(YGNodeHandle node, YogaEdge edge, float padding); + + [DllImport(DllName)] + public static extern void YGNodeStyleSetPaddingPercent(YGNodeHandle node, YogaEdge edge, float padding); [DllImport(DllName)] public static extern YogaValue YGNodeStyleGetPadding(YGNodeHandle node, YogaEdge edge); diff --git a/csharp/Facebook.Yoga/YogaConstants.cs b/csharp/Facebook.Yoga/YogaConstants.cs index 76e51f9c..ca8ecfbb 100644 --- a/csharp/Facebook.Yoga/YogaConstants.cs +++ b/csharp/Facebook.Yoga/YogaConstants.cs @@ -20,7 +20,7 @@ namespace Facebook.Yoga public static bool IsUndefined(YogaValue value) { - return !value.IsDefined; + return value.Unit == YogaUnit.Undefined; } } } diff --git a/csharp/Facebook.Yoga/YogaNode.cs b/csharp/Facebook.Yoga/YogaNode.cs index 02b1954c..7395102d 100644 --- a/csharp/Facebook.Yoga/YogaNode.cs +++ b/csharp/Facebook.Yoga/YogaNode.cs @@ -236,7 +236,14 @@ namespace Facebook.Yoga set { - Native.YGNodeStyleSetFlexBasis(_ygNode, value); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetFlexBasisPercent(_ygNode, value.Value); + } + else + { + Native.YGNodeStyleSetFlexBasis(_ygNode, value.Value); + } } } @@ -247,7 +254,14 @@ namespace Facebook.Yoga public void SetMargin(YogaEdge edge, YogaValue value) { - Native.YGNodeStyleSetMargin(_ygNode, edge, value); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetMarginPercent(_ygNode, edge, value.Value); + } + else + { + Native.YGNodeStyleSetMargin(_ygNode, edge, value.Value); + } } public YogaValue GetPadding(YogaEdge edge) @@ -255,9 +269,16 @@ namespace Facebook.Yoga return Native.YGNodeStyleGetPadding(_ygNode, edge); } - public void SetPadding(YogaEdge edge, YogaValue padding) + public void SetPadding(YogaEdge edge, YogaValue value) { - Native.YGNodeStyleSetPadding(_ygNode, edge, padding); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetPaddingPercent(_ygNode, edge, value.Value); + } + else + { + Native.YGNodeStyleSetPadding(_ygNode, edge, value.Value); + } } public float GetBorder(YogaEdge edge) @@ -275,9 +296,16 @@ namespace Facebook.Yoga return Native.YGNodeStyleGetPosition(_ygNode, edge); } - public void SetPosition(YogaEdge edge, YogaValue position) + public void SetPosition(YogaEdge edge, YogaValue value) { - Native.YGNodeStyleSetPosition(_ygNode, edge, position); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetPositionPercent(_ygNode, edge, value.Value); + } + else + { + Native.YGNodeStyleSetPosition(_ygNode, edge, value.Value); + } } public YogaValue Width @@ -289,7 +317,14 @@ namespace Facebook.Yoga set { - Native.YGNodeStyleSetWidth(_ygNode, value); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetWidthPercent(_ygNode, value.Value); + } + else + { + Native.YGNodeStyleSetWidth(_ygNode, value.Value); + } } } @@ -302,7 +337,14 @@ namespace Facebook.Yoga set { - Native.YGNodeStyleSetHeight(_ygNode, value); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetHeightPercent(_ygNode, value.Value); + } + else + { + Native.YGNodeStyleSetHeight(_ygNode, value.Value); + } } } @@ -315,7 +357,14 @@ namespace Facebook.Yoga set { - Native.YGNodeStyleSetMaxWidth(_ygNode, value); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetMaxWidthPercent(_ygNode, value.Value); + } + else + { + Native.YGNodeStyleSetMaxWidth(_ygNode, value.Value); + } } } @@ -328,7 +377,14 @@ namespace Facebook.Yoga set { - Native.YGNodeStyleSetMaxHeight(_ygNode, value); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetMaxHeightPercent(_ygNode, value.Value); + } + else + { + Native.YGNodeStyleSetMaxHeight(_ygNode, value.Value); + } } } @@ -341,7 +397,14 @@ namespace Facebook.Yoga set { - Native.YGNodeStyleSetMinWidth(_ygNode, value); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetMinWidthPercent(_ygNode, value.Value); + } + else + { + Native.YGNodeStyleSetMinWidth(_ygNode, value.Value); + } } } @@ -354,7 +417,14 @@ namespace Facebook.Yoga set { - Native.YGNodeStyleSetMinHeight(_ygNode, value); + if (value.Unit == YogaUnit.Percent) + { + Native.YGNodeStyleSetMinHeightPercent(_ygNode, value.Value); + } + else + { + Native.YGNodeStyleSetMinHeight(_ygNode, value.Value); + } } } diff --git a/csharp/Facebook.Yoga/YogaUnit.cs b/csharp/Facebook.Yoga/YogaUnit.cs index 660cb9d6..d8cfe2f8 100644 --- a/csharp/Facebook.Yoga/YogaUnit.cs +++ b/csharp/Facebook.Yoga/YogaUnit.cs @@ -11,6 +11,7 @@ namespace Facebook.Yoga { public enum YogaUnit { + Undefined, Pixel, Percent } diff --git a/csharp/Facebook.Yoga/YogaValue.cs b/csharp/Facebook.Yoga/YogaValue.cs index 9e2bc258..055f7d68 100644 --- a/csharp/Facebook.Yoga/YogaValue.cs +++ b/csharp/Facebook.Yoga/YogaValue.cs @@ -14,25 +14,24 @@ namespace Facebook.Yoga [StructLayout(LayoutKind.Sequential)] public struct YogaValue { - private float Value; - private YogaUnit Unit; - private byte isDefined; /* use byte to keep struct blitable */ + private float value; + private YogaUnit unit; - public bool IsDefined => isDefined != 0; + public YogaUnit Unit => unit; + public float Value => value; public static YogaValue Pixel(float value) { return new YogaValue { - Value = value, - isDefined = YogaConstants.IsUndefined(value) ? (byte)0 : (byte)1, - Unit = YogaUnit.Pixel + value = value, + unit = YogaConstants.IsUndefined(value) ? YogaUnit.Undefined : YogaUnit.Pixel }; } public bool Equals(YogaValue other) { - return Value.Equals(other.Value) && Unit == other.Unit; + return Unit == other.Unit && (Value.Equals(other.Value) || Unit == YogaUnit.Undefined); } public override bool Equals(object obj) @@ -53,9 +52,8 @@ namespace Facebook.Yoga { return new YogaValue { - Value = YogaConstants.Undefined, - isDefined = 0, - Unit = YogaUnit.Pixel + value = YogaConstants.Undefined, + unit = YogaUnit.Undefined }; } @@ -63,9 +61,8 @@ namespace Facebook.Yoga { return new YogaValue { - Value = value, - isDefined = YogaConstants.IsUndefined(value) ? (byte)0 : (byte)1, - Unit = YogaUnit.Percent + value = value, + unit = YogaConstants.IsUndefined(value) ? YogaUnit.Undefined : YogaUnit.Percent }; } diff --git a/csharp/tests/Facebook.Yoga/YogaNodeTest.cs b/csharp/tests/Facebook.Yoga/YogaNodeTest.cs index a404c5fc..69c4f177 100644 --- a/csharp/tests/Facebook.Yoga/YogaNodeTest.cs +++ b/csharp/tests/Facebook.Yoga/YogaNodeTest.cs @@ -212,7 +212,7 @@ namespace Facebook.Yoga parent.Insert(0, child0); parent.Insert(0, child1); parent.CalculateLayout(); - Assert.AreEqual(parent.Print(), "{layout: {width: 100, height: 120, top: 0, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, overflow: 'visible', width: 100px, height: 120px, children: [\n {layout: {width: 35, height: 45, top: 0, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, overflow: 'visible', width: 35px, height: 45px, },\n {layout: {width: 30, height: 40, top: 45, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, overflow: 'visible', width: 30px, height: 40px, },\n]},\n"); + Assert.AreEqual("{layout: {width: 100, height: 120, top: 0, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, overflow: 'visible', width: 100px, height: 120px, children: [\n {layout: {width: 35, height: 45, top: 0, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, overflow: 'visible', width: 35px, height: 45px, },\n {layout: {width: 30, height: 40, top: 45, left: 0}, flexDirection: 'column', alignItems: 'stretch', flexGrow: 0, flexShrink: 0, overflow: 'visible', width: 30px, height: 40px, },\n]},\n", parent.Print()); } [Test] diff --git a/yoga/Yoga.c b/yoga/Yoga.c index 90d1a5d9..316e77ae 100644 --- a/yoga/Yoga.c +++ b/yoga/Yoga.c @@ -145,7 +145,7 @@ typedef struct YGNode { [YGDimensionHeight] = YG_UNDEFINED_VALUES, \ } -YGNode gYGNodeDefaults = { +static YGNode gYGNodeDefaults = { .parent = NULL, .children = NULL, .hasNewLayout = true, @@ -195,10 +195,7 @@ YGCalloc gYGCalloc = &calloc; YGRealloc gYGRealloc = &realloc; YGFree gYGFree = &free; -static YGValue YGValueUndefined = { - .value = YGUndefined, - .unit = YGUnitUndefined -}; +static YGValue YGValueUndefined = YG_UNDEFINED_VALUES; static YGValue YGValueZero = { .value = 0, @@ -598,9 +595,14 @@ inline bool YGFloatIsUndefined(const float value) { } static inline bool YGValueEqual(const YGValue a, const YGValue b) { - if (a.unit != YGUnitUndefined != b.unit != YGUnitUndefined || a.unit != b.unit) { + if (a.unit != b.unit) { return false; } + + if(a.unit == YGUnitUndefined) + { + return true; + } return fabs(a.value - b.value) < 0.0001f; }