Skip to content

Commit

Permalink
Converted a few if towers for pattern matching in `SimpleEffectDial…
Browse files Browse the repository at this point in the history
…og` to `switch` statements
  • Loading branch information
Lehonti Ramos committed Aug 26, 2023
1 parent 652211c commit 4fa0fb8
Showing 1 changed file with 59 additions and 37 deletions.
96 changes: 59 additions & 37 deletions Pinta.Gui.Widgets/Dialogs/SimpleEffectDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 131,20 @@ private void BuildDialog (IAddinLocalizer localizer)
var attrs = mi.GetCustomAttributes (false);

foreach (var attr in attrs) {
if (attr is SkipAttribute)
skip = true;
else if (attr is CaptionAttribute)
caption = ((CaptionAttribute) attr).Caption;
else if (attr is HintAttribute)
hint = ((HintAttribute) attr).Hint;
else if (attr is StaticListAttribute)
combo = true;

switch (attr) {
case SkipAttribute:
skip = true;
break;
case CaptionAttribute captionAttr:
caption = captionAttr.Caption;
break;
case HintAttribute hintAttr:
hint = hintAttr.Hint;
break;
case StaticListAttribute:
combo = true;
break;
}
}

if (skip || string.Compare (mi.Name, "IsDefault", true) == 0)
Expand Down Expand Up @@ -254,14 259,20 @@ private HScaleSpinButtonWidget CreateDoubleSlider (string caption, object o, Mem
var digits_value = 2;

foreach (var attr in attributes) {
if (attr is MinimumValueAttribute min)
min_value = min.Value;
else if (attr is MaximumValueAttribute max)
max_value = max.Value;
else if (attr is IncrementValueAttribute inc)
inc_value = inc.Value;
else if (attr is DigitsValueAttribute digits)
digits_value = digits.Value;
switch (attr) {
case MinimumValueAttribute min:
min_value = min.Value;
break;
case MaximumValueAttribute max:
max_value = max.Value;
break;
case IncrementValueAttribute inc:
inc_value = inc.Value;
break;
case DigitsValueAttribute digits:
digits_value = digits.Value;
break;
}
}

var widget = new HScaleSpinButtonWidget {
Expand Down Expand Up @@ -293,14 304,20 @@ private HScaleSpinButtonWidget CreateSlider (string caption, object o, MemberInf
var digits_value = 0;

foreach (var attr in attributes) {
if (attr is MinimumValueAttribute min)
min_value = min.Value;
else if (attr is MaximumValueAttribute max)
max_value = max.Value;
else if (attr is IncrementValueAttribute inc)
inc_value = inc.Value;
else if (attr is DigitsValueAttribute digits)
digits_value = digits.Value;
switch (attr) {
case MinimumValueAttribute min:
min_value = min.Value;
break;
case MaximumValueAttribute max:
max_value = max.Value;
break;
case IncrementValueAttribute inc:
inc_value = inc.Value;
break;
case DigitsValueAttribute digits:
digits_value = digits.Value;
break;
}
}

var widget = new HScaleSpinButtonWidget {
Expand Down Expand Up @@ -427,12 444,15 @@ private void SetValue (MemberInfo mi, object o, object val)
{
string? fieldName = null;

if (mi is FieldInfo fi) {
fi.SetValue (o, val);
fieldName = fi.Name;
} else if (mi is PropertyInfo pi) {
pi.GetSetMethod ()?.Invoke (o, new object[] { val });
fieldName = pi.Name;
switch (mi) {
case FieldInfo fi:
fi.SetValue (o, val);
fieldName = fi.Name;
break;
case PropertyInfo pi:
pi.GetSetMethod ()?.Invoke (o, new object[] { val });
fieldName = pi.Name;
break;
}

EffectDataChanged?.Invoke (this, new PropertyChangedEventArgs (fieldName));
Expand All @@ -441,12 461,14 @@ private void SetValue (MemberInfo mi, object o, object val)
// Returns the type for fields and properties and null for everything else
private static Type? GetTypeForMember (MemberInfo mi)
{
if (mi is FieldInfo fi)
return fi.FieldType;
else if (mi is PropertyInfo pi)
return pi.PropertyType;

return null;
switch (mi) {
case FieldInfo fi:
return fi.FieldType;
case PropertyInfo pi:
return pi.PropertyType;
default:
return null;
}
}

private static string MakeCaption (string name)
Expand Down

0 comments on commit 4fa0fb8

Please sign in to comment.