From d2514a319338542825e756a3cf4eeb1fba1e96dc Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 30 Aug 2024 12:45:12 +0200 Subject: [PATCH 1/7] Simplify ConvertTo functionality --- .../Input/Command/MouseActionConverter.cs | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs index a94410caced..9e0bae40f64 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs @@ -114,35 +114,29 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul { ArgumentNullException.ThrowIfNull(destinationType); - if (destinationType == typeof(string) && value != null) + if (destinationType == typeof(string) && value is not null) { - MouseAction mouseActionValue = (MouseAction)value ; - if (MouseActionConverter.IsDefinedMouseAction(mouseActionValue)) + return (MouseAction)value switch { - string mouseAction = null; - switch (mouseActionValue) - { - case MouseAction.None : mouseAction=String.Empty; break; - case MouseAction.LeftClick : mouseAction="LeftClick"; break; - case MouseAction.RightClick : mouseAction="RightClick"; break; - case MouseAction.MiddleClick : mouseAction="MiddleClick"; break; - case MouseAction.WheelClick : mouseAction="WheelClick"; break; - case MouseAction.LeftDoubleClick : mouseAction="LeftDoubleClick"; break; - case MouseAction.RightDoubleClick : mouseAction="RightDoubleClick"; break; - case MouseAction.MiddleDoubleClick: mouseAction="MiddleDoubleClick"; break; - } - if (mouseAction != null) - return mouseAction; - } - throw new InvalidEnumArgumentException("value", (int)mouseActionValue, typeof(MouseAction)); + MouseAction.None => string.Empty, + MouseAction.LeftClick => "LeftClick", + MouseAction.RightClick => "RightClick", + MouseAction.MiddleClick => "MiddleClick", + MouseAction.WheelClick => "WheelClick", + MouseAction.LeftDoubleClick => "LeftDoubleClick", + MouseAction.RightDoubleClick => "RightDoubleClick", + MouseAction.MiddleDoubleClick => "MiddleDoubleClick", + _ => throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(MouseAction)) + }; } + throw GetConvertToException(value,destinationType); } - // Helper like Enum.IsDefined, for MouseAction. + // Helper like Enum.IsDefined, for MouseAction. internal static bool IsDefinedMouseAction(MouseAction mouseAction) { - return (mouseAction >= MouseAction.None && mouseAction <= MouseAction.MiddleDoubleClick); + return mouseAction >= MouseAction.None && mouseAction <= MouseAction.MiddleDoubleClick; } } } From 43bdfafcc8bf482656138de310744fc4c7088e17 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 30 Aug 2024 12:52:13 +0200 Subject: [PATCH 2/7] remove indendation, swap condition --- .../Input/Command/MouseActionConverter.cs | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs index 9e0bae40f64..c0a586aba63 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs @@ -114,23 +114,21 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul { ArgumentNullException.ThrowIfNull(destinationType); - if (destinationType == typeof(string) && value is not null) - { - return (MouseAction)value switch - { - MouseAction.None => string.Empty, - MouseAction.LeftClick => "LeftClick", - MouseAction.RightClick => "RightClick", - MouseAction.MiddleClick => "MiddleClick", - MouseAction.WheelClick => "WheelClick", - MouseAction.LeftDoubleClick => "LeftDoubleClick", - MouseAction.RightDoubleClick => "RightDoubleClick", - MouseAction.MiddleDoubleClick => "MiddleDoubleClick", - _ => throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(MouseAction)) - }; - } + if (value is null || destinationType != typeof(string)) + throw GetConvertToException(value, destinationType); - throw GetConvertToException(value,destinationType); + return (MouseAction)value switch + { + MouseAction.None => string.Empty, + MouseAction.LeftClick => "LeftClick", + MouseAction.RightClick => "RightClick", + MouseAction.MiddleClick => "MiddleClick", + MouseAction.WheelClick => "WheelClick", + MouseAction.LeftDoubleClick => "LeftDoubleClick", + MouseAction.RightDoubleClick => "RightDoubleClick", + MouseAction.MiddleDoubleClick => "MiddleDoubleClick", + _ => throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(MouseAction)) + }; } // Helper like Enum.IsDefined, for MouseAction. From 1a70d2b9a32bc762118249798ba3fddc54e0e3b0 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 30 Aug 2024 12:55:30 +0200 Subject: [PATCH 3/7] Remove allocations on ConvertFrom, unnecessary string uppercasing --- .../Input/Command/MouseActionConverter.cs | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs index c0a586aba63..6467d62b4f2 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs @@ -76,30 +76,23 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati /// public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source) { - if (source != null && source is string) + if (source is not string mouseAction) + throw GetConvertFromException(source); + + ReadOnlySpan mouseActionToken = mouseAction.AsSpan().Trim(); + return mouseActionToken switch { - string mouseActionToken = ((string)source).Trim(); - mouseActionToken = mouseActionToken.ToUpper(CultureInfo.InvariantCulture); - if (mouseActionToken == String.Empty) - return MouseAction.None; - - MouseAction mouseAction = MouseAction.None; - switch (mouseActionToken) - { - case "NONE" : mouseAction = MouseAction.None; break; - case "LEFTCLICK" : mouseAction = MouseAction.LeftClick; break; - case "RIGHTCLICK" : mouseAction = MouseAction.RightClick; break; - case "MIDDLECLICK" : mouseAction = MouseAction.MiddleClick; break; - case "WHEELCLICK" : mouseAction = MouseAction.WheelClick; break; - case "LEFTDOUBLECLICK" : mouseAction = MouseAction.LeftDoubleClick; break; - case "RIGHTDOUBLECLICK" : mouseAction = MouseAction.RightDoubleClick; break; - case "MIDDLEDOUBLECLICK": mouseAction = MouseAction.MiddleDoubleClick; break; - default : - throw new NotSupportedException(SR.Format(SR.Unsupported_MouseAction, mouseActionToken)); - } - return mouseAction; - } - throw GetConvertFromException(source); + _ when mouseActionToken.IsEmpty => MouseAction.None, //Special casing as produced by "ConvertTo" + _ when mouseActionToken.Equals("None", StringComparison.OrdinalIgnoreCase) => MouseAction.None, + _ when mouseActionToken.Equals("LeftClick", StringComparison.OrdinalIgnoreCase) => MouseAction.LeftClick, + _ when mouseActionToken.Equals("RightClick", StringComparison.OrdinalIgnoreCase) => MouseAction.RightClick, + _ when mouseActionToken.Equals("MiddleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.MiddleClick, + _ when mouseActionToken.Equals("WheelClick", StringComparison.OrdinalIgnoreCase) => MouseAction.WheelClick, + _ when mouseActionToken.Equals("LeftDoubleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.LeftDoubleClick, + _ when mouseActionToken.Equals("RightDoubleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.RightDoubleClick, + _ when mouseActionToken.Equals("MiddleDoubleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.MiddleDoubleClick, + _ => throw new NotSupportedException(SR.Format(SR.Unsupported_MouseAction, mouseActionToken.ToString())) + }; } /// From ae2c88d3446534496f83e7adbadc14be7de25191 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 30 Aug 2024 13:04:12 +0200 Subject: [PATCH 4/7] Documentation/formatting changes only --- .../Input/Command/MouseActionConverter.cs | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs index 6467d62b4f2..b7531ad87cb 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs @@ -18,7 +18,7 @@ using System.Windows.Input; using MS.Utility; -using SR=MS.Internal.PresentationCore.SR; +using SR = MS.Internal.PresentationCore.SR; namespace System.Windows.Input { @@ -28,11 +28,11 @@ namespace System.Windows.Input public class MouseActionConverter : TypeConverter { /// - /// CanConvertFrom - Used to check whether we can convert a string into a MouseAction + /// Used to check whether we can convert a into a . /// ///ITypeDescriptorContext ///type to convert from - ///true if the given type can be converted, false otherwise + /// if the given can be converted from, otherwise. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { // We can only handle string. @@ -46,13 +46,12 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT } } - - /// - ///TypeConverter method override. - /// - ///ITypeDescriptorContext - ///Type to convert to - ///true if conversion is possible + /// + /// Used to check whether we can convert specified value to . + /// + /// ITypeDescriptorContext + /// Type to convert to + /// if conversion to is possible, otherwise. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { // We can convert to an InstanceDescriptor or to a string. @@ -61,19 +60,19 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati // When invoked by the serialization engine we can convert to string only for known type if (context != null && context.Instance != null) { - return (MouseActionConverter.IsDefinedMouseAction((MouseAction)context.Instance)); - } + return IsDefinedMouseAction((MouseAction)context.Instance); + } } return false; } /// - /// ConvertFrom() + /// Converts of type to its represensation. /// /// Parser Context /// Culture Info /// MouseAction String - /// + /// A representing the specified by . public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source) { if (source is not string mouseAction) @@ -92,17 +91,17 @@ _ when mouseActionToken.Equals("LeftDoubleClick", StringComparison.OrdinalIgnore _ when mouseActionToken.Equals("RightDoubleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.RightDoubleClick, _ when mouseActionToken.Equals("MiddleDoubleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.MiddleDoubleClick, _ => throw new NotSupportedException(SR.Format(SR.Unsupported_MouseAction, mouseActionToken.ToString())) - }; + }; } /// - /// ConvertTo() + /// Converts a of to its represensation. /// /// Serialization Context /// Culture Info /// MouseAction value /// Type to Convert - /// string if parameter is a MouseAction + /// A representing the specified by . public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) { ArgumentNullException.ThrowIfNull(destinationType); From 4152445ea0d15b923b38662672efa62cfb8e3e3e Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 30 Aug 2024 13:08:11 +0200 Subject: [PATCH 5/7] Simplify CanConvertFrom/CanConvertTo --- .../Input/Command/MouseActionConverter.cs | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs index b7531ad87cb..d98c26d6055 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs @@ -35,15 +35,8 @@ public class MouseActionConverter : TypeConverter /// if the given can be converted from, otherwise. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { - // We can only handle string. - if (sourceType == typeof(string)) - { - return true; - } - else - { - return false; - } + // We can only handle string + return sourceType == typeof(string); } /// @@ -54,16 +47,16 @@ public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceT /// if conversion to is possible, otherwise. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) { - // We can convert to an InstanceDescriptor or to a string. - if (destinationType == typeof(string)) - { - // When invoked by the serialization engine we can convert to string only for known type - if (context != null && context.Instance != null) - { - return IsDefinedMouseAction((MouseAction)context.Instance); - } - } - return false; + // We can convert to an InstanceDescriptor or to a string + if (destinationType != typeof(string)) + return false; + + // When invoked by the serialization engine we can convert to string only for known type + if (context is null || context.Instance is null) + return false; + + // Make sure the value falls within defined set + return IsDefinedMouseAction((MouseAction)context.Instance); } /// @@ -81,7 +74,7 @@ public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo c ReadOnlySpan mouseActionToken = mouseAction.AsSpan().Trim(); return mouseActionToken switch { - _ when mouseActionToken.IsEmpty => MouseAction.None, //Special casing as produced by "ConvertTo" + _ when mouseActionToken.IsEmpty => MouseAction.None, // Special casing as produced by "ConvertTo" _ when mouseActionToken.Equals("None", StringComparison.OrdinalIgnoreCase) => MouseAction.None, _ when mouseActionToken.Equals("LeftClick", StringComparison.OrdinalIgnoreCase) => MouseAction.LeftClick, _ when mouseActionToken.Equals("RightClick", StringComparison.OrdinalIgnoreCase) => MouseAction.RightClick, From 4cab64a9ca5dc8cb20b5db74f17b35ea6b160683 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 30 Aug 2024 13:09:11 +0200 Subject: [PATCH 6/7] Remove redundant usings --- .../System/Windows/Input/Command/MouseActionConverter.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs index d98c26d6055..15300b3b8a9 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs @@ -9,21 +9,15 @@ // to the *Type* that the string represents // -using System; using System.ComponentModel; // for TypeConverter using System.Globalization; // for CultureInfo -using System.Reflection; -using MS.Internal; -using System.Windows; -using System.Windows.Input; -using MS.Utility; using SR = MS.Internal.PresentationCore.SR; namespace System.Windows.Input { /// - /// MouseAction - Converter class for converting between a string and the Type of a MouseAction + /// Converter class for converting between a and . /// public class MouseActionConverter : TypeConverter { From ee01b27d1090d220df9ea9698ce9752a9e47f707 Mon Sep 17 00:00:00 2001 From: h3xds1nz Date: Fri, 30 Aug 2024 13:23:59 +0200 Subject: [PATCH 7/7] Add documentation for IsDefinedMouseAction too --- .../System/Windows/Input/Command/MouseActionConverter.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs index 15300b3b8a9..a6612b9724d 100644 --- a/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs +++ b/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Command/MouseActionConverter.cs @@ -110,7 +110,11 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul }; } - // Helper like Enum.IsDefined, for MouseAction. + /// + /// Helper function similar to , just lighter and faster. + /// + /// The value to test against. + /// if falls in enumeration range, otherwise. internal static bool IsDefinedMouseAction(MouseAction mouseAction) { return mouseAction >= MouseAction.None && mouseAction <= MouseAction.MiddleDoubleClick;