Skip to content

Commit 502a26d

Browse files
promote nested class to Converters namespace
1 parent 3cec334 commit 502a26d

File tree

17 files changed

+101
-96
lines changed

17 files changed

+101
-96
lines changed

src/Controls/Foldable/src/TwoPaneView.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Maui.Devices;
66
using Microsoft.Maui.Foldable;
77
using Microsoft.Maui.Graphics;
8+
using Microsoft.Maui.Converters;
89

910
namespace Microsoft.Maui.Controls.Foldable
1011
{
@@ -152,7 +153,7 @@ public double MinWideModeWidth
152153
/// <summary>
153154
/// Gets the calculated width (in wide mode) or height (in tall mode) of pane 1, or sets the GridLength value of pane 1.
154155
/// </summary>
155-
[System.ComponentModel.TypeConverter(typeof(GridLength.GridLengthTypeConverter))]
156+
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
156157
public GridLength Pane1Length
157158
{
158159
get { return (GridLength)GetValue(Pane1LengthProperty); }
@@ -162,7 +163,7 @@ public GridLength Pane1Length
162163
/// <summary>
163164
/// Gets the calculated width (in wide mode) or height (in tall mode) of pane 2, or sets the GridLength value of pane 2.
164165
/// </summary>
165-
[System.ComponentModel.TypeConverter(typeof(GridLength.GridLengthTypeConverter))]
166+
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
166167
public GridLength Pane2Length
167168
{
168169
get { return (GridLength)GetValue(Pane2LengthProperty); }

src/Controls/src/Build.Tasks/XamlCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public ICollection<string> GetResourceNamesInUse(VariableDefinition variableDefi
7474
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "FlexAlignSelfTypeConverter")), typeof(EnumTypeConverter<Layouts.FlexAlignSelf>) },
7575
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "FlexWrapTypeConverter")), typeof(EnumTypeConverter<Layouts.FlexWrap>) },
7676
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "FlexBasisTypeConverter")), typeof(FlexBasisTypeConverter) },
77-
// { module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui", "GridLength.GridLengthTypeConverter")), typeof(GridLengthTypeConverter) },
77+
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "GridLengthTypeConverter")), typeof(Converters.GridLengthTypeConverter) },
7878

7979
};
8080

src/Controls/src/Core/ColumnDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public ColumnDefinition(GridLength width)
1919
=> SetValue(WidthProperty, width);
2020

