Skip to content

Commit

Permalink
Refactor converters with early returns (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
apman authored Dec 16, 2024
1 parent d958c22 commit ba430c0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@ public class SelectedIndexToPopupOffsetConverter : IMultiValueConverter
{
public object Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{
double offset = 0;
if (values is not [int index, int rowHeight, int initialBottomMargin, double maxDropDownHeight, int popupTrimHeight]
|| index < 0) return 0D;

if (values is [int index, int rowHeight, int initialBottomMargin, double maxDropDownHeight, int popupTrimHeight] &&
index >= 0)
{
var effectivePopupHeight = maxDropDownHeight - popupTrimHeight;
var baseOffset = (index + 1) * -rowHeight - initialBottomMargin;
offset = -effectivePopupHeight < baseOffset ? baseOffset : -effectivePopupHeight;
}

return offset;
var effectivePopupHeight = maxDropDownHeight - popupTrimHeight;
double baseOffset = (index + 1) * -rowHeight - initialBottomMargin;
return -effectivePopupHeight < baseOffset ? baseOffset : -effectivePopupHeight;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ public class TotalWidthConverter : IMultiValueConverter
{
public object Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
{
const double defaultWidth = 200;
if (values[0] is double width && values[1] is int sideMargin)
return width + 2 * sideMargin;
return defaultWidth;
if (values[0] is not double width || values[1] is not int sideMargin) return 200D;

return width + 2 * sideMargin;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
Expand Down

0 comments on commit ba430c0

Please sign in to comment.