Clean up MeasureFunc and PrintFunc
Summary: - Remove unneeded instance variables - Simplify MeasureFunc call - Add unittest Reviewed By: emilsjolander Differential Revision: D4013176 fbshipit-source-id: 4cdfa3ac3ad60a6bac5cda10644376ee9c46300b
This commit is contained in:
committed by
Facebook Github Bot
parent
722bfb9032
commit
7548164edb
@@ -22,14 +22,10 @@ namespace Facebook.CSSLayout
|
|||||||
private WeakReference _parent;
|
private WeakReference _parent;
|
||||||
private List<CSSNode> _children;
|
private List<CSSNode> _children;
|
||||||
private MeasureFunction _measureFunction;
|
private MeasureFunction _measureFunction;
|
||||||
private CSSMeasureFunc _measureFunc;
|
|
||||||
private CSSPrintFunc _printFunc;
|
|
||||||
private object _data;
|
private object _data;
|
||||||
|
|
||||||
public CSSNode()
|
public CSSNode()
|
||||||
{
|
{
|
||||||
_measureFunc = MeasureInternal;
|
|
||||||
_printFunc = PrintInternal;
|
|
||||||
Reinitialize();
|
Reinitialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,7 +112,7 @@ namespace Facebook.CSSLayout
|
|||||||
CSSAssert.Initialize();
|
CSSAssert.Initialize();
|
||||||
_cssNode = Native.CSSNodeNew();
|
_cssNode = Native.CSSNodeNew();
|
||||||
_children = new List<CSSNode>(4);
|
_children = new List<CSSNode>(4);
|
||||||
Native.CSSNodeSetPrintFunc(_cssNode, _printFunc);
|
Native.CSSNodeSetPrintFunc(_cssNode, PrintInternal);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Free()
|
public void Free()
|
||||||
@@ -681,7 +677,7 @@ namespace Facebook.CSSLayout
|
|||||||
{
|
{
|
||||||
AssertNativeInstance();
|
AssertNativeInstance();
|
||||||
_measureFunction = measureFunction;
|
_measureFunction = measureFunction;
|
||||||
Native.CSSNodeSetMeasureFunc(_cssNode, measureFunction != null ? _measureFunc : null);
|
Native.CSSNodeSetMeasureFunc(_cssNode, measureFunction != null ? MeasureInternal : (CSSMeasureFunc)null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CalculateLayout()
|
public void CalculateLayout()
|
||||||
@@ -690,25 +686,6 @@ namespace Facebook.CSSLayout
|
|||||||
Native.CSSNodeCalculateLayout(_cssNode, CSSConstants.Undefined, CSSConstants.Undefined, Native.CSSNodeStyleGetDirection(_cssNode));
|
Native.CSSNodeCalculateLayout(_cssNode, CSSConstants.Undefined, CSSConstants.Undefined, Native.CSSNodeStyleGetDirection(_cssNode));
|
||||||
}
|
}
|
||||||
|
|
||||||
public long Measure(CSSNode node, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode)
|
|
||||||
{
|
|
||||||
if (_measureFunction == null)
|
|
||||||
{
|
|
||||||
throw new InvalidOperationException(@"Measure function is not defined.");
|
|
||||||
}
|
|
||||||
|
|
||||||
var output = new MeasureOutput();
|
|
||||||
|
|
||||||
_measureFunction(this,
|
|
||||||
width,
|
|
||||||
widthMode,
|
|
||||||
height,
|
|
||||||
heightMode,
|
|
||||||
output);
|
|
||||||
|
|
||||||
return ((long)output.Width) << 32 | ((long)output.Height);
|
|
||||||
}
|
|
||||||
|
|
||||||
private CSSSize MeasureInternal(
|
private CSSSize MeasureInternal(
|
||||||
IntPtr context,
|
IntPtr context,
|
||||||
float width,
|
float width,
|
||||||
@@ -716,11 +693,15 @@ namespace Facebook.CSSLayout
|
|||||||
float height,
|
float height,
|
||||||
CSSMeasureMode heightMode)
|
CSSMeasureMode heightMode)
|
||||||
{
|
{
|
||||||
var measureResult = Measure(this, width, widthMode, height, heightMode);
|
if (_measureFunction == null)
|
||||||
var measuredWidth = 0xFFFFFFFF & (measureResult >> 32);
|
{
|
||||||
var measuredHeight = 0xFFFFFFFF & measureResult;
|
throw new InvalidOperationException("Measure function is not defined.");
|
||||||
|
}
|
||||||
|
|
||||||
return new CSSSize { width = measuredWidth, height = measuredHeight };
|
var measureResult = new MeasureOutput();
|
||||||
|
_measureFunction(this, width, widthMode, height, heightMode, measureResult);
|
||||||
|
|
||||||
|
return new CSSSize { width = measureResult.Width, height = measureResult.Height };
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PrintInternal(IntPtr context)
|
private void PrintInternal(IntPtr context)
|
||||||
|
@@ -241,6 +241,19 @@ namespace Facebook.CSSLayout
|
|||||||
Assert.AreEqual(0, parent.IndexOf(child1));
|
Assert.AreEqual(0, parent.IndexOf(child1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void TestMeasureFunc()
|
||||||
|
{
|
||||||
|
CSSNode node = new CSSNode();
|
||||||
|
node.SetMeasureFunction((_, width, widthMode, height, heightMode, measureResult) => {
|
||||||
|
measureResult.Width = 100;
|
||||||
|
measureResult.Height = 150;
|
||||||
|
});
|
||||||
|
node.CalculateLayout();
|
||||||
|
Assert.AreEqual(100, (int)node.LayoutWidth);
|
||||||
|
Assert.AreEqual(150, (int)node.LayoutHeight);
|
||||||
|
}
|
||||||
|
|
||||||
private void ForceGC()
|
private void ForceGC()
|
||||||
{
|
{
|
||||||
GC.Collect(GC.MaxGeneration);
|
GC.Collect(GC.MaxGeneration);
|
||||||
|
Reference in New Issue
Block a user