Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeConverters API Docs #9780

Merged
merged 2 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Core/src/Converters/CornerRadiusTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

namespace Microsoft.Maui.Converters
{
/// <inheritdoc/>
public class CornerRadiusTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType)
=> sourceType == typeof(string);

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType)
=> destinationType == typeof(string);

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
{
var strValue = value?.ToString();

if (strValue != null)
{
value = strValue.Trim();
Expand Down Expand Up @@ -61,11 +66,14 @@ public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo?
throw new InvalidOperationException($"Cannot convert \"{strValue}\" into {typeof(CornerRadius)}");
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType)
{
if (value is not CornerRadius cr)
throw new NotSupportedException();
return $"{cr.TopLeft.ToString(CultureInfo.InvariantCulture)}, {cr.TopRight.ToString(CultureInfo.InvariantCulture)}, {cr.BottomLeft.ToString(CultureInfo.InvariantCulture)}, {cr.BottomRight.ToString(CultureInfo.InvariantCulture)}";

return $"{cr.TopLeft.ToString(CultureInfo.InvariantCulture)}, {cr.TopRight.ToString(CultureInfo.InvariantCulture)}, " +
$"{cr.BottomLeft.ToString(CultureInfo.InvariantCulture)}, {cr.BottomRight.ToString(CultureInfo.InvariantCulture)}";

}
}
Expand Down
34 changes: 22 additions & 12 deletions src/Core/src/Converters/EasingTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@

namespace Microsoft.Maui.Converters
{
/// <inheritdoc/>
public class EasingTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> destinationType == typeof(string);

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();
Expand All @@ -26,6 +30,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c

strValue = strValue?.Trim() ?? "";
var parts = strValue.Split('.');

if (parts.Length == 2 && parts[0] == nameof(Easing))
strValue = parts[parts.Length - 1];

Expand Down Expand Up @@ -64,6 +69,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
throw new InvalidOperationException($"Cannot convert \"{strValue}\" into {typeof(Easing)}");
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not Easing easing)
Expand Down Expand Up @@ -91,28 +97,32 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
return nameof(SpringIn);
if (easing == SpringOut)
return nameof(SpringOut);

throw new NotSupportedException();
}

/// <inheritdoc/>
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
=> true;

/// <inheritdoc/>
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
=> false;

/// <inheritdoc/>
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
=> new(new[] {
"Linear",
"SinOut",
"SinIn",
"SinInOut",
"CubicIn",
"CubicOut",
"CubicInOut",
"BounceOut",
"BounceIn",
"SpringIn",
"SpringOut"
"Linear",
"SinOut",
"SinIn",
"SinInOut",
"CubicIn",
"CubicOut",
"CubicInOut",
"BounceOut",
"BounceIn",
"SpringIn",
"SpringOut"
});
}
}
}
53 changes: 52 additions & 1 deletion src/Core/src/Converters/FlexEnumsConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
using System.ComponentModel;
using System.Globalization;
using Microsoft.Maui.Layouts;
using Flex = Microsoft.Maui.Layouts.Flex;

#nullable disable
namespace Microsoft.Maui.Converters
{
/// <inheritdoc/>
public class FlexJustifyTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> true;

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();
Expand All @@ -32,25 +35,32 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
if (strValue.Equals("space-around", StringComparison.OrdinalIgnoreCase))
return FlexJustify.SpaceAround;
}

throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(FlexJustify)));
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not FlexJustify fj)
throw new NotSupportedException();

return fj.ToString();
}
}

/// <inheritdoc/>
public class FlexDirectionTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> true;

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();
Expand All @@ -64,25 +74,32 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
if (strValue.Equals("column-reverse", StringComparison.OrdinalIgnoreCase))
return FlexDirection.ColumnReverse;
}

throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(FlexDirection)));
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not FlexDirection fd)
throw new NotSupportedException();

return fd.ToString();
}
}

/// <inheritdoc/>
public class FlexAlignContentTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> true;

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();
Expand All @@ -100,25 +117,32 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
if (strValue.Equals("space-around", StringComparison.OrdinalIgnoreCase))
return FlexAlignContent.SpaceAround;
}

throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(FlexAlignContent)));
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not FlexAlignContent fac)
throw new NotSupportedException();

return fac.ToString();
}
}

/// <inheritdoc/>
public class FlexAlignItemsTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> true;

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();
Expand All @@ -132,25 +156,32 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
if (strValue.Equals("flex-end", StringComparison.OrdinalIgnoreCase))
return FlexAlignItems.End;
}

throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(FlexAlignItems)));
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not FlexAlignItems fai)
throw new NotSupportedException();

return fai.ToString();
}
}

/// <inheritdoc/>
public class FlexAlignSelfTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> true;

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();
Expand All @@ -164,25 +195,32 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
if (strValue.Equals("flex-end", StringComparison.OrdinalIgnoreCase))
return FlexAlignSelf.End;
}

throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(FlexAlignSelf)));
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not FlexAlignSelf fes)
throw new NotSupportedException();

return fes.ToString();
}
}

/// <inheritdoc/>
public class FlexWrapTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> true;

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();
Expand All @@ -194,49 +232,62 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
if (strValue.Equals("wrap-reverse", StringComparison.OrdinalIgnoreCase))
return FlexWrap.Reverse;
}

throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(FlexWrap)));
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not FlexWrap fw)
throw new NotSupportedException();

return fw.ToString();
}
}

/// <inheritdoc/>
public class FlexBasisTypeConverter : TypeConverter
{
/// <inheritdoc/>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
=> sourceType == typeof(string);

/// <inheritdoc/>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
=> true;

/// <inheritdoc/>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var strValue = value?.ToString();

if (strValue != null)
{
strValue = strValue.Trim();

if (strValue.Equals("auto", StringComparison.OrdinalIgnoreCase))
return FlexBasis.Auto;
if (strValue.EndsWith("%", StringComparison.OrdinalIgnoreCase) && float.TryParse(strValue.Substring(0, strValue.Length - 1), NumberStyles.Number, CultureInfo.InvariantCulture, out float relflex))
return new FlexBasis(relflex / 100, isRelative: true);
if (float.TryParse(strValue, NumberStyles.Number, CultureInfo.InvariantCulture, out float flex))
return new FlexBasis(flex);
}

throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(FlexBasis)));
}

/// <inheritdoc/>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is not FlexBasis basis)
throw new NotSupportedException();

if (basis.IsAuto)
return "auto";
if (basis.IsRelative)
return $"{(basis.Length * 100).ToString(CultureInfo.InvariantCulture)}%";

return $"{basis.Length.ToString(CultureInfo.InvariantCulture)}";
}
}
Expand Down
Loading