Skip to content

Commit 1ad764a

Browse files
committed
Merge pull request Flow-Launcher#3765 from Flow-Launcher/code_quality
1 parent 87e39df commit 1ad764a

File tree

6 files changed

+69
-50
lines changed

6 files changed

+69
-50
lines changed

Plugins/Flow.Launcher.Plugin.Calculator/DecimalSeparator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ namespace Flow.Launcher.Plugin.Calculator
66
[TypeConverter(typeof(LocalizationConverter))]
77
public enum DecimalSeparator
88
{
9-
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_use_system_locale")]
9+
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_separator_use_system_locale")]
1010
UseSystemLocale,
11-
12-
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_dot")]
13-
Dot,
14-
15-
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_seperator_comma")]
11+
12+
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_separator_dot")]
13+
Dot,
14+
15+
[LocalizedDescription("flowlauncher_plugin_calculator_decimal_separator_comma")]
1616
Comma
1717
}
1818
}

Plugins/Flow.Launcher.Plugin.Calculator/Languages/en.xaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
<system:String x:Key="flowlauncher_plugin_caculator_plugin_name">Calculator</system:String>
77
<system:String x:Key="flowlauncher_plugin_caculator_plugin_description">Allows to do mathematical calculations.(Try 5*3-2 in Flow Launcher)</system:String>
88
<system:String x:Key="flowlauncher_plugin_calculator_copy_number_to_clipboard">Copy this number to the clipboard</system:String>
9-
<system:String x:Key="flowlauncher_plugin_calculator_output_decimal_seperator">Decimal separator</system:String>
10-
<system:String x:Key="flowlauncher_plugin_calculator_output_decimal_seperator_help">The decimal separator to be used in the output.</system:String>
11-
<system:String x:Key="flowlauncher_plugin_calculator_decimal_seperator_use_system_locale">Use system locale</system:String>
12-
<system:String x:Key="flowlauncher_plugin_calculator_decimal_seperator_comma">Comma (,)</system:String>
13-
<system:String x:Key="flowlauncher_plugin_calculator_decimal_seperator_dot">Dot (.)</system:String>
9+
<system:String x:Key="flowlauncher_plugin_calculator_output_decimal_separator">Decimal separator</system:String>
10+
<system:String x:Key="flowlauncher_plugin_calculator_output_decimal_separator_help">The decimal separator to be used in the output.</system:String>
11+
<system:String x:Key="flowlauncher_plugin_calculator_decimal_separator_use_system_locale">Use system locale</system:String>
12+
<system:String x:Key="flowlauncher_plugin_calculator_decimal_separator_comma">Comma (,)</system:String>
13+
<system:String x:Key="flowlauncher_plugin_calculator_decimal_separator_dot">Dot (.)</system:String>
1414
<system:String x:Key="flowlauncher_plugin_calculator_max_decimal_places">Max. decimal places</system:String>
1515
<system:String x:Key="flowlauncher_plugin_calculator_failed_to_copy">Copy failed, please try later</system:String>
1616
</ResourceDictionary>

