From f5e3a56ff1a8fbfa9912d3474f50fe52768ee6ad Mon Sep 17 00:00:00 2001 From: Max Katz Date: Fri, 4 Nov 2022 21:19:26 -0400 Subject: [PATCH] Add some missing TypeConverters --- src/Avalonia.Base/CornerRadius.cs | 4 +++- src/Avalonia.Base/CornerRadiusConverter.cs | 21 ++++++++++++++++++++ src/Avalonia.Base/Matrix.cs | 4 +++- src/Avalonia.Base/MatrixConverter.cs | 21 ++++++++++++++++++++ src/Avalonia.Base/Media/Color.cs | 4 +++- src/Avalonia.Base/Media/ColorConverter.cs | 21 ++++++++++++++++++++ src/Avalonia.Base/Point.cs | 2 ++ src/Avalonia.Base/PointConverter.cs | 21 ++++++++++++++++++++ src/Avalonia.Base/Rect.cs | 2 ++ src/Avalonia.Base/RectConverter.cs | 21 ++++++++++++++++++++ src/Avalonia.Base/RelativePoint.cs | 4 +++- src/Avalonia.Base/RelativePointConverter.cs | 21 ++++++++++++++++++++ src/Avalonia.Base/Size.cs | 2 ++ src/Avalonia.Base/SizeConverter.cs | 21 ++++++++++++++++++++ src/Avalonia.Base/Thickness.cs | 4 +++- src/Avalonia.Base/ThicknessConverter.cs | 21 ++++++++++++++++++++ src/Avalonia.Controls/GridLength.cs | 2 ++ src/Avalonia.Controls/GridLengthConverter.cs | 21 ++++++++++++++++++++ 18 files changed, 212 insertions(+), 5 deletions(-) create mode 100644 src/Avalonia.Base/CornerRadiusConverter.cs create mode 100644 src/Avalonia.Base/MatrixConverter.cs create mode 100644 src/Avalonia.Base/Media/ColorConverter.cs create mode 100644 src/Avalonia.Base/PointConverter.cs create mode 100644 src/Avalonia.Base/RectConverter.cs create mode 100644 src/Avalonia.Base/RelativePointConverter.cs create mode 100644 src/Avalonia.Base/SizeConverter.cs create mode 100644 src/Avalonia.Base/ThicknessConverter.cs create mode 100644 src/Avalonia.Controls/GridLengthConverter.cs diff --git a/src/Avalonia.Base/CornerRadius.cs b/src/Avalonia.Base/CornerRadius.cs index 893f7c4565..d68e8a5ccf 100644 --- a/src/Avalonia.Base/CornerRadius.cs +++ b/src/Avalonia.Base/CornerRadius.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Globalization; #if !BUILDTASK using Avalonia.Animation.Animators; @@ -11,6 +12,7 @@ namespace Avalonia /// Represents the radii of a rectangle's corners. /// #if !BUILDTASK + [TypeConverter(typeof(CornerRadiusConverter))] public #endif readonly struct CornerRadius : IEquatable @@ -79,7 +81,7 @@ namespace Avalonia { // ReSharper disable CompareOfFloatsByEqualityOperator return TopLeft == other.TopLeft && - + TopRight == other.TopRight && BottomRight == other.BottomRight && BottomLeft == other.BottomLeft; diff --git a/src/Avalonia.Base/CornerRadiusConverter.cs b/src/Avalonia.Base/CornerRadiusConverter.cs new file mode 100644 index 0000000000..cd03821fe9 --- /dev/null +++ b/src/Avalonia.Base/CornerRadiusConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Avalonia; + +/// +/// Creates a from a string representation. +/// +public class CornerRadiusConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) + { + return value is string s ? CornerRadius.Parse(s) : null; + } +} diff --git a/src/Avalonia.Base/Matrix.cs b/src/Avalonia.Base/Matrix.cs index df6439b1b3..72468d7951 100644 --- a/src/Avalonia.Base/Matrix.cs +++ b/src/Avalonia.Base/Matrix.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Globalization; using System.Linq; using System.Numerics; @@ -14,10 +15,11 @@ namespace Avalonia /// 1st row | scaleX | skewY | perspX | /// 2nd row | skewX | scaleY | perspY | /// 3rd row | transX | transY | perspZ | - /// + /// /// Note: Skia.SkMatrix uses a transposed layout (where for example skewX/skewY and persp0/transX are swapped). /// #if !BUILDTASK + [TypeConverter(typeof(MatrixConverter))] public #endif readonly struct Matrix : IEquatable diff --git a/src/Avalonia.Base/MatrixConverter.cs b/src/Avalonia.Base/MatrixConverter.cs new file mode 100644 index 0000000000..54600c23a4 --- /dev/null +++ b/src/Avalonia.Base/MatrixConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Avalonia; + +/// +/// Creates a from a string representation. +/// +public class MatrixConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) + { + return value is string s ? Matrix.Parse(s) : null; + } +} diff --git a/src/Avalonia.Base/Media/Color.cs b/src/Avalonia.Base/Media/Color.cs index 5470a735b3..6581fe6826 100644 --- a/src/Avalonia.Base/Media/Color.cs +++ b/src/Avalonia.Base/Media/Color.cs @@ -6,6 +6,7 @@ // Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation. using System; +using System.ComponentModel; using System.Globalization; #if !BUILDTASK using Avalonia.Animation.Animators; @@ -18,6 +19,7 @@ namespace Avalonia.Media /// An ARGB color. /// #if !BUILDTASK + [TypeConverter(typeof(ColorConverter))] public #endif readonly struct Color : IEquatable @@ -314,7 +316,7 @@ namespace Avalonia.Media if (input.Length == 3 || input.Length == 4) { var extendedLength = 2 * input.Length; - + #if !BUILDTASK Span extended = stackalloc char[extendedLength]; #else diff --git a/src/Avalonia.Base/Media/ColorConverter.cs b/src/Avalonia.Base/Media/ColorConverter.cs new file mode 100644 index 0000000000..68a12f6a74 --- /dev/null +++ b/src/Avalonia.Base/Media/ColorConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Avalonia.Media; + +/// +/// Creates a from a string representation. +/// +public class ColorConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) + { + return value is string s ? Color.Parse(s) : null; + } +} diff --git a/src/Avalonia.Base/Point.cs b/src/Avalonia.Base/Point.cs index 0c789ff20f..a3f3758f16 100644 --- a/src/Avalonia.Base/Point.cs +++ b/src/Avalonia.Base/Point.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Globalization; using System.Numerics; #if !BUILDTASK @@ -12,6 +13,7 @@ namespace Avalonia /// Defines a point. /// #if !BUILDTASK + [TypeConverter(typeof(PointConverter))] public #endif readonly struct Point : IEquatable diff --git a/src/Avalonia.Base/PointConverter.cs b/src/Avalonia.Base/PointConverter.cs new file mode 100644 index 0000000000..7952dd93a7 --- /dev/null +++ b/src/Avalonia.Base/PointConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Avalonia; + +/// +/// Creates a from a string representation. +/// +public class PointConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) + { + return value is string s ? Point.Parse(s) : null; + } +} diff --git a/src/Avalonia.Base/Rect.cs b/src/Avalonia.Base/Rect.cs index 7930228d99..a16de984d1 100644 --- a/src/Avalonia.Base/Rect.cs +++ b/src/Avalonia.Base/Rect.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Globalization; using Avalonia.Animation.Animators; using Avalonia.Utilities; @@ -8,6 +9,7 @@ namespace Avalonia /// /// Defines a rectangle. /// + [TypeConverter(typeof(RectConverter))] public readonly struct Rect : IEquatable { static Rect() diff --git a/src/Avalonia.Base/RectConverter.cs b/src/Avalonia.Base/RectConverter.cs new file mode 100644 index 0000000000..2efd97962f --- /dev/null +++ b/src/Avalonia.Base/RectConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Avalonia; + +/// +/// Creates a from a string representation. +/// +public class RectConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) + { + return value is string s ? Rect.Parse(s) : null; + } +} diff --git a/src/Avalonia.Base/RelativePoint.cs b/src/Avalonia.Base/RelativePoint.cs index e1fd0093b6..9943297166 100644 --- a/src/Avalonia.Base/RelativePoint.cs +++ b/src/Avalonia.Base/RelativePoint.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Globalization; #if !BUILDTASK using Avalonia.Animation.Animators; @@ -8,10 +9,11 @@ using Avalonia.Utilities; namespace Avalonia { /// - /// Defines the reference point units of an or + /// Defines the reference point units of an or /// . /// #if !BUILDTASK + [TypeConverter(typeof(RelativePointConverter))] public #endif enum RelativeUnit diff --git a/src/Avalonia.Base/RelativePointConverter.cs b/src/Avalonia.Base/RelativePointConverter.cs new file mode 100644 index 0000000000..d62e7b2e61 --- /dev/null +++ b/src/Avalonia.Base/RelativePointConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Avalonia; + +/// +/// Creates a from a string representation. +/// +public class RelativePointConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) + { + return value is string s ? RelativePoint.Parse(s) : null; + } +} diff --git a/src/Avalonia.Base/Size.cs b/src/Avalonia.Base/Size.cs index 5f20206200..6f31b48cd3 100644 --- a/src/Avalonia.Base/Size.cs +++ b/src/Avalonia.Base/Size.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Globalization; #if !BUILDTASK using Avalonia.Animation.Animators; @@ -11,6 +12,7 @@ namespace Avalonia /// Defines a size. /// #if !BUILDTASK + [TypeConverter(typeof(SizeConverter))] public #endif readonly struct Size : IEquatable diff --git a/src/Avalonia.Base/SizeConverter.cs b/src/Avalonia.Base/SizeConverter.cs new file mode 100644 index 0000000000..1eec9ead71 --- /dev/null +++ b/src/Avalonia.Base/SizeConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Avalonia; + +/// +/// Creates a from a string representation. +/// +public class SizeConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) + { + return value is string s ? Size.Parse(s) : null; + } +} diff --git a/src/Avalonia.Base/Thickness.cs b/src/Avalonia.Base/Thickness.cs index da3a98088f..1777fe5923 100644 --- a/src/Avalonia.Base/Thickness.cs +++ b/src/Avalonia.Base/Thickness.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Globalization; #if !BUILDTASK using Avalonia.Animation.Animators; @@ -11,6 +12,7 @@ namespace Avalonia /// Describes the thickness of a frame around a rectangle. /// #if !BUILDTASK + [TypeConverter(typeof(ThicknessConverter))] public #endif readonly struct Thickness : IEquatable @@ -292,7 +294,7 @@ namespace Avalonia left = this._left; top = this._top; right = this._right; - bottom = this._bottom; + bottom = this._bottom; } /// diff --git a/src/Avalonia.Base/ThicknessConverter.cs b/src/Avalonia.Base/ThicknessConverter.cs new file mode 100644 index 0000000000..ed8712914b --- /dev/null +++ b/src/Avalonia.Base/ThicknessConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Avalonia; + +/// +/// Creates a from a string representation. +/// +public class ThicknessConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) + { + return value is string s ? Thickness.Parse(s) : null; + } +} diff --git a/src/Avalonia.Controls/GridLength.cs b/src/Avalonia.Controls/GridLength.cs index 0e2f8e7d0c..f310396f7a 100644 --- a/src/Avalonia.Controls/GridLength.cs +++ b/src/Avalonia.Controls/GridLength.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.Globalization; using Avalonia.Utilities; @@ -33,6 +34,7 @@ namespace Avalonia.Controls /// Holds the width or height of a 's column and row definitions. /// #if !BUILDTASK + [TypeConverter(typeof(GridLengthConverter))] public #endif struct GridLength : IEquatable diff --git a/src/Avalonia.Controls/GridLengthConverter.cs b/src/Avalonia.Controls/GridLengthConverter.cs new file mode 100644 index 0000000000..6a25e001f2 --- /dev/null +++ b/src/Avalonia.Controls/GridLengthConverter.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel; +using System.Globalization; + +namespace Avalonia.Controls; + +/// +/// Creates a from a string representation. +/// +public class GridLengthConverter : TypeConverter +{ + public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) + { + return sourceType == typeof(string); + } + + public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) + { + return value is string s ? GridLength.Parse(s) : null; + } +}