Browse Source

Allow Grid column/row definitions to be supplied as a string.

Closes #48.
pull/58/head
Steven Kirk 11 years ago
parent
commit
2e54b2be19
  1. 10
      Perspex.Controls/ColumnDefinitions.cs
  2. 53
      Perspex.Controls/Parsers/GridLengthsParser.cs
  3. 1
      Perspex.Controls/Perspex.Controls.csproj
  4. 10
      Perspex.Controls/RowDefinitions.cs
  5. 6
      Perspex.Diagnostics/DevTools.cs
  6. 54
      Tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs
  7. 1
      Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj

10
Perspex.Controls/ColumnDefinitions.cs

@ -6,9 +6,19 @@
namespace Perspex.Controls
{
using System.Linq;
using Perspex.Collections;
using Perspex.Controls.Parsers;
public class ColumnDefinitions : PerspexList<ColumnDefinition>
{
public ColumnDefinitions()
{
}
public ColumnDefinitions(string s)
{
this.AddRange(GridLengthsParser.Parse(s).Select(x => new ColumnDefinition(x)));
}
}
}

53
Perspex.Controls/Parsers/GridLengthsParser.cs

@ -0,0 +1,53 @@
// -----------------------------------------------------------------------
// <copyright file="GridLengthsParser.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls.Parsers
{
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Parses a string of <see cref="GridLength"/>s for <see cref="ColumnDefinitions"/> and
/// <see cref="RowDefinitions"/>.
/// </summary>
public static class GridLengthsParser
{
/// <summary>
/// Parses a string of <see cref="GridLength"/>s.
/// </summary>
/// <param name="s">The string.</param>
/// <returns>A collection of <see cref="GridLength"/>s.</returns>
public static IEnumerable<GridLength> Parse(string s)
{
var parts = s.Split(',').Select(x => x.ToUpperInvariant().Trim());
foreach (var part in parts)
{
if (part == "AUTO")
{
yield return GridLength.Auto;
}
else if (part.EndsWith("*"))
{
var valueString = part.Substring(0, part.Length - 1).Trim();
var value = valueString.Length > 0 ? double.Parse(valueString) : 1;
yield return new GridLength(value, GridUnitType.Star);
}
else if (part.EndsWith("PX"))
{
var value = double.Parse(part.Substring(0, part.Length - 2));
yield return new GridLength(value, GridUnitType.Pixel);
}
else
{
throw new FormatException("Invalid grid length: " + part);
}
}
}
}
}

1
Perspex.Controls/Perspex.Controls.csproj

@ -37,6 +37,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="MenuItemAccessKeyHandler.cs" />
<Compile Include="Parsers\GridLengthsParser.cs" />
<Compile Include="Primitives\AccessText.cs" />
<Compile Include="Border.cs" />
<Compile Include="GlobalSuppressions.cs" />

10
Perspex.Controls/RowDefinitions.cs

@ -6,9 +6,19 @@
namespace Perspex.Controls
{
using System.Linq;
using Perspex.Collections;
using Perspex.Controls.Parsers;
public class RowDefinitions : PerspexList<RowDefinition>
{
public RowDefinitions()
{
}
public RowDefinitions(string s)
{
this.AddRange(GridLengthsParser.Parse(s).Select(x => new RowDefinition(x)));
}
}
}

6
Perspex.Diagnostics/DevTools.cs

@ -66,11 +66,7 @@ namespace Perspex.Diagnostics
this.Content = new Grid
{
RowDefinitions = new RowDefinitions
{
new RowDefinition(new GridLength(1, GridUnitType.Star)),
new RowDefinition(GridLength.Auto),
},
RowDefinitions = new RowDefinitions("*,Auto"),
Children = new Controls
{
new TabControl

54
Tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs

@ -0,0 +1,54 @@
// -----------------------------------------------------------------------
// <copyright file="GridLengthsParserTests.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls.UnitTests.Parsers
{
using System;
using System.Linq;
using Perspex.Controls.Parsers;
using Xunit;
public class GridLengthsParserTests
{
[Fact]
public void Parser_Should_Correctly_Parse_Grid_Lengths()
{
var s = "*,Auto,2*,4px";
var result = GridLengthsParser.Parse(s);
Assert.Equal(
new[]
{
new GridLength(1, GridUnitType.Star),
GridLength.Auto,
new GridLength(2, GridUnitType.Star),
new GridLength(4, GridUnitType.Pixel),
},
result);
}
[Fact]
public void Parser_Should_Throw_For_Invalid_Star_Value()
{
var s = "*,Auto,x*,4px";
Assert.Throws<FormatException>(() => GridLengthsParser.Parse(s).ToList());
}
[Fact]
public void Parser_Should_Throw_For_Invalid_Unit_Value()
{
var s = "*,Auto,4ab,4px";
Assert.Throws<FormatException>(() => GridLengthsParser.Parse(s).ToList());
}
[Fact]
public void Parser_Should_Throw_For_Empty_Entry()
{
var s = "*,Auto,,4px";
Assert.Throws<FormatException>(() => GridLengthsParser.Parse(s).ToList());
}
}
}

1
Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj

@ -91,6 +91,7 @@
<Compile Include="ContentPresenterTests.cs" />
<Compile Include="BorderTests.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Parsers\GridLengthsParserTests.cs" />
<Compile Include="PopupTests.cs" />
<Compile Include="DropDownTests.cs" />
<Compile Include="Presenters\ItemsPresenterTests.cs" />

Loading…
Cancel
Save