From d6704ed5da47077b0751176d0e3185fb77ef320b Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 3 May 2025 22:09:25 +0800 Subject: [PATCH] Support history items --- Flow.Launcher/ViewModel/MainViewModel.cs | 81 ++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 06cce3026..d850e0f7c 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -50,6 +50,12 @@ namespace Flow.Launcher.ViewModel private readonly IReadOnlyList _emptyResult = new List(); + private readonly PluginMetadata _historyMetadata = new() + { + ID = "298303A65D128A845D28A7B83B3968C2", + Priority = 0 + }; + #endregion #region Constructor @@ -1300,11 +1306,30 @@ namespace Flow.Launcher.ViewModel // plugins are ICollection, meaning LINQ will get the Count and preallocate Array - var tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch + Task[] tasks; + if (homeQuery) { - false => QueryTaskAsync(plugin, _updateSource.Token), - true => Task.CompletedTask - }).ToArray(); + var homeTasks = plugins.Select(plugin => plugin.Metadata.HomeDisabled switch + { + false => QueryTaskAsync(plugin, _updateSource.Token), + true => Task.CompletedTask + }).ToList(); + + if (Settings.ShowHistoryResultsForHomePage) + { + homeTasks.Add(QueryHistoryTaskAsync()); + } + + tasks = homeTasks.ToArray(); + } + else + { + tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch + { + false => QueryTaskAsync(plugin, _updateSource.Token), + true => Task.CompletedTask + }).ToArray(); + } try { @@ -1377,6 +1402,54 @@ namespace Flow.Launcher.ViewModel App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); } } + + async Task QueryHistoryTaskAsync() + { + // Since it is wrapped within a ThreadPool Thread, the synchronous context is null + // Task.Yield will force it to run in ThreadPool + await Task.Yield(); + + // Select last history results and revert its order to make sure last history results are on top + var lastHistoryResults = _history.Items.TakeLast(Settings.MaxHistoryResultsToShowForHomePage).Reverse(); + + var historyResults = new List(); + foreach (var h in lastHistoryResults) + { + var title = App.API.GetTranslation("executeQuery"); + var time = App.API.GetTranslation("lastExecuteTime"); + var result = new Result + { + Title = string.Format(title, h.Query), + SubTitle = string.Format(time, h.ExecutedDateTime), + IcoPath = "Images\\history.png", + Preview = new Result.PreviewInfo + { + PreviewImagePath = Constant.HistoryIcon, + Description = string.Format(time, h.ExecutedDateTime) + }, + OriginQuery = new Query { RawQuery = h.Query }, + Action = _ => + { + SelectedResults = Results; + App.API.ChangeQuery(h.Query); + return false; + } + }; + historyResults.Add(result); + } + + // No need to make copy of results and update badge ico property + + if (_updateSource.Token.IsCancellationRequested) return; + + if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(historyResults, _historyMetadata, query, + _updateSource.Token))) + { + App.API.LogError(ClassName, "Unable to add item to Result Update Queue"); + } + + await Task.CompletedTask; + } } private async Task ConstructQueryAsync(string queryText, IEnumerable customShortcuts,