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 a94410cac..a6612b972 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,140 +9,115 @@ // 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; +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 { /// - /// 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. - if (sourceType == typeof(string)) - { - return true; - } - else - { - return false; - } - } - - - /// - ///TypeConverter method override. - /// - ///ITypeDescriptorContext - ///Type to convert to - ///true if conversion is possible - 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 (MouseActionConverter.IsDefinedMouseAction((MouseAction)context.Instance)); - } - } - return false; + // We can only handle string + return sourceType == typeof(string); } /// - /// ConvertFrom() + /// 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 + 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); + } + + /// + /// 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 != 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())) + }; } /// - /// 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); - if (destinationType == typeof(string) && value != null) + if (value is null || destinationType != typeof(string)) + throw GetConvertToException(value, destinationType); + + return (MouseAction)value switch { - MouseAction mouseActionValue = (MouseAction)value ; - if (MouseActionConverter.IsDefinedMouseAction(mouseActionValue)) - { - 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)); - } - throw GetConvertToException(value,destinationType); + 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. + /// + /// 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); + return mouseAction >= MouseAction.None && mouseAction <= MouseAction.MiddleDoubleClick; } } }