Merge pull request #6 from jjw24/update_websearchplugin_as_systemplugin

Wox.Plugin.WebSearches- Update plugin to search web without specific action key word
This commit is contained in:
Jeremy Wu
2019-08-20 07:43:37 +10:00
committed by GitHub
3 changed files with 83 additions and 66 deletions

View File

@@ -8,6 +8,7 @@ using System.Threading.Tasks;
using System.Windows.Controls; using System.Windows.Controls;
using Wox.Infrastructure; using Wox.Infrastructure;
using Wox.Infrastructure.Storage; using Wox.Infrastructure.Storage;
using Wox.Plugin.SharedCommands;
namespace Wox.Plugin.WebSearch namespace Wox.Plugin.WebSearch
{ {
@@ -23,6 +24,8 @@ namespace Wox.Plugin.WebSearch
public const string Images = "Images"; public const string Images = "Images";
public static string ImagesDirectory; public static string ImagesDirectory;
private readonly string SearchSourceGlobalPluginWildCardSign = "*";
public void Save() public void Save()
{ {
_viewModel.Save(); _viewModel.Save();
@@ -30,18 +33,26 @@ namespace Wox.Plugin.WebSearch
public List<Result> Query(Query query) public List<Result> Query(Query query)
{ {
var searchSourceList = new List<SearchSource>();
var results = new List<Result>();
_updateSource?.Cancel(); _updateSource?.Cancel();
_updateSource = new CancellationTokenSource(); _updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token; _updateToken = _updateSource.Token;
SearchSource searchSource = _settings.SearchSources.Where(o => (o.ActionKeyword == query.ActionKeyword || o.ActionKeyword == SearchSourceGlobalPluginWildCardSign)
_settings.SearchSources.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled); && o.Enabled)
.ToList()
.ForEach(x => searchSourceList.Add(x));
if (searchSource != null) if (searchSourceList.Any())
{
foreach (SearchSource searchSource in searchSourceList)
{ {
string keyword = query.Search; string keyword = query.Search;
string title = keyword; string title = keyword;
string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " + searchSource.Title; string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " +
searchSource.Title;
if (string.IsNullOrEmpty(keyword)) if (string.IsNullOrEmpty(keyword))
{ {
var result = new Result var result = new Result
@@ -50,11 +61,10 @@ namespace Wox.Plugin.WebSearch
SubTitle = string.Empty, SubTitle = string.Empty,
IcoPath = searchSource.IconPath IcoPath = searchSource.IconPath
}; };
return new List<Result> {result}; results.Add(result);
} }
else else
{ {
var results = new List<Result>();
var result = new Result var result = new Result
{ {
Title = title, Title = title,
@@ -63,20 +73,19 @@ namespace Wox.Plugin.WebSearch
IcoPath = searchSource.IconPath, IcoPath = searchSource.IconPath,
Action = c => Action = c =>
{ {
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword))); searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)).NewBrowserWindow("");
return true; return true;
} }
}; };
results.Add(result); results.Add(result);
UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query); UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query);
}
}
}
return results; return results;
} }
}
else
{
return new List<Result>();
}
}
private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle, private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle,
SearchSource searchSource, Query query) SearchSource searchSource, Query query)
@@ -115,7 +124,7 @@ namespace Wox.Plugin.WebSearch
IcoPath = searchSource.IconPath, IcoPath = searchSource.IconPath,
Action = c => Action = c =>
{ {
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o))); searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)).NewBrowserWindow("");
return true; return true;
} }
}); });

View File

@@ -1,4 +1,4 @@
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using Wox.Core.Plugin; using Wox.Core.Plugin;
@@ -27,6 +27,8 @@ namespace Wox.Plugin.WebSearch
} }
private void OnDeleteSearchSearchClick(object sender, RoutedEventArgs e) private void OnDeleteSearchSearchClick(object sender, RoutedEventArgs e)
{
if (_settings.SelectedSearchSource != null)
{ {
var selected = _settings.SelectedSearchSource; var selected = _settings.SelectedSearchSource;
var warning = _context.API.GetTranslation("wox_plugin_websearch_delete_warning"); var warning = _context.API.GetTranslation("wox_plugin_websearch_delete_warning");
@@ -40,15 +42,19 @@ namespace Wox.Plugin.WebSearch
_settings.SearchSources.Remove(selected); _settings.SearchSources.Remove(selected);
} }
} }
}
private void OnEditSearchSourceClick(object sender, RoutedEventArgs e) private void OnEditSearchSourceClick(object sender, RoutedEventArgs e)
{ {
var selected = _settings.SelectedSearchSource; if (_settings.SelectedSearchSource != null)
{
var webSearch = new SearchSourceSettingWindow var webSearch = new SearchSourceSettingWindow
( (
_settings.SearchSources, _context, selected _settings.SearchSources, _context, _settings.SelectedSearchSource
); );
webSearch.ShowDialog(); webSearch.ShowDialog();
} }
} }
}
} }

View File

@@ -106,16 +106,12 @@ namespace Wox.Core.Plugin
foreach (var plugin in AllPlugins) foreach (var plugin in AllPlugins)
{ {
if (IsGlobalPlugin(plugin.Metadata)) if (IsGlobalPlugin(plugin.Metadata))
{
GlobalPlugins.Add(plugin); GlobalPlugins.Add(plugin);
}
else // Plugins may have multiple ActionKeywords, eg. WebSearch
{ plugin.Metadata.ActionKeywords.Where(x => x != Query.GlobalPluginWildcardSign)
foreach (string actionKeyword in plugin.Metadata.ActionKeywords) .ToList()
{ .ForEach(x => NonGlobalPlugins[x] = plugin);
NonGlobalPlugins[actionKeyword] = plugin;
}
}
} }
} }
@@ -289,14 +285,20 @@ namespace Wox.Core.Plugin
public static void RemoveActionKeyword(string id, string oldActionkeyword) public static void RemoveActionKeyword(string id, string oldActionkeyword)
{ {
var plugin = GetPluginForId(id); var plugin = GetPluginForId(id);
if (oldActionkeyword == Query.GlobalPluginWildcardSign) if (oldActionkeyword == Query.GlobalPluginWildcardSign
&& // Plugins may have multiple ActionKeywords that are global, eg. WebSearch
plugin.Metadata.ActionKeywords
.Where(x => x == Query.GlobalPluginWildcardSign)
.ToList()
.Count == 1)
{ {
GlobalPlugins.Remove(plugin); GlobalPlugins.Remove(plugin);
} }
else
{ if(oldActionkeyword != Query.GlobalPluginWildcardSign)
NonGlobalPlugins.Remove(oldActionkeyword); NonGlobalPlugins.Remove(oldActionkeyword);
}
plugin.Metadata.ActionKeywords.Remove(oldActionkeyword); plugin.Metadata.ActionKeywords.Remove(oldActionkeyword);
} }