Skip to content

Commit dfd1b8c

Browse files
authored
Merge pull request #2582 from Yusyuriv/code-cleanup-flow-launcher-converters
Code cleanup: Flow.Launcher/Converters
2 parents c7bb633 + 455b221 commit dfd1b8c

10 files changed

+106
-174
lines changed

Flow.Launcher/Converters/BoolToIMEConversionModeConverter.cs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@ internal class BoolToIMEConversionModeConverter : IValueConverter
99
{
1010
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1111
{
12-
if (value is bool v)
12+
return value switch
1313
{
14-
if (v)
15-
{
16-
return ImeConversionModeValues.Alphanumeric;
17-
}
18-
else
19-
{
20-
return ImeConversionModeValues.DoNotCare;
21-
}
22-
}
23-
return ImeConversionModeValues.DoNotCare;
14+
true => ImeConversionModeValues.Alphanumeric,
15+
_ => ImeConversionModeValues.DoNotCare
16+
};
2417
}
2518

2619
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
@@ -33,18 +26,11 @@ internal class BoolToIMEStateConverter : IValueConverter
3326
{
3427
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
3528
{
36-
if (value is bool v)
29+
return value switch
3730
{
38-
if (v)
39-
{
40-
return InputMethodState.Off;
41-
}
42-
else
43-
{
44-
return InputMethodState.DoNotCare;
45-
}
46-
}
47-
return InputMethodState.DoNotCare;
31+
true => InputMethodState.Off,
32+
_ => InputMethodState.DoNotCare
33+
};
4834
}
4935

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

Flow.Launcher/Converters/BoolToVisibilityConverter.cs

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,14 @@ public class BoolToVisibilityConverter : IValueConverter
88
{
99
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
1010
{
11-
if (parameter != null)
11+
return (value, parameter) switch
1212
{
13-
if (value is true)
14-
{
15-
return Visibility.Collapsed;
16-
}
13+
(true, not null) => Visibility.Collapsed,
14+
(_, not null) => Visibility.Visible,
1715

18-
else
19-
{
20-
return Visibility.Visible;
21-
}
22-
}
23-
else {
24-
if (value is true)
25-
{
26-
return Visibility.Visible;
27-
}
28-
29-
else {
30-
return Visibility.Collapsed;
31-
}
32-
}
16+
(true, null) => Visibility.Visible,
17+
(_, null) => Visibility.Collapsed
18+
};
3319
}
3420

3521
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();
@@ -40,30 +26,14 @@ public class SplitterConverter : IValueConverter
4026
{
4127
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
4228
{
43-
if (parameter != null)
44-
{
45-
if (value is true)
46-
{
47-
return 0;
48-
}
49-
50-
else
51-
{
52-
return 5;
53-
}
54-
}
55-
else
29+
return (value, parameter) switch
5630
{
57-
if (value is true)
58-
{
59-
return 5;
60-
}
31+
(true, not null) => 0,
32+
(_, not null) => 5,
6133

62-
else
63-
{
64-
return 0;
65-
}
66-
}
34+
(true, null) => 5,
35+
(_, null) => 0
36+
};
6737
}
6838

6939
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();

Flow.Launcher/Converters/BorderClipConverter.cs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,31 @@ public class BorderClipConverter : IMultiValueConverter
1313
{
1414
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
1515
{
16-
if (values.Length == 3 && values[0] is double && values[1] is double && values[2] is CornerRadius)
16+
if (values is not [double width, double height, CornerRadius radius])
1717
{
18-
var width = (double)values[0];
19-
var height = (double)values[1];
20-
Path myPath = new Path();
21-
if (width < Double.Epsilon || height < Double.Epsilon)
22-
{
23-
return Geometry.Empty;
24-
}
25-
var radius = (CornerRadius)values[2];
26-
var radiusHeight = radius.TopLeft;
18+
return DependencyProperty.UnsetValue;
19+
}
20+
21+
Path myPath = new Path();
22+
if (width < Double.Epsilon || height < Double.Epsilon)
23+
{
24+
return Geometry.Empty;
25+
}
26+
var radiusHeight = radius.TopLeft;
2727

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

32-
GeometryGroup myGeometryGroup = new GeometryGroup();
33-
myGeometryGroup.Children.Add(corner);
34-
myGeometryGroup.Children.Add(box);
32+
GeometryGroup myGeometryGroup = new GeometryGroup();
33+
myGeometryGroup.Children.Add(corner);
34+
myGeometryGroup.Children.Add(box);
3535

36-
CombinedGeometry c1 = new CombinedGeometry(GeometryCombineMode.Union, corner, box);
37-
myPath.Data = c1;
38-
39-
myPath.Data.Freeze();
40-
return myPath.Data;
41-
}
36+
CombinedGeometry c1 = new CombinedGeometry(GeometryCombineMode.Union, corner, box);
37+
myPath.Data = c1;
4238

43-
return DependencyProperty.UnsetValue;
39+
myPath.Data.Freeze();
40+
return myPath.Data;
4441
}
4542

