mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Fix web search plugin for new result panel
This commit is contained in:
@@ -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<Result> 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<Result>()
|
||||
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<Result> 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<Result>();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user