From e3527f47ea8f74119c63758a1437b72fd7f2a957 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 13 Oct 2025 15:27:06 +0800 Subject: [PATCH] Add RecordKey for precise history matching and refactor Added a `RecordKey` property to `HistoryItem` for unique identification of history records, enabling more accurate matching during queries and executions. Updated `HistoryHelper` methods to utilize `RecordKey` for matching, with fallback to `Title` and `SubTitle`. Enhanced `GetExecuteAction` with error handling, nullable reference types, and improved matching logic. Included `RecordKey` in `History` object creation. Enabled nullable reference types in `HistoryHelper.cs` for better code safety. Refactored code for clarity and maintainability. --- Flow.Launcher/Storage/History.cs | 1 + Flow.Launcher/Storage/HistoryHelper.cs | 44 +++++++++++++++++++------- Flow.Launcher/Storage/HistoryItem.cs | 1 + 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/Flow.Launcher/Storage/History.cs b/Flow.Launcher/Storage/History.cs index 346964f9f..5b69df056 100644 --- a/Flow.Launcher/Storage/History.cs +++ b/Flow.Launcher/Storage/History.cs @@ -81,6 +81,7 @@ namespace Flow.Launcher.Storage SubTitle = result.SubTitle, PluginID = result.PluginID, RawQuery = result.OriginQuery.RawQuery, + RecordKey = result.RecordKey, ExecutedDateTime = DateTime.Now, ExecuteAction = result.Action }; diff --git a/Flow.Launcher/Storage/HistoryHelper.cs b/Flow.Launcher/Storage/HistoryHelper.cs index 7da77af2b..bb7f62eba 100644 --- a/Flow.Launcher/Storage/HistoryHelper.cs +++ b/Flow.Launcher/Storage/HistoryHelper.cs @@ -6,40 +6,62 @@ using Flow.Launcher.Core.Plugin; using Flow.Launcher.Plugin; namespace Flow.Launcher.Storage; + +#nullable enable + public static class HistoryHelper { - internal static List PopulateActions(this List items, bool isQuery) + internal static List PopulateActions(this List items, bool isQuery) { foreach (var item in items) { if (item.QueryAction != null && item.ExecuteAction != null) continue; if (isQuery && item.QueryAction == null) item.QueryAction = GetQueryAction(item.RawQuery); - if (!isQuery && item.ExecuteAction == null) item.ExecuteAction = GetExecuteAction(item.PluginID, item.RawQuery, item.Title, item.SubTitle); + if (!isQuery && item.ExecuteAction == null) item.ExecuteAction = GetExecuteAction(item.PluginID, item.RawQuery, item.Title, item.SubTitle, item.RecordKey) ?? GetQueryAction(item.RawQuery); } return items; } - public static Func GetQueryAction(string query) + public static Func GetQueryAction(string rawQuery) { return _ => { App.API.BackToQueryResults(); - App.API.ChangeQuery(query); + App.API.ChangeQuery(rawQuery); return false; }; } - private static Func GetExecuteAction(string pluginId, string rawQuery, string title, string subTitle) + private static Func? GetExecuteAction(string pluginId, string rawQuery, string title, string subTitle, string recordKey) { var plugin = PluginManager.GetPluginForId(pluginId); + if (plugin == null) return null; var query = QueryBuilder.Build(rawQuery, PluginManager.NonGlobalPlugins); - var freshResults = plugin.Plugin - .QueryAsync(query, CancellationToken.None) - .GetAwaiter() - .GetResult(); - return freshResults?.FirstOrDefault(r => r.Title == title - && r.SubTitle == subTitle)?.Action; + if (query == null) return null; + try + { +#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits + var freshResults = plugin.Plugin + .QueryAsync(query, CancellationToken.None) + .GetAwaiter() + .GetResult(); +#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits + // Try to match by record key first if it is valid, otherwise fall back to title + subtitle match + if (string.IsNullOrEmpty(recordKey)) + { + return freshResults?.FirstOrDefault(r => r.Title == title && r.SubTitle == subTitle)?.Action; + } + else + { + return freshResults?.FirstOrDefault(r => r.RecordKey == recordKey)?.Action ?? + freshResults?.FirstOrDefault(r => r.Title == title && r.SubTitle == subTitle)?.Action; + } + } + catch + { + return null; + } } } diff --git a/Flow.Launcher/Storage/HistoryItem.cs b/Flow.Launcher/Storage/HistoryItem.cs index 04b97118e..dc058c960 100644 --- a/Flow.Launcher/Storage/HistoryItem.cs +++ b/Flow.Launcher/Storage/HistoryItem.cs @@ -10,6 +10,7 @@ public class HistoryItem public string SubTitle { get; set; } = string.Empty; public string PluginID { get; set; } = string.Empty; public string RawQuery { get; set; } + public string RecordKey { get; set; } = string.Empty; public DateTime ExecutedDateTime { get; set; } [JsonIgnore]