diff --git a/csharp/Facebook.CSSLayout/CSSNode.Create.cs b/csharp/Facebook.CSSLayout/CSSNode.Create.cs new file mode 100644 index 00000000..1b5bb207 --- /dev/null +++ b/csharp/Facebook.CSSLayout/CSSNode.Create.cs @@ -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; + } + } +} + diff --git a/csharp/Facebook.CSSLayout/CSSNode.cs b/csharp/Facebook.CSSLayout/CSSNode.cs index 985aa650..f2ba1810 100644 --- a/csharp/Facebook.CSSLayout/CSSNode.cs +++ b/csharp/Facebook.CSSLayout/CSSNode.cs @@ -15,7 +15,7 @@ using System.Text; namespace Facebook.CSSLayout { - public class CSSNode : IEnumerable + public partial class CSSNode : IEnumerable { private IntPtr _cssNode; private WeakReference _parent; diff --git a/csharp/Facebook.CSSLayout/Spacing.cs b/csharp/Facebook.CSSLayout/Spacing.cs new file mode 100644 index 00000000..b3cee58c --- /dev/null +++ b/csharp/Facebook.CSSLayout/Spacing.cs @@ -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; + } + } +} diff --git a/csharp/tests/Facebook.CSSLayout/CSSNodeCreateTest.cs b/csharp/tests/Facebook.CSSLayout/CSSNodeCreateTest.cs new file mode 100644 index 00000000..84f064f0 --- /dev/null +++ b/csharp/tests/Facebook.CSSLayout/CSSNodeCreateTest.cs @@ -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); + } + } +}