2121
/// <include file="../../docs/Microsoft.Maui.Controls/ColumnDefinition.xml" path="//Member[@MemberName='Width']/Docs/*" />
22-
[System.ComponentModel.TypeConverter(typeof(Microsoft.Maui.GridLength.GridLengthTypeConverter))]
22+
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
2323
public GridLength Width
2424
{
2525
get { return (GridLength)GetValue(WidthProperty); }

src/Controls/src/Core/ColumnDefinitionCollectionTypeConverter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
1717
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
1818
=> destinationType == typeof(string);
1919

20+
static TypeConverter converter = new Converters.GridLengthTypeConverter();
2021
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
2122
{
2223
var strValue = value?.ToString();
2324

2425
if (strValue != null)
2526
{
2627
var lengths = strValue.Split(',');
27-
var converter = new Microsoft.Maui.GridLength.GridLengthTypeConverter();
2828
var definitions = new ColumnDefinition[lengths.Length];
2929
for (var i = 0; i < lengths.Length; i++)
3030
definitions[i] = new ColumnDefinition { Width = (GridLength)converter.ConvertFromInvariantString(lengths[i]) };
@@ -39,7 +39,6 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
3939
{
4040
if (value is not ColumnDefinitionCollection cdc)
4141
throw new NotSupportedException();
42-
var converter = new Microsoft.Maui.GridLength.GridLengthTypeConverter();
4342
return string.Join(", ", cdc.Select(cd => converter.ConvertToInvariantString(cd.Width)));
4443
}
4544
}

src/Controls/src/Core/GridLengthTypeConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace Microsoft.Maui.Controls
88
{
99
[ProvideCompiled("Microsoft.Maui.Controls.XamlC.GridLengthTypeConverter")]
10-
[Obsolete("Microsoft.Maui.Controls.GridLengthTypeConverter is obsolete. Use Microsoft.Maui.GridLength.GridLengthConverter instead.")]
10+
[Obsolete("Microsoft.Maui.Controls.GridLengthTypeConverter is obsolete. Use Microsoft.Maui.Converters.GridLengthConverter instead.")]
1111
public class GridLengthTypeConverter : TypeConverter
1212
{
1313
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)

src/Controls/src/Core/RowDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public RowDefinition(GridLength height)
2121
}
2222

2323
/// <include file="../../docs/Microsoft.Maui.Controls/RowDefinition.xml" path="//Member[@MemberName='Height']/Docs/*" />
24-
[System.ComponentModel.TypeConverter(typeof(Microsoft.Maui.GridLength.GridLengthTypeConverter))]
24+
[System.ComponentModel.TypeConverter(typeof(Converters.GridLengthTypeConverter))]
2525
public GridLength Height
2626
{
2727
get { return (GridLength)GetValue(HeightProperty); }

src/Controls/src/Core/RowDefinitionCollectionTypeConverter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
1717
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
1818
=> true;
1919

20+
static TypeConverter converter = new Converters.GridLengthTypeConverter();
2021
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
2122
{
2223
var strValue = value?.ToString();
2324

2425
if (strValue != null)
2526
{
2627
var lengths = strValue.Split(',');
27-
var converter = new Microsoft.Maui.GridLength.GridLengthTypeConverter();
2828
var definitions = new RowDefinition[lengths.Length];
2929
for (var i = 0; i < lengths.Length; i++)
3030
definitions[i] = new RowDefinition { Height = (GridLength)converter.ConvertFromInvariantString(lengths[i]) };
@@ -38,7 +38,6 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
3838
{
3939
if (value is not RowDefinitionCollection rdc)
4040
throw new NotSupportedException();
41-
var converter = new Microsoft.Maui.GridLength.GridLengthTypeConverter();
4241
return string.Join(", ", rdc.Select(rd => converter.ConvertToInvariantString(rd.Height)));
4342
}
4443
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Globalization;
4+
5+
namespace Microsoft.Maui.Converters;
6+
7+
public sealed class GridLengthTypeConverter : TypeConverter
8+
{
9+
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
10+
=> sourceType == typeof(double) || sourceType == typeof(string);
11+
12+
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
13+
=> value switch
14+
{
15+
double d => (GridLength)d,
16+
string strValue => strValue.Trim().ToLowerInvariant() switch
17+
{
18+
"auto" => GridLength.Auto,
19+
"*" => new GridLength(1, GridUnitType.Star),
20+
#pragma warning disable CA1846, CA1865
21+
_ when strValue.EndsWith("*", StringComparison.Ordinal) && double.TryParse(strValue.Substring(0, strValue.Length - 1), NumberStyles.Number, CultureInfo.InvariantCulture, out var length) => new GridLength(length, GridUnitType.Star),
22+
#pragma warning restore CA1846, CA1865
23+
_ when double.TryParse(strValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var length) => new GridLength(length),
24+
_ => throw new FormatException(),
25+
},
26+
_ => throw new NotSupportedException(),
27+
};
28+
29+
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) => destinationType == typeof(string);
30+
public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType)
31+
{
32+
if (destinationType == typeof(string) && value is GridLength length)
33+
{
34+
if (length.IsAuto)
35+
return "auto";
36+
if (length.IsStar)
37+
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}*";
38+
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}";
39+
}
40+
throw new NotSupportedException($"Cannot convert {value?.GetType()} to {destinationType}");
41+
42+
}
43+
}

src/Core/src/Primitives/GridLength.cs

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel;
44
using System.Diagnostics;
55
using System.Globalization;
6+
using Microsoft.Maui.Converters;
67

78
namespace Microsoft.Maui
89
{
@@ -119,43 +120,5 @@ public override string ToString()
119120
/// <param name="right">The second GridLength to compare.</param>
120121
/// <returns><see langword="true"/> if the two GridLengths differ; otherwise, <see langword="false"/>.</returns>
121122
public static bool operator !=(GridLength left, GridLength right) => !(left == right);
122-
123-
public sealed class GridLengthTypeConverter : TypeConverter
124-
{
125-
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
126-
=> sourceType == typeof(double) || sourceType == typeof(string);
127-
128-
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
129-
=> value switch
130-
{
131-
double d => (GridLength)d,
132-
string strValue => strValue.Trim().ToLowerInvariant() switch
133-
{
134-
"auto" => GridLength.Auto,
135-
"*" => new GridLength(1, GridUnitType.Star),
136-
#pragma warning disable CA1846, CA1865
137-
_ when strValue.EndsWith("*", StringComparison.Ordinal) && double.TryParse(strValue.Substring(0, strValue.Length - 1), NumberStyles.Number, CultureInfo.InvariantCulture, out var length) => new GridLength(length, GridUnitType.Star),
138-
#pragma warning restore CA1846, CA1865
139-
_ when double.TryParse(strValue, NumberStyles.Number, CultureInfo.InvariantCulture, out var length) => new GridLength(length),
140-
_ => throw new FormatException(),
141-
},
142-
_ => throw new NotSupportedException(),
143-
};
144-
145-
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) => destinationType == typeof(string);
146-
public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType)
147-
{
148-
if (destinationType == typeof(string) && value is GridLength length)
149-
{
150-
if (length.IsAuto)
151-
return "auto";
152-
if (length.IsStar)
153-
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}*";
154-
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}";
155-
}
156-
throw new NotSupportedException($"Cannot convert {value?.GetType()} to {destinationType}");
157-
158-
}
159-
}
160123
}
161124
}

src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#nullable enable
22
Microsoft.Maui.ElementHandlerExtensions
3-
Microsoft.Maui.GridLength.GridLengthTypeConverter
4-
Microsoft.Maui.GridLength.GridLengthTypeConverter.GridLengthTypeConverter() -> void
5-
override Microsoft.Maui.GridLength.GridLengthTypeConverter.CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) -> bool
6-
override Microsoft.Maui.GridLength.GridLengthTypeConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType) -> bool
7-
override Microsoft.Maui.GridLength.GridLengthTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value) -> object!
8-
override Microsoft.Maui.GridLength.GridLengthTypeConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type? destinationType) -> object!
3+
Microsoft.Maui.Converters.GridLengthTypeConverter
4+
Microsoft.Maui.Converters.GridLengthTypeConverter.GridLengthTypeConverter() -> void
5+
override Microsoft.Maui.Converters.GridLengthTypeConverter.CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType) -> bool
6+
override Microsoft.Maui.Converters.GridLengthTypeConverter.CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? destinationType) -> bool
7+
override Microsoft.Maui.Converters.GridLengthTypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value) -> object!
8+
override Microsoft.Maui.Converters.GridLengthTypeConverter.ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type? destinationType) -> object!
99
Microsoft.Maui.HandlerDisconnectPolicy
1010
Microsoft.Maui.HandlerDisconnectPolicy.Automatic = 0 -> Microsoft.Maui.HandlerDisconnectPolicy
1111
Microsoft.Maui.HandlerDisconnectPolicy.Manual = 1 -> Microsoft.Maui.HandlerDisconnectPolicy

0 commit comments

Comments
 (0)