Browse Source

Add some missing TypeConverters

add-missing-type-converters
Max Katz 3 years ago
parent
commit
f5e3a56ff1
  1. 4
      src/Avalonia.Base/CornerRadius.cs
  2. 21
      src/Avalonia.Base/CornerRadiusConverter.cs
  3. 4
      src/Avalonia.Base/Matrix.cs
  4. 21
      src/Avalonia.Base/MatrixConverter.cs
  5. 4
      src/Avalonia.Base/Media/Color.cs
  6. 21
      src/Avalonia.Base/Media/ColorConverter.cs
  7. 2
      src/Avalonia.Base/Point.cs
  8. 21
      src/Avalonia.Base/PointConverter.cs
  9. 2
      src/Avalonia.Base/Rect.cs
  10. 21
      src/Avalonia.Base/RectConverter.cs
  11. 4
      src/Avalonia.Base/RelativePoint.cs
  12. 21
      src/Avalonia.Base/RelativePointConverter.cs
  13. 2
      src/Avalonia.Base/Size.cs
  14. 21
      src/Avalonia.Base/SizeConverter.cs
  15. 4
      src/Avalonia.Base/Thickness.cs
  16. 21
      src/Avalonia.Base/ThicknessConverter.cs
  17. 2
      src/Avalonia.Controls/GridLength.cs
  18. 21
      src/Avalonia.Controls/GridLengthConverter.cs

4
src/Avalonia.Base/CornerRadius.cs

@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
#if !BUILDTASK #if !BUILDTASK
using Avalonia.Animation.Animators; using Avalonia.Animation.Animators;
@ -11,6 +12,7 @@ namespace Avalonia
/// Represents the radii of a rectangle's corners. /// Represents the radii of a rectangle's corners.
/// </summary> /// </summary>
#if !BUILDTASK #if !BUILDTASK
[TypeConverter(typeof(CornerRadiusConverter))]
public public
#endif #endif
readonly struct CornerRadius : IEquatable<CornerRadius> readonly struct CornerRadius : IEquatable<CornerRadius>
@ -79,7 +81,7 @@ namespace Avalonia
{ {
// ReSharper disable CompareOfFloatsByEqualityOperator // ReSharper disable CompareOfFloatsByEqualityOperator
return TopLeft == other.TopLeft && return TopLeft == other.TopLeft &&
TopRight == other.TopRight && TopRight == other.TopRight &&
BottomRight == other.BottomRight && BottomRight == other.BottomRight &&
BottomLeft == other.BottomLeft; BottomLeft == other.BottomLeft;

21
src/Avalonia.Base/CornerRadiusConverter.cs

@ -0,0 +1,21 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Avalonia;
/// <summary>
/// Creates a <see cref="CornerRadius"/> from a string representation.
/// </summary>
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;
}
}

4
src/Avalonia.Base/Matrix.cs

@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using System.Numerics; using System.Numerics;
@ -14,10 +15,11 @@ namespace Avalonia
/// 1st row | scaleX | skewY | perspX | /// 1st row | scaleX | skewY | perspX |
/// 2nd row | skewX | scaleY | perspY | /// 2nd row | skewX | scaleY | perspY |
/// 3rd row | transX | transY | perspZ | /// 3rd row | transX | transY | perspZ |
/// ///
/// Note: Skia.SkMatrix uses a transposed layout (where for example skewX/skewY and persp0/transX are swapped). /// Note: Skia.SkMatrix uses a transposed layout (where for example skewX/skewY and persp0/transX are swapped).
/// </remarks> /// </remarks>
#if !BUILDTASK #if !BUILDTASK
[TypeConverter(typeof(MatrixConverter))]
public public
#endif #endif
readonly struct Matrix : IEquatable<Matrix> readonly struct Matrix : IEquatable<Matrix>

21
src/Avalonia.Base/MatrixConverter.cs

@ -0,0 +1,21 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Avalonia;
/// <summary>
/// Creates a <see cref="Matrix"/> from a string representation.
/// </summary>
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;
}
}

4
src/Avalonia.Base/Media/Color.cs

@ -6,6 +6,7 @@
// Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation. // Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation.
using System; using System;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
#if !BUILDTASK #if !BUILDTASK
using Avalonia.Animation.Animators; using Avalonia.Animation.Animators;
@ -18,6 +19,7 @@ namespace Avalonia.Media
/// An ARGB color. /// An ARGB color.
/// </summary> /// </summary>
#if !BUILDTASK #if !BUILDTASK
[TypeConverter(typeof(ColorConverter))]
public public
#endif #endif
readonly struct Color : IEquatable<Color> readonly struct Color : IEquatable<Color>
@ -314,7 +316,7 @@ namespace Avalonia.Media
if (input.Length == 3 || input.Length == 4) if (input.Length == 3 || input.Length == 4)
{ {
var extendedLength = 2 * input.Length; var extendedLength = 2 * input.Length;
#if !BUILDTASK #if !BUILDTASK
Span<char> extended = stackalloc char[extendedLength]; Span<char> extended = stackalloc char[extendedLength];
#else #else

21
src/Avalonia.Base/Media/ColorConverter.cs

@ -0,0 +1,21 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Avalonia.Media;
/// <summary>
/// Creates a <see cref="Color"/> from a string representation.
/// </summary>
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;
}
}

2
src/Avalonia.Base/Point.cs

