Improve code quality

This commit is contained in:
Jack251970 2025-04-09 12:14:07 +08:00
parent 49dc657bec
commit 9c07989edf
7 changed files with 51 additions and 58 deletions

View file

@ -2,7 +2,6 @@
using System.IO;
using System.Text.Json;
using System;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Plugin.BrowserBookmark.Models;
using Microsoft.Data.Sqlite;
@ -116,8 +115,7 @@ public abstract class ChromiumBookmarkLoader : IBookmarkLoader
}
else
{
Log.Error(
$"ChromiumBookmarkLoader: EnumerateFolderBookmark: type property not found for {subElement.GetString()}");
Main._context.API.LogError(ClassName, $"type property not found for {subElement.GetString()}");
}
}
}

View file

@ -2,13 +2,12 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.Plugin.Explorer.Search;
using Flow.Launcher.Plugin.Explorer.Search.QuickAccessLinks;
using System.Linq;
using Flow.Launcher.Plugin.Explorer.Helper;
using Flow.Launcher.Plugin.Explorer.ViewModels;
@ -470,22 +469,16 @@ namespace Flow.Launcher.Plugin.Explorer
private void LogException(string message, Exception e)
{
Log.Exception($"|Flow.Launcher.Plugin.Folder.ContextMenu|{message}", e);
Context.API.LogException(nameof(Main), message, e);
}
private bool CanRunAsDifferentUser(string path)
private static bool CanRunAsDifferentUser(string path)
{
switch (Path.GetExtension(path))
return Path.GetExtension(path) switch
{
case ".exe":
case ".bat":
case ".msi":
return true;
default:
return false;
}
".exe" or ".bat" or ".msi" => true,
_ => false,
};
}
}
}

View file

@ -1,10 +1,9 @@
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Plugin.SharedCommands;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Flow.Launcher.Plugin.SharedCommands;
namespace Flow.Launcher.Plugin.Explorer.Search.DirectoryInfo
{
@ -76,7 +75,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.DirectoryInfo
}
catch (Exception e)
{
Log.Exception(nameof(DirectoryInfoSearch), "Error occurred while searching path", e);
Main.Context.API.LogException(nameof(DirectoryInfoSearch), "Error occurred while searching path", e);
throw;
}

View file

