Skip to content

Commit

Permalink
Fix code style issues (MaterialDesignInXAML#3667)
Browse files Browse the repository at this point in the history
* Fix code style issues

F to M Converters

* Add pattern matching

* Reverting a few if states to use curly braces

Doing this when the if statement is not trivial

* Revert MathConverter

DrawerHost_OpenAndClose_RaisesEvents Test failed with pattern match changes in previous commit

---------

Co-authored-by: Kevin Bost <kitokeboo@gmail.com>
  • Loading branch information
JLdgu and Keboo authored Nov 25, 2024
1 parent af27540 commit 58bd930
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ public class FloatingHintClippingGridConverter : IMultiValueConverter
{
public object? Convert(object?[] values, Type targetType, object? parameter, CultureInfo culture)
{
if (values is not [double actualWidth, double actualHeight, double floatingScale])
{
return null;
}
if (values is not [double actualWidth, double actualHeight, double floatingScale]) return null;

RectangleGeometry geometry = new(new Rect(new Point(0, 0), new Size(actualWidth, actualHeight * 2 * floatingScale)));
geometry.Freeze();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,15 @@ public class FloatingHintContainerMarginConverter : IMultiValueConverter

public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values is [double scale, Thickness floatingMargin, double floatingScale])
if (values is not [double scale, Thickness floatingMargin, double floatingScale]) return EmptyThickness;

return floatingMargin with
{
return floatingMargin with
{
Left = (floatingMargin.Left * scale) / floatingScale,
Top = (floatingMargin.Top * scale) / floatingScale,
Right = (floatingMargin.Right * scale) / floatingScale,
Bottom = (floatingMargin.Bottom * scale) / floatingScale
};
}
return EmptyThickness;
Left = (floatingMargin.Left * scale) / floatingScale,
Top = (floatingMargin.Top * scale) / floatingScale,
Right = (floatingMargin.Right * scale) / floatingScale,
Bottom = (floatingMargin.Bottom * scale) / floatingScale
};
}

public object[]? ConvertBack(object? value, Type[] targetTypes, object? parameter, CultureInfo culture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,28 @@ public class FloatingHintInitialHorizontalOffsetConverter : IMultiValueConverter
{
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values is [double prefixWidth, Thickness prefixMargin, double suffixWidth, Thickness suffixMargin, PrefixSuffixVisibility prefixVisibility, PrefixSuffixVisibility suffixVisibility, PrefixSuffixHintBehavior prefixHintBehavior, PrefixSuffixHintBehavior suffixHintBehavior, HorizontalAlignment horizontalContentAlignment])
if (values is not
[
double prefixWidth,
Thickness prefixMargin,
double suffixWidth,
Thickness suffixMargin,
PrefixSuffixVisibility prefixVisibility,
PrefixSuffixVisibility suffixVisibility,
PrefixSuffixHintBehavior prefixHintBehavior,
PrefixSuffixHintBehavior suffixHintBehavior,
HorizontalAlignment horizontalContentAlignment
])
{
return horizontalContentAlignment switch
{
HorizontalAlignment.Center => 0,
HorizontalAlignment.Right => GetRightOffset(),
_ => GetLeftOffset(),
};
return 0;
}
return 0;

return horizontalContentAlignment switch
{
HorizontalAlignment.Center => 0,
HorizontalAlignment.Right => GetRightOffset(),
_ => GetLeftOffset(),
};

double GetLeftOffset()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Globalization;
using System.Globalization;
using System.Windows.Data;

namespace MaterialDesignThemes.Wpf.Converters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ public class FloatingHintScaleTransformConverter : IMultiValueConverter
{
public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture)
{
if (values is not [double scale, double lower, double upper])
{
return Transform.Identity;
}
if (values is not [double scale, double lower, double upper]) return Transform.Identity;

double scalePercentage = upper + (lower - upper) * scale;
return new ScaleTransform(scalePercentage, scalePercentage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ internal class FloatingHintTextBlockMarginConverter : IMultiValueConverter
if (values is not
[
FloatingHintHorizontalAlignment restingAlignmentOverride,
FloatingHintHorizontalAlignment floatingAlignment, HorizontalAlignment restingAlignment,
double desiredWidth, double availableWidth, double scale, double lower, double upper
FloatingHintHorizontalAlignment floatingAlignment,
HorizontalAlignment restingAlignment,
double desiredWidth,
double availableWidth,
double scale,
double lower,
double upper
])
{
return Transform.Identity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ internal class GridLinesVisibilityBorderToThicknessConverter : IValueConverter

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is DataGridGridLinesVisibility visibility))
if (value is not DataGridGridLinesVisibility visibility)
return Binding.DoNothing;

var thickness = parameter as double? ?? GridLinesThickness;
double thickness = parameter as double? ?? GridLinesThickness;

return visibility switch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ internal class HorizontalThicknessConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Thickness thickness)
{
return new Thickness(thickness.Left, 0, thickness.Right, 0);
}
return Binding.DoNothing;
if (value is not Thickness thickness) return Binding.DoNothing;

return new Thickness(thickness.Left, 0, thickness.Right, 0);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
=> throw new NotImplementedException();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ public class HsbLinearGradientConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var v = (double)value;
if (value is not double hue) return Binding.DoNothing;

return new LinearGradientBrush(Colors.White, new Hsb(v, 1, 1).ToColor(), 0);
return new LinearGradientBrush(Colors.White, new Hsb(hue, 1, 1).ToColor(), 0);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
=> throw new NotImplementedException();
}
20 changes: 9 additions & 11 deletions src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,25 @@ public class HsbToColorConverter : IValueConverter, IMultiValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Hsb hsb) return new SolidColorBrush(hsb.ToColor());
return Binding.DoNothing;
if (value is not Hsb hsb) return Binding.DoNothing;

