From 15dbfdbf32e90e6f1104776c6156e0e18ed6bf40 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 28 Feb 2025 15:09:58 +0800 Subject: [PATCH] Add StringMatcherKey --- Flow.Launcher.Infrastructure/StringMatcher.cs | 4 ++-- Flow.Launcher.Plugin/Result.cs | 7 ++++++ Flow.Launcher/ViewModel/MainViewModel.cs | 23 +++++++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Flow.Launcher.Infrastructure/StringMatcher.cs b/Flow.Launcher.Infrastructure/StringMatcher.cs index e85c5d6f442..d43a0ecca2f 100644 --- a/Flow.Launcher.Infrastructure/StringMatcher.cs +++ b/Flow.Launcher.Infrastructure/StringMatcher.cs @@ -228,7 +228,7 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption return new MatchResult(false, UserSettingSearchPrecision); } - private bool IsAcronym(string stringToCompare, int compareStringIndex) + private static bool IsAcronym(string stringToCompare, int compareStringIndex) { if (IsAcronymChar(stringToCompare, compareStringIndex) || IsAcronymNumber(stringToCompare, compareStringIndex)) return true; @@ -237,7 +237,7 @@ private bool IsAcronym(string stringToCompare, int compareStringIndex) } // When counting acronyms, treat a set of numbers as one acronym ie. Visual 2019 as 2 acronyms instead of 5 - private bool IsAcronymCount(string stringToCompare, int compareStringIndex) + private static bool IsAcronymCount(string stringToCompare, int compareStringIndex) { if (IsAcronymChar(stringToCompare, compareStringIndex)) return true; diff --git a/Flow.Launcher.Plugin/Result.cs b/Flow.Launcher.Plugin/Result.cs index 9b16cc1cbf0..7c7edcdf9c9 100644 --- a/Flow.Launcher.Plugin/Result.cs +++ b/Flow.Launcher.Plugin/Result.cs @@ -270,6 +270,13 @@ public ValueTask ExecuteAsync(ActionContext context) /// public string RecordKey { get; set; } = null; + /// + /// The key to be compared with the user input for fuzzy search. + /// This can be useful when the plugin wants to provide a different string for fuzzy search. + /// If the plugin does not specific this, FL just uses Title and SubTitle to compare with the user input. + /// + public string StringMatcherKey { get; set; } + /// /// Info of the preview section of a /// diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 650a276109d..b3d6bad14df 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -26,6 +26,7 @@ using Flow.Launcher.Infrastructure.Image; using System.Windows.Media; using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Plugin.SharedModels; namespace Flow.Launcher.ViewModel { @@ -1016,10 +1017,20 @@ private void QueryContextMenu() ( r => { - var match = StringMatcher.FuzzySearch(query, r.Title); - if (!match.IsSearchPrecisionScoreMet()) + MatchResult match; + if (!string.IsNullOrEmpty(r.StringMatcherKey)) { - match = StringMatcher.FuzzySearch(query, r.SubTitle); + match = StringMatcher.FuzzySearch(query, r.StringMatcherKey); + // If StringMatcherKey is not met, we should not check Title and SubTitle because + // developers have set the StringMatcherKey to be the only searchable field + } + else + { + match = StringMatcher.FuzzySearch(query, r.Title); + if (!match.IsSearchPrecisionScoreMet()) + { + match = StringMatcher.FuzzySearch(query, r.SubTitle); + } } if (!match.IsSearchPrecisionScoreMet()) return false; @@ -1067,8 +1078,10 @@ private void QueryHistory() { var filtered = results.Where ( - r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() || - StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet() + r => (!string.IsNullOrEmpty(r.StringMatcherKey)) ? + StringMatcher.FuzzySearch(query, r.StringMatcherKey).IsSearchPrecisionScoreMet() : + StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() || + StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet() ).ToList(); History.AddResults(filtered, id); }