Skip to content

Commit 9c07989

Browse files
committed
Improve code quality
1 parent 49dc657 commit 9c07989

File tree

7 files changed

+51
-58
lines changed

7 files changed

+51
-58
lines changed

Plugins/Flow.Launcher.Plugin.BrowserBookmark/ChromiumBookmarkLoader.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.IO;
33
using System.Text.Json;
44
using System;
5-
using Flow.Launcher.Infrastructure.Logger;
65
using Flow.Launcher.Plugin.BrowserBookmark.Models;
76
using Microsoft.Data.Sqlite;
87

@@ -116,8 +115,7 @@ private static void EnumerateFolderBookmark(JsonElement folderElement, ICollecti
116115
}
117116
else
118117
{
119-
Log.Error(
120-
$"ChromiumBookmarkLoader: EnumerateFolderBookmark: type property not found for {subElement.GetString()}");
118+
Main._context.API.LogError(ClassName, $"type property not found for {subElement.GetString()}");
121119
}
122120
}
123121
}

Plugins/Flow.Launcher.Plugin.Explorer/ContextMenu.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.IO;
5+
using System.Linq;
56
using System.Threading.Tasks;
67
using System.Windows;
7-
using Flow.Launcher.Infrastructure.Logger;
88
using Flow.Launcher.Plugin.SharedCommands;
99
using Flow.Launcher.Plugin.Explorer.Search;
1010
using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks;
11-
using System.Linq;
1211
using Flow.Launcher.Plugin.Explorer.Helper;
1312
using Flow.Launcher.Plugin.Explorer.ViewModels;
1413

@@ -470,22 +469,16 @@ private Result CreateOpenWithMenu(SearchResult record)
470469

471470
private void LogException(string message, Exception e)
472471
{
473-
Log.Exception($"|Flow.Launcher.Plugin.Folder.ContextMenu|{message}", e);
472+
Context.API.LogException(nameof(Main), message, e);
474473
}
475474

476-
private bool CanRunAsDifferentUser(string path)
475+
private static bool CanRunAsDifferentUser(string path)
477476
{
478-
switch (Path.GetExtension(path))
477+
return Path.GetExtension(path) switch
479478
{
480-
case ".exe":
481-
case ".bat":
482-
case ".msi":
483-
return true;
484-
485-
default:
486-
return false;
487-
488-
}
479+
".exe" or ".bat" or ".msi" => true,
480+
_ => false,
481+
};
489482
}
490483
}
491484
}

Plugins/Flow.Launcher.Plugin.Explorer/Search/DirectoryInfo/DirectoryInfoSearch.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
using Flow.Launcher.Infrastructure.Logger;
2-
using Flow.Launcher.Plugin.SharedCommands;
3-
using System;
1+
using System;
42
using System.Collections.Generic;
53
using System.IO;
64
using System.Linq;
75
using System.Threading;
6+
using Flow.Launcher.Plugin.SharedCommands;
87