Plugins/Flow.Launcher.Plugin.Calculator/Main.cs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,16 @@ namespace Flow.Launcher.Plugin.Calculator
1212
{
1313
public class Main : IPlugin, IPluginI18n, ISettingProvider
1414
{
15-
private static readonly Regex RegValidExpressChar = new Regex(
16-
@"^(" +
17-
@"ceil|floor|exp|pi|e|max|min|det|abs|log|ln|sqrt|" +
18-
@"sin|cos|tan|arcsin|arccos|arctan|" +
19-
@"eigval|eigvec|eig|sum|polar|plot|round|sort|real|zeta|" +
20-
@"bin2dec|hex2dec|oct2dec|" +
21-
@"factorial|sign|isprime|isinfty|" +
22-
@"==|~=|&&|\|\||(?:\<|\>)=?|" +
23-
@"[ei]|[0-9]|0x[\da-fA-F]+|0b[01]+|0o[0-7]+|" +
24-
@"[\+\%\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
25-
@")+$", RegexOptions.Compiled);
26-
private static readonly Regex RegBrackets = new Regex(@"[\(\)\[\]]", RegexOptions.Compiled);
15+
private static readonly Regex RegValidExpressChar = MainRegexHelper.GetRegValidExpressChar();
16+
private static readonly Regex RegBrackets = MainRegexHelper.GetRegBrackets();
2717
private static Engine MagesEngine;
28-
private const string comma = ",";
29-
private const string dot = ".";
18+
private const string Comma = ",";
19+
private const string Dot = ".";
3020

31-
private PluginInitContext Context { get; set; }
21+
internal static PluginInitContext Context { get; set; } = null!;
3222

33-
private static Settings _settings;
34-
private static SettingsViewModel _viewModel;
23+
private Settings _settings;
24+
private SettingsViewModel _viewModel;
3525

3626
public void Init(PluginInitContext context)
3727
{
@@ -76,9 +66,10 @@ public List<Result> Query(Query query)
7666
decimal roundedResult = Math.Round(Convert.ToDecimal(result), _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero);
7767
string newResult = ChangeDecimalSeparator(roundedResult, GetDecimalSeparator());
7868

79-
return
80-
[
81-
new() {
69+
return new List<Result>
70+
{
71+
new Result
72+
{
8273
Title = newResult,
8374
IcoPath = "Images/calculator.png",
8475
Score = 300,
@@ -98,7 +89,7 @@ public List<Result> Query(Query query)
9889
}
9990
}
10091
}
101-
];
92+
};
10293
}
10394
}
10495
catch (Exception)
@@ -127,16 +118,16 @@ private bool CanCalculate(Query query)
127118
return false;
128119
}
129120

130-
if ((query.Search.Contains(dot) && GetDecimalSeparator() != dot) ||
131-
(query.Search.Contains(comma) && GetDecimalSeparator() != comma))
121+
if ((query.Search.Contains(Dot) && GetDecimalSeparator() != Dot) ||
122+
(query.Search.Contains(Comma) && GetDecimalSeparator() != Comma))
132123
return false;
133124

134125
return true;
135126
}
136127

137-
private string ChangeDecimalSeparator(decimal value, string newDecimalSeparator)
128+
private static string ChangeDecimalSeparator(decimal value, string newDecimalSeparator)
138129
{
139-
if (String.IsNullOrEmpty(newDecimalSeparator))
130+
if (string.IsNullOrEmpty(newDecimalSeparator))
140131
{
141132
return value.ToString();
142133
}
@@ -148,19 +139,19 @@ private string ChangeDecimalSeparator(decimal value, string newDecimalSeparator)
148139
return value.ToString(numberFormatInfo);
149140
}
150141

151-
private static string GetDecimalSeparator()
142+
private string GetDecimalSeparator()
152143
{
153144
string systemDecimalSeparator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
154145
return _settings.DecimalSeparator switch
155146
{
156147
DecimalSeparator.UseSystemLocale => systemDecimalSeparator,
157-
DecimalSeparator.Dot => dot,
158-
DecimalSeparator.Comma => comma,
148+
DecimalSeparator.Dot => Dot,
149+
DecimalSeparator.Comma => Comma,
159150
_ => systemDecimalSeparator,
160151
};
161152
}
162153

