Skip to content

Commit 6e763f0

Browse files
committed
Remove ReplaceActionKeyword api function & Add action keyword same noticification
1 parent 3840284 commit 6e763f0

File tree

7 files changed

+57
-77
lines changed

7 files changed

+57
-77
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,16 @@ public static void AddActionKeyword(string id, string newActionKeyword)
364364
NonGlobalPlugins[newActionKeyword] = plugin;
365365
}
366366

367-
// Update action keywords in plugin metadata
367+
// Update action keywords and action keyword in plugin metadata
368368
plugin.Metadata.ActionKeywords.Add(newActionKeyword);
369+
if (plugin.Metadata.ActionKeywords.Count > 0)
370+
{
371+
plugin.Metadata.ActionKeyword = plugin.Metadata.ActionKeywords[0];
372+
}
373+
else
374+
{
375+
plugin.Metadata.ActionKeyword = string.Empty;
376+
}
369377
}
370378

371379
/// <summary>
@@ -386,67 +394,15 @@ public static void RemoveActionKeyword(string id, string oldActionkeyword)
386394
if (oldActionkeyword != Query.GlobalPluginWildcardSign)
387395
NonGlobalPlugins.Remove(oldActionkeyword);
388396

389-
// Update action keywords in plugin metadata
397+
// Update action keywords and action keyword in plugin metadata
390398
plugin.Metadata.ActionKeywords.Remove(oldActionkeyword);
391-
}
392-
393-
public static void ReplaceActionKeyword(string id, IReadOnlyList<string> oldActionKeywords, IReadOnlyList<string> newActionKeywords)
394-
{
395-
if (CheckActionKeywordChanged(oldActionKeywords, newActionKeywords))
399+
if (plugin.Metadata.ActionKeywords.Count > 0)
396400
{
397-
// Fix collection modified while iterating exception
398-
var oldActionKeywordsClone = oldActionKeywords.ToList();
399-
foreach (var actionKeyword in oldActionKeywordsClone)
400-
{
401-
RemoveActionKeyword(id, actionKeyword);
402-
}
403-
foreach (var actionKeyword in newActionKeywords)
404-
{
405-
AddActionKeyword(id, actionKeyword);
406-
}
407-
408-
// Update action keyword in plugin metadata
409-
var plugin = GetPluginForId(id);
410-
if (newActionKeywords.Count > 0)
411-
{
412-
plugin.Metadata.ActionKeyword = newActionKeywords[0];
413-
}
414-
else
415-
{
416-
plugin.Metadata.ActionKeyword = string.Empty;
417-
}
401+
plugin.Metadata.ActionKeyword = plugin.Metadata.ActionKeywords[0];
418402
}
419-
}
420-
421-
private static bool CheckActionKeywordChanged(IReadOnlyList<string> oldActionKeywords, IReadOnlyList<string> newActionKeywords)
422-
{
423-
if (oldActionKeywords.Count != newActionKeywords.Count)
424-
return true;
425-
426-
var sortedOldActionKeywords = oldActionKeywords.OrderBy(s => s).ToList();
427-
var sortedNewActionKeywords = newActionKeywords.OrderBy(s => s).ToList();
428-
429-
return !sortedOldActionKeywords.SequenceEqual(sortedNewActionKeywords);
430-
}
431-
432-
public static void ReplaceActionKeyword(string id, string oldActionKeyword, string newActionKeyword)
433-
{
434-
if (oldActionKeyword != newActionKeyword)
403+
else
435404
{
436-
RemoveActionKeyword(id, oldActionKeyword);
437-
AddActionKeyword(id, newActionKeyword);
438-
439-
// Update action keyword in plugin metadata
440-
var plugin = GetPluginForId(id);
441-
var newActionKeywords = plugin.Metadata.ActionKeywords;
442-
if (newActionKeywords.Count > 0)
443-
{
444-
plugin.Metadata.ActionKeyword = newActionKeywords[0];
445-
}
446-
else
447-
{
448-
plugin.Metadata.ActionKeyword = string.Empty;
449-
}
405+
plugin.Metadata.ActionKeyword = string.Empty;
450406
}
451407
}
452408

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ public interface IPublicAPI
189189
Task HttpDownloadAsync([NotNull] string url, [NotNull] string filePath, Action<double> reportProgress = null, CancellationToken token = default);
190190

191191
/// <summary>
192-
/// Add ActionKeyword for specific plugin
192+
/// Add ActionKeyword and update action keyword metadata for specific plugin
193193
/// </summary>
194194
/// <param name="pluginId">ID for plugin that needs to add action keyword</param>
195195
/// <param name="newActionKeyword">The actionkeyword that is supposed to be added</param>
196196
void AddActionKeyword(string pluginId, string newActionKeyword);
197197

198198
/// <summary>
199-
/// Remove ActionKeyword for specific plugin
199+
/// Remove ActionKeyword and update action keyword metadata for specific plugin
200200
/// </summary>
201201
/// <param name="pluginId">ID for plugin that needs to remove action keyword</param>
202202
/// <param name="oldActionKeyword">The actionkeyword that is supposed to be removed</param>
@@ -333,13 +333,5 @@ public interface IPublicAPI
333333
/// <param name="forceClosed">When user closes the progress box manually by button or esc key, this action will be called.</param>
334334
/// <returns>A progress box interface.</returns>
335335
public Task ShowProgressBoxAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action forceClosed = null);
336-
337-
/// <summary>
338-
/// Replace ActionKeyword for specific plugin
339-
/// </summary>
340-
/// <param name="pluginId">ID for plugin that needs to remove action keyword</param>
341-
/// <param name="oldActionKeyword">The actionkeyword that is supposed to be removed</param>
342-
/// <param name="newActionKeyword">The actionkeyword that is supposed to be added</param>
343-
public void ReplaceActionKeyword(string pluginId, string oldActionKeyword, string newActionKeyword);
344336
}
345337
}

