-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[NET10.0] Annotate converters for nullability 4/n #29549
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| #nullable disable | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
|
|
@@ -27,17 +26,17 @@ public class BrushTypeConverter : TypeConverter | |
| /// <include file="../../docs/Microsoft.Maui.Controls/BrushTypeConverter.xml" path="//Member[@MemberName='Hsla']/Docs/*" /> | ||
| public const string Hsla = "hsla"; | ||
|
|
||
| readonly ColorTypeConverter _colorTypeConverter = new ColorTypeConverter(); | ||
| readonly ColorTypeConverter _colorTypeConverter = new(); | ||
|
|
||
| public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) | ||
| public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) | ||
| => sourceType == typeof(string) | ||
| || sourceType == typeof(Color) | ||
| || sourceType == typeof(Paint); | ||
|
|
||
| public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) | ||
| public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) | ||
| => destinationType == typeof(Paint); | ||
|
|
||
| public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) | ||
| public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||
| { | ||
| if (value is Color colorValue) | ||
| { | ||
|
|
@@ -50,7 +49,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c | |
|
|
||
| var strValue = value?.ToString(); | ||
|
|
||
| if (strValue != null) | ||
| if (strValue is not null) | ||
| { | ||
| strValue = strValue.Trim(); | ||
|
|
||
|
|
@@ -59,29 +58,34 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c | |
| var gradientBrushParser = new GradientBrushParser(_colorTypeConverter); | ||
| var brush = gradientBrushParser.Parse(strValue); | ||
|
|
||
| if (brush != null) | ||
| if (brush is not null) | ||
| { | ||
| return brush; | ||
| } | ||
| } | ||
|
|
||
| if (strValue.StartsWith(Rgb, StringComparison.InvariantCulture) || strValue.StartsWith(Rgba, StringComparison.InvariantCulture) || strValue.StartsWith(Hsl, StringComparison.InvariantCulture) || strValue.StartsWith(Hsla)) | ||
| if (strValue.StartsWith(Rgb, StringComparison.InvariantCulture) | ||
| || strValue.StartsWith(Rgba, StringComparison.InvariantCulture) | ||
| || strValue.StartsWith(Hsl, StringComparison.InvariantCulture) | ||
| || strValue.StartsWith(Hsla, StringComparison.InvariantCulture)) | ||
| { | ||
| var color = (Color)_colorTypeConverter.ConvertFromInvariantString(strValue); | ||
| var color = (Color?)_colorTypeConverter.ConvertFromInvariantString(strValue); | ||
| return new SolidColorBrush(color); | ||
| } | ||
| } | ||
|
|
||
| string[] parts = strValue.Split('.'); | ||
| string[] parts = strValue.Split('.'); | ||
|
|
||
| if (parts.Length == 1 || (parts.Length == 2 && parts[0] == "Color")) | ||
| { | ||
| var color = (Color)_colorTypeConverter.ConvertFromInvariantString(strValue); | ||
| return new SolidColorBrush(color); | ||
| if (parts.Length == 1 || (parts.Length == 2 && parts[0] == "Color")) | ||
| { | ||
| var color = (Color?)_colorTypeConverter.ConvertFromInvariantString(strValue); | ||
| return new SolidColorBrush(color); | ||
| } | ||
| } | ||
|
|
||
| return new SolidColorBrush(null); | ||
| } | ||
|
|
||
| public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) | ||
| public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) | ||
| { | ||
| if (value is Brush brush && destinationType == typeof(Paint)) | ||
| { | ||
|
|
@@ -94,26 +98,26 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul | |
| public class GradientBrushParser | ||
| { | ||
| readonly ColorTypeConverter _colorConverter; | ||
| GradientBrush _gradient; | ||
| string[] _parts; | ||
| GradientBrush? _gradient; | ||
| string[]? _parts; | ||
| int _position; | ||
|
|
||
| public GradientBrushParser(ColorTypeConverter colorConverter = null) | ||
| public GradientBrushParser(ColorTypeConverter? colorConverter = null) | ||
| { | ||
| _colorConverter = colorConverter ?? new ColorTypeConverter(); | ||
| _colorConverter = colorConverter ?? new(); | ||
| } | ||
|
|
||
| public GradientBrush Parse(string css) | ||
| public GradientBrush? Parse(string? css) | ||
| { | ||
| if (string.IsNullOrWhiteSpace(css)) | ||
| { | ||
| return _gradient; | ||
| } | ||
|
|
||
| #if NETSTANDARD2_0 | ||
| _parts = css.Replace("\r\n", "") | ||
| _parts = css!.Replace("\r\n", "") | ||
| #else | ||
| _parts = css.Replace("\r\n", "", StringComparison.Ordinal) | ||
| _parts = css!.Replace("\r\n", "", StringComparison.Ordinal) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason |
||
| #endif | ||
| .Split(new[] { '(', ')', ',' }, StringSplitOptions.RemoveEmptyEntries); | ||
|
|
||
|
|
@@ -125,7 +129,7 @@ public GradientBrush Parse(string css) | |
| if (part.StartsWith("#", StringComparison.Ordinal)) | ||
| { | ||
| var parts = part.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); | ||
| var color = (Color)_colorConverter.ConvertFromInvariantString(parts[0]); | ||
| var color = (Color)_colorConverter.ConvertFromInvariantString(parts[0])!; | ||
|
|
||
| if (TryParseOffsets(parts, out var offsets)) | ||
| AddGradientStops(color, offsets); | ||
|
|
@@ -138,7 +142,7 @@ public GradientBrush Parse(string css) | |
| if (colorParts[0].Equals("Color", StringComparison.Ordinal)) | ||
| { | ||
| var parts = part.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); | ||
| var color = (Color)_colorConverter.ConvertFromInvariantString(parts[0]); | ||
| var color = (Color)_colorConverter.ConvertFromInvariantString(parts[0])!; | ||
|
|
||
| if (TryParseOffsets(parts, out var offsets)) | ||
| AddGradientStops(color, offsets); | ||
|
|
@@ -169,7 +173,7 @@ public GradientBrush Parse(string css) | |
|
|
||
| colorString.Append(')'); | ||
|
|
||
| var color = (Color)_colorConverter.ConvertFromInvariantString(colorString.ToString()); | ||
| var color = (Color)_colorConverter.ConvertFromInvariantString(colorString.ToString())!; | ||
| var parts = GetNextPart().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); | ||
|
|
||
| if (TryParseOffsets(parts, out var offsets)) | ||
|
|
@@ -211,7 +215,7 @@ public GradientBrush Parse(string css) | |
|
|
||
| string GetPart() | ||
| { | ||
| if (!(_position < _parts.Length)) | ||
| if (!(_position < _parts!.Length)) | ||
| return string.Empty; | ||
|
|
||
| return _parts[_position]; | ||
|
|
@@ -259,13 +263,15 @@ void AddGradientStop(Color color, float? offset = null) | |
| Offset = offset ?? -1 | ||
| }; | ||
|
|
||
| _gradient.GradientStops.Add(gradientStop); | ||
| _gradient!.GradientStops.Add(gradientStop); | ||
| } | ||
|
|
||
| void AddGradientStops(Color color, IEnumerable<float> offsets) | ||
| { | ||
| foreach (var offset in offsets) | ||
| { | ||
| AddGradientStop(color, offset); | ||
| } | ||
| } | ||
|
|
||
| Tuple<Point, Point> GetCoordinatesByAngle(double angle) | ||
|
|
@@ -417,7 +423,9 @@ bool TryParseOffsets(string[] parts, out float[] result) | |
| foreach (var part in parts) | ||
| { | ||
| if (TryParseOffset(part, out var offset)) | ||
| { | ||
| offsets.Add(offset); | ||
| } | ||
| } | ||
|
|
||
| result = offsets.ToArray(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,3 @@ | ||||||
| #nullable disable | ||||||
| using System; | ||||||
| using System.ComponentModel; | ||||||
| using System.Globalization; | ||||||
|
|
@@ -11,34 +10,41 @@ namespace Microsoft.Maui.Controls | |||||
| [ProvideCompiled("Microsoft.Maui.Controls.XamlC.ColumnDefinitionCollectionTypeConverter")] | ||||||
| public class ColumnDefinitionCollectionTypeConverter : TypeConverter | ||||||
| { | ||||||
| public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) | ||||||
| public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) | ||||||
| => sourceType == typeof(string); | ||||||
|
|
||||||
| public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) | ||||||
| public override bool CanConvertTo(ITypeDescriptorContext? context, Type? destinationType) | ||||||
| => destinationType == typeof(string); | ||||||
|
|
||||||
| public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) | ||||||
| public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) | ||||||
| { | ||||||
| var strValue = value?.ToString(); | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In an ideal world, we would go with
Suggested change
but that would change behavior. This approach is used even in other converters. |
||||||
|
|
||||||
| if (strValue != null) | ||||||
| if (strValue is not null) | ||||||
| { | ||||||
| var lengths = strValue.Split(','); | ||||||
| var converter = new GridLengthTypeConverter(); | ||||||
| var definitions = new ColumnDefinition[lengths.Length]; | ||||||
|
|
||||||
| for (var i = 0; i < lengths.Length; i++) | ||||||
| definitions[i] = new ColumnDefinition { Width = (GridLength)converter.ConvertFromInvariantString(lengths[i]) }; | ||||||
| { | ||||||
| definitions[i] = new ColumnDefinition { Width = (GridLength)converter.ConvertFromInvariantString(lengths[i])! }; | ||||||
| } | ||||||
|
|
||||||
| return new ColumnDefinitionCollection(definitions); | ||||||
| } | ||||||
|
|
||||||
| throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(ColumnDefinitionCollection))); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) | ||||||
| public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) | ||||||
| { | ||||||
| if (value is not ColumnDefinitionCollection cdc) | ||||||
| { | ||||||
| throw new NotSupportedException(); | ||||||
| } | ||||||
|
|
||||||
| var converter = new GridLengthTypeConverter(); | ||||||
| return string.Join(", ", cdc.Select(cd => converter.ConvertToInvariantString(cd.Width))); | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringComparison.InvariantCulturewas missing forHsla, so I added it.