diff --git a/Perspex.Controls/ColumnDefinitions.cs b/Perspex.Controls/ColumnDefinitions.cs index f8c9928eb8..58a5663b0d 100644 --- a/Perspex.Controls/ColumnDefinitions.cs +++ b/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 { + public ColumnDefinitions() + { + } + + public ColumnDefinitions(string s) + { + this.AddRange(GridLengthsParser.Parse(s).Select(x => new ColumnDefinition(x))); + } } } diff --git a/Perspex.Controls/Parsers/GridLengthsParser.cs b/Perspex.Controls/Parsers/GridLengthsParser.cs new file mode 100644 index 0000000000..d9895019ce --- /dev/null +++ b/Perspex.Controls/Parsers/GridLengthsParser.cs @@ -0,0 +1,53 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Parsers +{ + using System; + using System.Collections.Generic; + using System.Linq; + + + /// + /// Parses a string of s for and + /// . + /// + public static class GridLengthsParser + { + /// + /// Parses a string of s. + /// + /// The string. + /// A collection of s. + public static IEnumerable 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); + } + } + } + } +} diff --git a/Perspex.Controls/Perspex.Controls.csproj b/Perspex.Controls/Perspex.Controls.csproj index f38451ed02..a6ab911d1e 100644 --- a/Perspex.Controls/Perspex.Controls.csproj +++ b/Perspex.Controls/Perspex.Controls.csproj @@ -37,6 +37,7 @@ + diff --git a/Perspex.Controls/RowDefinitions.cs b/Perspex.Controls/RowDefinitions.cs index 7affaa019f..9263859186 100644 --- a/Perspex.Controls/RowDefinitions.cs +++ b/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 { + public RowDefinitions() + { + } + + public RowDefinitions(string s) + { + this.AddRange(GridLengthsParser.Parse(s).Select(x => new RowDefinition(x))); + } } } diff --git a/Perspex.Diagnostics/DevTools.cs b/Perspex.Diagnostics/DevTools.cs index 817c94b6e9..54391d2de2 100644 --- a/Perspex.Diagnostics/DevTools.cs +++ b/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 diff --git a/Tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs b/Tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs new file mode 100644 index 0000000000..340923056b --- /dev/null +++ b/Tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs @@ -0,0 +1,54 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +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(() => GridLengthsParser.Parse(s).ToList()); + } + + [Fact] + public void Parser_Should_Throw_For_Invalid_Unit_Value() + { + var s = "*,Auto,4ab,4px"; + Assert.Throws(() => GridLengthsParser.Parse(s).ToList()); + } + + [Fact] + public void Parser_Should_Throw_For_Empty_Entry() + { + var s = "*,Auto,,4px"; + Assert.Throws(() => GridLengthsParser.Parse(s).ToList()); + } + } +} diff --git a/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj b/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj index 6170b6a7ea..2198ebc5ed 100644 --- a/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj +++ b/Tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj @@ -91,6 +91,7 @@ +