Fix UI issues when using web search plugin with suggestions

This commit is contained in:
qianlifeng
2015-02-28 18:20:06 +08:00
parent 72988cc6fb
commit 45f60ae646
7 changed files with 83 additions and 21 deletions

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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

@@ -7,6 +7,7 @@
<system:String x:Key="wox_plugin_websearch_add">Add</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">Action Keyword</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">Search</system:String>
<system:String x:Key="wox_plugin_websearch_enable_suggestion">Enable search suggestions</system:String>
<system:String x:Key="wox_plugin_websearch_pls_select_web_search">Please select a web search</system:String>
<system:String x:Key="wox_plugin_websearch_delete_warning">Are your sure to delete {0}?</system:String>

View File

@@ -7,6 +7,7 @@
<system:String x:Key="wox_plugin_websearch_add">添加</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">触发关键字</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">搜索</system:String>
<system:String x:Key="wox_plugin_websearch_enable_suggestion">启用搜索建议</system:String>
<system:String x:Key="wox_plugin_websearch_pls_select_web_search">请选择一项</system:String>
<system:String x:Key="wox_plugin_websearch_delete_warning">你确定要删除 {0} 吗?</system:String>

View File

@@ -7,6 +7,7 @@
<system:String x:Key="wox_plugin_websearch_add">添加</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">觸發關鍵字</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">搜索</system:String>
<system:String x:Key="wox_plugin_websearch_enable_suggestion">啟用搜索建議</system:String>
<system:String x:Key="wox_plugin_websearch_pls_select_web_search">請選擇一項</system:String>
<system:String x:Key="wox_plugin_websearch_delete_warning">你確定要刪除 {0} 嗎?</system:String>

View File

@@ -4,6 +4,8 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Timers;
using System.Windows.Threading;
using Wox.Plugin.Features;
using Wox.Plugin.WebSearch.SuggestionSources;
@@ -12,6 +14,7 @@ namespace Wox.Plugin.WebSearch
public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery, IExclusiveQuery
{
private PluginInitContext context;
private IDisposable suggestionTimer;
public List<Result> Query(Query query)
{
@@ -28,7 +31,7 @@ namespace Wox.Plugin.WebSearch
{
string keyword = query.SecondToEndSearch;
string title = keyword;
string subtitle = "Search " + webSearch.Title;
string subtitle = context.API.GetTranslation("wox_plugin_websearch_search") + " " + webSearch.Title;
if (string.IsNullOrEmpty(keyword))
{
title = subtitle;
@@ -52,34 +55,42 @@ namespace Wox.Plugin.WebSearch
if (WebSearchStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword))
{
ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(
WebSearchStorage.Instance.WebSearchSuggestionSource,context);
if (sugg != null)
if (suggestionTimer != null)
{
var result = sugg.GetSuggestions(keyword);
if (result != null)
{
context.API.PushResults(query, context.CurrentPluginMetadata,
result.Select(o => new Result()
{
Title = o,
SubTitle = subtitle,
Score = 5,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o)));
return true;
}
}).ToList());
}
suggestionTimer.Dispose();
}
suggestionTimer = EasyTimer.SetTimeout(() => { QuerySuggestions(keyword, query, subtitle, webSearch); }, 350);
}
}
return results;
}
private void QuerySuggestions(string keyword, Query query, string subtitle, WebSearch webSearch)
{
ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(WebSearchStorage.Instance.WebSearchSuggestionSource, context);
if (sugg != null)
{
var result = sugg.GetSuggestions(keyword);
if (result != null)
{
context.API.PushResults(query, context.CurrentPluginMetadata,
result.Select(o => new Result()
{
Title = o,
SubTitle = subtitle,
Score = 5,
IcoPath = webSearch.IconPath,
Action = (c) =>
{
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o)));
return true;
}
}).ToList());
}
}
}
public void Init(PluginInitContext context)
{
this.context = context;

View File

@@ -51,6 +51,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="EasyTimer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SuggestionSources\Baidu.cs" />
<Compile Include="SuggestionSources\Google.cs" />