Skip to content

Commit

Permalink
feat(Style): Adds percent style support
Browse files Browse the repository at this point in the history
  • Loading branch information
rozele committed Mar 6, 2017
1 parent cf7a301 commit a46686b
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ private void UpdateTextBlockCore(TextBlock textBlock, bool measureOnly)
if (!measureOnly)
{
textBlock.Padding = new Thickness(
this.GetPaddingSpace(EdgeSpacing.Left),
this.GetPaddingSpace(EdgeSpacing.Top),
GetPadding(YogaEdge.Left),
GetPadding(YogaEdge.Top),
0,
0);
}
Expand Down
40 changes: 20 additions & 20 deletions ReactWindows/ReactNative.Shared/UIManager/EdgeSpacing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,24 @@ class EdgeSpacing
256, /*All*/
};

private readonly float[] _spacing = NewFullSpacingArray();
private readonly float _defaultValue;
private readonly YogaValue[] _spacing = NewFullSpacingArray();
private readonly YogaValue _defaultValue;
private int _valueFlags = 0;
private bool _hasAliasesSet;

/// <summary>
/// Instantiates a <see cref="EdgeSpacing"/>
/// </summary>
public EdgeSpacing()
: this(0)
: this(YogaValue.Point(0))
{
}

/// <summary>
/// Instantiates a <see cref="EdgeSpacing"/>.
/// </summary>
/// <param name="defaultValue">The default spacing value.</param>
public EdgeSpacing(float defaultValue)
public EdgeSpacing(YogaValue defaultValue)
{
_defaultValue = defaultValue;
}
Expand All @@ -99,7 +99,7 @@ public EdgeSpacing(float defaultValue)
/// <code>true</code> if the spacing has changed, or <code>false</code>
/// if the same value was already set.
/// </returns>
public bool Set(int spacingType, float value)
public bool Set(int spacingType, YogaValue value)
{
if (!EpsilonEqualityComparer.Instance.Equals(_spacing[spacingType], value))
{
Expand Down Expand Up @@ -132,7 +132,7 @@ public bool Set(int spacingType, float value)
/// One of <see cref="Left"/>, <see cref="Top" />, <see cref="Right" />, <see cref="Bottom" />.
/// </param>
/// <returns>The spacing value.</returns>
public float Get(int spacingType)
public YogaValue Get(int spacingType)
{
var defaultValue = (spacingType == Start || spacingType == End
? YogaConstants.Undefined
Expand Down Expand Up @@ -174,7 +174,7 @@ public float Get(int spacingType)
/// <see cref="All" />.
/// </param>
/// <returns>The raw spacing value.</returns>
public float GetRaw(int spacingType)
public YogaValue GetRaw(int spacingType)
{
return _spacing[spacingType];
}
Expand Down Expand Up @@ -202,28 +202,28 @@ public void Reset()
/// <param name="spacingType">The preferred spacing type.</param>
/// <param name="fallbackType">The fallback spacing type.</param>
/// <returns>The spacing value.</returns>
internal float GetWithFallback(int spacingType, int fallbackType)
internal YogaValue GetWithFallback(int spacingType, int fallbackType)
{
return
(_valueFlags & s_flagsMap[spacingType]) != 0
? _spacing[spacingType]
: Get(fallbackType);
}

private static float[] NewFullSpacingArray()
private static YogaValue[] NewFullSpacingArray()
{
return new float[] {
YogaConstants.Undefined,
YogaConstants.Undefined,
YogaConstants.Undefined,
YogaConstants.Undefined,
YogaConstants.Undefined,
YogaConstants.Undefined,
YogaConstants.Undefined,
YogaConstants.Undefined,
YogaConstants.Undefined,
var undefined = YogaValue.Undefined();
return new[] {
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
};
}

}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
using System;
using Facebook.Yoga;
using System;
using System.Collections.Generic;

namespace ReactNative.UIManager
{
class EpsilonEqualityComparer : IEqualityComparer<float>
class EpsilonEqualityComparer : IEqualityComparer<YogaValue>
{
private const float Epsilon = .00001f;
private const int Decimals = 4;

public static EpsilonEqualityComparer Instance { get; } = new EpsilonEqualityComparer();

public bool Equals(float x, float y)
public bool Equals(YogaValue x, YogaValue y)
{
if (float.IsNaN(x) || float.IsNaN(y))
if (x.Unit != y.Unit)
{
return float.IsNaN(x) && float.IsNaN(y);
return false;
}

return Math.Abs(x - y) < Epsilon;
if (float.IsNaN(x.Value) || float.IsNaN(y.Value))
{
return float.IsNaN(x.Value) && float.IsNaN(y.Value);
}

return Math.Abs(x.Value - y.Value) < Epsilon;
}

public int GetHashCode(float obj)
public int GetHashCode(YogaValue obj)
{
return Math.Round(obj, Decimals).GetHashCode();
return (Math.Round(obj.Value, Decimals).GetHashCode()*397) ^ (int) obj.Unit;
}
}
}
67 changes: 49 additions & 18 deletions ReactWindows/ReactNative.Shared/UIManager/LayoutShadowNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Facebook.Yoga;
using Newtonsoft.Json.Linq;
using ReactNative.Reflection;
using ReactNative.UIManager.Annotations;
using System;
using static System.FormattableString;

namespace ReactNative.UIManager
{
Expand Down Expand Up @@ -33,89 +36,89 @@ public LayoutShadowNode(bool isVirtual)
/// </summary>
/// <param name="width">The width.</param>
[ReactProp(ViewProps.Width, DefaultSingle = YogaConstants.Undefined)]
public void SetWidth(float width)
public void SetWidth(JValue width)
{
if (IsVirtual)
{
return;
}

StyleWidth = width;
StyleWidth = ToYogaValue(width);
}

/// <summary>
/// Sets the minimum width of the shadow node.
/// </summary>
/// <param name="minWidth">The minimum width.</param>
[ReactProp(ViewProps.MinWidth, DefaultSingle = YogaConstants.Undefined)]
public void SetMinWidth(float minWidth)
public void SetMinWidth(JValue minWidth)
{
if (IsVirtual)
{
return;
}

StyleMinWidth = minWidth;
StyleMinWidth = ToYogaValue(minWidth);
}

/// <summary>
/// Sets the maximum width of the shadow node.
/// </summary>
/// <param name="maxWidth">The maximum width.</param>
[ReactProp(ViewProps.MaxWidth, DefaultSingle = YogaConstants.Undefined)]
public void SetMaxWidth(float maxWidth)
public void SetMaxWidth(JValue maxWidth)
{
if (IsVirtual)
{
return;
}

StyleMaxWidth = maxWidth;
StyleMaxWidth = ToYogaValue(maxWidth);
}

/// <summary>
/// Set the heigth of the shadow node.
/// </summary>
/// <param name="height">The height.</param>
[ReactProp(ViewProps.Height, DefaultSingle = YogaConstants.Undefined)]
public void SetHeight(float height)
public void SetHeight(JValue height)
{
if (IsVirtual)
{
return;
}

StyleHeight = height;
StyleHeight = ToYogaValue(height);
}

/// <summary>
/// Sets the minimum height of the shadow node.
/// </summary>
/// <param name="minHeight">The minimum height.</param>
[ReactProp(ViewProps.MinHeight, DefaultSingle = YogaConstants.Undefined)]
public void SetMinHeight(float minHeight)
public void SetMinHeight(JValue minHeight)
{
if (IsVirtual)
{
return;
}

StyleMinHeight = minHeight;
StyleMinHeight = ToYogaValue(minHeight);
}

/// <summary>
/// Sets the maximum height of the shadow node.
/// </summary>
/// <param name="maxHeight">The maximum height.</param>
[ReactProp(ViewProps.MaxHeight, DefaultSingle = YogaConstants.Undefined)]
public void SetMaxHeight(float maxHeight)
public void SetMaxHeight(JValue maxHeight)
{
if (IsVirtual)
{
return;
}

StyleMaxHeight = maxHeight;
StyleMaxHeight = ToYogaValue(maxHeight);
}

/// <summary>
Expand Down Expand Up @@ -287,14 +290,14 @@ public void SetOverflow(string overflow)
ViewProps.MarginTop,
ViewProps.MarginBottom,
DefaultSingle = YogaConstants.Undefined)]
public void SetMargins(int index, float margin)
public void SetMargins(int index, JValue margin)
{
if (IsVirtual)
{
return;
}

SetMargin(ViewProps.PaddingMarginSpacingTypes[index], margin);
SetMargin(ViewProps.PaddingMarginSpacingTypes[index], ToYogaValue(margin));
}

/// <summary>
Expand All @@ -311,9 +314,9 @@ public void SetMargins(int index, float margin)
ViewProps.PaddingTop,
ViewProps.PaddingBottom,
DefaultSingle = YogaConstants.Undefined)]
public virtual void SetPaddings(int index, float padding)
public virtual void SetPaddings(int index, JValue padding)
{
SetPadding(ViewProps.PaddingMarginSpacingTypes[index], padding);
SetPadding(ViewProps.PaddingMarginSpacingTypes[index], ToYogaValue(padding));
}

/// <summary>
Expand Down Expand Up @@ -344,14 +347,14 @@ public void SetBorderWidth(int index, float borderWidth)
ViewProps.Top,
ViewProps.Bottom,
DefaultSingle = YogaConstants.Undefined)]
public void SetPositionValues(int index, float position)
public void SetPositionValues(int index, JValue position)
{
if (IsVirtual)
{
return;
}

SetPosition(ViewProps.PositionSpacingTypes[index], position);
SetPosition(ViewProps.PositionSpacingTypes[index], ToYogaValue(position));
}

/// <summary>
Expand All @@ -375,5 +378,33 @@ public void SetShouldNotifyOnLayout(bool shouldNotifyOnLayout)
{
ShouldNotifyOnLayout = shouldNotifyOnLayout;
}

private static YogaValue ToYogaValue(JValue value)
{
if (value == null || value.Type == JTokenType.Null || value.Type == JTokenType.Undefined)
{
return YogaValue.Undefined();
}

if (value.Type == JTokenType.String)
{
var s = value.Value<string>();

if (s == "auto")
{
return YogaValue.Auto();
}

if (s.EndsWith("%"))
{
return YogaValue.Percent(float.Parse(s.Substring(0, s.Length - 1)));
}

throw new InvalidOperationException(
Invariant($"Unknown value: '{s}'"));
}

return YogaValue.Point(value.Value<float>());
}
}
}
Loading

0 comments on commit a46686b

Please sign in to comment.