Skip to content

Commit 4152445

Browse files
committed
Simplify CanConvertFrom/CanConvertTo
1 parent ae2c88d commit 4152445

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,8 @@ public class MouseActionConverter : TypeConverter
3535
///<returns><see langword="true"/> if the given <paramref name="sourceType"/> can be converted from, <see langword="false"/> otherwise.</returns>
3636
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
3737
{
38-
// We can only handle string.
39-
if (sourceType == typeof(string))
40-
{
41-
return true;
42-
}
43-
else
44-
{
45-
return false;
46-
}
38+
// We can only handle string
39+
return sourceType == typeof(string);
4740
}
4841

4942
/// <summary>
@@ -54,16 +47,16 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT
5447
/// <returns><see langword="true"/> if conversion to <see langword="string"/> is possible, <see langword="false"/> otherwise.</returns>
5548
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
5649
{
57-
// We can convert to an InstanceDescriptor or to a string.
58-
if (destinationType == typeof(string))
59-
{
60-
// When invoked by the serialization engine we can convert to string only for known type
61-
if (context != null && context.Instance != null)
62-
{
63-
return IsDefinedMouseAction((MouseAction)context.Instance);
64-
}
65-
}
66-
return false;
50+
// We can convert to an InstanceDescriptor or to a string
51+
if (destinationType != typeof(string))
52+
return false;
53+
54+
// When invoked by the serialization engine we can convert to string only for known type
55+
if (context is null || context.Instance is null)
56+
return false;
57+
58+
// Make sure the value falls within defined set
59+
return IsDefinedMouseAction((MouseAction)context.Instance);
6760
}
6861

6962
/// <summary>
@@ -81,7 +74,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c
8174
ReadOnlySpan<char> mouseActionToken = mouseAction.AsSpan().Trim();
8275
return mouseActionToken switch
8376
{
84-
_ when mouseActionToken.IsEmpty => MouseAction.None, //Special casing as produced by "ConvertTo"
77+
_ when mouseActionToken.IsEmpty => MouseAction.None, // Special casing as produced by "ConvertTo"
8578
_ when mouseActionToken.Equals("None", StringComparison.OrdinalIgnoreCase) => MouseAction.None,
8679
_ when mouseActionToken.Equals("LeftClick", StringComparison.OrdinalIgnoreCase) => MouseAction.LeftClick,
8780
_ when mouseActionToken.Equals("RightClick", StringComparison.OrdinalIgnoreCase) => MouseAction.RightClick,

0 commit comments

Comments
 (0)