4643
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)

Flow.Launcher/Converters/HighlightTextConverter.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4-
using System.Linq;
54
using System.Windows;
65
using System.Windows.Data;
76
using System.Windows.Documents;
@@ -12,30 +11,27 @@ public class HighlightTextConverter : IMultiValueConverter
1211
{
1312
public object Convert(object[] value, Type targetType, object parameter, CultureInfo cultureInfo)
1413
{
15-
var text = value[0] as string;
16-
var highlightData = value[1] as List<int>;
17-
18-
var textBlock = new Span();
19-
20-
if (highlightData == null || !highlightData.Any())
21-
{
14+
if (value.Length < 2)
15+
return new Run(string.Empty);
16+
17+
if (value[0] is not string text)
18+
return new Run(string.Empty);
19+
20+
if (value[1] is not List<int> { Count: > 0 } highlightData)
2221
// No highlight data, just return the text
2322
return new Run(text);
24-
}
23+
24+
var highlightStyle = (Style)Application.Current.FindResource("HighlightStyle");
25+
var textBlock = new Span();
2526

2627
for (var i = 0; i < text.Length; i++)
2728
{
2829
var currentCharacter = text.Substring(i, 1);
29-
if (this.ShouldHighlight(highlightData, i))
30-
{
31-
32-
textBlock.Inlines.Add(new Run(currentCharacter) { Style = (Style)Application.Current.FindResource("HighlightStyle") });
33-
34-
}
35-
else
30+
var run = new Run(currentCharacter)
3631
{
37-
textBlock.Inlines.Add(new Run(currentCharacter));
38-
}
32+
Style = ShouldHighlight(highlightData, i) ? highlightStyle : null
33+
};
34+
textBlock.Inlines.Add(run);
3935
}
4036
return textBlock;
4137
}

Flow.Launcher/Converters/IconRadiusConverter.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ public class IconRadiusConverter : IMultiValueConverter
88
{
99
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
1010
{
11-
if (values.Length != 2)
12-
throw new ArgumentException("IconRadiusConverter must have 2 parameters");
11+
if (values is not [double size, bool isIconCircular])
12+
throw new ArgumentException("IconRadiusConverter must have 2 parameters: [double, bool]");
1313

14-
return values[1] switch
15-
{
16-
true => (double)values[0] / 2,
17-
false => (double)values[0],
18-
_ => throw new ArgumentException("The second argument should be boolean", nameof(values))
19-
};
14+
return isIconCircular ? size / 2 : size;
2015
}
2116
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
2217
{

Flow.Launcher/Converters/OpenResultHotkeyVisibilityConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
2222
return number <= MaxVisibleHotkeys ? Visibility.Visible : Visibility.Collapsed;
2323
}
2424

25-
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();
25+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new InvalidOperationException();
2626
}
2727
}
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Globalization;
23
using System.Windows.Controls;
34
using System.Windows.Data;
@@ -6,18 +7,19 @@ namespace Flow.Launcher.Converters
67
{
78
public class OrdinalConverter : IValueConverter
89
{
9-
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
10+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1011
{
11-
if (value is ListBoxItem listBoxItem
12-
&& ItemsControl.ItemsControlFromItemContainer(listBoxItem) is ListBox listBox)
12+
if (value is not ListBoxItem listBoxItem
13+
|| ItemsControl.ItemsControlFromItemContainer(listBoxItem) is not ListBox listBox)
1314
{
14-
var res = listBox.ItemContainerGenerator.IndexFromContainer(listBoxItem) + 1;
15-
return res == 10 ? 0 : res; // 10th item => HOTKEY+0
15+
return 0;
1616
}
1717

18-
return 0;
18+
var res = listBox.ItemContainerGenerator.IndexFromContainer(listBoxItem) + 1;
19+
return res == 10 ? 0 : res; // 10th item => HOTKEY+0
20+
1921
}
2022

21-
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();
23+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new InvalidOperationException();
2224
}
2325
}

