Skip to content

Commit 5b6ea73

Browse files
authored
Code cleanup & Use Flow.Launcher.Localization to improve code quality (#4009)
* Use Flow.Launcher.Localization to improve code quality * Code cleanup * Improve code quality * Improve code quality * Use internal static Context & Improve code quality * Use Flow.Launcher.Localization to improve code quality * Code cleanup * Use Flow.Launcher.Localization to improve code quality * Improve code quality * Improve code quality * Use Flow.Launcher.Localization to improve code quality * Fix logic issue * Fix the variable name typo * Fix redundant boolean cast and ensure consistent default value handling * Use Flow.Launcher.Localization to improve code quality * Revert namespace styles * Fix indent format * Revert namespace style * Fix indent format * Fix namespace style * Fix indent format * Fix indent format
1 parent 2d00ab1 commit 5b6ea73

26 files changed

+249
-270
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ public enum DecimalSeparator
77
{
88
[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_separator_use_system_locale))]
99
UseSystemLocale,
10-
10+
1111
[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_separator_dot))]
12-
Dot,
13-
12+
Dot,
13+
1414
[EnumLocalizeKey(nameof(Localize.flowlauncher_plugin_calculator_decimal_separator_comma))]
1515
Comma
1616
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
using System.Runtime.InteropServices;
66
using System.Text.RegularExpressions;
77
using System.Windows.Controls;
8-
using Mages.Core;
9-
using Flow.Launcher.Plugin.Calculator.Views;
108
using Flow.Launcher.Plugin.Calculator.ViewModels;
9+
using Flow.Launcher.Plugin.Calculator.Views;
10+
using Mages.Core;
1111

