55namespace Microsoft . Maui . Converters ;
66
77public 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