@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
using System.Numerics; using System.Numerics;
#if !BUILDTASK #if !BUILDTASK
@ -12,6 +13,7 @@ namespace Avalonia
/// Defines a point. /// Defines a point.
/// </summary> /// </summary>
#if !BUILDTASK #if !BUILDTASK
[TypeConverter(typeof(PointConverter))]
public public
#endif #endif
readonly struct Point : IEquatable<Point> readonly struct Point : IEquatable<Point>

21
src/Avalonia.Base/PointConverter.cs

@ -0,0 +1,21 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Avalonia;
/// <summary>
/// Creates a <see cref="Thickness"/> from a string representation.
/// </summary>
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;
}
}

2
src/Avalonia.Base/Rect.cs

@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
using Avalonia.Animation.Animators; using Avalonia.Animation.Animators;
using Avalonia.Utilities; using Avalonia.Utilities;
@ -8,6 +9,7 @@ namespace Avalonia
/// <summary> /// <summary>
/// Defines a rectangle. /// Defines a rectangle.
/// </summary> /// </summary>
[TypeConverter(typeof(RectConverter))]
public readonly struct Rect : IEquatable<Rect> public readonly struct Rect : IEquatable<Rect>
{ {
static Rect() static Rect()

21
src/Avalonia.Base/RectConverter.cs

@ -0,0 +1,21 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Avalonia;
/// <summary>
/// Creates a <see cref="Rect"/> from a string representation.
/// </summary>
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;
}
}

4
src/Avalonia.Base/RelativePoint.cs

@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
#if !BUILDTASK #if !BUILDTASK
using Avalonia.Animation.Animators; using Avalonia.Animation.Animators;
@ -8,10 +9,11 @@ using Avalonia.Utilities;
namespace Avalonia namespace Avalonia
{ {
/// <summary> /// <summary>
/// Defines the reference point units of an <see cref="RelativePoint"/> or /// Defines the reference point units of an <see cref="RelativePoint"/> or
/// <see cref="RelativeRect"/>. /// <see cref="RelativeRect"/>.
/// </summary> /// </summary>
#if !BUILDTASK #if !BUILDTASK
[TypeConverter(typeof(RelativePointConverter))]
public public
#endif #endif
enum RelativeUnit enum RelativeUnit

21
src/Avalonia.Base/RelativePointConverter.cs

@ -0,0 +1,21 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Avalonia;
/// <summary>
/// Creates a <see cref="RelativePoint"/> from a string representation.
/// </summary>
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;
}
}

2
src/Avalonia.Base/Size.cs

@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
#if !BUILDTASK #if !BUILDTASK
using Avalonia.Animation.Animators; using Avalonia.Animation.Animators;
@ -11,6 +12,7 @@ namespace Avalonia
/// Defines a size. /// Defines a size.
/// </summary> /// </summary>
#if !BUILDTASK #if !BUILDTASK
[TypeConverter(typeof(SizeConverter))]
public public
#endif #endif
readonly struct Size : IEquatable<Size> readonly struct Size : IEquatable<Size>

21
src/Avalonia.Base/SizeConverter.cs

@ -0,0 +1,21 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Avalonia;
/// <summary>
/// Creates a <see cref="Size"/> from a string representation.
/// </summary>
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;
}
}

4
src/Avalonia.Base/Thickness.cs

@ -1,4 +1,5 @@
using System; using System;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
#if !BUILDTASK #if !BUILDTASK
using Avalonia.Animation.Animators; using Avalonia.Animation.Animators;
@ -11,6 +12,7 @@ namespace Avalonia
/// Describes the thickness of a frame around a rectangle. /// Describes the thickness of a frame around a rectangle.
/// </summary> /// </summary>
#if !BUILDTASK #if !BUILDTASK
[TypeConverter(typeof(ThicknessConverter))]
public public
#endif #endif
readonly struct Thickness : IEquatable<Thickness> readonly struct Thickness : IEquatable<Thickness>
@ -292,7 +294,7 @@ namespace Avalonia
left = this._left; left = this._left;
top = this._top; top = this._top;
right = this._right; right = this._right;
bottom = this._bottom; bottom = this._bottom;
} }
/// <summary> /// <summary>

21
src/Avalonia.Base/ThicknessConverter.cs

@ -0,0 +1,21 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Avalonia;
/// <summary>
/// Creates a <see cref="Thickness"/> from a string representation.
/// </summary>
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;
}
}

2
src/Avalonia.Controls/GridLength.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization; using System.Globalization;
using Avalonia.Utilities; using Avalonia.Utilities;
@ -33,6 +34,7 @@ namespace Avalonia.Controls
/// Holds the width or height of a <see cref="Grid"/>'s column and row definitions. /// Holds the width or height of a <see cref="Grid"/>'s column and row definitions.
/// </summary> /// </summary>
#if !BUILDTASK #if !BUILDTASK
[TypeConverter(typeof(GridLengthConverter))]
public public
#endif #endif
struct GridLength : IEquatable<GridLength> struct GridLength : IEquatable<GridLength>

21
src/Avalonia.Controls/GridLengthConverter.cs

@ -0,0 +1,21 @@
using System;
using System.ComponentModel;
using System.Globalization;
namespace Avalonia.Controls;
/// <summary>
/// Creates a <see cref="GridLength"/> from a string representation.
/// </summary>
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;
}
}
Loading…
Cancel
Save