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,52 +33,58 @@ 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())
{ {
string keyword = query.Search; foreach (SearchSource searchSource in searchSourceList)
string title = keyword;
string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " + searchSource.Title;
if (string.IsNullOrEmpty(keyword))
{ {
var result = new Result string keyword = query.Search;
string title = keyword;
string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " +
searchSource.Title;
if (string.IsNullOrEmpty(keyword))
{ {
Title = subtitle, var result = new Result
SubTitle = string.Empty,
IcoPath = searchSource.IconPath
};
return new List<Result> {result};
}
else
{
var results = new List<Result>();
var result = new Result
{
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = searchSource.IconPath,
Action = c =>
{ {
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword))); Title = subtitle,
return true; SubTitle = string.Empty,
} IcoPath = searchSource.IconPath
}; };
results.Add(result); results.Add(result);
UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query); }
return results; else
{
var result = new Result
{
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = searchSource.IconPath,
Action = c =>
{
searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)).NewBrowserWindow("");
return true;
}
};
results.Add(result);
UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query);
}
} }
} }
else
{ return results;
return new List<Result>();
}
} }
private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle, private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle,
@@ -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;
@@ -28,27 +28,33 @@ namespace Wox.Plugin.WebSearch
private void OnDeleteSearchSearchClick(object sender, RoutedEventArgs e) private void OnDeleteSearchSearchClick(object sender, RoutedEventArgs e)
{ {
var selected = _settings.SelectedSearchSource; if (_settings.SelectedSearchSource != null)
var warning = _context.API.GetTranslation("wox_plugin_websearch_delete_warning");
var formated = string.Format(warning, selected.Title);
var result = MessageBox.Show(formated, string.Empty, MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{ {
var id = _context.CurrentPluginMetadata.ID; var selected = _settings.SelectedSearchSource;
PluginManager.RemoveActionKeyword(id, selected.ActionKeyword); var warning = _context.API.GetTranslation("wox_plugin_websearch_delete_warning");
_settings.SearchSources.Remove(selected); var formated = string.Format(warning, selected.Title);
var result = MessageBox.Show(formated, string.Empty, MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
var id = _context.CurrentPluginMetadata.ID;
PluginManager.RemoveActionKeyword(id, selected.ActionKeyword);
_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);
} }