1212
namespace Flow.Launcher.Plugin.Calculator
1313
{
@@ -26,7 +26,7 @@ public class Main : IPlugin, IPluginI18n, ISettingProvider
2626
private const string IcoPath = "Images/calculator.png";
2727
private static readonly List<Result> EmptyResults = [];
2828

29-
internal static PluginInitContext Context { get; set; } = null!;
29+
internal static PluginInitContext Context { get; private set; } = null!;
3030

3131
private Settings _settings;
3232
private SettingsViewModel _viewModel;
@@ -57,10 +57,10 @@ public List<Result> Query(Query query)
5757
{
5858
var search = query.Search;
5959
bool isFunctionPresent = FunctionRegex.IsMatch(search);
60-
60+
6161
// Mages is case sensitive, so we need to convert all function names to lower case.
6262
search = FunctionRegex.Replace(search, m => m.Value.ToLowerInvariant());
63-
63+
6464
var decimalSep = GetDecimalSeparator();
6565
var groupSep = GetGroupSeparator(decimalSep);
6666
var expression = NumberRegex.Replace(search, m => NormalizeNumber(m.Value, isFunctionPresent, decimalSep, groupSep));
@@ -292,7 +292,7 @@ private static string NormalizeNumber(string numberStr, bool isFunctionPresent,
292292
{
293293
processedStr = processedStr.Replace(decimalSep, ".");
294294
}
295-
295+
296296
return processedStr;
297297
}
298298
else
@@ -310,7 +310,7 @@ private static string NormalizeNumber(string numberStr, bool isFunctionPresent,
310310
return processedStr;
311311
}
312312
}
313-
313+
314314
private static bool IsValidGrouping(string[] parts, int[] groupSizes)
315315
{
316316
if (parts.Length <= 1) return true;
@@ -326,7 +326,7 @@ private static bool IsValidGrouping(string[] parts, int[] groupSizes)
326326

327327
var lastGroupSize = groupSizes.Last();
328328
var canRepeatLastGroup = lastGroupSize != 0;
329-
329+
330330
int groupIndex = 0;
331331
for (int i = parts.Length - 1; i > 0; i--)
332332
{
@@ -335,7 +335,7 @@ private static bool IsValidGrouping(string[] parts, int[] groupSizes)
335335
{
336336
expectedSize = groupSizes[groupIndex];
337337
}
338-
else if(canRepeatLastGroup)
338+
else if (canRepeatLastGroup)
339339
{
340340
expectedSize = lastGroupSize;
341341
}
@@ -345,7 +345,7 @@ private static bool IsValidGrouping(string[] parts, int[] groupSizes)
345345
}
346346

347347
if (parts[i].Length != expectedSize) return false;
348-
348+
349349
groupIndex++;
350350
}
351351

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-

2-
namespace Flow.Launcher.Plugin.Calculator;
1+
namespace Flow.Launcher.Plugin.Calculator;
32

43
public class Settings
54
{

Plugins/Flow.Launcher.Plugin.PluginIndicator/Flow.Launcher.Plugin.PluginIndicator.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<ErrorReport>prompt</ErrorReport>
3333
<WarningLevel>4</WarningLevel>
3434
<Prefer32Bit>false</Prefer32Bit>
35+
<NoWarn>$(NoWarn);FLSG0007</NoWarn>
3536
</PropertyGroup>
3637

3738
<ItemGroup>
@@ -54,5 +55,9 @@
5455
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5556
</Content>
5657
</ItemGroup>
58+
59+
<ItemGroup>
60+
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.6" />
61+
</ItemGroup>
5762

5863
</Project>

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ namespace Flow.Launcher.Plugin.PluginIndicator
55
{
66
public class Main : IPlugin, IPluginI18n, IHomeQuery
77
{
8-
internal PluginInitContext Context { get; private set; }
8+
internal static PluginInitContext Context { get; private set; }
99

10-
public List<Result> Query(Query query)
10+
public void Init(PluginInitContext context)
1111
{
12-
return QueryResults(query);
12+
Context = context;
1313
}
1414

15-
public List<Result> HomeQuery()
15+
public List<Result> Query(Query query)
1616
{
17-
return QueryResults();
17+
return QueryResults(query);
1818
}
1919

20-
private List<Result> QueryResults(Query query = null)
20+
private static List<Result> QueryResults(Query query = null)
2121
{
2222
var nonGlobalPlugins = GetNonGlobalPlugins();
2323
var querySearch = query?.Search ?? string.Empty;
@@ -34,7 +34,7 @@ from keyword in nonGlobalPlugins.Keys
3434
select new Result
3535
{
3636
Title = keyword,
37-
SubTitle = string.Format(Context.API.GetTranslation("flowlauncher_plugin_pluginindicator_result_subtitle"), plugin.Name),
37+
SubTitle = Localize.flowlauncher_plugin_pluginindicator_result_subtitle(plugin.Name),
3838
Score = score,
3939
IcoPath = plugin.IcoPath,
4040
AutoCompleteText = $"{keyword}{Plugin.Query.TermSeparator}",
@@ -44,10 +44,10 @@ from keyword in nonGlobalPlugins.Keys
4444
return false;
4545
}
4646
};
47-
return results.ToList();
47+
return [.. results];
4848
}
4949

50-
private Dictionary<string, PluginPair> GetNonGlobalPlugins()
50+
private static Dictionary<string, PluginPair> GetNonGlobalPlugins()
5151
{
5252
var nonGlobalPlugins = new Dictionary<string, PluginPair>();
5353
foreach (var plugin in Context.API.GetAllPlugins())
@@ -66,19 +66,19 @@ private Dictionary<string, PluginPair> GetNonGlobalPlugins()
6666
return nonGlobalPlugins;
6767
}
6868

69-
public void Init(PluginInitContext context)
69+
public string GetTranslatedPluginTitle()
7070
{
71-
Context = context;
71+
return Localize.flowlauncher_plugin_pluginindicator_plugin_name();
7272
}
7373

74-
public string GetTranslatedPluginTitle()
74+
public string GetTranslatedPluginDescription()
7575
{
76-
return Context.API.GetTranslation("flowlauncher_plugin_pluginindicator_plugin_name");
76+
return Localize.flowlauncher_plugin_pluginindicator_plugin_description();
7777
}
7878

79-
public string GetTranslatedPluginDescription()
79+
public List<Result> HomeQuery()
8080
{
81-
return Context.API.GetTranslation("flowlauncher_plugin_pluginindicator_plugin_description");
81+
return QueryResults();
8282
}
8383
}
8484
}

Plugins/Flow.Launcher.Plugin.ProcessKiller/Flow.Launcher.Plugin.ProcessKiller.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<ErrorReport>prompt</ErrorReport>
3636
<WarningLevel>4</WarningLevel>
3737
<Prefer32Bit>false</Prefer32Bit>
38+
<NoWarn>$(NoWarn);FLSG0007</NoWarn>
3839
</PropertyGroup>
3940

4041
<ItemGroup>
@@ -52,6 +53,7 @@
5253
</ItemGroup>
5354

5455
<ItemGroup>
56+
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.6" />
5557
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.205">
5658
<PrivateAssets>all</PrivateAssets>
5759
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ namespace Flow.Launcher.Plugin.ProcessKiller
99
{
1010
public class Main : IPlugin, IPluginI18n, IContextMenu, ISettingProvider
1111
{
12-
private readonly ProcessHelper processHelper = new();
12+
internal static PluginInitContext Context { get; private set; }
1313

14-
private static PluginInitContext _context;
14+
private Settings _settings;
1515

16-
internal Settings Settings;
16+
private readonly ProcessHelper processHelper = new();
1717

1818
private SettingsViewModel _viewModel;
1919

2020
public void Init(PluginInitContext context)
2121
{
22-
_context = context;
23-
Settings = context.API.LoadSettingJsonStorage<Settings>();
24-
_viewModel = new SettingsViewModel(Settings);
22+
Context = context;
23+
_settings = context.API.LoadSettingJsonStorage<Settings>();
24+
_viewModel = new SettingsViewModel(_settings);
2525
}
2626

2727
public List<Result> Query(Query query)
@@ -31,12 +31,12 @@ public List<Result> Query(Query query)
3131

3232
public string GetTranslatedPluginTitle()
3333
{
34-
return _context.API.GetTranslation("flowlauncher_plugin_processkiller_plugin_name");
34+
return Localize.flowlauncher_plugin_processkiller_plugin_name();
3535
}
3636

3737
public string GetTranslatedPluginDescription()
3838
{
39-
return _context.API.GetTranslation("flowlauncher_plugin_processkiller_plugin_description");
39+
return Localize.flowlauncher_plugin_processkiller_plugin_description();
4040
}
4141

4242
public List<Result> LoadContextMenus(Result result)
@@ -51,13 +51,13 @@ public List<Result> LoadContextMenus(Result result)
5151
{
5252
menuOptions.Add(new Result
5353
{
54-
Title = _context.API.GetTranslation("flowlauncher_plugin_processkiller_kill_instances"),
54+
Title = Localize.flowlauncher_plugin_processkiller_kill_instances(),
5555
SubTitle = processPath,
5656
Action = _ =>
5757
{
5858
foreach (var p in similarProcesses)
5959
{
60-
processHelper.TryKill(_context, p);
60+
ProcessHelper.TryKill(p);
6161
}
6262

6363
return true;
@@ -72,8 +72,8 @@ public List<Result> LoadContextMenus(Result result)
7272
private List<Result> CreateResultsFromQuery(Query query)
7373
{
7474
// Get all non-system processes
75-
var allPocessList = processHelper.GetMatchingProcesses();
76-
if (!allPocessList.Any())
75+
var allProcessList = processHelper.GetMatchingProcesses();
76+
if (allProcessList.Count == 0)
7777
{
7878
return null;
7979
}
@@ -82,12 +82,12 @@ private List<Result> CreateResultsFromQuery(Query query)
8282
var searchTerm = query.Search;
8383
var processlist = new List<ProcessResult>();
8484
var processWindowTitle =
85-
Settings.ShowWindowTitle || Settings.PutVisibleWindowProcessesTop ?
85+
_settings.ShowWindowTitle || _settings.PutVisibleWindowProcessesTop ?
8686
ProcessHelper.GetProcessesWithNonEmptyWindowTitle() :
87-
new Dictionary<int, string>();
87+
[];
8888
if (string.IsNullOrWhiteSpace(searchTerm))
8989
{
90-
foreach (var p in allPocessList)
90+
foreach (var p in allProcessList)
9191
{
9292
var progressNameIdTitle = ProcessHelper.GetProcessNameIdTitle(p);
9393

@@ -97,8 +97,8 @@ private List<Result> CreateResultsFromQuery(Query query)
9797
// Use window title for those processes if enabled
9898
processlist.Add(new ProcessResult(
9999
p,
100-
Settings.PutVisibleWindowProcessesTop ? 200 : 0,
101-
Settings.ShowWindowTitle ? windowTitle : progressNameIdTitle,
100+
_settings.PutVisibleWindowProcessesTop ? 200 : 0,
101+
_settings.ShowWindowTitle ? windowTitle : progressNameIdTitle,
102102
null,
103103
progressNameIdTitle));
104104
}
@@ -115,35 +115,35 @@ private List<Result> CreateResultsFromQuery(Query query)
115115
}
116116
else
117117
{
118-
foreach (var p in allPocessList)
118+
foreach (var p in allProcessList)
119119
{
120120
var progressNameIdTitle = ProcessHelper.GetProcessNameIdTitle(p);
121121

122122
if (processWindowTitle.TryGetValue(p.Id, out var windowTitle))
123123
{
124124
// Get max score from searching process name, window title and process id
125-
var windowTitleMatch = _context.API.FuzzySearch(searchTerm, windowTitle);
126-
var processNameIdMatch = _context.API.FuzzySearch(searchTerm, progressNameIdTitle);
125+
var windowTitleMatch = Context.API.FuzzySearch(searchTerm, windowTitle);
126+
var processNameIdMatch = Context.API.FuzzySearch(searchTerm, progressNameIdTitle);
127127
var score = Math.Max(windowTitleMatch.Score, processNameIdMatch.Score);
128128
if (score > 0)
129129
{
130130
// Add score to prioritize processes with visible windows
131131
// Use window title for those processes
132-
if (Settings.PutVisibleWindowProcessesTop)
132+
if (_settings.PutVisibleWindowProcessesTop)
133133
{
134134
score += 200;
135135
}
136136
processlist.Add(new ProcessResult(
137137
p,
138138
score,
139-
Settings.ShowWindowTitle ? windowTitle : progressNameIdTitle,
139+
_settings.ShowWindowTitle ? windowTitle : progressNameIdTitle,
140140
score == windowTitleMatch.Score ? windowTitleMatch : null,
141141
progressNameIdTitle));
142142
}
143143
}
144144
else
145145
{
146-
var processNameIdMatch = _context.API.FuzzySearch(searchTerm, progressNameIdTitle);
146+
var processNameIdMatch = Context.API.FuzzySearch(searchTerm, progressNameIdTitle);
147147
var score = processNameIdMatch.Score;
148148
if (score > 0)
149149
{
@@ -162,7 +162,7 @@ private List<Result> CreateResultsFromQuery(Query query)
162162
foreach (var pr in processlist)
163163
{
164164
var p = pr.Process;
165-
var path = processHelper.TryGetProcessFilename(p);
165+
var path = ProcessHelper.TryGetProcessFilename(p);
166166
results.Add(new Result()
167167
{
168168
IcoPath = path,
@@ -172,12 +172,12 @@ private List<Result> CreateResultsFromQuery(Query query)
172172
TitleHighlightData = pr.TitleMatch?.MatchData,
173173
Score = pr.Score,
174174
ContextData = p.ProcessName,
175-
AutoCompleteText = $"{_context.CurrentPluginMetadata.ActionKeyword}{Plugin.Query.TermSeparator}{p.ProcessName}",
175+
AutoCompleteText = $"{Context.CurrentPluginMetadata.ActionKeyword}{Plugin.Query.TermSeparator}{p.ProcessName}",
176176
Action = (c) =>
177177
{
178-
processHelper.TryKill(_context, p);
178+
ProcessHelper.TryKill(p);
179179
// Re-query to refresh process list
180-
_context.API.ReQuery();
180+
Context.API.ReQuery();
181181
return true;
182182
}
183183
});
@@ -194,17 +194,17 @@ private List<Result> CreateResultsFromQuery(Query query)
194194
sortedResults.Insert(1, new Result()
195195
{
196196
IcoPath = firstResult?.IcoPath,
197-
Title = string.Format(_context.API.GetTranslation("flowlauncher_plugin_processkiller_kill_all"), firstResult?.ContextData),
198-
SubTitle = string.Format(_context.API.GetTranslation("flowlauncher_plugin_processkiller_kill_all_count"), processlist.Count),
197+
Title = Localize.flowlauncher_plugin_processkiller_kill_all(firstResult?.ContextData),
198+
SubTitle = Localize.flowlauncher_plugin_processkiller_kill_all_count(processlist.Count),
199199
Score = 200,
200200
Action = (c) =>
201201
{
202202
foreach (var p in processlist)
203203
{
204-
processHelper.TryKill(_context, p.Process);
204+
ProcessHelper.TryKill(p.Process);
205205
}
206206
// Re-query to refresh process list
207-
_context.API.ReQuery();
207+
Context.API.ReQuery();
208208
return true;
209209
}
210210
});

0 commit comments

Comments
 (0)