Browse Source

Rect parse & type converter

Eli Arbel 8 years ago
parent
commit
d9b20fada4

+ 26 - 0
src/Avalonia.Visuals/Rect.cs

@@ -481,5 +481,31 @@ namespace Avalonia
                 _width,
                 _height);
         }
+
+        /// <summary>
+        /// Parses a <see cref="Rect"/> string.
+        /// </summary>
+        /// <param name="s">The string.</param>
+        /// <param name="culture">The current culture.</param>
+        /// <returns>The parsed <see cref="Rect"/>.</returns>
+        public static Rect Parse(string s, CultureInfo culture)
+        {
+            var parts = s.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
+                .Select(x => x.Trim())
+                .ToList();
+
+            if (parts.Count == 4)
+            {
+                return new Rect(
+                    double.Parse(parts[0], culture),
+                    double.Parse(parts[1], culture),
+                    double.Parse(parts[2], culture),
+                    double.Parse(parts[3], culture));
+            }
+            else
+            {
+                throw new FormatException("Invalid Rect.");
+            }
+        }
     }
 }

+ 1 - 1
src/Avalonia.Visuals/RelativeRect.cs

@@ -203,7 +203,7 @@ namespace Avalonia
             }
             else
             {
-                throw new FormatException("Invalid Rect.");
+                throw new FormatException("Invalid RelativeRect.");
             }
         }
     }

+ 1 - 0
src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj

@@ -32,6 +32,7 @@
         <Compile Include="AvaloniaXamlLoaderPortableXaml.cs" />
         <Compile Include="AvaloniaXamlLoader.cs" />
         <Compile Include="Converters\MatrixTypeConverter.cs" />
+        <Compile Include="Converters\RectTypeConverter.cs" />
         <Compile Include="Converters\SetterValueTypeConverter.cs" />
         <Compile Include="MarkupExtensions\StyleIncludeExtension.cs" />
         <Compile Include="PortableXaml\AvaloniaXamlContext.cs" />

+ 23 - 0
src/Markup/Avalonia.Markup.Xaml/Converters/RectTypeConverter.cs

@@ -0,0 +1,23 @@
+// Copyright (c) The Avalonia Project. All rights reserved.
+// Licensed under the MIT license. See licence.md file in the project root for full license information.
+
+using System;
+using System.Globalization;
+
+namespace Avalonia.Markup.Xaml.Converters
+{
+	using System.ComponentModel;
+
+    public class RectTypeConverter : TypeConverter
+    {
+        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
+        {
+            return sourceType == typeof(string);
+        }
+
+        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
+        {
+            return Rect.Parse((string)value, culture);
+        }
+    }
+}

+ 1 - 0
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaDefaultTypeConverters.cs

@@ -39,6 +39,7 @@ namespace Avalonia.Markup.Xaml.PortableXaml
             { typeof(RelativeRect), typeof(RelativeRectTypeConverter) },
             { typeof(RowDefinitions), typeof(RowDefinitionsTypeConverter) },
             { typeof(Size), typeof(SizeTypeConverter) },
+            { typeof(Rect), typeof(RectTypeConverter) },
             { typeof(Selector), typeof(SelectorTypeConverter)},
             { typeof(SolidColorBrush), typeof(BrushTypeConverter) },
             { typeof(Thickness), typeof(ThicknessTypeConverter) },

+ 16 - 0
tests/Avalonia.Visuals.UnitTests/Media/RectTests.cs

@@ -0,0 +1,16 @@
+using System.Globalization;
+using Xunit;
+
+namespace Avalonia.Visuals.UnitTests.Media
+{
+    public class RectTests
+    {
+        [Fact]
+        public void Parse_Parses()
+        {
+            var rect = Rect.Parse("1,2 3,-4", CultureInfo.CurrentCulture);
+            var expected = new Rect(1, 2, 3, -4);
+            Assert.Equal(expected, rect);
+        }
+    }
+}