Skip to content

Commit

Permalink
Code cleanup: Flow.Launcher/{Converters,Helper}
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusyuriv committed Apr 11, 2024
1 parent da63003 commit 950b4c9
Show file tree
Hide file tree
Showing 23 changed files with 971 additions and 989 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ dotnet_style_prefer_conditional_expression_over_return = true:silent
###############################
# Style Definitions
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Use PascalCase for constant fields
# Use PascalCase for constant fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
Expand Down Expand Up @@ -134,7 +134,7 @@ csharp_preserve_single_line_statements = true
csharp_preserve_single_line_blocks = true
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_namespace_declarations = block_scoped:silent
csharp_style_namespace_declarations = file_scoped:silent
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = false:silent
Expand Down
49 changes: 24 additions & 25 deletions Flow.Launcher/Converters/BoolToIMEConversionModeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,38 @@
using System.Windows.Data;
using System.Windows.Input;

namespace Flow.Launcher.Converters
namespace Flow.Launcher.Converters;

internal class BoolToIMEConversionModeConverter : IValueConverter
{
internal class BoolToIMEConversionModeConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
return value switch
{
return value switch
{
true => ImeConversionModeValues.Alphanumeric,
_ => ImeConversionModeValues.DoNotCare
};
}
true => ImeConversionModeValues.Alphanumeric,
_ => ImeConversionModeValues.DoNotCare
};
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}

internal class BoolToIMEStateConverter : IValueConverter
internal class BoolToIMEStateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
return value switch
{
return value switch
{
true => InputMethodState.Off,
_ => InputMethodState.DoNotCare
};
}
true => InputMethodState.Off,
_ => InputMethodState.DoNotCare
};
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
51 changes: 25 additions & 26 deletions Flow.Launcher/Converters/BoolToVisibilityConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@
using System.Windows;
using System.Windows.Data;

namespace Flow.Launcher.Converters
namespace Flow.Launcher.Converters;

public class BoolToVisibilityConverter : IValueConverter
{
public class BoolToVisibilityConverter : IValueConverter
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
{
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
return (value, parameter) switch
{
return (value, parameter) switch
{
(true, not null) => Visibility.Collapsed,
(_, not null) => Visibility.Visible,

(true, null) => Visibility.Visible,
(_, null) => Visibility.Collapsed
};
}
(true, not null) => Visibility.Collapsed,
(_, not null) => Visibility.Visible,

public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();
(true, null) => Visibility.Visible,
(_, null) => Visibility.Collapsed
};
}

public class SplitterConverter : IValueConverter
/* Prevents the dragging part of the preview area from working when preview is turned off. */
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();
}

public class SplitterConverter : IValueConverter
/* Prevents the dragging part of the preview area from working when preview is turned off. */
{
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
{
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
return (value, parameter) switch
{
return (value, parameter) switch
{
(true, not null) => 0,
(_, not null) => 5,

(true, null) => 5,
(_, null) => 0
};
}
(true, not null) => 0,
(_, not null) => 5,

public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();
(true, null) => 5,
(_, null) => 0
};
}

public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();
}
58 changes: 28 additions & 30 deletions Flow.Launcher/Converters/BorderClipConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,41 @@

// For Clipping inside listbox item

namespace Flow.Launcher.Converters
namespace Flow.Launcher.Converters;

