diff --git a/csharp/Facebook.Yoga/BaselineFunction.cs b/csharp/Facebook.Yoga/BaselineFunction.cs new file mode 100644 index 00000000..cf055b19 --- /dev/null +++ b/csharp/Facebook.Yoga/BaselineFunction.cs @@ -0,0 +1,13 @@ +/** + * 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. + */ + +namespace Facebook.Yoga +{ + public delegate float BaselineFunction(YogaNode node, float width, float height); +} diff --git a/csharp/Facebook.Yoga/Facebook.Yoga.Shared.projitems b/csharp/Facebook.Yoga/Facebook.Yoga.Shared.projitems index 5889d249..b48fb339 100644 --- a/csharp/Facebook.Yoga/Facebook.Yoga.Shared.projitems +++ b/csharp/Facebook.Yoga/Facebook.Yoga.Shared.projitems @@ -9,12 +9,14 @@ Facebook.Yoga.Shared + + diff --git a/csharp/Facebook.Yoga/Native.cs b/csharp/Facebook.Yoga/Native.cs index b66cf8a6..248fc17f 100644 --- a/csharp/Facebook.Yoga/Native.cs +++ b/csharp/Facebook.Yoga/Native.cs @@ -104,8 +104,9 @@ namespace Facebook.Yoga [MarshalAs(UnmanagedType.FunctionPtr)] YogaMeasureFunc measureFunc); [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] - [return: MarshalAs(UnmanagedType.FunctionPtr)] - public static extern YogaMeasureFunc YGNodeGetMeasureFunc(YGNodeHandle node); + public static extern void YGNodeSetBaselineFunc( + YGNodeHandle node, + [MarshalAs(UnmanagedType.FunctionPtr)] YogaBaselineFunc baselineFunc); [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] public static extern void YGNodeSetHasNewLayout(YGNodeHandle node, [MarshalAs(UnmanagedType.I1)] bool hasNewLayout); diff --git a/csharp/Facebook.Yoga/YogaBaselineFunc.cs b/csharp/Facebook.Yoga/YogaBaselineFunc.cs new file mode 100644 index 00000000..78575434 --- /dev/null +++ b/csharp/Facebook.Yoga/YogaBaselineFunc.cs @@ -0,0 +1,17 @@ +/** + * 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. + */ + +using System; +using System.Runtime.InteropServices; + +namespace Facebook.Yoga +{ + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate float YogaBaselineFunc(IntPtr node, float width, float height); +} diff --git a/csharp/Facebook.Yoga/YogaNode.cs b/csharp/Facebook.Yoga/YogaNode.cs index 5732748b..ffc2c6f6 100644 --- a/csharp/Facebook.Yoga/YogaNode.cs +++ b/csharp/Facebook.Yoga/YogaNode.cs @@ -21,6 +21,8 @@ namespace Facebook.Yoga private List _children; private MeasureFunction _measureFunction; private YogaMeasureFunc _ygMeasureFunc; + private BaselineFunction _baselineFunction; + private YogaBaselineFunc _ygBaselineFunc; private object _data; public YogaNode() @@ -37,6 +39,7 @@ namespace Facebook.Yoga public void Reset() { _measureFunction = null; + _baselineFunction = null; _data = null; Native.YGNodeReset(_ygNode); @@ -84,6 +87,14 @@ namespace Facebook.Yoga } } + public bool IsBaselineDefined + { + get + { + return _baselineFunction != null; + } + } + public void CopyStyle(YogaNode srcNode) { Native.YGNodeCopyStyle(_ygNode, srcNode._ygNode); @@ -585,6 +596,13 @@ namespace Facebook.Yoga Native.YGNodeSetMeasureFunc(_ygNode, _ygMeasureFunc); } + public void SetBaselineFunction(BaselineFunction baselineFunction) + { + _baselineFunction = baselineFunction; + _ygBaselineFunc = baselineFunction != null ? BaselineInternal : (YogaBaselineFunc)null; + Native.YGNodeSetBaselineFunc(_ygNode, _ygBaselineFunc); + } + public void CalculateLayout() { Native.YGNodeCalculateLayout( @@ -609,6 +627,16 @@ namespace Facebook.Yoga return _measureFunction(this, width, widthMode, height, heightMode); } + private float BaselineInternal(IntPtr node, float width, float height) + { + if (_baselineFunction == null) + { + throw new InvalidOperationException("Baseline function is not defined."); + } + + return _baselineFunction(this, width, height); + } + public string Print(YogaPrintOptions options = YogaPrintOptions.Layout|YogaPrintOptions.Style|YogaPrintOptions.Children) { diff --git a/csharp/tests/Facebook.Yoga/YogaNodeTest.cs b/csharp/tests/Facebook.Yoga/YogaNodeTest.cs index 9979f091..a0fc5240 100644 --- a/csharp/tests/Facebook.Yoga/YogaNodeTest.cs +++ b/csharp/tests/Facebook.Yoga/YogaNodeTest.cs @@ -197,6 +197,54 @@ namespace Facebook.Yoga }); } + [Test] + public void TestBaselineFunc() + { + YogaNode node = new YogaNode(); + node.Height = 200; + node.FlexDirection = YogaFlexDirection.Row; + node.AlignItems = YogaAlign.Baseline; + + YogaNode child0 = new YogaNode(); + child0.Width = 100; + child0.Height = 110; + child0.SetBaselineFunction((_, width, height) => { + Assert.AreEqual(100, width); + Assert.AreEqual(110, height); + return 65; + }); + node.Insert(0, child0); + + YogaNode child1 = new YogaNode(); + child1.Width = 100; + child1.Height = 110; + child1.SetBaselineFunction((_, width, height) => { + Assert.AreEqual(100, width); + Assert.AreEqual(110, height); + return 80; + }); + node.Insert(1, child1); + + YogaNode child2 = new YogaNode(); + child2.Width = 100; + child2.Height = 110; + child2.SetBaselineFunction((_, width, height) => { + Assert.AreEqual(100, width); + Assert.AreEqual(110, height); + return 88; + }); + node.Insert(2, child2); + + node.CalculateLayout(); + + Assert.AreEqual(0, child0.LayoutX); + Assert.AreEqual(23, child0.LayoutY); + Assert.AreEqual(100, child1.LayoutX); + Assert.AreEqual(8, child1.LayoutY); + Assert.AreEqual(200, child2.LayoutX); + Assert.AreEqual(0, child2.LayoutY); + } + [Test] public void TestPrint() {