Skip to content

Commit 57538eb

Browse files
update
1 parent baa8a9b commit 57538eb

File tree

4 files changed

+77
-38
lines changed

4 files changed

+77
-38
lines changed

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.Converters", "GridLengthTypeConverter")), typeof(Converters.GridLengthTypeConverter) },
77+
{ module.ImportReference(this, ("Microsoft.Maui", "Microsoft.Maui.Converters", "GridLengthTypeConverter")), typeof(Microsoft.Maui.Controls.XamlC.GridLengthTypeConverter) },
7878

7979
};
8080

src/Controls/src/Core/ColumnDefinitionCollectionTypeConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
3434
var definitions = new List<ColumnDefinition>(count);
3535
foreach (var range in unsplit.Split(','))
3636
{
37-
var length = GridLengthTypeConverter.ParseStringToGridLength(unsplit[range]);
37+
var length = Converters.GridLengthTypeConverter.ParseStringToGridLength(unsplit[range]);
3838
definitions.Add(new ColumnDefinition(length));
3939
}
4040
#else
@@ -43,7 +43,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
4343
var definitions = new List<ColumnDefinition>(count);
4444
foreach (var lengthStr in lengths)
4545
{
46-
var length = GridLengthTypeConverter.ParseStringToGridLength(lengthStr);
46+
var length = Converters.GridLengthTypeConverter.ParseStringToGridLength(lengthStr);
4747
definitions.Add(new ColumnDefinition(length));
4848
}
4949
#endif
@@ -63,15 +63,15 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
6363
if (count == 0)
6464
return string.Empty;
6565
if (count == 1)
66-
return GridLengthTypeConverter.ConvertToString(definitions[0].Width);
66+
return Converters.GridLengthTypeConverter.ConvertToString(definitions[0].Width);
6767

6868
// for multiple items
6969
var pool = ArrayPool<string>.Shared;
7070
var rentedArray = pool.Rent(definitions.Count);
7171
for (var i = 0; i < definitions.Count; i++)
7272
{
7373
var definition = definitions[i];
74-
rentedArray[i] = GridLengthTypeConverter.ConvertToString(definition.Width);
74+
rentedArray[i] = Converters.GridLengthTypeConverter.ConvertToString(definition.Width);
7575
}
7676
var result = string.Join(", ", rentedArray, 0, definitions.Count);
7777
pool.Return(rentedArray);

src/Controls/src/Core/RowDefinitionCollectionTypeConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
3333
var definitions = new List<RowDefinition>(count);
3434
foreach (var range in unsplit.Split(','))
3535
{
36-
var length = GridLengthTypeConverter.ParseStringToGridLength(unsplit[range]);
36+
var length = Converters.GridLengthTypeConverter.ParseStringToGridLength(unsplit[range]);
3737
definitions.Add(new RowDefinition(length));
3838
}
3939
#else
@@ -42,7 +42,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
4242
var definitions = new List<RowDefinition>(count);
4343
foreach (var lengthStr in lengths)
4444
{
45-
var length = GridLengthTypeConverter.ParseStringToGridLength(lengthStr);
45+
var length = Converters.GridLengthTypeConverter.ParseStringToGridLength(lengthStr);
4646
definitions.Add(new RowDefinition(length));
4747
}
4848
#endif
@@ -62,15 +62,15 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
6262
if (count == 0)
6363
return string.Empty;
6464
if (count == 1)
65-
return GridLengthTypeConverter.ConvertToString(definitions[0].Height);
65+
return Converters.GridLengthTypeConverter.ConvertToString(definitions[0].Height);
6666

6767
// for multiple items
6868
var pool = ArrayPool<string>.Shared;
6969
var rentedArray = pool.Rent(definitions.Count);
7070
for (var i = 0; i < definitions.Count; i++)
7171
{
7272
var definition = definitions[i];
73-
rentedArray[i] = GridLengthTypeConverter.ConvertToString(definition.Height);
73+
rentedArray[i] = Converters.GridLengthTypeConverter.ConvertToString(definition.Height);
7474
}
7575
var result = string.Join(", ", rentedArray, 0, definitions.Count);
7676
pool.Return(rentedArray);

src/Core/src/Converters/GridLengthTypeConverter.cs

Lines changed: 68 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,78 @@
55
namespace Microsoft.Maui.Converters;
66

77
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)
813
{
9-
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType)
10-
=> sourceType == typeof(double) || sourceType == typeof(string);
14+
if (value is double d)
15+
return (GridLength)d;
1116

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)
17+
var strValue = value as string ?? value?.ToString();
18+
if (strValue is null)
19+
throw new FormatException($"Invalid GridLength format: {value}");
20+
21+
return ParseStringToGridLength(strValue);
22+
}
23+
24+
#if NET6_0_OR_GREATER
25+
internal static GridLength ParseStringToGridLength(ReadOnlySpan<char> value)
26+
#else
27+
internal static GridLength ParseStringToGridLength(string value)
28+
#endif
29+
{
30+
value = value.Trim();
31+
32+
if (value.Length != 0)
3133
{
32-
if (destinationType == typeof(string) && value is GridLength length)
34+
if (value.Length == 4 && value.Equals("auto", StringComparison.OrdinalIgnoreCase))
35+
return GridLength.Auto;
36+
37+
if (value.Length == 1 && value[0] == '*')
38+
return GridLength.Star;
39+
40+
#if NET6_0_OR_GREATER
41+
var lastChar = value[^1];
42+
#else
43+
var lastChar = value[value.Length - 1];
44+
#endif
45+
if (lastChar == '*')
3346
{
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)}";
47+
#if NET6_0_OR_GREATER
48+
var prefix = value[..^1];
49+
#else
50+
var prefix = value.Substring(0, value.Length - 1);
51+
#endif
52+
53+
if (double.TryParse(prefix, NumberStyles.Number, CultureInfo.InvariantCulture, out var starLength))
54+
return new GridLength(starLength, GridUnitType.Star);
3955
}
40-
throw new NotSupportedException($"Cannot convert {value?.GetType()} to {destinationType}");
4156

57+
if (double.TryParse(value, NumberStyles.Number, CultureInfo.InvariantCulture, out var absoluteLength))
58+
return new GridLength(absoluteLength);
4259
}
43-
}
60+
61+
throw new FormatException($"Invalid GridLength format: {value.ToString()}");
62+
}
63+
64+
public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) => destinationType == typeof(string);
65+
66+
public override object ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type? destinationType)
67+
{
68+
if (destinationType == typeof(string) && value is GridLength length)
69+
return ConvertToString(length);
70+
throw new NotSupportedException($"Cannot convert {value?.GetType()} to {destinationType}");
71+
72+
}
73+
74+
internal static string ConvertToString(GridLength length)
75+
{
76+
if (length.IsAuto)
77+
return "auto";
78+
if (length.IsStar)
79+
return $"{length.Value.ToString(CultureInfo.InvariantCulture)}*";
80+
return length.Value.ToString(CultureInfo.InvariantCulture);
81+
}
82+
}

0 commit comments

Comments
 (0)