Merge pull request #2582 from Yusyuriv/code-cleanup-flow-launcher-converters

Code cleanup: Flow.Launcher/Converters
This commit is contained in:
Kevin Zhang 2024-02-25 00:22:48 -06:00 committed by GitHub
commit dfd1b8c66a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 108 additions and 176 deletions

View file

@ -9,18 +9,11 @@ namespace Flow.Launcher.Converters
{ {
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 bool v) return value switch
{ {
if (v) true => ImeConversionModeValues.Alphanumeric,
{ _ => ImeConversionModeValues.DoNotCare
return ImeConversionModeValues.Alphanumeric; };
}
else
{
return ImeConversionModeValues.DoNotCare;
}
}
return ImeConversionModeValues.DoNotCare;
} }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
@ -33,18 +26,11 @@ namespace Flow.Launcher.Converters
{ {
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 bool v) return value switch
{ {
if (v) true => InputMethodState.Off,
{ _ => InputMethodState.DoNotCare
return InputMethodState.Off; };
}
else
{
return InputMethodState.DoNotCare;
}
}
return InputMethodState.DoNotCare;
} }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

View file

@ -8,28 +8,14 @@ namespace Flow.Launcher.Converters
{ {
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture) public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
{ {
if (parameter != null) return (value, parameter) switch
{ {
if (value is true) (true, not null) => Visibility.Collapsed,
{ (_, not null) => Visibility.Visible,
return Visibility.Collapsed;
}
else (true, null) => Visibility.Visible,
{ (_, null) => Visibility.Collapsed
return Visibility.Visible; };
}
}
else {
if (value is true)
{
return Visibility.Visible;
}
else {
return Visibility.Collapsed;
}
}
} }
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException(); public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();
@ -40,30 +26,14 @@ namespace Flow.Launcher.Converters
{ {
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture) public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture)
{ {
if (parameter != null) return (value, parameter) switch
{ {
if (value is true) (true, not null) => 0,
{ (_, not null) => 5,
return 0;
}
else (true, null) => 5,
{ (_, null) => 0
return 5; };
}
}
else
{
if (value is true)
{
return 5;
}
else
{
return 0;
}
}
} }
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException(); public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException();

View file

@ -13,34 +13,31 @@ namespace Flow.Launcher.Converters
{ {
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{ {
if (values.Length == 3 && values[0] is double && values[1] is double && values[2] is CornerRadius) if (values is not [double width, double height, CornerRadius radius])
{ {
var width = (double)values[0]; return DependencyProperty.UnsetValue;
var height = (double)values[1];
Path myPath = new Path();
if (width < Double.Epsilon || height < Double.Epsilon)
{
return Geometry.Empty;
}
var radius = (CornerRadius)values[2];
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; 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;
} }
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq;
using System.Windows; using System.Windows;
using System.Windows.Data; using System.Windows.Data;
using System.Windows.Documents; using System.Windows.Documents;
@ -12,30 +11,27 @@ namespace Flow.Launcher.Converters
{ {
public object Convert(object[] value, Type targetType, object parameter, CultureInfo cultureInfo) public object Convert(object[] value, Type targetType, object parameter, CultureInfo cultureInfo)
{ {
var text = value[0] as string; if (value.Length < 2)
var highlightData = value[1] as List<int>; return new Run(string.Empty);
var textBlock = new Span(); if (value[0] is not string text)
return new Run(string.Empty);
if (highlightData == null || !highlightData.Any()) if (value[1] is not List<int> { Count: > 0 } highlightData)
{
// No highlight data, just return the text // No highlight data, just return the text
return new Run(text); return new Run(text);
}
var highlightStyle = (Style)Application.Current.FindResource("HighlightStyle");
var textBlock = new Span();
for (var i = 0; i < text.Length; i++) for (var i = 0; i < text.Length; i++)
{ {
var currentCharacter = text.Substring(i, 1); var currentCharacter = text.Substring(i, 1);
if (this.ShouldHighlight(highlightData, i)) var run = new Run(currentCharacter)
{ {
Style = ShouldHighlight(highlightData, i) ? highlightStyle : null
textBlock.Inlines.Add(new Run(currentCharacter) { Style = (Style)Application.Current.FindResource("HighlightStyle") }); };
textBlock.Inlines.Add(run);
}
else
{
textBlock.Inlines.Add(new Run(currentCharacter));
}
} }
return textBlock; return textBlock;
} }

View file

@ -8,15 +8,10 @@ namespace Flow.Launcher.Converters
{ {
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{ {
if (values.Length != 2) if (values is not [double size, bool isIconCircular])
throw new ArgumentException("IconRadiusConverter must have 2 parameters"); throw new ArgumentException("IconRadiusConverter must have 2 parameters: [double, bool]");
return values[1] switch return isIconCircular ? size / 2 : size;
{
true => (double)values[0] / 2,
false => (double)values[0],
_ => throw new ArgumentException("The second argument should be boolean", nameof(values))
};
} }
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{ {

View file

@ -22,6 +22,6 @@ namespace Flow.Launcher.Converters
return number <= MaxVisibleHotkeys ? Visibility.Visible : Visibility.Collapsed; return number <= MaxVisibleHotkeys ? Visibility.Visible : Visibility.Collapsed;
} }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException(); public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new InvalidOperationException();
} }
} }

View file

@ -1,3 +1,4 @@
using System;
using System.Globalization; using System.Globalization;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
@ -6,18 +7,19 @@ namespace Flow.Launcher.Converters
{ {
public class OrdinalConverter : IValueConverter public class OrdinalConverter : IValueConverter
{ {
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture) public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ {
if (value is ListBoxItem listBoxItem if (value is not ListBoxItem listBoxItem
&& ItemsControl.ItemsControlFromItemContainer(listBoxItem) is ListBox listBox) || ItemsControl.ItemsControlFromItemContainer(listBoxItem) is not ListBox listBox)
{ {
var res = listBox.ItemContainerGenerator.IndexFromContainer(listBoxItem) + 1; return 0;
return res == 10 ? 0 : res; // 10th item => HOTKEY+0
} }
return 0; var res = listBox.ItemContainerGenerator.IndexFromContainer(listBoxItem) + 1;
return res == 10 ? 0 : res; // 10th item => HOTKEY+0
} }
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException(); public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new InvalidOperationException();
} }
} }

View file

@ -12,32 +12,23 @@ namespace Flow.Launcher.Converters
{ {
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{ {
if (values.Length != 3) // values[0] is TextBox: The textbox displaying the autocomplete suggestion
{ // values[1] is ResultViewModel: Currently selected item in the list
return string.Empty; // values[2] is string: Query text
} if (
var QueryTextBox = values[0] as TextBox; values.Length != 3 ||
values[0] is not TextBox queryTextBox ||
var queryText = (string)values[2]; values[1] is null ||
values[2] is not string queryText ||
if (string.IsNullOrEmpty(queryText)) string.IsNullOrEmpty(queryText)
)
return string.Empty; return string.Empty;
// second prop is the current selected item result if (values[1] is not ResultViewModel selectedItem)
var val = values[1]; return Binding.DoNothing;
if (val == null)
{
return string.Empty;
}
if (!(val is ResultViewModel))
{
return System.Windows.Data.Binding.DoNothing;
}
try try
{ {
var selectedItem = (ResultViewModel)val;
var selectedResult = selectedItem.Result; var selectedResult = selectedItem.Result;
var selectedResultActionKeyword = string.IsNullOrEmpty(selectedResult.ActionKeywordAssigned) ? "" : selectedResult.ActionKeywordAssigned + " "; var selectedResultActionKeyword = string.IsNullOrEmpty(selectedResult.ActionKeywordAssigned) ? "" : selectedResult.ActionKeywordAssigned + " ";
var selectedResultPossibleSuggestion = selectedResultActionKeyword + selectedResult.Title; var selectedResultPossibleSuggestion = selectedResultActionKeyword + selectedResult.Title;
@ -50,17 +41,15 @@ namespace Flow.Launcher.Converters
// When user typed lower case and result title is uppercase, we still want to display suggestion // When user typed lower case and result title is uppercase, we still want to display suggestion
selectedItem.QuerySuggestionText = queryText + selectedResultPossibleSuggestion.Substring(queryText.Length); selectedItem.QuerySuggestionText = queryText + selectedResultPossibleSuggestion.Substring(queryText.Length);
// Check if Text will be larger then our QueryTextBox // Check if Text will be larger than our QueryTextBox
System.Windows.Media.Typeface typeface = new Typeface(QueryTextBox.FontFamily, QueryTextBox.FontStyle, QueryTextBox.FontWeight, QueryTextBox.FontStretch); Typeface typeface = new Typeface(queryTextBox.FontFamily, queryTextBox.FontStyle, queryTextBox.FontWeight, queryTextBox.FontStretch);
// TODO: Obsolete warning? // TODO: Obsolete warning?
System.Windows.Media.FormattedText ft = new FormattedText(QueryTextBox.Text, System.Globalization.CultureInfo.DefaultThreadCurrentCulture, System.Windows.FlowDirection.LeftToRight, typeface, QueryTextBox.FontSize, Brushes.Black); var ft = new FormattedText(queryTextBox.Text, CultureInfo.DefaultThreadCurrentCulture, System.Windows.FlowDirection.LeftToRight, typeface, queryTextBox.FontSize, Brushes.Black);
var offset = QueryTextBox.Padding.Right; var offset = queryTextBox.Padding.Right;
if ((ft.Width + offset) > QueryTextBox.ActualWidth || QueryTextBox.HorizontalOffset != 0) if (ft.Width + offset > queryTextBox.ActualWidth || queryTextBox.HorizontalOffset != 0)
{
return string.Empty; return string.Empty;
};
return selectedItem.QuerySuggestionText; return selectedItem.QuerySuggestionText;
} }

View file

@ -9,19 +9,17 @@ namespace Flow.Launcher.Converters
{ {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ {
var mode = parameter as string; if (parameter is not string mode || value is not string hotkeyStr)
var hotkeyStr = value as string; return null;
var converter = new KeyGestureConverter(); var converter = new KeyGestureConverter();
var key = (KeyGesture)converter.ConvertFromString(hotkeyStr); var key = (KeyGesture)converter.ConvertFromString(hotkeyStr);
if (mode == "key") return mode switch
{ {
return key.Key; "key" => key?.Key,
} "modifiers" => key?.Modifiers,
else if (mode == "modifiers") _ => null
{ };
return key.Modifiers;
}
return null;
} }
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

View file

@ -10,23 +10,22 @@ namespace Flow.Launcher.Converters
{ {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{ {
var ID = value.ToString(); var id = value?.ToString();
switch(ID) var translationKey = id switch
{ {
case PluginStoreItemViewModel.NewRelease: PluginStoreItemViewModel.NewRelease => "pluginStore_NewRelease",
return InternationalizationManager.Instance.GetTranslation("pluginStore_NewRelease"); PluginStoreItemViewModel.RecentlyUpdated => "pluginStore_RecentlyUpdated",
case PluginStoreItemViewModel.RecentlyUpdated: PluginStoreItemViewModel.None => "pluginStore_None",
return InternationalizationManager.Instance.GetTranslation("pluginStore_RecentlyUpdated"); PluginStoreItemViewModel.Installed => "pluginStore_Installed",
case PluginStoreItemViewModel.None: _ => null
return InternationalizationManager.Instance.GetTranslation("pluginStore_None"); };
case PluginStoreItemViewModel.Installed:
return InternationalizationManager.Instance.GetTranslation("pluginStore_Installed");
default:
return ID;
}
if (translationKey is null)
return id;
return InternationalizationManager.Instance.GetTranslation(translationKey);
} }
public object ConvertBack(object value, System.Type targetType, object parameter, CultureInfo culture) => throw new System.InvalidOperationException(); public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new InvalidOperationException();
} }
} }