Skip to content
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

Refine enum conversion logic in IsEqualStateTrigger #3678

Merged
2 changes: 1 addition & 1 deletion Microsoft.Toolkit.Uwp.UI/Microsoft.Toolkit.Uwp.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>uap10.0.17763</TargetFrameworks>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<Title>Windows Community Toolkit UI</Title>
<Description>
This library provides UI components, such as XAML extensions, helpers, converters and more. It is part of the Windows Community Toolkit.
Expand Down
20 changes: 11 additions & 9 deletions Microsoft.Toolkit.Uwp.UI/Triggers/IsEqualStateTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ public object To

internal static bool AreValuesEqual(object value1, object value2, bool convertType)
{
if (value1 == value2)
if (object.Equals(value1, value2))
{
return true;
}

if (value1 != null && value2 != null && convertType)
// If they are the same type but fail with Equals check, don't bother with conversion.
if (value1 is not null && value2 is not null && convertType
&& value1.GetType() != value2.GetType())
{
// Try the conversion in both ways:
return ConvertTypeEquals(value1, value2) || ConvertTypeEquals(value2, value1);
Expand All @@ -92,14 +94,14 @@ private static bool ConvertTypeEquals(object value1, object value2)

private static object ConvertToEnum(Type enumType, object value)
{
try
// value cannot be the same type of enum now
return value switch
{
return Enum.IsDefined(enumType, value) ? Enum.ToObject(enumType, value) : null;
}
catch
{
return null;
}
string str => Enum.TryParse(enumType, str, out var e) ? e : null,
int or uint or byte or sbyte or long or ulong or short or ushort
=> Enum.ToObject(enumType, value),
Comment on lines +101 to +102
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor note: now that we no longer have Enum.IsDefined, this PR introduces a small behavior change. As in, if value happens to be a boxed number that is outside of the defined range for the current enum type, this will now still succeed and just return an undefined (boxed) enum value, whereas before this PR it'd return null instead.

I mean, this should likely not happen anyway, but just worth pointing it out.
I'm fine with just leaving up to consumers to ensure input raw numeric values are in fact in range.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's fine. As a StateTrigger this should be somethings Devs are working with in a VSM, so it'd be a bit odd to try and use something that wasn't defined?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup agreed, just figured I'd point that out to clarify since it's effectively a (small) functional change though 🙂

_ => null
};
}
}
}