Fix web search plugin for new result panel

This commit is contained in:
bao-qian 2015-11-08 02:27:08 +00:00
parent 2b27e84956
commit e928b4c9e0
3 changed files with 39 additions and 91 deletions

View file

@ -1,40 +0,0 @@
using System;
namespace Wox.Plugin.WebSearch
{
public static class EasyTimer
{
public static IDisposable SetInterval(Action method, int delayInMilliseconds)
{
System.Timers.Timer timer = new System.Timers.Timer(delayInMilliseconds);
timer.Elapsed += (source, e) =>
{
method();
};
timer.Enabled = true;
timer.Start();
// Returns a stop handle which can be used for stopping
// the timer, if required
return timer as IDisposable;
}
public static IDisposable SetTimeout(Action method, int delayInMilliseconds)
{
System.Timers.Timer timer = new System.Timers.Timer(delayInMilliseconds);
timer.Elapsed += (source, e) =>
{
method();
};
timer.AutoReset = false;
timer.Enabled = true;
timer.Start();
// Returns a stop handle which can be used for stopping
// the timer, if required
return timer as IDisposable;
}
}
}

View file

@ -4,14 +4,14 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Windows.Controls;
using Wox.Plugin.WebSearch.SuggestionSources; using Wox.Plugin.WebSearch.SuggestionSources;
namespace Wox.Plugin.WebSearch namespace Wox.Plugin.WebSearch
{ {
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery
{ {
private PluginInitContext context; private PluginInitContext _context;
private IDisposable suggestionTimer;
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
@ -21,71 +21,63 @@ namespace Wox.Plugin.WebSearch
if (webSearch != null) if (webSearch != null)
{ {
string keyword = query.ActionKeyword; string keyword = query.Search;
string title = keyword; string title = keyword;
string subtitle = context.API.GetTranslation("wox_plugin_websearch_search") + " " + webSearch.Title; string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " + webSearch.Title;
if (string.IsNullOrEmpty(keyword)) if (string.IsNullOrEmpty(keyword))
{ {
title = subtitle; title = subtitle;
subtitle = null; subtitle = string.Empty;
} }
context.API.PushResults(query, context.CurrentPluginMetadata, new List<Result>() var result = new Result
{ {
new Result() Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = webSearch.IconPath,
Action = c =>
{ {
Title = title, Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword ?? string.Empty)));
SubTitle = subtitle, return true;
Score = 6,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
return true;
}
} }
}); };
results.Add(result);
if (WebSearchStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword)) if (WebSearchStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword))
{ {
if (suggestionTimer != null) // todo use Task.Wait when .net upgraded
{ results.AddRange(ResultsFromSuggestions(keyword, subtitle, webSearch));
suggestionTimer.Dispose();
}
suggestionTimer = EasyTimer.SetTimeout(() => { QuerySuggestions(keyword, query, subtitle, webSearch); }, 350);
} }
} }
return results; return results;
} }
private void QuerySuggestions(string keyword, Query query, string subtitle, WebSearch webSearch) private IEnumerable<Result> ResultsFromSuggestions(string keyword, string subtitle, WebSearch webSearch)
{ {
ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(WebSearchStorage.Instance.WebSearchSuggestionSource, context); ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(WebSearchStorage.Instance.WebSearchSuggestionSource, _context);
if (sugg != null) var suggestions = sugg?.GetSuggestions(keyword);
if (suggestions != null)
{ {
var result = sugg.GetSuggestions(keyword); var resultsFromSuggestion = suggestions.Select(o => new Result
if (result != null)
{ {
context.API.PushResults(query, context.CurrentPluginMetadata, Title = o,
result.Select(o => new Result() SubTitle = subtitle,
{ Score = 5,
Title = o, IcoPath = webSearch.IconPath,
SubTitle = subtitle, Action = c =>
Score = 5, {
IcoPath = webSearch.IconPath, Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o)));
Action = (c) => return true;
{ }
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o))); });
return true; return resultsFromSuggestion;
}
}).ToList());
}
} }
return new List<Result>();
} }
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
this.context = context; _context = context;
if (WebSearchStorage.Instance.WebSearches == null) if (WebSearchStorage.Instance.WebSearches == null)
WebSearchStorage.Instance.WebSearches = WebSearchStorage.Instance.LoadDefaultWebSearches(); WebSearchStorage.Instance.WebSearches = WebSearchStorage.Instance.LoadDefaultWebSearches();
@ -93,9 +85,9 @@ namespace Wox.Plugin.WebSearch
#region ISettingProvider Members #region ISettingProvider Members
public System.Windows.Controls.Control CreateSettingPanel() public Control CreateSettingPanel()
{ {
return new WebSearchesSetting(context); return new WebSearchesSetting(_context);
} }
#endregion #endregion
@ -107,12 +99,12 @@ namespace Wox.Plugin.WebSearch
public string GetTranslatedPluginTitle() public string GetTranslatedPluginTitle()
{ {
return context.API.GetTranslation("wox_plugin_websearch_plugin_name"); return _context.API.GetTranslation("wox_plugin_websearch_plugin_name");
} }
public string GetTranslatedPluginDescription() public string GetTranslatedPluginDescription()
{ {
return context.API.GetTranslation("wox_plugin_websearch_plugin_description"); return _context.API.GetTranslation("wox_plugin_websearch_plugin_description");
} }
public bool IsInstantQuery(string query) => false; public bool IsInstantQuery(string query) => false;

View file

@ -49,7 +49,6 @@
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="EasyTimer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SuggestionSources\Baidu.cs" /> <Compile Include="SuggestionSources\Baidu.cs" />
<Compile Include="SuggestionSources\Google.cs" /> <Compile Include="SuggestionSources\Google.cs" />
@ -93,9 +92,6 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
</Page> </Page>
</ItemGroup> </ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\Wox.Infrastructure\Wox.Infrastructure.csproj"> <ProjectReference Include="..\..\Wox.Infrastructure\Wox.Infrastructure.csproj">
<Project>{4fd29318-a8ab-4d8f-aa47-60bc241b8da3}</Project> <Project>{4fd29318-a8ab-4d8f-aa47-60bc241b8da3}</Project>