public class BorderClipConverter : IMultiValueConverter
{
public class BorderClipConverter : IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
if (values is not [double width, double height, CornerRadius radius])
{
if (values is not [double width, double height, CornerRadius radius])
{
return DependencyProperty.UnsetValue;
}

Path myPath = new Path();
if (width < Double.Epsilon || height < Double.Epsilon)
{
return Geometry.Empty;
}
var radiusHeight = radius.TopLeft;

// Drawing Round box for bottom round, and rect for top area of listbox.
var corner = new RectangleGeometry(new Rect(0, 0, width, height), radius.TopLeft, radius.TopLeft);
var box = new RectangleGeometry(new Rect(0, 0, width, radiusHeight), 0, 0);

GeometryGroup myGeometryGroup = new GeometryGroup();
myGeometryGroup.Children.Add(corner);
myGeometryGroup.Children.Add(box);

CombinedGeometry c1 = new CombinedGeometry(GeometryCombineMode.Union, corner, box);
myPath.Data = c1;

myPath.Data.Freeze();
return myPath.Data;
return DependencyProperty.UnsetValue;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
Path myPath = new Path();
if (width < Double.Epsilon || height < Double.Epsilon)
{
throw new NotSupportedException();
return Geometry.Empty;
}
var radiusHeight = radius.TopLeft;

// Drawing Round box for bottom round, and rect for top area of listbox.
var corner = new RectangleGeometry(new Rect(0, 0, width, height), radius.TopLeft, radius.TopLeft);
var box = new RectangleGeometry(new Rect(0, 0, width, radiusHeight), 0, 0);

GeometryGroup myGeometryGroup = new GeometryGroup();
myGeometryGroup.Children.Add(corner);
myGeometryGroup.Children.Add(box);

CombinedGeometry c1 = new CombinedGeometry(GeometryCombineMode.Union, corner, box);
myPath.Data = c1;

myPath.Data.Freeze();
return myPath.Data;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
21 changes: 10 additions & 11 deletions Flow.Launcher/Converters/DateTimeFormatToNowConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
using System.Globalization;
using System.Windows.Data;

namespace Flow.Launcher.Converters
namespace Flow.Launcher.Converters;

public class DateTimeFormatToNowConverter : IValueConverter
{
public class DateTimeFormatToNowConverter : IValueConverter
{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is not string format ? null : DateTime.Now.ToString(format);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is not string format ? null : DateTime.Now.ToString(format);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
25 changes: 12 additions & 13 deletions Flow.Launcher/Converters/DiameterToCenterPointConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
using System.Windows;
using System.Windows.Data;

namespace Flow.Launcher.Converters
namespace Flow.Launcher.Converters;

public class DiameterToCenterPointConverter : IValueConverter
{
public class DiameterToCenterPointConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
if (value is double d)
{
if (value is double d)
{
return new Point(d / 2, d / 2);
}

return new Point(0, 0);
return new Point(d / 2, d / 2);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
return new Point(0, 0);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
65 changes: 32 additions & 33 deletions Flow.Launcher/Converters/HighlightTextConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,44 @@
using System.Windows.Data;
using System.Windows.Documents;

namespace Flow.Launcher.Converters
namespace Flow.Launcher.Converters;

public class HighlightTextConverter : IMultiValueConverter
{
public class HighlightTextConverter : IMultiValueConverter
public object Convert(object[] value, Type targetType, object parameter, CultureInfo cultureInfo)
{
public object Convert(object[] value, Type targetType, object parameter, CultureInfo cultureInfo)
if (value.Length < 2)
return new Run(string.Empty);

if (value[0] is not string text)
return new Run(string.Empty);

if (value[1] is not List<int> { Count: > 0 } highlightData)
// No highlight data, just return the text
return new Run(text);

var highlightStyle = (Style)Application.Current.FindResource("HighlightStyle");
var textBlock = new Span();

for (var i = 0; i < text.Length; i++)
{
if (value.Length < 2)
return new Run(string.Empty);

if (value[0] is not string text)
return new Run(string.Empty);

if (value[1] is not List<int> { Count: > 0 } highlightData)
// No highlight data, just return the text
return new Run(text);

var highlightStyle = (Style)Application.Current.FindResource("HighlightStyle");
var textBlock = new Span();

for (var i = 0; i < text.Length; i++)
var currentCharacter = text.Substring(i, 1);
var run = new Run(currentCharacter)
{
var currentCharacter = text.Substring(i, 1);
var run = new Run(currentCharacter)
{
Style = ShouldHighlight(highlightData, i) ? highlightStyle : null
};
textBlock.Inlines.Add(run);
}
return textBlock;
Style = ShouldHighlight(highlightData, i) ? highlightStyle : null
};
textBlock.Inlines.Add(run);
}
return textBlock;
}

public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
{
return new[] { DependencyProperty.UnsetValue, DependencyProperty.UnsetValue };
}
public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
{
return new[] { DependencyProperty.UnsetValue, DependencyProperty.UnsetValue };
}

private bool ShouldHighlight(List<int> highlightData, int index)
{
return highlightData.Contains(index);
}
private bool ShouldHighlight(List<int> highlightData, int index)
{
return highlightData.Contains(index);
}
}
23 changes: 11 additions & 12 deletions Flow.Launcher/Converters/IconRadiusConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
using System.Globalization;
using System.Windows.Data;

namespace Flow.Launcher.Converters
namespace Flow.Launcher.Converters;

public class IconRadiusConverter : IMultiValueConverter
{
public class IconRadiusConverter : IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values is not [double size, bool isIconCircular])
throw new ArgumentException("IconRadiusConverter must have 2 parameters: [double, bool]");
if (values is not [double size, bool isIconCircular])
throw new ArgumentException("IconRadiusConverter must have 2 parameters: [double, bool]");

return isIconCircular ? size / 2 : size;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
return isIconCircular ? size / 2 : size;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
Loading

0 comments on commit 950b4c9

Please sign in to comment.