Flow.Launcher/Plugins/Flow.Launcher.Plugin.PluginIndicator/Main.cs

85 lines
3 KiB
C#
Raw Permalink Normal View History

using System.Collections.Generic;
2014-01-06 11:03:20 +00:00
using System.Linq;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Plugin.PluginIndicator
2014-01-06 11:03:20 +00:00
{
2025-05-03 08:31:49 +00:00
public class Main : IPlugin, IPluginI18n, IHomeQuery
2014-01-06 11:03:20 +00:00
{
internal static PluginInitContext Context { get; private set; }
2014-01-06 11:03:20 +00:00
public void Init(PluginInitContext context)
2025-05-03 08:31:49 +00:00
{
Context = context;
2025-05-03 08:31:49 +00:00
}
public List<Result> Query(Query query)
2025-05-03 08:31:49 +00:00
{
return QueryResults(query);
2025-05-03 08:31:49 +00:00
}
private static List<Result> QueryResults(Query query = null)
2014-01-06 11:03:20 +00:00
{
var nonGlobalPlugins = GetNonGlobalPlugins();
2025-05-03 08:31:49 +00:00
var querySearch = query?.Search ?? string.Empty;
var results =
from keyword in nonGlobalPlugins.Keys
let plugin = nonGlobalPlugins[keyword].Metadata
2025-05-03 08:31:49 +00:00
let keywordSearchResult = Context.API.FuzzySearch(querySearch, keyword)
let searchResult = keywordSearchResult.IsSearchPrecisionScoreMet() ? keywordSearchResult : Context.API.FuzzySearch(querySearch, plugin.Name)
2023-01-01 02:57:27 +00:00
let score = searchResult.Score
where (searchResult.IsSearchPrecisionScoreMet()
2025-05-03 08:31:49 +00:00
|| string.IsNullOrEmpty(querySearch)) // To list all available action keywords
2023-01-01 02:57:27 +00:00
&& !plugin.Disabled
select new Result
{
Title = keyword,
SubTitle = Localize.flowlauncher_plugin_pluginindicator_result_subtitle(plugin.Name),
2023-01-01 02:57:27 +00:00
Score = score,
IcoPath = plugin.IcoPath,
AutoCompleteText = $"{keyword}{Plugin.Query.TermSeparator}",
Action = c =>
{
Context.API.ChangeQuery($"{keyword}{Plugin.Query.TermSeparator}");
return false;
}
};
return [.. results];
2014-01-06 11:03:20 +00:00
}
private static Dictionary<string, PluginPair> GetNonGlobalPlugins()
{
var nonGlobalPlugins = new Dictionary<string, PluginPair>();
foreach (var plugin in Context.API.GetAllPlugins())
{
foreach (var actionKeyword in plugin.Metadata.ActionKeywords)
{
// Skip global keywords
if (actionKeyword == Plugin.Query.GlobalPluginWildcardSign) continue;
// Skip dulpicated keywords
if (nonGlobalPlugins.ContainsKey(actionKeyword)) continue;
nonGlobalPlugins.Add(actionKeyword, plugin);
}
}
return nonGlobalPlugins;
}
public string GetTranslatedPluginTitle()
2014-01-06 11:03:20 +00:00
{
return Localize.flowlauncher_plugin_pluginindicator_plugin_name();
2014-01-06 11:03:20 +00:00
}
2015-02-07 13:27:48 +00:00
public string GetTranslatedPluginDescription()
2015-02-07 13:27:48 +00:00
{
return Localize.flowlauncher_plugin_pluginindicator_plugin_description();
2015-02-07 13:27:48 +00:00
}
public List<Result> HomeQuery()
2015-02-07 13:27:48 +00:00
{
return QueryResults();
2015-02-07 13:27:48 +00:00
}
2014-01-06 11:03:20 +00:00
}
}