Skip to content

Commit

Permalink
fix: Adjust UWP sample head
Browse files Browse the repository at this point in the history
In UWP (non-WinUI) GridLengthHelper.Auto fails with MethodAccessException, this is a UWP-only workaround.
  • Loading branch information
jeromelaban committed Jun 1, 2020
1 parent 72c6603 commit d330404
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 14 deletions.
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using Uno.UI.Samples.Helper;

#if !NETFX_CORE && !__ANDROID__ && !__IOS__ && !__WASM__ && !__MACOS__
using System.Windows;
Expand Down Expand Up @@ -106,15 +107,15 @@ private void MeasureChild(UIElement child, Size availableSize, Orientation orien
{
var sizeHint = GetChildSizeHint(child);

if (GridLengthHelper.GetIsStar(sizeHint))
if (GridLengthHelper2.GetIsStar(sizeHint))
{
starTotal += sizeHint.Value;
}
else if (GridLengthHelper.GetIsAuto(sizeHint))
else if (GridLengthHelper2.GetIsAuto(sizeHint))
{
MesureChildAuto(child, orientation, availableSize, ref totalSize);
}
else if (GridLengthHelper.GetIsAbsolute(sizeHint))
else if (GridLengthHelper2.GetIsAbsolute(sizeHint))
{
MesureChildAbsolute(child, orientation, availableSize, sizeHint, ref totalSize);
}
Expand Down Expand Up @@ -178,7 +179,7 @@ private void MeasureStarChildren(Size availableSize, Orientation orientation, re
foreach (UIElement child in children)
{
var sizeHint = GetChildSizeHint(child);
if (GridLengthHelper.GetIsStar(sizeHint))
if (GridLengthHelper2.GetIsStar(sizeHint))
{
MesureChildStar(child, orientation, availableSize, sizeHint, starTotal, ref totalSize);
}
Expand Down Expand Up @@ -298,15 +299,15 @@ private Record ComputeChildSize(UIElement child, Orientation orientation, ref Si
var sizeHint = GetChildSizeHint(child);
var size = new Size();

if (GridLengthHelper.GetIsStar(sizeHint))
if (GridLengthHelper2.GetIsStar(sizeHint))
{
starTotal += sizeHint.Value;
}
else if (GridLengthHelper.GetIsAuto(sizeHint))
else if (GridLengthHelper2.GetIsAuto(sizeHint))
{
ComputeChildAutoSize(child, orientation, ref finalSize, ref totalLength, ref size);
}
else if (GridLengthHelper.GetIsAbsolute(sizeHint))
else if (GridLengthHelper2.GetIsAbsolute(sizeHint))
{
ComputeChildAbsoluteSize(orientation, ref finalSize, ref totalLength, ref sizeHint, ref size);
}
Expand Down Expand Up @@ -409,7 +410,7 @@ private static Size ComputeFinalChildSize(Record record, Orientation orientation
{
var size = record.Size;

if (GridLengthHelper.GetIsStar(record.SizeHint))
if (GridLengthHelper2.GetIsStar(record.SizeHint))
{
var portion = record.SizeHint.Value * starRatio;

Expand Down Expand Up @@ -550,7 +551,7 @@ private static GridLength[] ParseGridLength(string s)
{
if (string.IsNullOrEmpty(part))
{
result.Add(GridLengthHelper.FromValueAndType(0, GridUnitType.Auto));
result.Add(GridLengthHelper2.FromValueAndType(0, GridUnitType.Auto));
continue;
}

Expand All @@ -563,7 +564,7 @@ private static GridLength[] ParseGridLength(string s)
var autoGroup = match.Groups["auto"];
if (autoGroup.Success)
{
result.Add(GridLengthHelper.FromValueAndType(0, GridUnitType.Auto));
result.Add(GridLengthHelper2.FromValueAndType(0, GridUnitType.Auto));
continue;
}

Expand All @@ -574,22 +575,22 @@ private static GridLength[] ParseGridLength(string s)
!string.IsNullOrWhiteSpace(starsGroup.Value)
? double.Parse(starsGroup.Value, CultureInfo.InvariantCulture)
: 1;
result.Add(GridLengthHelper.FromValueAndType(value, GridUnitType.Star));
result.Add(GridLengthHelper2.FromValueAndType(value, GridUnitType.Star));
continue;
}

var starGroup = match.Groups["star"];
if (starGroup.Success)
{
result.Add(GridLengthHelper.FromValueAndType(1, GridUnitType.Star));
result.Add(GridLengthHelper2.FromValueAndType(1, GridUnitType.Star));
continue;
}

var absGroup = match.Groups["abs"];
if (absGroup.Success)
{
var value = double.Parse(absGroup.Value, CultureInfo.InvariantCulture);
result.Add(GridLengthHelper.FromValueAndType(value, GridUnitType.Pixel));
result.Add(GridLengthHelper2.FromValueAndType(value, GridUnitType.Pixel));
continue;
}

Expand Down Expand Up @@ -631,7 +632,7 @@ public static void SetSize(DependencyObject obj, GridLength value)
"Size",
typeof(GridLength),
typeof(StarStackPanel),
new PropertyMetadata(GridLengthHelper.Auto, HandleSizePropertyChanged)
new PropertyMetadata(GridLengthHelper2.Auto, HandleSizePropertyChanged)
);

private static void HandleSizePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand Down
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Text;
using Windows.UI.Xaml;

namespace Uno.UI.Samples.Helper
{
class GridLengthHelper2
{
public static GridLength Auto =>
#if NETFX_CORE && !HAS_UNO_WINUI
GridLength.Auto;
#else
GridLengthHelper.Auto;
#endif

internal static GridLength FromValueAndType(double value, GridUnitType pixel) =>
#if NETFX_CORE && !HAS_UNO_WINUI
new GridLength(value, pixel);
#else
GridLengthHelper.FromValueAndType(value, pixel);
#endif

internal static bool GetIsStar(GridLength sizeHint) =>
#if NETFX_CORE && !HAS_UNO_WINUI
sizeHint.IsStar;
#else
GridLengthHelper.GetIsStar(sizeHint);
#endif

internal static bool GetIsAbsolute(GridLength sizeHint) =>
#if NETFX_CORE && !HAS_UNO_WINUI
sizeHint.IsAbsolute;
#else
GridLengthHelper.GetIsAbsolute(sizeHint);
#endif

internal static bool GetIsAuto(GridLength sizeHint) =>
#if NETFX_CORE && !HAS_UNO_WINUI
sizeHint.IsAuto;
#else
GridLengthHelper.GetIsAuto(sizeHint);
#endif
}
}
Expand Up @@ -42,6 +42,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Views\Helper\ContentNavigationBehavior.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Views\Helper\DelegateCommand.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Views\Helper\DelegateCommand.T.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Views\Helper\GridLengthHelper2.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Views\Helper\PropertyMetadataHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Views\Helper\SampleHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UnitTest\UnitTestsControl.cs" />
Expand Down

0 comments on commit d330404

Please sign in to comment.