Flow.Launcher/ActionKeywords.xaml.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,49 @@ private void btnDone_OnClick(object sender, RoutedEventArgs _)
4545

4646
if (!newActionKeywords.Except(oldActionKeywords).Any(PluginManager.ActionKeywordRegistered))
4747
{
48-
pluginViewModel.ChangeActionKeyword(newActionKeywords, oldActionKeywords);
49-
Close();
48+
if (oldActionKeywords.Count != newActionKeywords.Count)
49+
{
50+
ReplaceActionKeyword(plugin.Metadata.ID, oldActionKeywords, newActionKeywords);
51+
}
52+
53+
var sortedOldActionKeywords = oldActionKeywords.OrderBy(s => s).ToList();
54+
var sortedNewActionKeywords = newActionKeywords.OrderBy(s => s).ToList();
55+
56+
if (sortedOldActionKeywords.SequenceEqual(sortedNewActionKeywords))
57+
{
58+
// User just changes the sequence of action keywords
59+
var msg = translater.GetTranslation("newActionKeywordsSameAsOld");
60+
MessageBoxEx.Show(msg);
61+
}
62+
else
63+
{
64+
ReplaceActionKeyword(plugin.Metadata.ID, oldActionKeywords, newActionKeywords);
65+
}
5066
}
5167
else
5268
{
5369
string msg = translater.GetTranslation("newActionKeywordsHasBeenAssigned");
5470
MessageBoxEx.Show(msg);
5571
}
5672
}
73+
74+
private void ReplaceActionKeyword(string id, IReadOnlyList<string> oldActionKeywords, IReadOnlyList<string> newActionKeywords)
75+
{
76+
// Because add & remove action keyword will change action keyword metadata,
77+
// so we need to clone it to fix collection modified while iterating exception
78+
var oldActionKeywordsClone = oldActionKeywords.ToList();
79+
foreach (var actionKeyword in oldActionKeywordsClone)
80+
{
81+
PluginManager.RemoveActionKeyword(id, actionKeyword);
82+
}
83+
foreach (var actionKeyword in newActionKeywords)
84+
{
85+
PluginManager.AddActionKeyword(id, actionKeyword);
86+
}
87+
88+
// Update action keywords text and close
89+
pluginViewModel.OnActionKeywordsChanged();
90+
Close();
91+
}
5792
}
5893
}

Flow.Launcher/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
<system:String x:Key="cannotFindSpecifiedPlugin">Can't find specified plugin</system:String>
334334
<system:String x:Key="newActionKeywordsCannotBeEmpty">New Action Keyword can't be empty</system:String>
335335
<system:String x:Key="newActionKeywordsHasBeenAssigned">This new Action Keyword is already assigned to another plugin, please choose a different one</system:String>
336+
<system:String x:Key="newActionKeywordsSameAsOld">This new Action Keyword is the same as old, please choose a different one</system:String>
336337
<system:String x:Key="success">Success</system:String>
337338
<system:String x:Key="completedSuccessfully">Completed successfully</system:String>
338339
<system:String x:Key="actionkeyword_tips">Enter the action keywords you like to use to start the plugin and use whitespace to divide them. Use * if you don't want to specify any, and the plugin will be triggered without any action keywords.</system:String>

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,6 @@ public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", M
326326

327327
public Task ShowProgressBoxAsync(string caption, Func<Action<double>, Task> reportProgressAsync, Action forceClosed = null) => ProgressBoxEx.ShowAsync(caption, reportProgressAsync, forceClosed);
328328

329-
public void ReplaceActionKeyword(string pluginId, string oldActionKeyword, string newActionKeyword) => PluginManager.ReplaceActionKeyword(pluginId, oldActionKeyword, newActionKeyword);
330-
331329
#endregion
332330

333331
#region Private Methods

Flow.Launcher/ViewModel/PluginViewModel.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,8 @@ public Control SettingControl
110110
public int Priority => PluginPair.Metadata.Priority;
111111
public Infrastructure.UserSettings.Plugin PluginSettingsObject { get; set; }
112112

113-
public void ChangeActionKeyword(IReadOnlyList<string> newActionKeywords, IReadOnlyList<string> oldActionKeywords)
113+
public void OnActionKeywordsChanged()
114114
{
115-
PluginManager.ReplaceActionKeyword(PluginPair.Metadata.ID, oldActionKeywords, newActionKeywords);
116115
OnPropertyChanged(nameof(ActionKeywordsText));
117116
}
118117

@@ -158,5 +157,4 @@ private void SetActionKeywords()
158157
changeKeywordsWindow.ShowDialog();
159158
}
160159
}
161-
162160
}

Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public partial class SearchSourceSettingWindow
1515
private SearchSourceViewModel _viewModel;
1616
private string selectedNewIconImageFullPath;
1717

18-
1918
public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context, SearchSource old)
2019
{
2120
_oldSearchSource = old;
@@ -102,7 +101,8 @@ private void EditSearchSource()
102101
if (!_context.API.ActionKeywordAssigned(newKeyword) || oldKeyword == newKeyword)
103102
{
104103
var id = _context.CurrentPluginMetadata.ID;
105-
_context.API.ReplaceActionKeyword(id, oldKeyword, newKeyword);
104+
_context.API.RemoveActionKeyword(id, oldKeyword);
105+
_context.API.AddActionKeyword(id, newKeyword);
106106

107107
var index = _searchSources.IndexOf(_oldSearchSource);
108108
_searchSources[index] = _searchSource;

0 commit comments

Comments
 (0)