From abfeee1423a011229ec6cd9dd7e85dddc2a90ac6 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 21 Feb 2025 12:59:56 +0800 Subject: [PATCH] Support multiple action keywords --- Flow.Launcher.Core/Plugin/PluginManager.cs | 29 +++++++++++++++++----- Flow.Launcher.Plugin/Query.cs | 4 +-- Flow.Launcher/ActionKeywords.xaml.cs | 15 +++++++---- Flow.Launcher/ViewModel/PluginViewModel.cs | 7 +++--- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/PluginManager.cs b/Flow.Launcher.Core/Plugin/PluginManager.cs index b1f397ea2..8c62c2f02 100644 --- a/Flow.Launcher.Core/Plugin/PluginManager.cs +++ b/Flow.Launcher.Core/Plugin/PluginManager.cs @@ -340,7 +340,12 @@ namespace Flow.Launcher.Core.Plugin return results; } - public static bool ActionKeywordRegistered(string actionKeyword) + public static bool ActionKeywordRegistered(IReadOnlyList actionKeywords) + { + return actionKeywords.Any(ActionKeywordRegistered); + } + + private static bool ActionKeywordRegistered(string actionKeyword) { // this method is only checking for action keywords (defined as not '*') registration // hence the actionKeyword != Query.GlobalPluginWildcardSign logic @@ -385,19 +390,31 @@ namespace Flow.Launcher.Core.Plugin if (oldActionkeyword != Query.GlobalPluginWildcardSign) NonGlobalPlugins.Remove(oldActionkeyword); - plugin.Metadata.ActionKeywords.Remove(oldActionkeyword); } - public static void ReplaceActionKeyword(string id, string oldActionKeyword, string newActionKeyword) + public static void ReplaceActionKeyword(string id, IReadOnlyList oldActionKeyword, IReadOnlyList newActionKeyword) { - if (oldActionKeyword != newActionKeyword) + if (CheckActionKeywordChanged(oldActionKeyword, newActionKeyword)) { - AddActionKeyword(id, newActionKeyword); - RemoveActionKeyword(id, oldActionKeyword); + foreach (var actionKeyword in newActionKeyword) + { + AddActionKeyword(id, actionKeyword); + } + foreach (var actionKeyword in oldActionKeyword) + { + RemoveActionKeyword(id, actionKeyword); + } } } + private static bool CheckActionKeywordChanged(IReadOnlyList oldActionKeyword, IReadOnlyList newActionKeyword) + { + if (oldActionKeyword.Count != newActionKeyword.Count) + return true; + return oldActionKeyword.Where((t, i) => t != newActionKeyword[i]).Any(); + } + private static string GetContainingFolderPathAfterUnzip(string unzippedParentFolderPath) { var unzippedFolderCount = Directory.GetDirectories(unzippedParentFolderPath).Length; diff --git a/Flow.Launcher.Plugin/Query.cs b/Flow.Launcher.Plugin/Query.cs index ab3c8cfeb..15b2dd171 100644 --- a/Flow.Launcher.Plugin/Query.cs +++ b/Flow.Launcher.Plugin/Query.cs @@ -39,9 +39,9 @@ namespace Flow.Launcher.Plugin public const string TermSeparator = " "; /// - /// User can set multiple action keywords seperated by ';' + /// User can set multiple action keywords seperated by whitespace /// - public const string ActionKeywordSeparator = ";"; + public const string ActionKeywordSeparator = TermSeparator; /// /// Wildcard action keyword. Plugins using this value will be queried on every search. diff --git a/Flow.Launcher/ActionKeywords.xaml.cs b/Flow.Launcher/ActionKeywords.xaml.cs index ba47a4ded..4a6bd10a8 100644 --- a/Flow.Launcher/ActionKeywords.xaml.cs +++ b/Flow.Launcher/ActionKeywords.xaml.cs @@ -3,6 +3,7 @@ using Flow.Launcher.Core.Resource; using Flow.Launcher.Plugin; using Flow.Launcher.ViewModel; using Flow.Launcher.Core; +using System.Linq; namespace Flow.Launcher { @@ -32,13 +33,17 @@ namespace Flow.Launcher private void btnDone_OnClick(object sender, RoutedEventArgs _) { - var oldActionKeyword = plugin.Metadata.ActionKeywords[0]; - var newActionKeyword = tbAction.Text.Trim(); - newActionKeyword = newActionKeyword.Length > 0 ? newActionKeyword : "*"; + var oldActionKeywords = plugin.Metadata.ActionKeywords; + + var newActionKeywords = tbAction.Text.Split(Query.ActionKeywordSeparator).ToList(); + newActionKeywords.RemoveAll(string.IsNullOrEmpty); + newActionKeywords = newActionKeywords.Distinct().ToList(); + + newActionKeywords = newActionKeywords.Count > 0 ? newActionKeywords : new() { Query.GlobalPluginWildcardSign }; - if (!PluginViewModel.IsActionKeywordRegistered(newActionKeyword)) + if (!PluginViewModel.IsActionKeywordRegistered(newActionKeywords)) { - pluginViewModel.ChangeActionKeyword(newActionKeyword, oldActionKeyword); + pluginViewModel.ChangeActionKeyword(newActionKeywords, oldActionKeywords); Close(); } else diff --git a/Flow.Launcher/ViewModel/PluginViewModel.cs b/Flow.Launcher/ViewModel/PluginViewModel.cs index 4b628f96e..748490c47 100644 --- a/Flow.Launcher/ViewModel/PluginViewModel.cs +++ b/Flow.Launcher/ViewModel/PluginViewModel.cs @@ -8,6 +8,7 @@ using System.Windows.Controls; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core.Resource; using Flow.Launcher.Resources.Controls; +using System.Collections.Generic; namespace Flow.Launcher.ViewModel { @@ -109,9 +110,9 @@ namespace Flow.Launcher.ViewModel public int Priority => PluginPair.Metadata.Priority; public Infrastructure.UserSettings.Plugin PluginSettingsObject { get; set; } - public void ChangeActionKeyword(string newActionKeyword, string oldActionKeyword) + public void ChangeActionKeyword(IReadOnlyList newActionKeywords, IReadOnlyList oldActionKeywords) { - PluginManager.ReplaceActionKeyword(PluginPair.Metadata.ID, oldActionKeyword, newActionKeyword); + PluginManager.ReplaceActionKeyword(PluginPair.Metadata.ID, oldActionKeywords, newActionKeywords); OnPropertyChanged(nameof(ActionKeywordsText)); } @@ -150,7 +151,7 @@ namespace Flow.Launcher.ViewModel PluginManager.API.ShowMainWindow(); } - public static bool IsActionKeywordRegistered(string newActionKeyword) => PluginManager.ActionKeywordRegistered(newActionKeyword); + public static bool IsActionKeywordRegistered(IReadOnlyList newActionKeywords) => PluginManager.ActionKeywordRegistered(newActionKeywords); [RelayCommand] private void SetActionKeywords()