Skip to content

Commit 1a70d2b

Browse files
committed
Remove allocations on ConvertFrom, unnecessary string uppercasing
1 parent 43bdfaf commit 1a70d2b

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,30 +76,23 @@ public override bool CanConvertTo(ITypeDescriptorContext context, Type destinati
7676
/// <returns></returns>
7777
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object source)
7878
{
79-
if (source != null && source is string)
79+
if (source is not string mouseAction)
80+
throw GetConvertFromException(source);
81+
82+
ReadOnlySpan<char> mouseActionToken = mouseAction.AsSpan().Trim();
83+
return mouseActionToken switch
8084
{
81-
string mouseActionToken = ((string)source).Trim();
82-
mouseActionToken = mouseActionToken.ToUpper(CultureInfo.InvariantCulture);
83-
if (mouseActionToken == String.Empty)
84-
return MouseAction.None;
85-
86-
MouseAction mouseAction = MouseAction.None;
87-
switch (mouseActionToken)
88-
{
89-
case "NONE" : mouseAction = MouseAction.None; break;
90-
case "LEFTCLICK" : mouseAction = MouseAction.LeftClick; break;
91-
case "RIGHTCLICK" : mouseAction = MouseAction.RightClick; break;
92-
case "MIDDLECLICK" : mouseAction = MouseAction.MiddleClick; break;
93-
case "WHEELCLICK" : mouseAction = MouseAction.WheelClick; break;
94-
case "LEFTDOUBLECLICK" : mouseAction = MouseAction.LeftDoubleClick; break;
95-
case "RIGHTDOUBLECLICK" : mouseAction = MouseAction.RightDoubleClick; break;
96-
case "MIDDLEDOUBLECLICK": mouseAction = MouseAction.MiddleDoubleClick; break;
97-
default :
98-
throw new NotSupportedException(SR.Format(SR.Unsupported_MouseAction, mouseActionToken));
99-
}
100-
return mouseAction;
101-
}
102-
throw GetConvertFromException(source);
85+
_ when mouseActionToken.IsEmpty => MouseAction.None, //Special casing as produced by "ConvertTo"
86+
_ when mouseActionToken.Equals("None", StringComparison.OrdinalIgnoreCase) => MouseAction.None,
87+
_ when mouseActionToken.Equals("LeftClick", StringComparison.OrdinalIgnoreCase) => MouseAction.LeftClick,
88+
_ when mouseActionToken.Equals("RightClick", StringComparison.OrdinalIgnoreCase) => MouseAction.RightClick,
89+
_ when mouseActionToken.Equals("MiddleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.MiddleClick,
90+
_ when mouseActionToken.Equals("WheelClick", StringComparison.OrdinalIgnoreCase) => MouseAction.WheelClick,
91+
_ when mouseActionToken.Equals("LeftDoubleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.LeftDoubleClick,
92+
_ when mouseActionToken.Equals("RightDoubleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.RightDoubleClick,
93+
_ when mouseActionToken.Equals("MiddleDoubleClick", StringComparison.OrdinalIgnoreCase) => MouseAction.MiddleDoubleClick,
94+
_ => throw new NotSupportedException(SR.Format(SR.Unsupported_MouseAction, mouseActionToken.ToString()))
95+
};
10396
}
10497

10598
/// <summary>

0 commit comments

Comments
 (0)