Add feature to use percentage as value unit

Summary:
Adds the feature to use percentage as a value unit.

You can use the function ```YGPx(float)``` and ```YGPercent(float)``` for convenience.

I did some benchmarks:

```
Without Percentage Feature - Release x86:

Stack with flex: median: 0.000000 ms, stddev: 0.146683 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.136525 ms
Nested flex: median: 0.000000 ms, stddev: 0.490101 ms
Huge nested layout: median: 23.000000 ms, stddev: 0.928291 ms

Stack with flex: median: 0.000000 ms, stddev: 0.170587 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.143384 ms
Nested flex: median: 0.000000 ms, stddev: 0.477791 ms
Huge nested layout: median: 22.000000 ms, stddev: 2.129779 ms

With Percentage Feature - Release x86:

Stack with flex: median: 0.000000 ms, stddev: 0.132951 ms
Align stretch in undefined axis: median: 0.000000 ms, stddev: 0.136525 ms
Nested flex: median: 0.000000 ms, stddev: 0.489570 ms
Huge nested layout: median: 21.000000 ms, stddev: 1.390476 ms
Closes https://github.com/facebook/yoga/pull/258

Reviewed By: dshahidehpour

Differential Revision: D4361945

Pulled By: emilsjolander

fbshipit-source-id: a8f5bc63ad352eb9410d792729e56664468cd76a
This commit is contained in:
Lukas Woehrl
2017-01-02 05:20:37 -08:00
committed by Facebook Github Bot
parent 6f462a72bf
commit a85bd4ad2a
48 changed files with 4948 additions and 1209 deletions

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.Yoga
{
public class Border
{
public float? Top;
public float? Bottom;
public float? Left;
public float? Right;
public Border(
float? top = null,
float? bottom = null,
float? left = null,
float? right = null)
{
Top = top;
Bottom = bottom;
Left = left;
Right = right;
}
}
}

View File

@@ -191,43 +191,64 @@ namespace Facebook.Yoga
public static extern void YGNodeStyleSetFlexBasis(YGNodeHandle node, float flexBasis);
[DllImport(DllName)]
public static extern float YGNodeStyleGetFlexBasis(YGNodeHandle node);
public static extern void YGNodeStyleSetFlexBasisPercent(YGNodeHandle node, float flexBasis);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetFlexBasis(YGNodeHandle node);
[DllImport(DllName)]
public static extern void YGNodeStyleSetWidth(YGNodeHandle node, float width);
[DllImport(DllName)]
public static extern float YGNodeStyleGetWidth(YGNodeHandle node);
public static extern void YGNodeStyleSetWidthPercent(YGNodeHandle node, float width);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetWidth(YGNodeHandle node);
[DllImport(DllName)]
public static extern void YGNodeStyleSetHeight(YGNodeHandle node, float height);
[DllImport(DllName)]
public static extern float YGNodeStyleGetHeight(YGNodeHandle node);
public static extern void YGNodeStyleSetHeightPercent(YGNodeHandle node, float height);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetHeight(YGNodeHandle node);
[DllImport(DllName)]
public static extern void YGNodeStyleSetMinWidth(YGNodeHandle node, float minWidth);
[DllImport(DllName)]
public static extern float YGNodeStyleGetMinWidth(YGNodeHandle node);
public static extern void YGNodeStyleSetMinWidthPercent(YGNodeHandle node, float minWidth);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetMinWidth(YGNodeHandle node);
[DllImport(DllName)]
public static extern void YGNodeStyleSetMinHeight(YGNodeHandle node, float minHeight);
[DllImport(DllName)]
public static extern float YGNodeStyleGetMinHeight(YGNodeHandle node);
public static extern void YGNodeStyleSetMinHeightPercent(YGNodeHandle node, float minHeight);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetMinHeight(YGNodeHandle node);
[DllImport(DllName)]
public static extern void YGNodeStyleSetMaxWidth(YGNodeHandle node, float maxWidth);
[DllImport(DllName)]
public static extern float YGNodeStyleGetMaxWidth(YGNodeHandle node);
public static extern void YGNodeStyleSetMaxWidthPercent(YGNodeHandle node, float maxWidth);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetMaxWidth(YGNodeHandle node);
[DllImport(DllName)]
public static extern void YGNodeStyleSetMaxHeight(YGNodeHandle node, float maxHeight);
[DllImport(DllName)]
public static extern float YGNodeStyleGetMaxHeight(YGNodeHandle node);
public static extern void YGNodeStyleSetMaxHeightPercent(YGNodeHandle node, float maxHeight);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetMaxHeight(YGNodeHandle node);
[DllImport(DllName)]
public static extern void YGNodeStyleSetAspectRatio(YGNodeHandle node, float aspectRatio);
@@ -243,19 +264,28 @@ namespace Facebook.Yoga
public static extern void YGNodeStyleSetPosition(YGNodeHandle node, YogaEdge edge, float position);
[DllImport(DllName)]
public static extern float YGNodeStyleGetPosition(YGNodeHandle node, YogaEdge edge);
public static extern void YGNodeStyleSetPositionPercent(YGNodeHandle node, YogaEdge edge, float position);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetPosition(YGNodeHandle node, YogaEdge edge);
[DllImport(DllName)]
public static extern void YGNodeStyleSetMargin(YGNodeHandle node, YogaEdge edge, float margin);
[DllImport(DllName)]
public static extern float YGNodeStyleGetMargin(YGNodeHandle node, YogaEdge edge);
public static extern void YGNodeStyleSetMarginPercent(YGNodeHandle node, YogaEdge edge, float margin);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetMargin(YGNodeHandle node, YogaEdge edge);
[DllImport(DllName)]
public static extern void YGNodeStyleSetPadding(YGNodeHandle node, YogaEdge edge, float padding);
[DllImport(DllName)]
public static extern float YGNodeStyleGetPadding(YGNodeHandle node, YogaEdge edge);
public static extern void YGNodeStyleSetPaddingPercent(YGNodeHandle node, YogaEdge edge, float padding);
[DllImport(DllName)]
public static extern YogaValue YGNodeStyleGetPadding(YGNodeHandle node, YogaEdge edge);
[DllImport(DllName)]
public static extern void YGNodeStyleSetBorder(YGNodeHandle node, YogaEdge edge, float border);

View File

@@ -11,16 +11,16 @@ namespace Facebook.Yoga
{
public class Spacing
{
public float? Top;
public float? Bottom;
public float? Left;
public float? Right;
public YogaValue? Top;
public YogaValue? Bottom;
public YogaValue? Left;
public YogaValue? Right;
public Spacing(
float? top = null,
float? bottom = null,
float? left = null,
float? right = null)
YogaValue? top = null,
YogaValue? bottom = null,
YogaValue? left = null,
YogaValue? right = null)
{
Top = top;
Bottom = bottom;

View File

@@ -17,5 +17,10 @@ namespace Facebook.Yoga
{
return float.IsNaN(value);
}
public static bool IsUndefined(YogaValue value)
{
return value.Unit == YogaUnit.Undefined;
}
}
}

View File

@@ -26,17 +26,17 @@ namespace Facebook.Yoga
float? flex = null,
float? flexGrow = null,
float? flexShrink = null,
float? flexBasis = null,
YogaValue? flexBasis = null,
Spacing position = null,
Spacing margin = null,
Spacing padding = null,
Spacing border = null,
float? width = null,
float? height = null,
float? maxWidth = null,
float? maxHeight = null,
float? minWidth = null,
float? minHeight = null)
Border border = null,
YogaValue? width = null,
YogaValue? height = null,
YogaValue? maxWidth = null,
YogaValue? maxHeight = null,
YogaValue? minWidth = null,
YogaValue? minHeight = null)
{
YogaNode node = new YogaNode();

View File

@@ -10,7 +10,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace Facebook.Yoga
@@ -228,7 +227,7 @@ namespace Facebook.Yoga
}
}
public float FlexBasis
public YogaValue FlexBasis
{
get
{
@@ -237,28 +236,49 @@ namespace Facebook.Yoga
set
{
Native.YGNodeStyleSetFlexBasis(_ygNode, value);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetFlexBasisPercent(_ygNode, value.Value);
}
else
{
Native.YGNodeStyleSetFlexBasis(_ygNode, value.Value);
}
}
}
public float GetMargin(YogaEdge edge)
public YogaValue GetMargin(YogaEdge edge)
{
return Native.YGNodeStyleGetMargin(_ygNode, edge);
}
public void SetMargin(YogaEdge edge, float value)
public void SetMargin(YogaEdge edge, YogaValue value)
{
Native.YGNodeStyleSetMargin(_ygNode, edge, value);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetMarginPercent(_ygNode, edge, value.Value);
}
else
{
Native.YGNodeStyleSetMargin(_ygNode, edge, value.Value);
}
}
public float GetPadding(YogaEdge edge)
public YogaValue GetPadding(YogaEdge edge)
{
return Native.YGNodeStyleGetPadding(_ygNode, edge);
}
public void SetPadding(YogaEdge edge, float padding)
public void SetPadding(YogaEdge edge, YogaValue value)
{
Native.YGNodeStyleSetPadding(_ygNode, edge, padding);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetPaddingPercent(_ygNode, edge, value.Value);
}
else
{
Native.YGNodeStyleSetPadding(_ygNode, edge, value.Value);
}
}
public float GetBorder(YogaEdge edge)
@@ -271,17 +291,24 @@ namespace Facebook.Yoga
Native.YGNodeStyleSetBorder(_ygNode, edge, border);
}
public float GetPosition(YogaEdge edge)
public YogaValue GetPosition(YogaEdge edge)
{
return Native.YGNodeStyleGetPosition(_ygNode, edge);
}
public void SetPosition(YogaEdge edge, float position)
public void SetPosition(YogaEdge edge, YogaValue value)
{
Native.YGNodeStyleSetPosition(_ygNode, edge, position);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetPositionPercent(_ygNode, edge, value.Value);
}
else
{
Native.YGNodeStyleSetPosition(_ygNode, edge, value.Value);
}
}
public float Width
public YogaValue Width
{
get
{
@@ -290,11 +317,18 @@ namespace Facebook.Yoga
set
{
Native.YGNodeStyleSetWidth(_ygNode, value);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetWidthPercent(_ygNode, value.Value);
}
else
{
Native.YGNodeStyleSetWidth(_ygNode, value.Value);
}
}
}
public float Height
public YogaValue Height
{
get
{
@@ -303,11 +337,18 @@ namespace Facebook.Yoga
set
{
Native.YGNodeStyleSetHeight(_ygNode, value);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetHeightPercent(_ygNode, value.Value);
}
else
{
Native.YGNodeStyleSetHeight(_ygNode, value.Value);
}
}
}
public float MaxWidth
public YogaValue MaxWidth
{
get
{
@@ -316,11 +357,18 @@ namespace Facebook.Yoga
set
{
Native.YGNodeStyleSetMaxWidth(_ygNode, value);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetMaxWidthPercent(_ygNode, value.Value);
}
else
{
Native.YGNodeStyleSetMaxWidth(_ygNode, value.Value);
}
}
}
public float MaxHeight
public YogaValue MaxHeight
{
get
{
@@ -329,11 +377,18 @@ namespace Facebook.Yoga
set
{
Native.YGNodeStyleSetMaxHeight(_ygNode, value);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetMaxHeightPercent(_ygNode, value.Value);
}
else
{
Native.YGNodeStyleSetMaxHeight(_ygNode, value.Value);
}
}
}
public float MinWidth
public YogaValue MinWidth
{
get
{
@@ -342,11 +397,18 @@ namespace Facebook.Yoga
set
{
Native.YGNodeStyleSetMinWidth(_ygNode, value);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetMinWidthPercent(_ygNode, value.Value);
}
else
{
Native.YGNodeStyleSetMinWidth(_ygNode, value.Value);
}
}
}
public float MinHeight
public YogaValue MinHeight
{
get
{
@@ -355,7 +417,14 @@ namespace Facebook.Yoga
set
{
Native.YGNodeStyleSetMinHeight(_ygNode, value);
if (value.Unit == YogaUnit.Percent)
{
Native.YGNodeStyleSetMinHeightPercent(_ygNode, value.Value);
}
else
{
Native.YGNodeStyleSetMinHeight(_ygNode, value.Value);
}
}
}

View File

@@ -0,0 +1,18 @@
/**
* 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 enum YogaUnit
{
Undefined,
Pixel,
Percent,
}
}

View File

@@ -0,0 +1,74 @@
/**
* 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.Runtime.InteropServices;
namespace Facebook.Yoga
{
[StructLayout(LayoutKind.Sequential)]
public struct YogaValue
{
private float value;
private YogaUnit unit;
public YogaUnit Unit => unit;
public float Value => value;
public static YogaValue Pixel(float value)
{
return new YogaValue
{
value = value,
unit = YogaConstants.IsUndefined(value) ? YogaUnit.Undefined : YogaUnit.Pixel
};
}
public bool Equals(YogaValue other)
{
return Unit == other.Unit && (Value.Equals(other.Value) || Unit == YogaUnit.Undefined);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
return obj is YogaValue && Equals((YogaValue) obj);
}
public override int GetHashCode()
{
unchecked
{
return (Value.GetHashCode() * 397) ^ (int) Unit;
}
}
public static YogaValue Undefined()
{
return new YogaValue
{
value = YogaConstants.Undefined,
unit = YogaUnit.Undefined
};
}
public static YogaValue Percent(float value)
{
return new YogaValue
{
value = value,
unit = YogaConstants.IsUndefined(value) ? YogaUnit.Undefined : YogaUnit.Percent
};
}
public static implicit operator YogaValue(float pixelValue)
{
return Pixel(pixelValue);
}
}
}

View File

@@ -0,0 +1,34 @@
/**
* 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 static class YogaValueExtensions
{
public static YogaValue Percent(this float value)
{
return YogaValue.Percent(value);
}
public static YogaValue Px(this float value)
{
return YogaValue.Pixel(value);
}
public static YogaValue Percent(this int value)
{
return YogaValue.Percent(value);
}
public static YogaValue Px(this int value)
{
return YogaValue.Pixel(value);
}
}
}