98
namespace Flow.Launcher.Plugin.Explorer.Search.DirectoryInfo
109
{
@@ -76,7 +75,7 @@ private static IEnumerable<SearchResult> DirectorySearch(EnumerationOptions enum
7675
}
7776
catch (Exception e)
7877
{
79-
Log.Exception(nameof(DirectoryInfoSearch), "Error occurred while searching path", e);
78+
Main.Context.API.LogException(nameof(DirectoryInfoSearch), "Error occurred while searching path", e);
8079

8180
throw;
8281
}

Plugins/Flow.Launcher.Plugin.Explorer/Search/WindowsIndex/WindowsIndex.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Flow.Launcher.Infrastructure.Logger;
2-
using Microsoft.Search.Interop;
3-
using System;
1+
using System;
42
using System.Collections.Generic;
53
using System.Data.OleDb;
64
using System.Linq;
@@ -9,11 +7,13 @@
97
using System.Text.RegularExpressions;
108
using System.Threading;
119
using Flow.Launcher.Plugin.Explorer.Exceptions;
10+
using Microsoft.Search.Interop;
1211

1312
namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
1413
{
1514
internal static class WindowsIndex
1615
{
16+
private static readonly string ClassName = nameof(WindowsIndex);
1717

1818
// Reserved keywords in oleDB
1919
private static Regex _reservedPatternMatcher = new(@"^[`\@\@\#\#\*\^,\&\&\/\\\$\%_;\[\]]+$", RegexOptions.Compiled);
@@ -33,7 +33,7 @@ private static async IAsyncEnumerable<SearchResult> ExecuteWindowsIndexSearchAsy
3333
}
3434
catch (OleDbException e)
3535
{
36-
Log.Exception($"|WindowsIndex.ExecuteWindowsIndexSearchAsync|Failed to execute windows index search query: {indexQueryString}", e);
36+
Main.Context.API.LogException(ClassName, $"Failed to execute windows index search query: {indexQueryString}", e);
3737
yield break;
3838
}
3939
await using var dataReader = dataReaderAttempt;

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

Lines changed: 7 additions & 6 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.Logger;
109
using Flow.Launcher.Infrastructure.UserSettings;
1110
using Flow.Launcher.Plugin.Program.Programs;
1211
using Flow.Launcher.Plugin.Program.Views;
@@ -20,6 +19,8 @@ namespace Flow.Launcher.Plugin.Program
2019
{
2120
public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, IAsyncReloadable, IDisposable
2221
{
22+
private static readonly string ClassName = nameof(Main);
23+
2324
private const string Win32CacheName = "Win32";
2425
private const string UwpCacheName = "UWP";
2526

@@ -109,7 +110,7 @@ public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
109110
}
110111
catch (OperationCanceledException)
111112
{
112-
Log.Debug("|Flow.Launcher.Plugin.Program.Main|Query operation cancelled");
113+
Context.API.LogDebug(ClassName, "Query operation cancelled");
113114
return emptyResults;
114115
}
115116
finally
@@ -253,8 +254,8 @@ static void MoveFile(string sourcePath, string destinationPath)
253254
_uwpsCount = _uwps.Count;
254255
_uwpsLock.Release();
255256
});
256-
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload win32 programs <{_win32sCount}>");
257-
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload uwps <{_uwpsCount}>");
257+
Context.API.LogInfo(ClassName, "Number of preload win32 programs <{_win32sCount}>");
258+
Context.API.LogInfo(ClassName, "Number of preload uwps <{_uwpsCount}>");
258259

259260
var cacheEmpty = _win32sCount == 0 || _uwpsCount == 0;
260261

@@ -295,7 +296,7 @@ public static async Task IndexWin32ProgramsAsync()
295296
}
296297
catch (Exception e)
297298
{
298-
Log.Exception("|Flow.Launcher.Plugin.Program.Main|Failed to index Win32 programs", e);
299+
Context.API.LogException(ClassName, "Failed to index Win32 programs", e);
299300
}
300301
finally
301302
{
@@ -320,7 +321,7 @@ public static async Task IndexUwpProgramsAsync()
320321
}
321322
catch (Exception e)
322323
{
323-
Log.Exception("|Flow.Launcher.Plugin.Program.Main|Failed to index Uwp programs", e);
324+
Context.API.LogException(ClassName, "Failed to index Uwp programs", e);
324325
}
325326
finally
326327
{

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

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using WindowsInput;
99
using WindowsInput.Native;
1010
using Flow.Launcher.Infrastructure.Hotkey;
11-
using Flow.Launcher.Infrastructure.Logger;
1211
using Flow.Launcher.Plugin.SharedCommands;
1312
using Control = System.Windows.Controls.Control;
1413
using Keys = System.Windows.Forms.Keys;
@@ -17,8 +16,11 @@ namespace Flow.Launcher.Plugin.Shell
1716
{
1817
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu
1918
{
19+
private static readonly string ClassName = nameof(Main);
20+
21+
internal PluginInitContext Context { get; private set; }
22+
2023
private const string Image = "Images/shell.png";
21-
private PluginInitContext context;
2224
private bool _winRStroked;
2325
private readonly KeyboardSimulator _keyboardSimulator = new KeyboardSimulator(new InputSimulator());
2426

@@ -88,7 +90,7 @@ public List<Result> Query(Query query)
8890
}
8991
catch (Exception e)
9092
{
91-
Log.Exception($"|Flow.Launcher.Plugin.Shell.Main.Query|Exception when query for <{query}>", e);
93+
Context.API.LogException(ClassName, $"Exception when query for <{query}>", e);
9294
}
9395
return results;
9496
}
@@ -102,14 +104,14 @@ private List<Result> GetHistoryCmds(string cmd, Result result)
102104
{
103105
if (m.Key == cmd)
104106
{
105-
result.SubTitle = string.Format(context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value);
107+
result.SubTitle = string.Format(Context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value);
106108
return null;
107109
}
108110

109111
var ret = new Result
110112
{
111113
Title = m.Key,
112-
SubTitle = string.Format(context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value),
114+
SubTitle = string.Format(Context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value),
113115
IcoPath = Image,
114116
Action = c =>
115117
{
@@ -139,7 +141,7 @@ private Result GetCurrentCmd(string cmd)
139141
{
140142
Title = cmd,
141143
Score = 5000,
142-
SubTitle = context.API.GetTranslation("flowlauncher_plugin_cmd_execute_through_shell"),
144+
SubTitle = Context.API.GetTranslation("flowlauncher_plugin_cmd_execute_through_shell"),
143145
IcoPath = Image,
144146
Action = c =>
145147
{
@@ -164,7 +166,7 @@ private List<Result> ResultsFromHistory()
164166
.Select(m => new Result
165167
{
166168
Title = m.Key,
167-
SubTitle = string.Format(context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value),
169+
SubTitle = string.Format(Context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value),
168170
IcoPath = Image,
169171
Action = c =>
170172
{
@@ -211,7 +213,7 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin
211213
info.FileName = "cmd.exe";
212214
}
213215

214-
info.ArgumentList.Add($"{(_settings.LeaveShellOpen ? "/k" : "/c")} {command} {(_settings.CloseShellAfterPress ? $"&& echo {context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")} && pause > nul /c" : "")}");
216+
info.ArgumentList.Add($"{(_settings.LeaveShellOpen ? "/k" : "/c")} {command} {(_settings.CloseShellAfterPress ? $"&& echo {Context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")} && pause > nul /c" : "")}");
215217
break;
216218
}
217219

@@ -234,7 +236,7 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin
234236
else
235237
{
236238
info.ArgumentList.Add("-Command");
237-
info.ArgumentList.Add($"{command}\\; {(_settings.CloseShellAfterPress ? $"Write-Host '{context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")}'\\; [System.Console]::ReadKey()\\; exit" : "")}");
239+
info.ArgumentList.Add($"{command}\\; {(_settings.CloseShellAfterPress ? $"Write-Host '{Context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")}'\\; [System.Console]::ReadKey()\\; exit" : "")}");
238240
}
239241
break;
240242
}
@@ -255,7 +257,7 @@ private ProcessStartInfo PrepareProcessStartInfo(string command, bool runAsAdmin
255257
info.ArgumentList.Add("-NoExit");
256258
}
257259
info.ArgumentList.Add("-Command");
258-
info.ArgumentList.Add($"{command}\\; {(_settings.CloseShellAfterPress ? $"Write-Host '{context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")}'\\; [System.Console]::ReadKey()\\; exit" : "")}");
260+
info.ArgumentList.Add($"{command}\\; {(_settings.CloseShellAfterPress ? $"Write-Host '{Context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")}'\\; [System.Console]::ReadKey()\\; exit" : "")}");
259261
break;
260262
}
261263

@@ -309,13 +311,13 @@ private void Execute(Func<ProcessStartInfo, Process> startProcess, ProcessStartI
309311
{
310312
var name = "Plugin: Shell";
311313
var message = $"Command not found: {e.Message}";
312-
context.API.ShowMsg(name, message);
314+
Context.API.ShowMsg(name, message);
313315
}
314316
catch (Win32Exception e)
315317
{
316318
var name = "Plugin: Shell";
317319
var message = $"Error running the command: {e.Message}";
318-
context.API.ShowMsg(name, message);
320+
Context.API.ShowMsg(name, message);
319321
}
320322
}
321323

@@ -350,14 +352,14 @@ private bool ExistInPath(string filename)
350352

351353
public void Init(PluginInitContext context)
352354
{
353-
this.context = context;
355+
Context = context;
354356
_settings = context.API.LoadSettingJsonStorage<Settings>();
355357
context.API.RegisterGlobalKeyboardCallback(API_GlobalKeyboardEvent);
356358
}
357359

358360
bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)
359361
{
360-
if (!context.CurrentPluginMetadata.Disabled && _settings.ReplaceWinR)
362+
if (!Context.CurrentPluginMetadata.Disabled && _settings.ReplaceWinR)
361363
{
362364
if (keyevent == (int)KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed)
363365
{
@@ -380,10 +382,9 @@ private void OnWinRPressed()
380382
// show the main window and set focus to the query box
381383
_ = Task.Run(() =>
382384
{
383-
context.API.ShowMainWindow();
384-
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}");
385+
Context.API.ShowMainWindow();
386+
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}");
385387
});
386-
387388
}
388389

389390
public Control CreateSettingPanel()
@@ -393,12 +394,12 @@ public Control CreateSettingPanel()
393394

394395
public string GetTranslatedPluginTitle()
395396
{
396-
return context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_name");
397+
return Context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_name");
397398
}
398399

399400
public string GetTranslatedPluginDescription()
400401
{
401-
return context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_description");
402+
return Context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_description");
402403
}
403404

404405
public List<Result> LoadContextMenus(Result selectedResult)
@@ -407,8 +408,8 @@ public List<Result> LoadContextMenus(Result selectedResult)
407408
{
408409
new()
409410
{
410-
Title = context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_different_user"),
411-
AsyncAction = async c =>
411+
Title = Context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_different_user"),
412+
Action = c =>
412413
{
413414
Execute(ShellCommand.RunAsDifferentUser, PrepareProcessStartInfo(selectedResult.Title));
414415
return true;
@@ -418,7 +419,7 @@ public List<Result> LoadContextMenus(Result selectedResult)
418419
},
419420
new()
420421
{
421-
Title = context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_administrator"),
422+
Title = Context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_administrator"),
422423
Action = c =>
423424
{
424425
Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, true));
@@ -429,10 +430,10 @@ public List<Result> LoadContextMenus(Result selectedResult)
429430
},
430431
new()
431432
{
432-
Title = context.API.GetTranslation("flowlauncher_plugin_cmd_copy"),
433+
Title = Context.API.GetTranslation("flowlauncher_plugin_cmd_copy"),
433434
Action = c =>
434435
{
435-
context.API.CopyToClipboard(selectedResult.Title);
436+
Context.API.CopyToClipboard(selectedResult.Title);
436437
return true;
437438
},
438439
IcoPath = "Images/copy.png",

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Runtime.InteropServices;
77
using System.Windows;
88
using Flow.Launcher.Infrastructure;
9-
using Flow.Launcher.Infrastructure.Logger;
109
using Flow.Launcher.Infrastructure.UserSettings;
1110
using Windows.Win32;
1211
using Windows.Win32.Foundation;
@@ -19,6 +18,8 @@ namespace Flow.Launcher.Plugin.Sys
1918
{
2019
public class Main : IPlugin, ISettingProvider, IPluginI18n
2120
{
21+
private static readonly string ClassName = nameof(Main);
22+
2223
private readonly Dictionary<string, string> KeywordTitleMappings = new()
2324
{
2425
{"Shutdown", "flowlauncher_plugin_sys_shutdown_computer_cmd"},
@@ -106,7 +107,7 @@ private string GetTitle(string key)
106107
{
107108
if (!KeywordTitleMappings.TryGetValue(key, out var translationKey))
108109
{
109-
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Title not found for: {key}");
110+
_context.API.LogError(ClassName, $"Title not found for: {key}");
110111
return "Title Not Found";
111112
}
112113

@@ -117,7 +118,7 @@ private string GetDescription(string key)
117118
{
118119
if (!KeywordDescriptionMappings.TryGetValue(key, out var translationKey))
119120
{
120-
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Description not found for: {key}");
121+
_context.API.LogError(ClassName, $"Description not found for: {key}");
121122
return "Description Not Found";
122123
}
123124

0 commit comments

Comments
 (0)