C# Baseline function

Summary:
same as Measure function, based on #317
Closes https://github.com/facebook/yoga/pull/321

Reviewed By: emilsjolander

Differential Revision: D4385336

Pulled By: splhack

fbshipit-source-id: b583ec79861d2e9abba31a72503e2f706bfda8e8
This commit is contained in:
Kazuki Sakamoto
2017-01-07 09:06:06 -08:00
committed by Facebook Github Bot
parent 8d320ceac2
commit 70a4221b9e
6 changed files with 111 additions and 2 deletions

View File

@@ -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);
}

View File

@@ -9,12 +9,14 @@
<Import_RootNamespace>Facebook.Yoga.Shared</Import_RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)BaselineFunction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Border.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MeasureFunction.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MeasureOutput.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Native.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Spacing.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaAlign.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaBaselineFunc.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaConstants.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaDimension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)YogaDirection.cs" />

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -21,6 +21,8 @@ namespace Facebook.Yoga
private List<YogaNode> _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)
{

View File

@@ -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()
{