@ -1,6 +1,4 @@
using Flow.Launcher.Infrastructure.Logger;
using Microsoft.Search.Interop;
using System;
using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Linq;
@ -9,11 +7,13 @@ using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Threading;
using Flow.Launcher.Plugin.Explorer.Exceptions;
using Microsoft.Search.Interop;
namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
{
internal static class WindowsIndex
{
private static readonly string ClassName = nameof(WindowsIndex);
// Reserved keywords in oleDB
private static Regex _reservedPatternMatcher = new(@"^[`\@\\#\\\^,\&\\/\\\$\%_;\[\]]+$", RegexOptions.Compiled);
@ -33,7 +33,7 @@ namespace Flow.Launcher.Plugin.Explorer.Search.WindowsIndex
}
catch (OleDbException e)
{
Log.Exception($"|WindowsIndex.ExecuteWindowsIndexSearchAsync|Failed to execute windows index search query: {indexQueryString}", e);
Main.Context.API.LogException(ClassName, $"Failed to execute windows index search query: {indexQueryString}", e);
yield break;
}
await using var dataReader = dataReaderAttempt;

View file

@ -6,7 +6,6 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin.Program.Programs;
using Flow.Launcher.Plugin.Program.Views;
@ -20,6 +19,8 @@ namespace Flow.Launcher.Plugin.Program
{
public class Main : ISettingProvider, IAsyncPlugin, IPluginI18n, IContextMenu, IAsyncReloadable, IDisposable
{
private static readonly string ClassName = nameof(Main);
private const string Win32CacheName = "Win32";
private const string UwpCacheName = "UWP";
@ -109,7 +110,7 @@ namespace Flow.Launcher.Plugin.Program
}
catch (OperationCanceledException)
{
Log.Debug("|Flow.Launcher.Plugin.Program.Main|Query operation cancelled");
Context.API.LogDebug(ClassName, "Query operation cancelled");
return emptyResults;
}
finally
@ -253,8 +254,8 @@ namespace Flow.Launcher.Plugin.Program
_uwpsCount = _uwps.Count;
_uwpsLock.Release();
});
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload win32 programs <{_win32sCount}>");
Log.Info($"|Flow.Launcher.Plugin.Program.Main|Number of preload uwps <{_uwpsCount}>");
Context.API.LogInfo(ClassName, "Number of preload win32 programs <{_win32sCount}>");
Context.API.LogInfo(ClassName, "Number of preload uwps <{_uwpsCount}>");
var cacheEmpty = _win32sCount == 0 || _uwpsCount == 0;
@ -295,7 +296,7 @@ namespace Flow.Launcher.Plugin.Program
}
catch (Exception e)
{
Log.Exception("|Flow.Launcher.Plugin.Program.Main|Failed to index Win32 programs", e);
Context.API.LogException(ClassName, "Failed to index Win32 programs", e);
}
finally
{
@ -320,7 +321,7 @@ namespace Flow.Launcher.Plugin.Program
}
catch (Exception e)
{
Log.Exception("|Flow.Launcher.Plugin.Program.Main|Failed to index Uwp programs", e);
Context.API.LogException(ClassName, "Failed to index Uwp programs", e);
}
finally
{

View file

@ -8,7 +8,6 @@ using System.Threading.Tasks;
using WindowsInput;
using WindowsInput.Native;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Plugin.SharedCommands;
using Control = System.Windows.Controls.Control;
using Keys = System.Windows.Forms.Keys;
@ -17,8 +16,11 @@ namespace Flow.Launcher.Plugin.Shell
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, IContextMenu
{
private static readonly string ClassName = nameof(Main);
internal PluginInitContext Context { get; private set; }
private const string Image = "Images/shell.png";
private PluginInitContext context;
private bool _winRStroked;
private readonly KeyboardSimulator _keyboardSimulator = new KeyboardSimulator(new InputSimulator());
@ -88,7 +90,7 @@ namespace Flow.Launcher.Plugin.Shell
}
catch (Exception e)
{
Log.Exception($"|Flow.Launcher.Plugin.Shell.Main.Query|Exception when query for <{query}>", e);
Context.API.LogException(ClassName, $"Exception when query for <{query}>", e);
}
return results;
}
@ -102,14 +104,14 @@ namespace Flow.Launcher.Plugin.Shell
{
if (m.Key == cmd)
{
result.SubTitle = string.Format(context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value);
result.SubTitle = string.Format(Context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value);
return null;
}
var ret = new Result
{
Title = m.Key,
SubTitle = string.Format(context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value),
SubTitle = string.Format(Context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value),
IcoPath = Image,
Action = c =>
{
@ -139,7 +141,7 @@ namespace Flow.Launcher.Plugin.Shell
{
Title = cmd,
Score = 5000,
SubTitle = context.API.GetTranslation("flowlauncher_plugin_cmd_execute_through_shell"),
SubTitle = Context.API.GetTranslation("flowlauncher_plugin_cmd_execute_through_shell"),
IcoPath = Image,
Action = c =>
{
@ -164,7 +166,7 @@ namespace Flow.Launcher.Plugin.Shell
.Select(m => new Result
{
Title = m.Key,
SubTitle = string.Format(context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value),
SubTitle = string.Format(Context.API.GetTranslation("flowlauncher_plugin_cmd_cmd_has_been_executed_times"), m.Value),
IcoPath = Image,
Action = c =>
{
@ -211,7 +213,7 @@ namespace Flow.Launcher.Plugin.Shell
info.FileName = "cmd.exe";
}
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" : "")}");
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" : "")}");
break;
}
@ -234,7 +236,7 @@ namespace Flow.Launcher.Plugin.Shell
else
{
info.ArgumentList.Add("-Command");
info.ArgumentList.Add($"{command}\\; {(_settings.CloseShellAfterPress ? $"Write-Host '{context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")}'\\; [System.Console]::ReadKey()\\; exit" : "")}");
info.ArgumentList.Add($"{command}\\; {(_settings.CloseShellAfterPress ? $"Write-Host '{Context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")}'\\; [System.Console]::ReadKey()\\; exit" : "")}");
}
break;
}
@ -255,7 +257,7 @@ namespace Flow.Launcher.Plugin.Shell
info.ArgumentList.Add("-NoExit");
}
info.ArgumentList.Add("-Command");
info.ArgumentList.Add($"{command}\\; {(_settings.CloseShellAfterPress ? $"Write-Host '{context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")}'\\; [System.Console]::ReadKey()\\; exit" : "")}");
info.ArgumentList.Add($"{command}\\; {(_settings.CloseShellAfterPress ? $"Write-Host '{Context.API.GetTranslation("flowlauncher_plugin_cmd_press_any_key_to_close")}'\\; [System.Console]::ReadKey()\\; exit" : "")}");
break;
}
@ -309,13 +311,13 @@ namespace Flow.Launcher.Plugin.Shell
{
var name = "Plugin: Shell";
var message = $"Command not found: {e.Message}";
context.API.ShowMsg(name, message);
Context.API.ShowMsg(name, message);
}
catch (Win32Exception e)
{
var name = "Plugin: Shell";
var message = $"Error running the command: {e.Message}";
context.API.ShowMsg(name, message);
Context.API.ShowMsg(name, message);
}
}
@ -350,14 +352,14 @@ namespace Flow.Launcher.Plugin.Shell
public void Init(PluginInitContext context)
{
this.context = context;
Context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
context.API.RegisterGlobalKeyboardCallback(API_GlobalKeyboardEvent);
}
bool API_GlobalKeyboardEvent(int keyevent, int vkcode, SpecialKeyState state)
{
if (!context.CurrentPluginMetadata.Disabled && _settings.ReplaceWinR)
if (!Context.CurrentPluginMetadata.Disabled && _settings.ReplaceWinR)
{
if (keyevent == (int)KeyEvent.WM_KEYDOWN && vkcode == (int)Keys.R && state.WinPressed)
{
@ -380,10 +382,9 @@ namespace Flow.Launcher.Plugin.Shell
// show the main window and set focus to the query box
_ = Task.Run(() =>
{
context.API.ShowMainWindow();
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}");
Context.API.ShowMainWindow();
Context.API.ChangeQuery($"{Context.CurrentPluginMetadata.ActionKeywords[0]}{Plugin.Query.TermSeparator}");
});
}
public Control CreateSettingPanel()
@ -393,12 +394,12 @@ namespace Flow.Launcher.Plugin.Shell
public string GetTranslatedPluginTitle()
{
return context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_name");
return Context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_name");
}
public string GetTranslatedPluginDescription()
{
return context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_description");
return Context.API.GetTranslation("flowlauncher_plugin_cmd_plugin_description");
}
public List<Result> LoadContextMenus(Result selectedResult)
@ -407,8 +408,8 @@ namespace Flow.Launcher.Plugin.Shell
{
new()
{
Title = context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_different_user"),
AsyncAction = async c =>
Title = Context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_different_user"),
Action = c =>
{
Execute(ShellCommand.RunAsDifferentUser, PrepareProcessStartInfo(selectedResult.Title));
return true;
@ -418,7 +419,7 @@ namespace Flow.Launcher.Plugin.Shell
},
new()
{
Title = context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_administrator"),
Title = Context.API.GetTranslation("flowlauncher_plugin_cmd_run_as_administrator"),
Action = c =>
{
Execute(Process.Start, PrepareProcessStartInfo(selectedResult.Title, true));
@ -429,10 +430,10 @@ namespace Flow.Launcher.Plugin.Shell
},
new()
{
Title = context.API.GetTranslation("flowlauncher_plugin_cmd_copy"),
Title = Context.API.GetTranslation("flowlauncher_plugin_cmd_copy"),
Action = c =>
{
context.API.CopyToClipboard(selectedResult.Title);
Context.API.CopyToClipboard(selectedResult.Title);
return true;
},
IcoPath = "Images/copy.png",

View file

@ -6,7 +6,6 @@ using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Windows.Win32;
using Windows.Win32.Foundation;
@ -19,6 +18,8 @@ namespace Flow.Launcher.Plugin.Sys
{
public class Main : IPlugin, ISettingProvider, IPluginI18n
{
private static readonly string ClassName = nameof(Main);
private readonly Dictionary<string, string> KeywordTitleMappings = new()
{
{"Shutdown", "flowlauncher_plugin_sys_shutdown_computer_cmd"},
@ -106,7 +107,7 @@ namespace Flow.Launcher.Plugin.Sys
{
if (!KeywordTitleMappings.TryGetValue(key, out var translationKey))
{
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Title not found for: {key}");
_context.API.LogError(ClassName, $"Title not found for: {key}");
return "Title Not Found";
}
@ -117,7 +118,7 @@ namespace Flow.Launcher.Plugin.Sys
{
if (!KeywordDescriptionMappings.TryGetValue(key, out var translationKey))
{
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Description not found for: {key}");
_context.API.LogError(ClassName, $"Description not found for: {key}");
return "Description Not Found";
}