From 698217f25d14a6ced6faaad23cb874d30c4b85f4 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 17 Mar 2025 10:07:40 +0800 Subject: [PATCH] Improve code quality --- Flow.Launcher/PublicAPIInstance.cs | 31 ++++++------ Flow.Launcher/ViewModel/MainViewModel.cs | 62 ++++++++++++------------ 2 files changed, 46 insertions(+), 47 deletions(-) diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index ac22170ae..d9f935833 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -1,33 +1,32 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Specialized; +using System.Diagnostics; +using System.IO; using System.Linq; using System.Net; +using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; using System.Windows; +using CommunityToolkit.Mvvm.DependencyInjection; using Squirrel; +using Flow.Launcher.Core; using Flow.Launcher.Core.Plugin; -using Flow.Launcher.Core.Resource; using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure; +using Flow.Launcher.Infrastructure.Http; using Flow.Launcher.Infrastructure.Hotkey; using Flow.Launcher.Infrastructure.Image; -using Flow.Launcher.Plugin; -using Flow.Launcher.ViewModel; -using Flow.Launcher.Plugin.SharedModels; -using Flow.Launcher.Plugin.SharedCommands; -using System.Threading; -using System.IO; -using Flow.Launcher.Infrastructure.Http; -using JetBrains.Annotations; -using System.Runtime.CompilerServices; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Storage; -using System.Collections.Concurrent; -using System.Diagnostics; -using System.Collections.Specialized; -using CommunityToolkit.Mvvm.DependencyInjection; -using Flow.Launcher.Core; using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Plugin; +using Flow.Launcher.Plugin.SharedModels; +using Flow.Launcher.Plugin.SharedCommands; +using Flow.Launcher.ViewModel; +using JetBrains.Annotations; namespace Flow.Launcher { @@ -153,7 +152,7 @@ namespace Flow.Launcher public void StopLoadingBar() => _mainVM.ProgressBarVisibility = Visibility.Collapsed; - public string GetTranslation(string key) => InternationalizationManager.Instance.GetTranslation(key); + public string GetTranslation(string key) => App.API.GetTranslation(key); public List GetAllPlugins() => PluginManager.AllPlugins.ToList(); diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index f7c683bc6..59ca356a7 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -14,7 +14,6 @@ using System.Windows.Threading; using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core.Plugin; -using Flow.Launcher.Core.Resource; using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Hotkey; using Flow.Launcher.Infrastructure.Image; @@ -47,8 +46,6 @@ namespace Flow.Launcher.ViewModel private CancellationTokenSource _updateSource; private CancellationToken _updateToken; - private readonly Internationalization _translator = InternationalizationManager.Instance; - private ChannelWriter _resultsUpdateChannelWriter; private Task _resultsViewUpdateTask; @@ -180,9 +177,9 @@ namespace Flow.Launcher.ViewModel var resultUpdateChannel = Channel.CreateUnbounded(); _resultsUpdateChannelWriter = resultUpdateChannel.Writer; _resultsViewUpdateTask = - Task.Run(updateAction).ContinueWith(continueAction, TaskContinuationOptions.OnlyOnFaulted); + Task.Run(UpdateActionAsync).ContinueWith(continueAction, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default); - async Task updateAction() + async Task UpdateActionAsync() { var queue = new Dictionary(); var channelReader = resultUpdateChannel.Reader; @@ -249,8 +246,8 @@ namespace Flow.Launcher.ViewModel Hide(); await PluginManager.ReloadDataAsync().ConfigureAwait(false); - Notification.Show(InternationalizationManager.Instance.GetTranslation("success"), - InternationalizationManager.Instance.GetTranslation("completedSuccessfully")); + Notification.Show(App.API.GetTranslation("success"), + App.API.GetTranslation("completedSuccessfully")); } [RelayCommand] @@ -272,14 +269,14 @@ namespace Flow.Launcher.ViewModel { if (SelectedIsFromQueryResults()) { - QueryResults(isReQuery: true); + _ = QueryResultsAsync(isReQuery: true); } } public void ReQuery(bool reselect) { BackToQueryResults(); - QueryResults(isReQuery: true, reSelect: reselect); + _ = QueryResultsAsync(isReQuery: true, reSelect: reselect); } [RelayCommand] @@ -387,11 +384,11 @@ namespace Flow.Launcher.ViewModel } var hideWindow = await result.ExecuteAsync(new ActionContext - { - // not null means pressing modifier key + number, should ignore the modifier key - SpecialKeyState = index is not null ? SpecialKeyState.Default : GlobalHotkey.CheckModifiers() - }) - .ConfigureAwait(false); + { + // not null means pressing modifier key + number, should ignore the modifier key + SpecialKeyState = index is not null ? SpecialKeyState.Default : GlobalHotkey.CheckModifiers() + }) + .ConfigureAwait(false); if (SelectedIsFromQueryResults()) { @@ -455,7 +452,6 @@ namespace Flow.Launcher.ViewModel SelectedResults.SelectLastResult(); } - [RelayCommand] private void SelectPrevPage() { @@ -482,7 +478,6 @@ namespace Flow.Launcher.ViewModel { SelectedResults.SelectPrevResult(); } - } [RelayCommand] @@ -559,7 +554,6 @@ namespace Flow.Launcher.ViewModel public bool GameModeStatus { get; set; } = false; private string _queryText; - public string QueryText { get => _queryText; @@ -808,8 +802,8 @@ namespace Flow.Launcher.ViewModel throw new NotImplementedException("ResultAreaColumn should match ResultAreaColumnPreviewShown/ResultAreaColumnPreviewHidden value"); #else Log.Error("MainViewModel", "ResultAreaColumnPreviewHidden/ResultAreaColumnPreviewShown int value not implemented", "InternalPreviewVisible"); -#endif return false; +#endif } } @@ -897,7 +891,7 @@ namespace Flow.Launcher.ViewModel ExternalPreviewVisible = false; } - private void SwitchExternalPreview(string path, bool sendFailToast = true) + private static void SwitchExternalPreview(string path, bool sendFailToast = true) { _ = PluginManager.SwitchExternalPreviewAsync(path,sendFailToast).ConfigureAwait(false); } @@ -979,7 +973,7 @@ namespace Flow.Launcher.ViewModel { if (SelectedIsFromQueryResults()) { - QueryResults(isReQuery); + _ = QueryResultsAsync(isReQuery); } else if (ContextMenuSelected()) { @@ -1042,8 +1036,8 @@ namespace Flow.Launcher.ViewModel var results = new List(); foreach (var h in _history.Items) { - var title = _translator.GetTranslation("executeQuery"); - var time = _translator.GetTranslation("lastExecuteTime"); + var title = App.API.GetTranslation("executeQuery"); + var time = App.API.GetTranslation("lastExecuteTime"); var result = new Result { Title = string.Format(title, h.Query), @@ -1077,7 +1071,7 @@ namespace Flow.Launcher.ViewModel private readonly IReadOnlyList _emptyResult = new List(); - private async void QueryResults(bool isReQuery = false, bool reSelect = true) + private async Task QueryResultsAsync(bool isReQuery = false, bool reSelect = true) { _updateSource?.Cancel(); @@ -1155,7 +1149,7 @@ namespace Flow.Launcher.ViewModel var tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch { - false => QueryTask(plugin, reSelect), + false => QueryTaskAsync(plugin, reSelect), true => Task.CompletedTask }).ToArray(); @@ -1183,7 +1177,7 @@ namespace Flow.Launcher.ViewModel } // Local function - async Task QueryTask(PluginPair plugin, bool reSelect = true) + async Task QueryTaskAsync(PluginPair plugin, bool reSelect = true) { // Since it is wrapped within a ThreadPool Thread, the synchronous context is null // Task.Yield will force it to run in ThreadPool @@ -1282,13 +1276,13 @@ namespace Flow.Launcher.ViewModel { menu = new Result { - Title = InternationalizationManager.Instance.GetTranslation("cancelTopMostInThisQuery"), + Title = App.API.GetTranslation("cancelTopMostInThisQuery"), IcoPath = "Images\\down.png", PluginDirectory = Constant.ProgramDirectory, Action = _ => { _topMostRecord.Remove(result); - App.API.ShowMsg(InternationalizationManager.Instance.GetTranslation("success")); + App.API.ShowMsg(App.API.GetTranslation("success")); App.API.ReQuery(); return false; } @@ -1298,14 +1292,14 @@ namespace Flow.Launcher.ViewModel { menu = new Result { - Title = InternationalizationManager.Instance.GetTranslation("setAsTopMostInThisQuery"), + Title = App.API.GetTranslation("setAsTopMostInThisQuery"), IcoPath = "Images\\up.png", Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\xeac2"), PluginDirectory = Constant.ProgramDirectory, Action = _ => { _topMostRecord.AddOrUpdate(result); - App.API.ShowMsg(InternationalizationManager.Instance.GetTranslation("success")); + App.API.ShowMsg(App.API.GetTranslation("success")); App.API.ReQuery(); return false; } @@ -1315,10 +1309,10 @@ namespace Flow.Launcher.ViewModel return menu; } - private Result ContextMenuPluginInfo(string id) + private static Result ContextMenuPluginInfo(string id) { var metadata = PluginManager.GetPluginForId(id).Metadata; - var translator = InternationalizationManager.Instance; + var translator = App.API; var author = translator.GetTranslation("author"); var website = translator.GetTranslation("website"); @@ -1400,12 +1394,16 @@ namespace Flow.Launcher.ViewModel }); } +#pragma warning disable VSTHRD100 // Avoid async void methods + public async void Hide() { lastHistoryIndex = 1; if (ExternalPreviewVisible) + { CloseExternalPreview(); + } if (!SelectedIsFromQueryResults()) { @@ -1476,6 +1474,8 @@ namespace Flow.Launcher.ViewModel VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = false }); } +#pragma warning restore VSTHRD100 // Avoid async void methods + /// /// Checks if Flow Launcher should ignore any hotkeys ///