From de42343e31ff1df4e14ba5e98693df7f90bd464b Mon Sep 17 00:00:00 2001 From: JLdgu Date: Sat, 7 Sep 2024 09:53:30 +0100 Subject: [PATCH 1/4] Fix code style issues F to M Converters --- ...nesVisibilityBorderToThicknessConverter.cs | 4 ++-- .../HorizontalThicknessConverter.cs | 4 +--- .../Converters/HsbLinearGradientConverter.cs | 4 +--- .../Converters/HsbToColorConverter.cs | 4 +--- .../Converters/IsDarkConverter.cs | 4 +--- .../Converters/MathConverter.cs | 22 +++++++------------ .../Converters/MathMultipleConverter.cs | 22 +++++++------------ .../Converters/MathConverterTests.cs | 11 ++++++++++ 8 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/MaterialDesignThemes.Wpf/Converters/GridLinesVisibilityBorderToThicknessConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/GridLinesVisibilityBorderToThicknessConverter.cs index 06f0f16a72..c06f45b9c0 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/GridLinesVisibilityBorderToThicknessConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/GridLinesVisibilityBorderToThicknessConverter.cs @@ -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 { diff --git a/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs index 00911a415a..dde427b0d2 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs @@ -15,7 +15,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(); } diff --git a/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs index 73b182a453..dda8f2ff02 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs @@ -15,7 +15,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(); } diff --git a/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs index 028317c249..931e645c74 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs @@ -29,7 +29,5 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) - { - throw new NotImplementedException(); - } + => throw new NotImplementedException(); } diff --git a/src/MaterialDesignThemes.Wpf/Converters/IsDarkConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/IsDarkConverter.cs index 252aef4ed2..e99cd9403a 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/IsDarkConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/IsDarkConverter.cs @@ -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(); } diff --git a/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs index f9f64591ed..80d88199a3 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs @@ -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) { diff --git a/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs index 7bba007c20..a7ae7977fb 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs @@ -14,21 +14,15 @@ public sealed class MathMultipleConverter : IMultiValueConverter 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) diff --git a/tests/MaterialDesignThemes.Wpf.Tests/Converters/MathConverterTests.cs b/tests/MaterialDesignThemes.Wpf.Tests/Converters/MathConverterTests.cs index c24b824138..21146323f9 100644 --- a/tests/MaterialDesignThemes.Wpf.Tests/Converters/MathConverterTests.cs +++ b/tests/MaterialDesignThemes.Wpf.Tests/Converters/MathConverterTests.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Windows.Data; using MaterialDesignThemes.Wpf.Converters; namespace MaterialDesignThemes.Wpf.Tests.Converters; @@ -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); + } } From c564e59352805966275439c5b35391fec51b397e Mon Sep 17 00:00:00 2001 From: JLdgu Date: Sun, 8 Sep 2024 11:54:55 +0100 Subject: [PATCH 2/4] Add pattern matching --- .../FloatingHintClippingGridConverter.cs | 5 +--- .../FloatingHintContainerMarginConverter.cs | 18 ++++++------- ...ingHintInitialHorizontalOffsetConverter.cs | 26 +++++++++++------- ...atingHintInitialVerticalOffsetConverter.cs | 2 +- .../Converters/FloatingHintMarginConverter.cs | 5 +--- .../FloatingHintScaleTransformConverter.cs | 5 +--- .../FloatingHintTextBlockMarginConverter.cs | 14 +++++----- .../HorizontalThicknessConverter.cs | 8 +++--- .../Converters/HsbLinearGradientConverter.cs | 4 +-- .../Converters/HsbToColorConverter.cs | 16 +++++------ .../ListViewItemContainerStyleConverter.cs | 7 ++--- .../Converters/MathConverter.cs | 27 +++++++------------ .../Converters/MathMultipleConverter.cs | 5 +--- 13 files changed, 63 insertions(+), 79 deletions(-) diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintClippingGridConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintClippingGridConverter.cs index 9cd1bc7844..8c4b5da848 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintClippingGridConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintClippingGridConverter.cs @@ -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(); diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintContainerMarginConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintContainerMarginConverter.cs index 23f276561c..b5b90596a3 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintContainerMarginConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintContainerMarginConverter.cs @@ -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) diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs index b7af5d31da..530bf3a55b 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs @@ -7,16 +7,24 @@ 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 0; + + return horizontalContentAlignment switch { - return horizontalContentAlignment switch - { - HorizontalAlignment.Center => 0, - HorizontalAlignment.Right => GetRightOffset(), - _ => GetLeftOffset(), - }; - } - return 0; + HorizontalAlignment.Center => 0, + HorizontalAlignment.Right => GetRightOffset(), + _ => GetLeftOffset(), + }; double GetLeftOffset() { diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialVerticalOffsetConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialVerticalOffsetConverter.cs index 35995bf423..9d915768b7 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialVerticalOffsetConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialVerticalOffsetConverter.cs @@ -1,4 +1,4 @@ -using System.Globalization; +using System.Globalization; using System.Windows.Data; namespace MaterialDesignThemes.Wpf.Converters; diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintMarginConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintMarginConverter.cs index e37e8a4ab5..fcfd7680e7 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintMarginConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintMarginConverter.cs @@ -20,10 +20,7 @@ public class FloatingHintMarginConverter : IMultiValueConverter Thickness suffixMargin, PrefixSuffixVisibility prefixVisibility, PrefixSuffixVisibility suffixVisibility - ]) - { - return EmptyThickness; - } + ]) return EmptyThickness; double prefixTotalWidth = prefixWidth > 0 ? prefixWidth + prefixMargin.Right : 0; double suffixTotalWidth = suffixWidth > 0 ? suffixWidth + suffixMargin.Left : 0; diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintScaleTransformConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintScaleTransformConverter.cs index 73121365a6..1fbc786a07 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintScaleTransformConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintScaleTransformConverter.cs @@ -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); diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs index 97c9d20353..26fbd18a73 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs @@ -11,12 +11,14 @@ internal class FloatingHintTextBlockMarginConverter : IMultiValueConverter if (values is not [ FloatingHintHorizontalAlignment restingAlignmentOverride, - FloatingHintHorizontalAlignment floatingAlignment, HorizontalAlignment restingAlignment, - double desiredWidth, double availableWidth, double scale, double lower, double upper - ]) - { - return Transform.Identity; - } + FloatingHintHorizontalAlignment floatingAlignment, + HorizontalAlignment restingAlignment, + double desiredWidth, + double availableWidth, + double scale, + double lower, + double upper + ]) return Transform.Identity; double scaleMultiplier = upper + (lower - upper) * scale; diff --git a/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs index dde427b0d2..dbd0ff3035 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/HorizontalThicknessConverter.cs @@ -7,11 +7,9 @@ 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) diff --git a/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs index dda8f2ff02..5ee66c335d 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/HsbLinearGradientConverter.cs @@ -9,9 +9,9 @@ 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) diff --git a/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs index 931e645c74..577ee60c4a 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/HsbToColorConverter.cs @@ -9,23 +9,23 @@ 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) diff --git a/src/MaterialDesignThemes.Wpf/Converters/ListViewItemContainerStyleConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/ListViewItemContainerStyleConverter.cs index f81bda4a59..7b5d41a1ec 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/ListViewItemContainerStyleConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/ListViewItemContainerStyleConverter.cs @@ -28,12 +28,9 @@ public class ListViewGridViewConverter : IValueConverter /// 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) diff --git a/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs index 80d88199a3..ce73102754 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs @@ -10,24 +10,17 @@ public sealed class MathConverter : IValueConverter public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) { - try - { - double value1 = System.Convert.ToDouble(value, CultureInfo.InvariantCulture); - double value2 = System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture); - return Operation switch - { - 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) + if (value is not double value1 || parameter is not double value2) return Binding.DoNothing; + + return Operation switch { - 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, + }; } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) diff --git a/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs index a7ae7977fb..a0380155c6 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/MathMultipleConverter.cs @@ -9,10 +9,7 @@ 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 (!double.TryParse(value[0]!.ToString(), out double value1) || !double.TryParse(value[1]!.ToString(), out double value2)) - return 0; + if (value is not [double value1, double value2]) return Binding.DoNothing; return Operation switch { From e28a0ac940b35185af38576116d2b9c204323765 Mon Sep 17 00:00:00 2001 From: Kevin Bost Date: Wed, 13 Nov 2024 22:36:35 -0800 Subject: [PATCH 3/4] Reverting a few if states to use curly braces Doing this when the if statement is not trivial --- .../FloatingHintInitialHorizontalOffsetConverter.cs | 8 ++++++-- .../Converters/FloatingHintMarginConverter.cs | 5 ++++- .../Converters/FloatingHintTextBlockMarginConverter.cs | 5 ++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs index 530bf3a55b..4ad2d992a5 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintInitialHorizontalOffsetConverter.cs @@ -8,7 +8,8 @@ public class FloatingHintInitialHorizontalOffsetConverter : IMultiValueConverter public object? Convert(object?[]? values, Type targetType, object? parameter, CultureInfo culture) { if (values is not - [ double prefixWidth, + [ + double prefixWidth, Thickness prefixMargin, double suffixWidth, Thickness suffixMargin, @@ -17,7 +18,10 @@ public class FloatingHintInitialHorizontalOffsetConverter : IMultiValueConverter PrefixSuffixHintBehavior prefixHintBehavior, PrefixSuffixHintBehavior suffixHintBehavior, HorizontalAlignment horizontalContentAlignment - ]) return 0; + ]) + { + return 0; + } return horizontalContentAlignment switch { diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintMarginConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintMarginConverter.cs index fcfd7680e7..e37e8a4ab5 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintMarginConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintMarginConverter.cs @@ -20,7 +20,10 @@ public class FloatingHintMarginConverter : IMultiValueConverter Thickness suffixMargin, PrefixSuffixVisibility prefixVisibility, PrefixSuffixVisibility suffixVisibility - ]) return EmptyThickness; + ]) + { + return EmptyThickness; + } double prefixTotalWidth = prefixWidth > 0 ? prefixWidth + prefixMargin.Right : 0; double suffixTotalWidth = suffixWidth > 0 ? suffixWidth + suffixMargin.Left : 0; diff --git a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs index 26fbd18a73..ab2dd28dae 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/FloatingHintTextBlockMarginConverter.cs @@ -18,7 +18,10 @@ internal class FloatingHintTextBlockMarginConverter : IMultiValueConverter double scale, double lower, double upper - ]) return Transform.Identity; + ]) + { + return Transform.Identity; + } double scaleMultiplier = upper + (lower - upper) * scale; From 57f15dbaa39701085d5c5c4cceb29f0389532c99 Mon Sep 17 00:00:00 2001 From: JLdgu Date: Thu, 21 Nov 2024 11:35:34 +0000 Subject: [PATCH 4/4] Revert MathConverter DrawerHost_OpenAndClose_RaisesEvents Test failed with pattern match changes in previous commit --- .../Converters/MathConverter.cs | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs b/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs index ce73102754..80d88199a3 100644 --- a/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs +++ b/src/MaterialDesignThemes.Wpf/Converters/MathConverter.cs @@ -10,17 +10,24 @@ public sealed class MathConverter : IValueConverter public object? Convert(object? value, Type? targetType, object? parameter, CultureInfo? culture) { - if (value is not double value1 || parameter is not double value2) return Binding.DoNothing; - - return Operation switch + try + { + double value1 = System.Convert.ToDouble(value, CultureInfo.InvariantCulture); + double value2 = System.Convert.ToDouble(parameter, CultureInfo.InvariantCulture); + return Operation switch + { + 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) { - 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, - }; + return Binding.DoNothing; + } } public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)