Flow.Launcher/Converters/QuerySuggestionBoxConverter.cs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,23 @@ public class QuerySuggestionBoxConverter : IMultiValueConverter
1212
{
1313
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
1414
{
15-
if (values.Length != 3)
16-
{
17-
return string.Empty;
18-
}
19-
var QueryTextBox = values[0] as TextBox;
20-
21-
var queryText = (string)values[2];
22-
23-
if (string.IsNullOrEmpty(queryText))
24-
return string.Empty;
25-
26-
// second prop is the current selected item result
27-
var val = values[1];
28-
if (val == null)
29-
{
15+
// values[0] is TextBox: The textbox displaying the autocomplete suggestion
16+
// values[1] is ResultViewModel: Currently selected item in the list
17+
// values[2] is string: Query text
18+
if (
19+
values.Length != 3 ||
20+
values[0] is not TextBox queryTextBox ||
21+
values[1] is null ||
22+
values[2] is not string queryText ||
23+
string.IsNullOrEmpty(queryText)
24+
)
3025
return string.Empty;
31-
}
32-
if (!(val is ResultViewModel))
33-
{
34-
return System.Windows.Data.Binding.DoNothing;
35-
}
26+
27+
if (values[1] is not ResultViewModel selectedItem)
28+
return Binding.DoNothing;
3629

3730
try
3831
{
39-
var selectedItem = (ResultViewModel)val;
40-
4132
var selectedResult = selectedItem.Result;
4233
var selectedResultActionKeyword = string.IsNullOrEmpty(selectedResult.ActionKeywordAssigned) ? "" : selectedResult.ActionKeywordAssigned + " ";
4334
var selectedResultPossibleSuggestion = selectedResultActionKeyword + selectedResult.Title;
@@ -50,17 +41,15 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
5041
// When user typed lower case and result title is uppercase, we still want to display suggestion
5142
selectedItem.QuerySuggestionText = queryText + selectedResultPossibleSuggestion.Substring(queryText.Length);
5243

53-
// Check if Text will be larger then our QueryTextBox
54-
System.Windows.Media.Typeface typeface = new Typeface(QueryTextBox.FontFamily, QueryTextBox.FontStyle, QueryTextBox.FontWeight, QueryTextBox.FontStretch);
44+
// Check if Text will be larger than our QueryTextBox
45+
Typeface typeface = new Typeface(queryTextBox.FontFamily, queryTextBox.FontStyle, queryTextBox.FontWeight, queryTextBox.FontStretch);
5546
// TODO: Obsolete warning?
56-
System.Windows.Media.FormattedText ft = new FormattedText(QueryTextBox.Text, System.Globalization.CultureInfo.DefaultThreadCurrentCulture, System.Windows.FlowDirection.LeftToRight, typeface, QueryTextBox.FontSize, Brushes.Black);
47+
var ft = new FormattedText(queryTextBox.Text, CultureInfo.DefaultThreadCurrentCulture, System.Windows.FlowDirection.LeftToRight, typeface, queryTextBox.FontSize, Brushes.Black);
5748

58-
var offset = QueryTextBox.Padding.Right;
49+
var offset = queryTextBox.Padding.Right;
5950

60-
if ((ft.Width + offset) > QueryTextBox.ActualWidth || QueryTextBox.HorizontalOffset != 0)
61-
{
51+
if (ft.Width + offset > queryTextBox.ActualWidth || queryTextBox.HorizontalOffset != 0)
6252
return string.Empty;
63-
};
6453

6554
return selectedItem.QuerySuggestionText;
6655
}

Flow.Launcher/Converters/StringToKeyBindingConverter.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ class StringToKeyBindingConverter : IValueConverter
99
{
1010
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
1111
{
12-
var mode = parameter as string;
13-
var hotkeyStr = value as string;
12+
if (parameter is not string mode || value is not string hotkeyStr)
13+
return null;
14+
1415
var converter = new KeyGestureConverter();
1516
var key = (KeyGesture)converter.ConvertFromString(hotkeyStr);
16-
if (mode == "key")
17+
return mode switch
1718
{
18-
return key.Key;
19-
}
20-
else if (mode == "modifiers")
21-
{
22-
return key.Modifiers;
23-
}
24-
return null;
19+
"key" => key?.Key,
20+
"modifiers" => key?.Modifiers,
21+
_ => null
22+
};
2523
}
2624

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

0 commit comments

Comments
 (0)