mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Add IExclusivePlugin
This commit is contained in:
@@ -78,7 +78,6 @@ namespace Wox.Core.Plugin
|
||||
try
|
||||
{
|
||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||
metadata.PluginType = PluginType.User;
|
||||
metadata.PluginDirectory = pluginDirectory;
|
||||
}
|
||||
catch (System.Exception)
|
||||
|
||||
@@ -112,7 +112,6 @@ namespace Wox.Core.Plugin
|
||||
try
|
||||
{
|
||||
metadata = JsonConvert.DeserializeObject<PluginMetadata>(File.ReadAllText(configPath));
|
||||
metadata.PluginType = PluginType.User;
|
||||
metadata.PluginDirectory = pluginDirectory;
|
||||
}
|
||||
catch (System.Exception)
|
||||
|
||||
@@ -23,8 +23,8 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
public const string ActionKeywordWildcardSign = "*";
|
||||
private static List<PluginMetadata> pluginMetadatas;
|
||||
private static List<KeyValuePair<PluginMetadata, IInstantSearch>> instantSearches;
|
||||
private static List<KeyValuePair<PluginPair,IExclusiveSearch>> exclusiveSearchPlugins;
|
||||
private static List<KeyValuePair<PluginMetadata, IInstantQuery>> instantSearches;
|
||||
private static List<KeyValuePair<PluginPair, IExclusiveQuery>> exclusiveSearchPlugins;
|
||||
|
||||
public static String DebuggerMode { get; private set; }
|
||||
public static IPublicAPI API { get; private set; }
|
||||
@@ -117,7 +117,7 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
if (!string.IsNullOrEmpty(query.RawQuery.Trim()))
|
||||
{
|
||||
query.Search = IsUserPluginQuery(query) ? query.RawQuery.Substring(query.RawQuery.IndexOf(' ') + 1) : query.RawQuery;
|
||||
query.Search = IsActionKeywordQuery(query) ? query.RawQuery.Substring(query.RawQuery.IndexOf(' ') + 1) : query.RawQuery;
|
||||
QueryDispatcher.QueryDispatcher.Dispatch(query);
|
||||
}
|
||||
}
|
||||
@@ -130,7 +130,12 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsUserPluginQuery(Query query)
|
||||
/// <summary>
|
||||
/// Check if a query contains action keyword
|
||||
/// </summary>
|
||||
/// <param name="query"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsActionKeywordQuery(Query query)
|
||||
{
|
||||
if (string.IsNullOrEmpty(query.RawQuery)) return false;
|
||||
var strings = query.RawQuery.Split(' ');
|
||||
@@ -139,10 +144,10 @@ namespace Wox.Core.Plugin
|
||||
var actionKeyword = strings[0].Trim();
|
||||
if (string.IsNullOrEmpty(actionKeyword)) return false;
|
||||
|
||||
return plugins.Any(o => o.Metadata.PluginType == PluginType.User && o.Metadata.ActionKeyword == actionKeyword);
|
||||
return plugins.Any(o => o.Metadata.ActionKeyword == actionKeyword);
|
||||
}
|
||||
|
||||
public static bool IsSystemPlugin(PluginMetadata metadata)
|
||||
public static bool IsGenericPlugin(PluginMetadata metadata)
|
||||
{
|
||||
return metadata.ActionKeyword == ActionKeywordWildcardSign;
|
||||
}
|
||||
@@ -152,9 +157,9 @@ namespace Wox.Core.Plugin
|
||||
DebuggerMode = path;
|
||||
}
|
||||
|
||||
public static bool IsInstantSearch(string query)
|
||||
public static bool IsInstantQuery(string query)
|
||||
{
|
||||
return LoadInstantSearches().Any(o => o.Value.IsInstantSearch(query));
|
||||
return LoadInstantSearches().Any(o => o.Value.IsInstantQuery(query));
|
||||
}
|
||||
|
||||
public static bool IsInstantSearchPlugin(PluginMetadata pluginMetadata)
|
||||
@@ -194,11 +199,11 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
}
|
||||
|
||||
private static List<KeyValuePair<PluginMetadata, IInstantSearch>> LoadInstantSearches()
|
||||
private static List<KeyValuePair<PluginMetadata, IInstantQuery>> LoadInstantSearches()
|
||||
{
|
||||
if (instantSearches != null) return instantSearches;
|
||||
|
||||
instantSearches = new List<KeyValuePair<PluginMetadata, IInstantSearch>>();
|
||||
instantSearches = new List<KeyValuePair<PluginMetadata, IInstantQuery>>();
|
||||
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
||||
|
||||
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
|
||||
@@ -206,7 +211,7 @@ namespace Wox.Core.Plugin
|
||||
try
|
||||
{
|
||||
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IInstantSearch))).ToList();
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IInstantQuery))).ToList();
|
||||
if (types.Count == 0)
|
||||
{
|
||||
continue;
|
||||
@@ -214,7 +219,7 @@ namespace Wox.Core.Plugin
|
||||
|
||||
foreach (Type type in types)
|
||||
{
|
||||
instantSearches.Add(new KeyValuePair<PluginMetadata, IInstantSearch>(metadata, Activator.CreateInstance(type) as IInstantSearch));
|
||||
instantSearches.Add(new KeyValuePair<PluginMetadata, IInstantQuery>(metadata, Activator.CreateInstance(type) as IInstantQuery));
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
@@ -241,11 +246,11 @@ namespace Wox.Core.Plugin
|
||||
return AllPlugins.FirstOrDefault(o => o.Metadata.ID == id);
|
||||
}
|
||||
|
||||
internal static List<KeyValuePair<PluginPair, IExclusiveSearch>> LoadExclusiveSearchPlugins()
|
||||
internal static List<KeyValuePair<PluginPair, IExclusiveQuery>> LoadExclusiveSearchPlugins()
|
||||
{
|
||||
if (exclusiveSearchPlugins != null) return exclusiveSearchPlugins;
|
||||
|
||||
exclusiveSearchPlugins = new List<KeyValuePair<PluginPair, IExclusiveSearch>>();
|
||||
exclusiveSearchPlugins = new List<KeyValuePair<PluginPair, IExclusiveQuery>>();
|
||||
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
||||
|
||||
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
|
||||
@@ -253,7 +258,7 @@ namespace Wox.Core.Plugin
|
||||
try
|
||||
{
|
||||
Assembly asm = Assembly.Load(AssemblyName.GetAssemblyName(metadata.ExecuteFilePath));
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IExclusiveSearch))).ToList();
|
||||
List<Type> types = asm.GetTypes().Where(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Contains(typeof(IExclusiveQuery))).ToList();
|
||||
if (types.Count == 0)
|
||||
{
|
||||
continue;
|
||||
@@ -261,8 +266,8 @@ namespace Wox.Core.Plugin
|
||||
|
||||
foreach (Type type in types)
|
||||
{
|
||||
exclusiveSearchPlugins.Add(new KeyValuePair<PluginPair, IExclusiveSearch>(AllPlugins.First(o => o.Metadata.ID == metadata.ID),
|
||||
Activator.CreateInstance(type) as IExclusiveSearch));
|
||||
exclusiveSearchPlugins.Add(new KeyValuePair<PluginPair, IExclusiveQuery>(AllPlugins.First(o => o.Metadata.ID == metadata.ID),
|
||||
Activator.CreateInstance(type) as IExclusiveQuery));
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
@@ -279,12 +284,31 @@ namespace Wox.Core.Plugin
|
||||
return exclusiveSearchPlugins;
|
||||
}
|
||||
|
||||
internal static PluginPair GetExclusiveSearchPlugin(Query query)
|
||||
internal static PluginPair GetExclusivePlugin(Query query)
|
||||
{
|
||||
KeyValuePair<PluginPair, IExclusiveSearch> plugin = LoadExclusiveSearchPlugins().FirstOrDefault(o => o.Value.IsExclusiveSearch((query)));
|
||||
if (plugin.Key != null) return plugin.Key;
|
||||
KeyValuePair<PluginPair, IExclusiveQuery> plugin = LoadExclusiveSearchPlugins().FirstOrDefault(o => o.Value.IsExclusiveQuery((query)));
|
||||
return plugin.Key;
|
||||
}
|
||||
|
||||
internal static PluginPair GetActionKeywordPlugin(Query query)
|
||||
{
|
||||
PluginPair exclusivePluginPair = AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.GetActionKeyword());
|
||||
if (exclusivePluginPair != null)
|
||||
{
|
||||
var customizedPluginConfig = UserSettingStorage.Instance.
|
||||
CustomizedPluginConfigs.FirstOrDefault(o => o.ID == exclusivePluginPair.Metadata.ID);
|
||||
if (customizedPluginConfig != null && !customizedPluginConfig.Disabled)
|
||||
{
|
||||
return exclusivePluginPair;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
internal static bool IsExclusivePluginQuery(Query query)
|
||||
{
|
||||
return GetExclusivePlugin(query) != null || GetActionKeywordPlugin(query) != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
Wox.Core/Plugin/QueryDispatcher/ExclusiveQueryDispatcher.cs
Normal file
30
Wox.Core/Plugin/QueryDispatcher/ExclusiveQueryDispatcher.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Wox.Core.Exception;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
public class ExclusiveQueryDispatcher : BaseQueryDispatcher
|
||||
{
|
||||
protected override List<PluginPair> GetPlugins(Query query)
|
||||
{
|
||||
List<PluginPair> pluginPairs = new List<PluginPair>();
|
||||
var exclusivePluginPair = PluginManager.GetExclusivePlugin(query) ??
|
||||
PluginManager.GetActionKeywordPlugin(query);
|
||||
if (exclusivePluginPair != null)
|
||||
{
|
||||
pluginPairs.Add(exclusivePluginPair);
|
||||
}
|
||||
|
||||
return pluginPairs;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -8,14 +8,11 @@ using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
public class SystemPluginQueryDispatcher : BaseQueryDispatcher
|
||||
public class GenericQueryDispatcher : BaseQueryDispatcher
|
||||
{
|
||||
private readonly List<PluginPair> allSytemPlugins =
|
||||
PluginManager.AllPlugins.Where(o => PluginManager.IsSystemPlugin(o.Metadata)).ToList();
|
||||
|
||||
protected override List<PluginPair> GetPlugins(Query query)
|
||||
{
|
||||
return allSytemPlugins;
|
||||
return PluginManager.AllPlugins.Where(o => PluginManager.IsGenericPlugin(o.Metadata)).ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,28 +6,18 @@ namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
internal static class QueryDispatcher
|
||||
{
|
||||
private static readonly IQueryDispatcher UserPluginDispatcher = new UserPluginQueryDispatcher();
|
||||
private static readonly IQueryDispatcher SystemPluginDispatcher = new SystemPluginQueryDispatcher();
|
||||
private static readonly IQueryDispatcher exclusivePluginDispatcher = new ExclusiveQueryDispatcher();
|
||||
private static readonly IQueryDispatcher genericQueryDispatcher = new GenericQueryDispatcher();
|
||||
|
||||
public static void Dispatch(Wox.Plugin.Query query)
|
||||
public static void Dispatch(Query query)
|
||||
{
|
||||
PluginPair exclusiveSearchPlugin = PluginManager.GetExclusiveSearchPlugin(query);
|
||||
if (exclusiveSearchPlugin != null)
|
||||
if (PluginManager.IsExclusivePluginQuery(query))
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(state =>
|
||||
{
|
||||
PluginManager.ExecutePluginQuery(exclusiveSearchPlugin, query);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (PluginManager.IsUserPluginQuery(query))
|
||||
{
|
||||
UserPluginDispatcher.Dispatch(query);
|
||||
exclusivePluginDispatcher.Dispatch(query);
|
||||
}
|
||||
else
|
||||
{
|
||||
SystemPluginDispatcher.Dispatch(query);
|
||||
genericQueryDispatcher.Dispatch(query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Wox.Core.Exception;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Infrastructure.Logger;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
public class UserPluginQueryDispatcher : BaseQueryDispatcher
|
||||
{
|
||||
protected override List<PluginPair> GetPlugins(Query query)
|
||||
{
|
||||
List<PluginPair> plugins = new List<PluginPair>();
|
||||
//only first plugin that matches action keyword will get executed
|
||||
PluginPair userPlugin = PluginManager.AllPlugins.FirstOrDefault(o => o.Metadata.ActionKeyword == query.GetActionKeyword());
|
||||
if (userPlugin != null)
|
||||
{
|
||||
var customizedPluginConfig = UserSettingStorage.Instance.
|
||||
CustomizedPluginConfigs.FirstOrDefault(o => o.ID == userPlugin.Metadata.ID);
|
||||
if (customizedPluginConfig != null && customizedPluginConfig.Disabled)
|
||||
{
|
||||
//need to stop the loading animation
|
||||
PluginManager.API.StopLoadingBar();
|
||||
}
|
||||
else
|
||||
{
|
||||
plugins.Add(userPlugin);
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user