mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 03:36:44 +02:00
Improve instant search ue
This commit is contained in:
@@ -21,7 +21,7 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
public const string ActionKeywordWildcardSign = "*";
|
||||
private static List<PluginMetadata> pluginMetadatas;
|
||||
private static List<IInstantSearch> instantSearches = new List<IInstantSearch>();
|
||||
private static List<KeyValuePair<PluginMetadata,IInstantSearch>> instantSearches;
|
||||
|
||||
|
||||
public static String DebuggerMode { get; private set; }
|
||||
@@ -98,7 +98,10 @@ namespace Wox.Core.Plugin
|
||||
});
|
||||
}
|
||||
|
||||
LoadInstantSearches();
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
{
|
||||
LoadInstantSearches();
|
||||
});
|
||||
}
|
||||
|
||||
public static void InstallPlugin(string path)
|
||||
@@ -146,12 +149,38 @@ namespace Wox.Core.Plugin
|
||||
|
||||
public static bool IsInstantSearch(string query)
|
||||
{
|
||||
return LoadInstantSearches().Any(o => o.IsInstantSearch(query));
|
||||
return LoadInstantSearches().Any(o => o.Value.IsInstantSearch(query));
|
||||
}
|
||||
|
||||
private static List<IInstantSearch> LoadInstantSearches()
|
||||
public static bool IsInstantSearchPlugin(PluginMetadata pluginMetadata)
|
||||
{
|
||||
if (instantSearches.Count > 0) return instantSearches;
|
||||
//todo:to improve performance, any instant search plugin that takes long than 200ms will not consider a instant plugin anymore
|
||||
return pluginMetadata.Language.ToUpper() == AllowedLanguage.CSharp &&
|
||||
LoadInstantSearches().Any(o => o.Key.ID == pluginMetadata.ID);
|
||||
}
|
||||
|
||||
internal static void ExecutePluginQuery(PluginPair pair, Query query)
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Result> results = pair.Plugin.Query(query) ?? new List<Result>();
|
||||
results.ForEach(o =>
|
||||
{
|
||||
o.PluginID = pair.Metadata.ID;
|
||||
});
|
||||
API.PushResults(query, pair.Metadata, results);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
throw new WoxPluginException(pair.Metadata.Name, e);
|
||||
}
|
||||
}
|
||||
|
||||
private static List<KeyValuePair<PluginMetadata,IInstantSearch>> LoadInstantSearches()
|
||||
{
|
||||
if (instantSearches != null) return instantSearches;
|
||||
|
||||
instantSearches = new List<KeyValuePair<PluginMetadata, IInstantSearch>>();
|
||||
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
||||
|
||||
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
|
||||
@@ -167,7 +196,7 @@ namespace Wox.Core.Plugin
|
||||
|
||||
foreach (Type type in types)
|
||||
{
|
||||
instantSearches.Add(Activator.CreateInstance(type) as IInstantSearch);
|
||||
instantSearches.Add(new KeyValuePair<PluginMetadata, IInstantSearch>(metadata,Activator.CreateInstance(type) as IInstantSearch));
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
|
||||
39
Wox.Core/Plugin/QueryDispatcher/BaseQueryDispatcher.cs
Normal file
39
Wox.Core/Plugin/QueryDispatcher/BaseQueryDispatcher.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
public abstract class BaseQueryDispatcher : IQueryDispatcher
|
||||
{
|
||||
protected abstract List<PluginPair> GetPlugins(Query query);
|
||||
|
||||
public void Dispatch(Query query)
|
||||
{
|
||||
foreach (PluginPair pair in GetPlugins(query))
|
||||
{
|
||||
PluginPair localPair = pair;
|
||||
if (query.IsIntantQuery && PluginManager.IsInstantSearchPlugin(pair.Metadata))
|
||||
{
|
||||
DebugHelper.WriteLine(string.Format("Plugin {0} is executing instant search.", pair.Metadata.Name));
|
||||
using (new Timeit(" => instant search took: "))
|
||||
{
|
||||
PluginManager.ExecutePluginQuery(localPair, query);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(state =>
|
||||
{
|
||||
PluginManager.ExecutePluginQuery(localPair, query);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,34 +8,14 @@ using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
public class SystemPluginQueryDispatcher : IQueryDispatcher
|
||||
public class SystemPluginQueryDispatcher : BaseQueryDispatcher
|
||||
{
|
||||
private IEnumerable<PluginPair> allSytemPlugins = PluginManager.AllPlugins.Where(o => PluginManager.IsSystemPlugin(o.Metadata));
|
||||
private readonly List<PluginPair> allSytemPlugins =
|
||||
PluginManager.AllPlugins.Where(o => PluginManager.IsSystemPlugin(o.Metadata)).ToList();
|
||||
|
||||
public void Dispatch(Query query)
|
||||
protected override List<PluginPair> GetPlugins(Query query)
|
||||
{
|
||||
var queryPlugins = allSytemPlugins;
|
||||
foreach (PluginPair pair in queryPlugins)
|
||||
{
|
||||
PluginPair pair1 = pair;
|
||||
ThreadPool.QueueUserWorkItem(state =>
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Result> results = pair1.Plugin.Query(query);
|
||||
results.ForEach(o =>
|
||||
{
|
||||
o.PluginID = pair1.Metadata.ID;
|
||||
});
|
||||
|
||||
PluginManager.API.PushResults(query, pair1.Metadata, results);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
throw new WoxPluginException(pair1.Metadata.Name,e);
|
||||
}
|
||||
});
|
||||
}
|
||||
return allSytemPlugins;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,38 +9,29 @@ using Wox.Plugin;
|
||||
|
||||
namespace Wox.Core.Plugin.QueryDispatcher
|
||||
{
|
||||
public class UserPluginQueryDispatcher : IQueryDispatcher
|
||||
public class UserPluginQueryDispatcher : BaseQueryDispatcher
|
||||
{
|
||||
public void Dispatch(Query query)
|
||||
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 && !string.IsNullOrEmpty(userPlugin.Metadata.ActionKeyword))
|
||||
if (userPlugin != null)
|
||||
{
|
||||
var customizedPluginConfig = UserSettingStorage.Instance.CustomizedPluginConfigs.FirstOrDefault(o => o.ID == userPlugin.Metadata.ID);
|
||||
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();
|
||||
return;
|
||||
}
|
||||
|
||||
ThreadPool.QueueUserWorkItem(t =>
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
List<Result> results = userPlugin.Plugin.Query(query) ?? new List<Result>();
|
||||
results.ForEach(o =>
|
||||
{
|
||||
o.PluginID = userPlugin.Metadata.ID;
|
||||
});
|
||||
PluginManager.API.PushResults(query, userPlugin.Metadata, results);
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
throw new WoxPluginException(userPlugin.Metadata.Name, e);
|
||||
}
|
||||
});
|
||||
plugins.Add(userPlugin);
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,7 @@
|
||||
<Compile Include="Exception\WoxI18nException.cs" />
|
||||
<Compile Include="Exception\WoxJsonRPCException.cs" />
|
||||
<Compile Include="Exception\WoxPluginException.cs" />
|
||||
<Compile Include="Plugin\QueryDispatcher\BaseQueryDispatcher.cs" />
|
||||
<Compile Include="Updater\Release.cs" />
|
||||
<Compile Include="Updater\UpdaterManager.cs" />
|
||||
<Compile Include="Updater\WoxUpdateSource.cs" />
|
||||
|
||||
Reference in New Issue
Block a user