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 Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin.SharedCommands;
namespace Wox.Plugin.WebSearch
{
@@ -23,6 +24,8 @@ namespace Wox.Plugin.WebSearch
public const string Images = "Images";
public static string ImagesDirectory;
private readonly string SearchSourceGlobalPluginWildCardSign = "*";
public void Save()
{
_viewModel.Save();
@@ -30,52 +33,58 @@ namespace Wox.Plugin.WebSearch
public List<Result> Query(Query query)
{
var searchSourceList = new List<SearchSource>();
var results = new List<Result>();
_updateSource?.Cancel();
_updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token;
_settings.SearchSources.Where(o => (o.ActionKeyword == query.ActionKeyword || o.ActionKeyword == SearchSourceGlobalPluginWildCardSign)
&& o.Enabled)
.ToList()
.ForEach(x => searchSourceList.Add(x));
SearchSource searchSource =
_settings.SearchSources.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled);
if (searchSource != null)
if (searchSourceList.Any())
{
string keyword = query.Search;
string title = keyword;
string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " + searchSource.Title;
if (string.IsNullOrEmpty(keyword))
foreach (SearchSource searchSource in searchSourceList)
{
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,
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 =>
var result = new Result
{
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
return true;
}
};
results.Add(result);
UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query);
return results;
Title = subtitle,
SubTitle = string.Empty,
IcoPath = searchSource.IconPath
};
results.Add(result);
}
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 new List<Result>();
}
return results;
}
private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle,
@@ -115,7 +124,7 @@ namespace Wox.Plugin.WebSearch
IcoPath = searchSource.IconPath,
Action = c =>
{
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)).NewBrowserWindow("");
return true;
}
});
@@ -160,4 +169,4 @@ namespace Wox.Plugin.WebSearch
public event ResultUpdatedEventHandler ResultsUpdated;
}
}
}

View File

@@ -1,4 +1,4 @@
using System.Windows;
using System.Windows;
using System.Windows.Controls;
using Wox.Core.Plugin;
@@ -28,27 +28,33 @@ namespace Wox.Plugin.WebSearch
private void OnDeleteSearchSearchClick(object sender, RoutedEventArgs e)
{
var selected = _settings.SelectedSearchSource;
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)
if (_settings.SelectedSearchSource != null)
{
var id = _context.CurrentPluginMetadata.ID;
PluginManager.RemoveActionKeyword(id, selected.ActionKeyword);
_settings.SearchSources.Remove(selected);
var selected = _settings.SelectedSearchSource;
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;
PluginManager.RemoveActionKeyword(id, selected.ActionKeyword);
_settings.SearchSources.Remove(selected);
}
}
}
private void OnEditSearchSourceClick(object sender, RoutedEventArgs e)
{
var selected = _settings.SelectedSearchSource;
var webSearch = new SearchSourceSettingWindow
if (_settings.SelectedSearchSource != null)
{
var webSearch = new SearchSourceSettingWindow
(
_settings.SearchSources, _context, selected
_settings.SearchSources, _context, _settings.SelectedSearchSource
);
webSearch.ShowDialog();
webSearch.ShowDialog();
}
}
}
}
}