From 45f60ae646c596b9e20f2a5f908880e8329c46c2 Mon Sep 17 00:00:00 2001 From: qianlifeng Date: Sat, 28 Feb 2015 18:20:06 +0800 Subject: [PATCH] Fix UI issues when using web search plugin with suggestions --- Plugins/Wox.Plugin.WebSearch/EasyTimer.cs | 43 +++++++++++++++ .../Wox.Plugin.WebSearch/Languages/en.xaml | 1 + .../Wox.Plugin.WebSearch/Languages/zh-cn.xaml | 1 + .../Wox.Plugin.WebSearch/Languages/zh-tw.xaml | 1 + .../Wox.Plugin.WebSearch/WebQueryPlugin.cs | 53 +++++++++++-------- .../Wox.Plugin.WebSearch.csproj | 1 + Wox.Plugin/Features/IInstantQuery.cs | 4 ++ 7 files changed, 83 insertions(+), 21 deletions(-) create mode 100644 Plugins/Wox.Plugin.WebSearch/EasyTimer.cs diff --git a/Plugins/Wox.Plugin.WebSearch/EasyTimer.cs b/Plugins/Wox.Plugin.WebSearch/EasyTimer.cs new file mode 100644 index 0000000000..78cfb5d6e5 --- /dev/null +++ b/Plugins/Wox.Plugin.WebSearch/EasyTimer.cs @@ -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; + } + } +} diff --git a/Plugins/Wox.Plugin.WebSearch/Languages/en.xaml b/Plugins/Wox.Plugin.WebSearch/Languages/en.xaml index 45ef8724a6..5d8eae569f 100644 --- a/Plugins/Wox.Plugin.WebSearch/Languages/en.xaml +++ b/Plugins/Wox.Plugin.WebSearch/Languages/en.xaml @@ -7,6 +7,7 @@ Add Action Keyword URL + Search Enable search suggestions Please select a web search Are your sure to delete {0}? diff --git a/Plugins/Wox.Plugin.WebSearch/Languages/zh-cn.xaml b/Plugins/Wox.Plugin.WebSearch/Languages/zh-cn.xaml index 28e65de3a1..95ab6c4c6f 100644 --- a/Plugins/Wox.Plugin.WebSearch/Languages/zh-cn.xaml +++ b/Plugins/Wox.Plugin.WebSearch/Languages/zh-cn.xaml @@ -7,6 +7,7 @@ 添加 触发关键字 URL + 搜索 启用搜索建议 请选择一项 你确定要删除 {0} 吗? diff --git a/Plugins/Wox.Plugin.WebSearch/Languages/zh-tw.xaml b/Plugins/Wox.Plugin.WebSearch/Languages/zh-tw.xaml index ec1f620ab1..2d4efa88a1 100644 --- a/Plugins/Wox.Plugin.WebSearch/Languages/zh-tw.xaml +++ b/Plugins/Wox.Plugin.WebSearch/Languages/zh-tw.xaml @@ -7,6 +7,7 @@ 添加 觸發關鍵字 URL + 搜索 啟用搜索建議 請選擇一項 你確定要刪除 {0} 嗎? diff --git a/Plugins/Wox.Plugin.WebSearch/WebQueryPlugin.cs b/Plugins/Wox.Plugin.WebSearch/WebQueryPlugin.cs index 16a7fcc850..7f4412f3d3 100644 --- a/Plugins/Wox.Plugin.WebSearch/WebQueryPlugin.cs +++ b/Plugins/Wox.Plugin.WebSearch/WebQueryPlugin.cs @@ -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 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; diff --git a/Plugins/Wox.Plugin.WebSearch/Wox.Plugin.WebSearch.csproj b/Plugins/Wox.Plugin.WebSearch/Wox.Plugin.WebSearch.csproj index 33a4586f9f..1a1f5b49bc 100644 --- a/Plugins/Wox.Plugin.WebSearch/Wox.Plugin.WebSearch.csproj +++ b/Plugins/Wox.Plugin.WebSearch/Wox.Plugin.WebSearch.csproj @@ -51,6 +51,7 @@ + diff --git a/Wox.Plugin/Features/IInstantQuery.cs b/Wox.Plugin/Features/IInstantQuery.cs index 154f167e23..00044ed6c0 100644 --- a/Wox.Plugin/Features/IInstantQuery.cs +++ b/Wox.Plugin/Features/IInstantQuery.cs @@ -1,5 +1,9 @@ namespace Wox.Plugin.Features { + /// + /// Represent plugin query will be executed in UI thread directly. Don't do long-running operation in Query method if you implement this interface + /// This will improve the performance of instant search like websearch or cmd plugin + /// public interface IInstantQuery { bool IsInstantQuery(string query);