mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-02-23 19:49:43 +01:00
Fix web search plugin for new result panel
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -49,7 +49,6 @@
|
||||
<Reference Include="WindowsBase" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="EasyTimer.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SuggestionSources\Baidu.cs" />
|
||||
<Compile Include="SuggestionSources\Google.cs" />
|
||||
@@ -93,9 +92,6 @@
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<WCFMetadata Include="Service References\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\Wox.Infrastructure\Wox.Infrastructure.csproj">
|
||||
<Project>{4fd29318-a8ab-4d8f-aa47-60bc241b8da3}</Project>
|
||||
|
||||
Reference in New Issue
Block a user