diff --git a/Plugins/Wox.Plugin.WebSearch/EasyTimer.cs b/Plugins/Wox.Plugin.WebSearch/EasyTimer.cs deleted file mode 100644 index 49c31e63d3..0000000000 --- a/Plugins/Wox.Plugin.WebSearch/EasyTimer.cs +++ /dev/null @@ -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; - } - } -} diff --git a/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs b/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs index d4ee840df0..f2dba4a35f 100644 --- a/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs +++ b/Plugins/Wox.Plugin.WebSearch/WebSearchPlugin.cs @@ -4,14 +4,14 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; +using System.Windows.Controls; using Wox.Plugin.WebSearch.SuggestionSources; namespace Wox.Plugin.WebSearch { public class WebSearchPlugin : IPlugin, ISettingProvider, IPluginI18n, IInstantQuery { - private PluginInitContext context; - private IDisposable suggestionTimer; + private PluginInitContext _context; public List Query(Query query) { @@ -21,71 +21,63 @@ namespace Wox.Plugin.WebSearch if (webSearch != null) { - string keyword = query.ActionKeyword; + string keyword = query.Search; 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)) { title = subtitle; - subtitle = null; + subtitle = string.Empty; } - context.API.PushResults(query, context.CurrentPluginMetadata, new List() + var result = new Result { - new Result() + Title = title, + SubTitle = subtitle, + Score = 6, + IcoPath = webSearch.IconPath, + Action = c => { - Title = title, - SubTitle = subtitle, - Score = 6, - IcoPath = webSearch.IconPath, - Action = (c) => - { - Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword))); - return true; - } + Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword ?? string.Empty))); + return true; } - }); + }; + results.Add(result); if (WebSearchStorage.Instance.EnableWebSearchSuggestion && !string.IsNullOrEmpty(keyword)) { - if (suggestionTimer != null) - { - suggestionTimer.Dispose(); - } - suggestionTimer = EasyTimer.SetTimeout(() => { QuerySuggestions(keyword, query, subtitle, webSearch); }, 350); + // todo use Task.Wait when .net upgraded + results.AddRange(ResultsFromSuggestions(keyword, subtitle, webSearch)); } } - return results; } - private void QuerySuggestions(string keyword, Query query, string subtitle, WebSearch webSearch) + private IEnumerable ResultsFromSuggestions(string keyword, string subtitle, WebSearch webSearch) { - ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(WebSearchStorage.Instance.WebSearchSuggestionSource, context); - if (sugg != null) + ISuggestionSource sugg = SuggestionSourceFactory.GetSuggestionSource(WebSearchStorage.Instance.WebSearchSuggestionSource, _context); + var suggestions = sugg?.GetSuggestions(keyword); + if (suggestions != null) { - var result = sugg.GetSuggestions(keyword); - if (result != null) + var resultsFromSuggestion = suggestions.Select(o => new Result { - 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()); - } + Title = o, + SubTitle = subtitle, + Score = 5, + IcoPath = webSearch.IconPath, + Action = c => + { + Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o))); + return true; + } + }); + return resultsFromSuggestion; } + return new List(); } public void Init(PluginInitContext context) { - this.context = context; + _context = context; if (WebSearchStorage.Instance.WebSearches == null) WebSearchStorage.Instance.WebSearches = WebSearchStorage.Instance.LoadDefaultWebSearches(); @@ -93,9 +85,9 @@ namespace Wox.Plugin.WebSearch #region ISettingProvider Members - public System.Windows.Controls.Control CreateSettingPanel() + public Control CreateSettingPanel() { - return new WebSearchesSetting(context); + return new WebSearchesSetting(_context); } #endregion @@ -107,12 +99,12 @@ namespace Wox.Plugin.WebSearch public string GetTranslatedPluginTitle() { - return context.API.GetTranslation("wox_plugin_websearch_plugin_name"); + return _context.API.GetTranslation("wox_plugin_websearch_plugin_name"); } 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; diff --git a/Plugins/Wox.Plugin.WebSearch/Wox.Plugin.WebSearch.csproj b/Plugins/Wox.Plugin.WebSearch/Wox.Plugin.WebSearch.csproj index 32a1b26e4a..a407ad508e 100644 --- a/Plugins/Wox.Plugin.WebSearch/Wox.Plugin.WebSearch.csproj +++ b/Plugins/Wox.Plugin.WebSearch/Wox.Plugin.WebSearch.csproj @@ -49,7 +49,6 @@ - @@ -93,9 +92,6 @@ Designer - - - {4fd29318-a8ab-4d8f-aa47-60bc241b8da3}