Skip to content

Commit 185695b

Browse files
committed
Code cleanup: Flow.Launcher/Converters
1 parent c7bb633 commit 185695b

10 files changed

+101
-176
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: 14 additions & 44 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 switch
1212
{
13-
if (value is true)
14-
{
15-
return Visibility.Collapsed;
16-
}
17-
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-
}
13+
true when parameter is not null => Visibility.Collapsed,
14+
_ when parameter is not null => Visibility.Visible,
15+
16+
true => Visibility.Visible,
17+
_ => 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)
29+
return value switch
4430
{
45-
if (value is true)
46-
{
47-
return 0;
48-
}
49-
50-
else
51-
{
52-
return 5;
53-
}
54-
}
55-
else
56-
{
57-
if (value is true)
58-
{
59-
return 5;
60-
}
61-
62-
else
63-
{
64-
return 0;
65-
}
66-
}
31+
true when parameter is not null => 0,
32+
_ when parameter is not null => 5,
33+
34+
true => 5,
35+
_ => 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 half])
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 half ? 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: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,20 @@ 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))
15+
if (
16+
values.Length != 3 ||
17+
values[0] is not TextBox queryTextBox ||
18+
values[1] is null ||
19+
values[2] is not string queryText ||
20+
string.IsNullOrEmpty(queryText)
21+
)
2422
return string.Empty;
25-
26-
// second prop is the current selected item result
27-
var val = values[1];
28-
if (val == null)
29-
{
30-
return string.Empty;
31-
}
32-
if (!(val is ResultViewModel))
33-
{
34-
return System.Windows.Data.Binding.DoNothing;
35-
}
23+
24+
if (values[1] is not ResultViewModel selectedItem)
25+
return Binding.DoNothing;
3626

3727
try
3828
{
39-
var selectedItem = (ResultViewModel)val;
4029

4130
var selectedResult = selectedItem.Result;
4231
var selectedResultActionKeyword = string.IsNullOrEmpty(selectedResult.ActionKeywordAssigned) ? "" : selectedResult.ActionKeywordAssigned + " ";
@@ -50,17 +39,15 @@ public object Convert(object[] values, Type targetType, object parameter, Cultur
5039
// When user typed lower case and result title is uppercase, we still want to display suggestion
5140
selectedItem.QuerySuggestionText = queryText + selectedResultPossibleSuggestion.Substring(queryText.Length);
5241

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);
42+
// Check if Text will be larger than our QueryTextBox
43+
Typeface typeface = new Typeface(queryTextBox.FontFamily, queryTextBox.FontStyle, queryTextBox.FontWeight, queryTextBox.FontStretch);
5544
// 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);
45+
var ft = new FormattedText(queryTextBox.Text, CultureInfo.DefaultThreadCurrentCulture, System.Windows.FlowDirection.LeftToRight, typeface, queryTextBox.FontSize, Brushes.Black);
5746

58-
var offset = QueryTextBox.Padding.Right;
47+
var offset = queryTextBox.Padding.Right;
5948

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

6552
return selectedItem.QuerySuggestionText;
6653
}

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)