return new SolidColorBrush(hsb.ToColor());
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is SolidColorBrush brush) return brush.Color.ToHsb();
return Binding.DoNothing;
if (value is not SolidColorBrush brush) return Binding.DoNothing;

return brush.Color.ToHsb();
}

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var h = (double)values[0];
var s = (double)values[1];
var b = (double)values[2];
if (values is not [double hue, double saturation, double brightness]) return Binding.DoNothing;

return new SolidColorBrush(new Hsb(h, s, b).ToColor());
return new SolidColorBrush(new Hsb(hue, saturation, brightness).ToColor());
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
=> throw new NotImplementedException();
}
4 changes: 1 addition & 3 deletions src/MaterialDesignThemes.Wpf/Converters/IsDarkConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
=> throw new NotImplementedException();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ public class ListViewGridViewConverter : IValueConverter
/// <returns></returns>
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is ListView listView)
{
return listView.View != null ? ViewValue : DefaultValue;
}
if (value is not ListView listView) return value is ViewBase ? ViewValue : DefaultValue;

return value is ViewBase ? ViewValue : DefaultValue;
return listView.View != null ? ViewValue : DefaultValue;
}

public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
Expand Down
22 changes: 8 additions & 14 deletions src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,15 @@ public sealed class MathConverter : IValueConverter
{
double value1 = System.Convert.ToDouble(value, CultureInfo.InvariantCulture);
double value2 = System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture);
switch (Operation)
return Operation switch
{
case MathOperation.Add:
return value1 + value2 + Offset;
case MathOperation.Divide:
return value1 / value2 + Offset;
case MathOperation.Multiply:
return value1 * value2 + Offset;
case MathOperation.Subtract:
return value1 - value2 + Offset;
case MathOperation.Pow:
return Math.Pow(value1, value2) + Offset;
default:
return Binding.DoNothing;
}
MathOperation.Add => value1 + value2 + Offset,
MathOperation.Divide => value1 / value2 + Offset,
MathOperation.Multiply => value1 * value2 + Offset,
MathOperation.Subtract => value1 - value2 + Offset,
MathOperation.Pow => Math.Pow(value1, value2) + Offset,
_ => Binding.DoNothing,
};
}
catch (FormatException)
{
Expand Down
27 changes: 9 additions & 18 deletions src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,17 @@ public sealed class MathMultipleConverter : IMultiValueConverter

public object? Convert(object?[]? value, Type? targetType, object? parameter, CultureInfo? culture)
{
if (value is null || value.Length < 2 || value[0] is null || value[1] is null) return Binding.DoNothing;
if (value is not [double value1, double value2]) return Binding.DoNothing;

if (!double.TryParse(value[0]!.ToString(), out double value1) || !double.TryParse(value[1]!.ToString(), out double value2))
return 0;

switch (Operation)
return Operation switch
{
default:
// (case MathOperation.Add:)
return value1 + value2;
case MathOperation.Divide:
return value1 / value2;
case MathOperation.Multiply:
return value1 * value2;
case MathOperation.Subtract:
return value1 - value2;
case MathOperation.Pow:
return Math.Pow(value1, value2);
}

MathOperation.Add => value1 + value2,
MathOperation.Divide => value1 / value2,
MathOperation.Multiply => value1 * value2,
MathOperation.Subtract => value1 - value2,
MathOperation.Pow => Math.Pow(value1, value2),
_ => Binding.DoNothing
};
}

public object?[]? ConvertBack(object? value, Type[]? targetTypes, object? parameter, CultureInfo? culture)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Globalization;
using System.Windows.Data;
using MaterialDesignThemes.Wpf.Converters;

namespace MaterialDesignThemes.Wpf.Tests.Converters;
Expand All @@ -16,4 +17,14 @@ public void EnumValues_AreAllHandled(MathOperation operation)

Assert.True(converter.Convert(1.0, null, 1.0, CultureInfo.CurrentUICulture) is double);
}

[Fact]
public void NoneDoubleArguments_ShouldReturnDoNothing()
{
MathConverter converter = new();
object? actual1 = converter.Convert("", null, 1.0, CultureInfo.CurrentUICulture);
Assert.Equal(Binding.DoNothing, actual1);
object? actual2 = converter.Convert(1.0, null, "", CultureInfo.CurrentUICulture);
Assert.Equal(Binding.DoNothing, actual2);
}
}

0 comments on commit 58bd930

Please sign in to comment.