Introduce CSSNode.Create API

Summary:
Add a convenience construction method for CSSNode.

    CSSNode node = CSSNode.Create(
        positionType: CSSPositionType.Absolute,
        wrap: CSSWrap.Wrap,
        position: new Spacing(top:6, right:4),
        margin: new Spacing(bottom:5, left:3));

Reviewed By: emilsjolander

Differential Revision: D4193380

fbshipit-source-id: 30b917f64e92997355a76e3b11799883b86fb9de
This commit is contained in:
Kazuki Sakamoto
2016-11-17 07:03:30 -08:00
committed by Facebook Github Bot
parent 32ba5ae647
commit 7e4bb732ff
4 changed files with 409 additions and 1 deletions

View File

@@ -0,0 +1,234 @@
/**
* 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;
namespace Facebook.CSSLayout
{
public partial class CSSNode
{
public static CSSNode Create(
CSSDirection? styleDirection = null,
CSSFlexDirection? flexDirection = null,
CSSJustify? justifyContent = null,
CSSAlign? alignContent = null,
CSSAlign? alignItems = null,
CSSAlign? alignSelf = null,
CSSPositionType? positionType = null,
CSSWrap? wrap = null,
CSSOverflow? overflow = null,
float? flex = null,
float? flexGrow = null,
float? flexShrink = null,
float? flexBasis = null,
Spacing position = null,
Spacing margin = null,
Spacing padding = null,
Spacing border = null,
float? styleWidth = null,
float? styleHeight = null,
float? styleMaxWidth = null,
float? styleMaxHeight = null,
float? styleMinWidth = null,
float? styleMinHeight = null)
{
CSSNode node = new CSSNode();
if (styleDirection.HasValue)
{
node.StyleDirection = styleDirection.Value;
}
if (flexDirection.HasValue)
{
node.FlexDirection = flexDirection.Value;
}
if (justifyContent.HasValue)
{
node.JustifyContent = justifyContent.Value;
}
if (alignContent.HasValue)
{
node.AlignContent = alignContent.Value;
}
if (alignItems.HasValue)
{
node.AlignItems = alignItems.Value;
}
if (alignSelf.HasValue)
{
node.AlignSelf = alignSelf.Value;
}
if (positionType.HasValue)
{
node.PositionType = positionType.Value;
}
if (wrap.HasValue)
{
node.Wrap = wrap.Value;
}
if (overflow.HasValue)
{
node.Overflow = overflow.Value;
}
if (flex.HasValue)
{
node.Flex = flex.Value;
}
if (flexGrow.HasValue)
{
node.FlexGrow = flexGrow.Value;
}
if (flexShrink.HasValue)
{
node.FlexShrink = flexShrink.Value;
}
if (flexBasis.HasValue)
{
node.FlexBasis = flexBasis.Value;
}
if (position != null)
{
if (position.Top.HasValue)
{
node.SetPosition(CSSEdge.Top, position.Top.Value);
}
if (position.Bottom.HasValue)
{
node.SetPosition(CSSEdge.Bottom, position.Bottom.Value);
}
if (position.Left.HasValue)
{
node.SetPosition(CSSEdge.Left, position.Left.Value);
}
if (position.Right.HasValue)
{
node.SetPosition(CSSEdge.Right, position.Right.Value);
}
}
if (margin != null)
{
if (margin.Top.HasValue)
{
node.SetMargin(CSSEdge.Top, margin.Top.Value);
}
if (margin.Bottom.HasValue)
{
node.SetMargin(CSSEdge.Bottom, margin.Bottom.Value);
}
if (margin.Left.HasValue)
{
node.SetMargin(CSSEdge.Left, margin.Left.Value);
}
if (margin.Right.HasValue)
{
node.SetMargin(CSSEdge.Right, margin.Right.Value);
}
}
if (padding != null)
{
if (padding.Top.HasValue)
{
node.SetPadding(CSSEdge.Top, padding.Top.Value);
}
if (padding.Bottom.HasValue)
{
node.SetPadding(CSSEdge.Bottom, padding.Bottom.Value);
}
if (padding.Left.HasValue)
{
node.SetPadding(CSSEdge.Left, padding.Left.Value);
}
if (padding.Right.HasValue)
{
node.SetPadding(CSSEdge.Right, padding.Right.Value);
}
}
if (border != null)
{
if (border.Top.HasValue)
{
node.SetBorder(CSSEdge.Top, border.Top.Value);
}
if (border.Bottom.HasValue)
{
node.SetBorder(CSSEdge.Bottom, border.Bottom.Value);
}
if (border.Left.HasValue)
{
node.SetBorder(CSSEdge.Left, border.Left.Value);
}
if (border.Right.HasValue)
{
node.SetBorder(CSSEdge.Right, border.Right.Value);
}
}
if (styleWidth.HasValue)
{
node.StyleWidth = styleWidth.Value;
}
if (styleHeight.HasValue)
{
node.StyleHeight = styleHeight.Value;
}
if (styleMinWidth.HasValue)
{
node.StyleMinWidth = styleMinWidth.Value;
}
if (styleMinHeight.HasValue)
{
node.StyleMinHeight = styleMinHeight.Value;
}
if (styleMaxWidth.HasValue)
{
node.StyleMaxWidth = styleMaxWidth.Value;
}
if (styleMaxHeight.HasValue)
{
node.StyleMaxHeight = styleMaxHeight.Value;
}
return node;
}
}
}

View File

@@ -15,7 +15,7 @@ using System.Text;
namespace Facebook.CSSLayout namespace Facebook.CSSLayout
{ {
public class CSSNode : IEnumerable<CSSNode> public partial class CSSNode : IEnumerable<CSSNode>
{ {
private IntPtr _cssNode; private IntPtr _cssNode;
private WeakReference _parent; private WeakReference _parent;

View File

@@ -0,0 +1,31 @@
/**
* 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.CSSLayout
{
public class Spacing
{
public float? Top;
public float? Bottom;
public float? Left;
public float? Right;
public Spacing(
float? top = null,
float? bottom = null,
float? left = null,
float? right = null)
{
Top = top;
Bottom = bottom;
Left = left;
Right = right;
}
}
}

View File

@@ -0,0 +1,143 @@
/**
* 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 NUnit.Framework;
using System;
/**
* Tests for {@link CSSNode}.
*/
namespace Facebook.CSSLayout
{
[TestFixture]
public class CSSNodeCreateTest
{
[Test]
public void TestSimple()
{
CSSNode nodeDefault = new CSSNode();
CSSNode nodeCreated = CSSNode.Create(flexDirection: CSSFlexDirection.Row);
Assert.AreEqual(CSSFlexDirection.Row, nodeCreated.FlexDirection);
Assert.IsFalse(nodeDefault.IsDirty);
nodeDefault.CopyStyle(nodeCreated);
Assert.IsTrue(nodeDefault.IsDirty);
}
[Test]
public void TestSame()
{
CSSNode nodeDefault = new CSSNode();
CSSNode nodeCreated = CSSNode.Create();
Assert.IsFalse(nodeDefault.IsDirty);
nodeDefault.CopyStyle(nodeCreated);
Assert.IsFalse(nodeDefault.IsDirty);
}
[Test]
public void TestMultiple()
{
CSSNode node = CSSNode.Create(
positionType: CSSPositionType.Absolute,
wrap: CSSWrap.Wrap,
position: new Spacing(top:6, right:4),
margin: new Spacing(bottom:5, left:3));
Assert.AreEqual(CSSFlexDirection.Column, node.FlexDirection);
Assert.AreEqual(CSSPositionType.Absolute, node.PositionType);
Assert.AreEqual(CSSWrap.Wrap, node.Wrap);
Assert.AreEqual(6, node.GetPosition(CSSEdge.Top));
Assert.IsTrue(CSSConstants.IsUndefined(node.GetPosition(CSSEdge.Bottom)));
Assert.AreEqual(4, node.GetPosition(CSSEdge.Right));
Assert.IsTrue(CSSConstants.IsUndefined(node.GetPosition(CSSEdge.Left)));
Assert.AreEqual(0, node.GetMargin(CSSEdge.Top));
Assert.AreEqual(5, node.GetMargin(CSSEdge.Bottom));
Assert.AreEqual(3, node.GetMargin(CSSEdge.Left));
Assert.AreEqual(0, node.GetMargin(CSSEdge.Right));
}
[Test]
public void TestFull()
{
CSSNode node = CSSNode.Create(
styleDirection: CSSDirection.RTL,
flexDirection: CSSFlexDirection.RowReverse,
justifyContent: CSSJustify.SpaceAround,
alignContent: CSSAlign.Center,
alignItems: CSSAlign.FlexEnd,
alignSelf: CSSAlign.Stretch,
positionType: CSSPositionType.Absolute,
wrap: CSSWrap.Wrap,
overflow: CSSOverflow.Scroll,
flex: 1,
flexGrow: 2,
flexShrink: 3,
flexBasis: 4,
position: new Spacing(top: 5, bottom: 6, left: 7, right: 8),
margin: new Spacing(top: 9, bottom: 10, left: 11, right: 12),
padding: new Spacing(top: 13, bottom: 14, left: 15, right: 16),
border: new Spacing(top: 17, bottom: 18, left: 19, right: 20),
styleWidth: 21,
styleHeight: 22,
styleMinWidth: 23,
styleMinHeight: 24,
styleMaxWidth: 25,
styleMaxHeight: 26);
Assert.AreEqual(CSSDirection.RTL, node.StyleDirection);
Assert.AreEqual(CSSFlexDirection.RowReverse, node.FlexDirection);
Assert.AreEqual(CSSJustify.SpaceAround, node.JustifyContent);
Assert.AreEqual(CSSAlign.Center, node.AlignContent);
Assert.AreEqual(CSSAlign.FlexEnd, node.AlignItems);
Assert.AreEqual(CSSAlign.Stretch, node.AlignSelf);
Assert.AreEqual(CSSPositionType.Absolute, node.PositionType);
Assert.AreEqual(CSSWrap.Wrap, node.Wrap);
Assert.AreEqual(CSSOverflow.Scroll, node.Overflow);
Assert.AreEqual(2, node.FlexGrow);
Assert.AreEqual(3, node.FlexShrink);
Assert.AreEqual(4, node.FlexBasis);
node.FlexGrow = CSSConstants.Undefined;
Assert.AreEqual(1, node.FlexGrow);
Assert.AreEqual(5, node.GetPosition(CSSEdge.Top));
Assert.AreEqual(6, node.GetPosition(CSSEdge.Bottom));
Assert.AreEqual(7, node.GetPosition(CSSEdge.Left));
Assert.AreEqual(8, node.GetPosition(CSSEdge.Right));
Assert.AreEqual(9, node.GetMargin(CSSEdge.Top));
Assert.AreEqual(10, node.GetMargin(CSSEdge.Bottom));
Assert.AreEqual(11, node.GetMargin(CSSEdge.Left));
Assert.AreEqual(12, node.GetMargin(CSSEdge.Right));
Assert.AreEqual(13, node.GetPadding(CSSEdge.Top));
Assert.AreEqual(14, node.GetPadding(CSSEdge.Bottom));
Assert.AreEqual(15, node.GetPadding(CSSEdge.Left));
Assert.AreEqual(16, node.GetPadding(CSSEdge.Right));
Assert.AreEqual(17, node.GetBorder(CSSEdge.Top));
Assert.AreEqual(18, node.GetBorder(CSSEdge.Bottom));
Assert.AreEqual(19, node.GetBorder(CSSEdge.Left));
Assert.AreEqual(20, node.GetBorder(CSSEdge.Right));
Assert.AreEqual(21, node.StyleWidth);
Assert.AreEqual(22, node.StyleHeight);
Assert.AreEqual(23, node.StyleMinWidth);
Assert.AreEqual(24, node.StyleMinHeight);
Assert.AreEqual(25, node.StyleMaxWidth);
Assert.AreEqual(26, node.StyleMaxHeight);
}
}
}