163-
private bool IsBracketComplete(string query)
154+
private static bool IsBracketComplete(string query)
164155
{
165156
var matchs = RegBrackets.Matches(query);
166157
var leftBracketCount = 0;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace Flow.Launcher.Plugin.Calculator;
4+
5+
internal static partial class MainRegexHelper
6+
{
7+
8+
[GeneratedRegex(@"[\(\)\[\]]", RegexOptions.Compiled)]
9+
public static partial Regex GetRegBrackets();
10+
11+
[GeneratedRegex(@"^(" +
12+
@"ceil|floor|exp|pi|e|max|min|det|abs|log|ln|sqrt|" +
13+
@"sin|cos|tan|arcsin|arccos|arctan|" +
14+
@"eigval|eigvec|eig|sum|polar|plot|round|sort|real|zeta|" +
15+
@"bin2dec|hex2dec|oct2dec|" +
16+
@"factorial|sign|isprime|isinfty|" +
17+
@"==|~=|&&|\|\||(?:\<|\>)=?|" +
18+
@"[ei]|[0-9]|0x[\da-fA-F]+|0b[01]+|0o[0-7]+|" +
19+
@"[\+\%\-\*\/\^\., ""]|[\(\)\|\!\[\]]" +
20+
@")+$", RegexOptions.Compiled)]
21+
public static partial Regex GetRegValidExpressChar();
22+
}

Plugins/Flow.Launcher.Plugin.Calculator/Views/CalculatorSettings.xaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
xmlns:viewModels="clr-namespace:Flow.Launcher.Plugin.Calculator.ViewModels"
1111
d:DesignHeight="450"
1212
d:DesignWidth="800"
13-
Loaded="CalculatorSettings_Loaded"
1413
mc:Ignorable="d">
1514

1615
<UserControl.Resources>
@@ -33,7 +32,7 @@
3332
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
3433
VerticalAlignment="Center"
3534
FontSize="14"
36-
Text="{DynamicResource flowlauncher_plugin_calculator_output_decimal_seperator}" />
35+
Text="{DynamicResource flowlauncher_plugin_calculator_output_decimal_separator}" />
3736
<ComboBox
3837
x:Name="DecimalSeparatorComboBox"
3938
Grid.Row="0"

Plugins/Flow.Launcher.Plugin.Program/Main.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using System.Windows.Controls;
9-
using Flow.Launcher.Infrastructure.UserSettings;
109
using Flow.Launcher.Plugin.Program.Programs;
1110
using Flow.Launcher.Plugin.Program.Views;
1211
using Flow.Launcher.Plugin.Program.Views.Models;
@@ -234,13 +233,21 @@ static void MoveFile(string sourcePath, string destinationPath)
234233
}
235234
}
236235

237-
// Move old cache files to the new cache directory
238-
var oldWin32CacheFile = Path.Combine(DataLocation.CacheDirectory, $"{Win32CacheName}.cache");
239-
var newWin32CacheFile = Path.Combine(pluginCacheDirectory, $"{Win32CacheName}.cache");
240-
MoveFile(oldWin32CacheFile, newWin32CacheFile);
241-
var oldUWPCacheFile = Path.Combine(DataLocation.CacheDirectory, $"{UwpCacheName}.cache");
242-
var newUWPCacheFile = Path.Combine(pluginCacheDirectory, $"{UwpCacheName}.cache");
243-
MoveFile(oldUWPCacheFile, newUWPCacheFile);
236+
// If plugin cache directory is this: D:\\Data\\Cache\\Plugins\\Flow.Launcher.Plugin.Program
237+
// then the parent directory is: D:\\Data\\Cache
238+
// So we can use the parent of the parent directory to get the cache directory path
239+
var directoryInfo = new DirectoryInfo(pluginCacheDirectory);
240+
var cacheDirectory = directoryInfo.Parent?.Parent?.FullName;
241+
// Move old cache files to the new cache directory if cache directory exists
242+
if (!string.IsNullOrEmpty(cacheDirectory))
243+
{
244+
var oldWin32CacheFile = Path.Combine(cacheDirectory, $"{Win32CacheName}.cache");
245+
var newWin32CacheFile = Path.Combine(pluginCacheDirectory, $"{Win32CacheName}.cache");
246+
MoveFile(oldWin32CacheFile, newWin32CacheFile);
247+
var oldUWPCacheFile = Path.Combine(cacheDirectory, $"{UwpCacheName}.cache");
248+
var newUWPCacheFile = Path.Combine(pluginCacheDirectory, $"{UwpCacheName}.cache");
249+
MoveFile(oldUWPCacheFile, newUWPCacheFile);
250+
}
244251

245252
await _win32sLock.WaitAsync();
246253
_win32s = await context.API.LoadCacheBinaryStorageAsync(Win32CacheName, pluginCacheDirectory, new List<Win32>());

0 commit comments

Comments
 (0)