mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-10 21:41:51 +02:00
Refactoring GetExclusivePlugin and GetActionKeywordPlugin
System plugin = plugin with action keyword * Non system plugin = exclusive plugin + action keyword plugin
This commit is contained in:
@@ -7,6 +7,7 @@ using System.Windows.Forms;
|
||||
using WindowsInput;
|
||||
using WindowsInput.Native;
|
||||
using Wox.Infrastructure.Hotkey;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin.Features;
|
||||
using Control = System.Windows.Controls.Control;
|
||||
|
||||
@@ -22,14 +23,13 @@ namespace Wox.Plugin.CMD
|
||||
{
|
||||
List<Result> results = new List<Result>();
|
||||
List<Result> pushedResults = new List<Result>();
|
||||
if (query.Search == ">")
|
||||
string cmd = query.Search;
|
||||
if (string.IsNullOrEmpty(cmd))
|
||||
{
|
||||
return GetAllHistoryCmds();
|
||||
}
|
||||
|
||||
if (query.Search.StartsWith(">") && query.Search.Length > 1)
|
||||
else
|
||||
{
|
||||
string cmd = query.Search.Substring(1);
|
||||
var queryCmd = GetCurrentCmd(cmd);
|
||||
context.API.PushResults(query, context.CurrentPluginMetadata, new List<Result>() { queryCmd });
|
||||
pushedResults.Add(queryCmd);
|
||||
@@ -49,7 +49,7 @@ namespace Wox.Plugin.CMD
|
||||
basedir = excmd;
|
||||
dir = cmd;
|
||||
}
|
||||
else if (Directory.Exists(Path.GetDirectoryName(excmd)))
|
||||
else if (Directory.Exists(Path.GetDirectoryName(excmd) ?? string.Empty))
|
||||
{
|
||||
basedir = Path.GetDirectoryName(excmd);
|
||||
var dirn = Path.GetDirectoryName(cmd);
|
||||
@@ -72,10 +72,12 @@ namespace Wox.Plugin.CMD
|
||||
}));
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private List<Result> GetHistoryCmds(string cmd, Result result)
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Wox.Plugin.PluginIndicator
|
||||
List<Result> results = new List<Result>();
|
||||
if (allPlugins.Count == 0)
|
||||
{
|
||||
allPlugins = context.API.GetAllPlugins().Where(o => !PluginManager.IsGenericPlugin(o.Metadata)).ToList();
|
||||
allPlugins = context.API.GetAllPlugins().Where(o => !PluginManager.IsSystemPlugin(o.Metadata)).ToList();
|
||||
}
|
||||
|
||||
foreach (PluginMetadata metadata in allPlugins.Select(o => o.Metadata))
|
||||
|
||||
@@ -21,11 +21,9 @@ namespace Wox.Core.Plugin
|
||||
/// </summary>
|
||||
public static class PluginManager
|
||||
{
|
||||
public const string ActionKeywordWildcardSign = "*";
|
||||
|
||||
private static List<PluginMetadata> pluginMetadatas;
|
||||
private static List<KeyValuePair<PluginPair, IInstantQuery>> instantSearches;
|
||||
private static List<KeyValuePair<PluginPair, IExclusiveQuery>> exclusiveSearchPlugins;
|
||||
private static IEnumerable<PluginPair> exclusiveSearchPlugins;
|
||||
private static List<KeyValuePair<PluginPair, IContextMenu>> contextMenuPlugins;
|
||||
|
||||
public static IPublicAPI API { get; private set; }
|
||||
@@ -133,7 +131,8 @@ namespace Wox.Core.Plugin
|
||||
|
||||
private static void QueryDispatch(Query query)
|
||||
{
|
||||
var pluginPairs = IsExclusivePluginQuery(query) ? GetExclusivePlugins(query) : GetGenericPlugins();
|
||||
var nonSystemPlugin = GetNonSystemPlugin(query);
|
||||
var pluginPairs = nonSystemPlugin != null ? new List<PluginPair> { nonSystemPlugin } : GetSystemPlugins();
|
||||
foreach (var plugin in pluginPairs)
|
||||
{
|
||||
var customizedPluginConfig = UserSettingStorage.Instance.
|
||||
@@ -175,24 +174,16 @@ namespace Wox.Core.Plugin
|
||||
/// <returns></returns>
|
||||
private static bool IsVailldActionKeyword(string actionKeyword)
|
||||
{
|
||||
if (string.IsNullOrEmpty(actionKeyword) || actionKeyword == Query.ActionKeywordWildcardSign) return false;
|
||||
PluginPair pair = plugins.FirstOrDefault(o => o.Metadata.ActionKeyword == actionKeyword);
|
||||
if (pair != null)
|
||||
{
|
||||
var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == pair.Metadata.ID);
|
||||
if (customizedPluginConfig != null && customizedPluginConfig.Disabled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
if (pair == null) return false;
|
||||
var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == pair.Metadata.ID);
|
||||
return customizedPluginConfig == null || !customizedPluginConfig.Disabled;
|
||||
}
|
||||
|
||||
public static bool IsGenericPlugin(PluginMetadata metadata)
|
||||
public static bool IsSystemPlugin(PluginMetadata metadata)
|
||||
{
|
||||
return metadata.ActionKeyword == ActionKeywordWildcardSign;
|
||||
return metadata.ActionKeyword == Query.ActionKeywordWildcardSign;
|
||||
}
|
||||
|
||||
public static bool IsInstantQuery(string query)
|
||||
@@ -256,62 +247,29 @@ namespace Wox.Core.Plugin
|
||||
return plugins.FirstOrDefault(o => o.Metadata.ID == id);
|
||||
}
|
||||
|
||||
private static List<KeyValuePair<PluginPair, IExclusiveQuery>> LoadExclusiveSearchPlugins()
|
||||
{
|
||||
if (exclusiveSearchPlugins != null) return exclusiveSearchPlugins;
|
||||
exclusiveSearchPlugins = AssemblyHelper.LoadPluginInterfaces<IExclusiveQuery>();
|
||||
return exclusiveSearchPlugins;
|
||||
}
|
||||
|
||||
private static PluginPair GetExclusivePlugin(Query query)
|
||||
{
|
||||
KeyValuePair<PluginPair, IExclusiveQuery> plugin = LoadExclusiveSearchPlugins().FirstOrDefault(o => o.Value.IsExclusiveQuery((query)));
|
||||
return plugin.Key;
|
||||
exclusiveSearchPlugins = exclusiveSearchPlugins ??
|
||||
plugins.Where(p => p.Plugin.GetType().GetInterfaces().Contains(typeof(IExclusiveQuery)));
|
||||
var plugin = exclusiveSearchPlugins.FirstOrDefault(p => ((IExclusiveQuery)p.Plugin).IsExclusiveQuery(query));
|
||||
return plugin;
|
||||
}
|
||||
|
||||
private static PluginPair GetActionKeywordPlugin(Query query)
|
||||
{
|
||||
//if a query doesn't contain a vaild action keyword, it should not be a action keword plugin query
|
||||
if (string.IsNullOrEmpty(query.ActionKeyword)) return null;
|
||||
|
||||
PluginPair actionKeywordPluginPair = AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionKeyword);
|
||||
if (actionKeywordPluginPair != null)
|
||||
{
|
||||
var customizedPluginConfig = UserSettingStorage.Instance.
|
||||
CustomizedPluginConfigs.FirstOrDefault(o => o.ID == actionKeywordPluginPair.Metadata.ID);
|
||||
if (customizedPluginConfig != null && customizedPluginConfig.Disabled)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return actionKeywordPluginPair;
|
||||
}
|
||||
|
||||
return null;
|
||||
return plugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.ActionKeyword);
|
||||
}
|
||||
|
||||
private static List<PluginPair> GetExclusivePlugins(Query query)
|
||||
private static PluginPair GetNonSystemPlugin(Query query)
|
||||
{
|
||||
List<PluginPair> pluginPairs = new List<PluginPair>();
|
||||
var exclusivePluginPair = GetExclusivePlugin(query) ??
|
||||
GetActionKeywordPlugin(query);
|
||||
if (exclusivePluginPair != null)
|
||||
{
|
||||
pluginPairs.Add(exclusivePluginPair);
|
||||
}
|
||||
|
||||
return pluginPairs;
|
||||
return GetExclusivePlugin(query) ?? GetActionKeywordPlugin(query);
|
||||
}
|
||||
|
||||
private static List<PluginPair> GetGenericPlugins()
|
||||
private static List<PluginPair> GetSystemPlugins()
|
||||
{
|
||||
return plugins.Where(o => IsGenericPlugin(o.Metadata)).ToList();
|
||||
}
|
||||
|
||||
|
||||
private static bool IsExclusivePluginQuery(Query query)
|
||||
{
|
||||
return GetExclusivePlugin(query) != null || GetActionKeywordPlugin(query) != null;
|
||||
return plugins.Where(o => IsSystemPlugin(o.Metadata)).ToList();
|
||||
}
|
||||
|
||||
public static List<Result> GetPluginContextMenus(Result result)
|
||||
|
||||
@@ -43,8 +43,8 @@ namespace Wox.Plugin
|
||||
{
|
||||
get
|
||||
{
|
||||
var index = string.IsNullOrEmpty(ActionKeyword) ? 1 : 2;
|
||||
return string.Join(Seperater, Terms.Skip(index).ToArray());
|
||||
var index = String.IsNullOrEmpty(ActionKeyword) ? 1 : 2;
|
||||
return String.Join(Seperater, Terms.Skip(index).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,11 +62,11 @@ namespace Wox.Plugin
|
||||
{
|
||||
try
|
||||
{
|
||||
return string.IsNullOrEmpty(ActionKeyword) ? Terms[index] : Terms[index + 1];
|
||||
return String.IsNullOrEmpty(ActionKeyword) ? Terms[index] : Terms[index + 1];
|
||||
}
|
||||
catch (IndexOutOfRangeException)
|
||||
{
|
||||
return string.Empty;
|
||||
return String.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ namespace Wox.Plugin
|
||||
{
|
||||
// replace multiple white spaces with one white space
|
||||
Terms = rawQuery.Split(new[] { Seperater }, StringSplitOptions.RemoveEmptyEntries);
|
||||
RawQuery = string.Join(Seperater, Terms.ToArray());
|
||||
RawQuery = String.Join(Seperater, Terms.ToArray());
|
||||
|
||||
ActionParameters = new List<string>();
|
||||
ParseQuery();
|
||||
@@ -91,7 +91,7 @@ namespace Wox.Plugin
|
||||
|
||||
private void ParseQuery()
|
||||
{
|
||||
if (string.IsNullOrEmpty(RawQuery)) return;
|
||||
if (String.IsNullOrEmpty(RawQuery)) return;
|
||||
|
||||
string[] strings = RawQuery.Split(' ');
|
||||
//todo:not exactly correct. query that didn't containing a space should be a valid query
|
||||
@@ -100,7 +100,7 @@ namespace Wox.Plugin
|
||||
ActionName = strings[0];
|
||||
for (int i = 1; i < strings.Length; i++)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(strings[i]))
|
||||
if (!String.IsNullOrEmpty(strings[i]))
|
||||
{
|
||||
ActionParameters.Add(strings[i]);
|
||||
}
|
||||
@@ -111,13 +111,15 @@ namespace Wox.Plugin
|
||||
public string GetAllRemainingParameter()
|
||||
{
|
||||
|
||||
string[] strings = RawQuery.Split(new char[] { ' ' }, 2, System.StringSplitOptions.None);
|
||||
string[] strings = RawQuery.Split(new char[] { ' ' }, 2, StringSplitOptions.None);
|
||||
if (strings.Length > 1)
|
||||
{
|
||||
return strings[1];
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
return String.Empty;
|
||||
}
|
||||
|
||||
public const string ActionKeywordWildcardSign = "*";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace Wox
|
||||
}
|
||||
|
||||
//check new action keyword didn't used by other plugin
|
||||
if (tbAction.Text.Trim() != PluginManager.ActionKeywordWildcardSign && PluginManager.AllPlugins.Exists(o => o.Metadata.ActionKeyword == tbAction.Text.Trim()))
|
||||
if (tbAction.Text.Trim() != Query.ActionKeywordWildcardSign && PluginManager.AllPlugins.Exists(o => o.Metadata.ActionKeyword == tbAction.Text.Trim()))
|
||||
{
|
||||
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("newActionKeywordHasBeenAssigned"));
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user