mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-10 21:41:51 +02:00
Implement IInstantSearch for CMD and WebSearch plugin
This commit is contained in:
@@ -6,12 +6,13 @@ using System.Reflection;
|
|||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
using WindowsInput;
|
using WindowsInput;
|
||||||
using WindowsInput.Native;
|
using WindowsInput.Native;
|
||||||
|
using Wox.Infrastructure;
|
||||||
using Wox.Infrastructure.Hotkey;
|
using Wox.Infrastructure.Hotkey;
|
||||||
using Control = System.Windows.Controls.Control;
|
using Control = System.Windows.Controls.Control;
|
||||||
|
|
||||||
namespace Wox.Plugin.CMD
|
namespace Wox.Plugin.CMD
|
||||||
{
|
{
|
||||||
public class CMD : IPlugin, ISettingProvider, IPluginI18n
|
public class CMD : IPlugin, ISettingProvider, IPluginI18n, IInstantSearch
|
||||||
{
|
{
|
||||||
private PluginInitContext context;
|
private PluginInitContext context;
|
||||||
private bool WinRStroked;
|
private bool WinRStroked;
|
||||||
@@ -37,6 +38,7 @@ namespace Wox.Plugin.CMD
|
|||||||
context.API.PushResults(query, context.CurrentPluginMetadata, history);
|
context.API.PushResults(query, context.CurrentPluginMetadata, history);
|
||||||
pushedResults.AddRange(history);
|
pushedResults.AddRange(history);
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
string basedir = null;
|
string basedir = null;
|
||||||
@@ -72,6 +74,7 @@ namespace Wox.Plugin.CMD
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception) { }
|
catch (Exception) { }
|
||||||
|
|
||||||
}
|
}
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
@@ -207,5 +210,11 @@ namespace Wox.Plugin.CMD
|
|||||||
{
|
{
|
||||||
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages");
|
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsInstantSearch(string query)
|
||||||
|
{
|
||||||
|
if (query.StartsWith(">")) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ using Wox.Plugin.WebSearch.SuggestionSources;
|
|||||||
|
|
||||||
namespace Wox.Plugin.WebSearch
|
namespace Wox.Plugin.WebSearch
|
||||||
{
|
{
|
||||||
public class WebSearchPlugin : IPlugin, ISettingProvider,IPluginI18n
|
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantSearch
|
||||||
{
|
{
|
||||||
private PluginInitContext context;
|
private PluginInitContext context;
|
||||||
|
|
||||||
@@ -97,5 +97,16 @@ namespace Wox.Plugin.WebSearch
|
|||||||
{
|
{
|
||||||
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages");
|
return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Languages");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool IsInstantSearch(string query)
|
||||||
|
{
|
||||||
|
var strings = query.Split(' ');
|
||||||
|
if (strings.Length > 1)
|
||||||
|
{
|
||||||
|
return WebSearchStorage.Instance.EnableWebSearchSuggestion &&
|
||||||
|
WebSearchStorage.Instance.WebSearches.Exists(o => o.ActionWord == strings[0] && o.Enabled);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ namespace Wox.Core.Plugin
|
|||||||
public static class PluginManager
|
public static class PluginManager
|
||||||
{
|
{
|
||||||
public const string ActionKeywordWildcardSign = "*";
|
public const string ActionKeywordWildcardSign = "*";
|
||||||
|
private static List<PluginMetadata> pluginMetadatas;
|
||||||
|
private static List<IInstantSearch> instantSearches = new List<IInstantSearch>();
|
||||||
|
|
||||||
|
|
||||||
public static String DebuggerMode { get; private set; }
|
public static String DebuggerMode { get; private set; }
|
||||||
public static IPublicAPI API { get; private set; }
|
public static IPublicAPI API { get; private set; }
|
||||||
@@ -31,7 +34,6 @@ namespace Wox.Core.Plugin
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private static List<string> pluginDirectories = new List<string>();
|
private static List<string> pluginDirectories = new List<string>();
|
||||||
|
|
||||||
|
|
||||||
private static void SetupPluginDirectories()
|
private static void SetupPluginDirectories()
|
||||||
{
|
{
|
||||||
pluginDirectories.Add(PluginDirectory);
|
pluginDirectories.Add(PluginDirectory);
|
||||||
@@ -72,7 +74,7 @@ namespace Wox.Core.Plugin
|
|||||||
API = api;
|
API = api;
|
||||||
plugins.Clear();
|
plugins.Clear();
|
||||||
|
|
||||||
List<PluginMetadata> pluginMetadatas = PluginConfig.Parse(pluginDirectories);
|
pluginMetadatas = PluginConfig.Parse(pluginDirectories);
|
||||||
plugins.AddRange(new CSharpPluginLoader().LoadPlugin(pluginMetadatas));
|
plugins.AddRange(new CSharpPluginLoader().LoadPlugin(pluginMetadatas));
|
||||||
plugins.AddRange(new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(pluginMetadatas));
|
plugins.AddRange(new JsonRPCPluginLoader<PythonPlugin>().LoadPlugin(pluginMetadatas));
|
||||||
|
|
||||||
@@ -95,6 +97,8 @@ namespace Wox.Core.Plugin
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LoadInstantSearches();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void InstallPlugin(string path)
|
public static void InstallPlugin(string path)
|
||||||
@@ -140,6 +144,46 @@ namespace Wox.Core.Plugin
|
|||||||
DebuggerMode = path;
|
DebuggerMode = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static bool IsInstantSearch(string query)
|
||||||
|
{
|
||||||
|
return LoadInstantSearches().Any(o => o.IsInstantSearch(query));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<IInstantSearch> LoadInstantSearches()
|
||||||
|
{
|
||||||
|
if (instantSearches.Count > 0) return instantSearches;
|
||||||
|
List<PluginMetadata> CSharpPluginMetadatas = pluginMetadatas.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp.ToUpper()).ToList();
|
||||||
|
|
||||||
|
foreach (PluginMetadata metadata in CSharpPluginMetadatas)
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
if (types.Count == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Type type in types)
|
||||||
|
{
|
||||||
|
instantSearches.Add(Activator.CreateInstance(type) as IInstantSearch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (System.Exception e)
|
||||||
|
{
|
||||||
|
Log.Error(string.Format("Couldn't load plugin {0}: {1}", metadata.Name, e.Message));
|
||||||
|
#if (DEBUG)
|
||||||
|
{
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return instantSearches;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// get specified plugin, return null if not found
|
/// get specified plugin, return null if not found
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
12
Wox.Plugin/IInstantSearch.cs
Normal file
12
Wox.Plugin/IInstantSearch.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Wox.Plugin
|
||||||
|
{
|
||||||
|
public interface IInstantSearch
|
||||||
|
{
|
||||||
|
bool IsInstantSearch(string query);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,4 +17,4 @@ using System.Runtime.InteropServices;
|
|||||||
|
|
||||||
[assembly: InternalsVisibleTo("Wox")]
|
[assembly: InternalsVisibleTo("Wox")]
|
||||||
[assembly: InternalsVisibleTo("Wox.Core")]
|
[assembly: InternalsVisibleTo("Wox.Core")]
|
||||||
[assembly: InternalsVisibleTo("Wox.Test")]
|
[assembly: InternalsVisibleTo("Wox.Test")]
|
||||||
|
|||||||
@@ -46,6 +46,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="AllowedLanguage.cs" />
|
<Compile Include="AllowedLanguage.cs" />
|
||||||
<Compile Include="EventHandler.cs" />
|
<Compile Include="EventHandler.cs" />
|
||||||
|
<Compile Include="IInstantSearch.cs" />
|
||||||
<Compile Include="IHttpProxy.cs" />
|
<Compile Include="IHttpProxy.cs" />
|
||||||
<Compile Include="IPluginI18n.cs" />
|
<Compile Include="IPluginI18n.cs" />
|
||||||
<Compile Include="IPlugin.cs" />
|
<Compile Include="IPlugin.cs" />
|
||||||
|
|||||||
@@ -337,18 +337,27 @@ namespace Wox
|
|||||||
if (pnlResult.Dirty) pnlResult.Clear();
|
if (pnlResult.Dirty) pnlResult.Clear();
|
||||||
}, TimeSpan.FromMilliseconds(100), null);
|
}, TimeSpan.FromMilliseconds(100), null);
|
||||||
queryHasReturn = false;
|
queryHasReturn = false;
|
||||||
var q = new Query(lastQuery);
|
Query query = new Query(lastQuery);
|
||||||
FireBeforeWoxQueryEvent(q);
|
FireBeforeWoxQueryEvent(query);
|
||||||
Query(q);
|
Query(query);
|
||||||
Dispatcher.DelayInvoke("ShowProgressbar", originQuery =>
|
Dispatcher.DelayInvoke("ShowProgressbar", originQuery =>
|
||||||
{
|
{
|
||||||
if (!queryHasReturn && originQuery == lastQuery && !string.IsNullOrEmpty(lastQuery))
|
if (!queryHasReturn && originQuery == tbQuery.Text && !string.IsNullOrEmpty(lastQuery))
|
||||||
{
|
{
|
||||||
StartProgress();
|
StartProgress();
|
||||||
}
|
}
|
||||||
}, TimeSpan.FromMilliseconds(150), lastQuery);
|
}, TimeSpan.FromMilliseconds(150), tbQuery.Text);
|
||||||
FireAfterWoxQueryEvent(q);
|
FireAfterWoxQueryEvent(query);
|
||||||
}, TimeSpan.FromMilliseconds(200));
|
}, TimeSpan.FromMilliseconds(GetSearchDelay(lastQuery)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GetSearchDelay(string query)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(query) && PluginManager.IsInstantSearch(query))
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FireAfterWoxQueryEvent(Query q)
|
private void FireAfterWoxQueryEvent(Query q)
|
||||||
|
|||||||
Reference in New Issue
Block a user