mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 19:26:39 +02:00
Fix setting for multiple action keywords
1. completed rewrite the action keyword setting logic. 2. Fix setting for multiple action keywords in #352 3. Fix setting for Web Search plugin
This commit is contained in:
@@ -5,37 +5,40 @@ using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Imaging;
|
||||
using Microsoft.Win32;
|
||||
using Wox.Infrastructure.Exception;
|
||||
|
||||
namespace Wox.Plugin.WebSearch
|
||||
{
|
||||
public partial class WebSearchSetting : Window
|
||||
{
|
||||
private string defaultWebSearchImageDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images\\websearch");
|
||||
private WebSearchesSetting settingWindow;
|
||||
private bool update;
|
||||
private WebSearch updateWebSearch;
|
||||
private PluginInitContext context;
|
||||
private string _defaultWebSearchImageDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Images\\websearch");
|
||||
private readonly WebSearchesSetting _settingWindow;
|
||||
private bool _isUpdate;
|
||||
private WebSearch _updateWebSearch;
|
||||
private readonly PluginInitContext _context;
|
||||
private readonly WebSearchPlugin _plguin;
|
||||
|
||||
public WebSearchSetting(WebSearchesSetting settingWidow,PluginInitContext context)
|
||||
public WebSearchSetting(WebSearchesSetting settingWidow)
|
||||
{
|
||||
this.context = context;
|
||||
this.settingWindow = settingWidow;
|
||||
_plguin = settingWidow.Plugin;
|
||||
_context = settingWidow.Context;
|
||||
_settingWindow = settingWidow;
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
public void UpdateItem(WebSearch webSearch)
|
||||
{
|
||||
updateWebSearch = WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o == webSearch);
|
||||
if (updateWebSearch == null || string.IsNullOrEmpty(updateWebSearch.Url))
|
||||
_updateWebSearch = WebSearchStorage.Instance.WebSearches.FirstOrDefault(o => o == webSearch);
|
||||
if (_updateWebSearch == null || string.IsNullOrEmpty(_updateWebSearch.Url))
|
||||
{
|
||||
|
||||
string warning = context.API.GetTranslation("wox_plugin_websearch_invalid_web_search");
|
||||
string warning = _context.API.GetTranslation("wox_plugin_websearch_invalid_web_search");
|
||||
MessageBox.Show(warning);
|
||||
Close();
|
||||
return;
|
||||
}
|
||||
|
||||
update = true;
|
||||
_isUpdate = true;
|
||||
lblAdd.Text = "Update";
|
||||
tbIconPath.Text = webSearch.IconPath;
|
||||
ShowIcon(webSearch.IconPath);
|
||||
@@ -49,7 +52,7 @@ namespace Wox.Plugin.WebSearch
|
||||
{
|
||||
try
|
||||
{
|
||||
imgIcon.Source = new BitmapImage(new Uri(path));
|
||||
imgIcon.Source = new BitmapImage(new Uri(path, UriKind.Relative));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -61,12 +64,15 @@ namespace Wox.Plugin.WebSearch
|
||||
Close();
|
||||
}
|
||||
|
||||
private void btnAdd_OnClick(object sender, RoutedEventArgs e)
|
||||
/// <summary>
|
||||
/// Confirm button for both add and update
|
||||
/// </summary>
|
||||
private void btnConfirm_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
string title = tbTitle.Text;
|
||||
if (string.IsNullOrEmpty(title))
|
||||
{
|
||||
string warning = context.API.GetTranslation("wox_plugin_websearch_input_title");
|
||||
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_title");
|
||||
MessageBox.Show(warning);
|
||||
return;
|
||||
}
|
||||
@@ -74,81 +80,75 @@ namespace Wox.Plugin.WebSearch
|
||||
string url = tbUrl.Text;
|
||||
if (string.IsNullOrEmpty(url))
|
||||
{
|
||||
string warning = context.API.GetTranslation("wox_plugin_websearch_input_url");
|
||||
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_url");
|
||||
MessageBox.Show(warning);
|
||||
return;
|
||||
}
|
||||
|
||||
string action = tbActionword.Text;
|
||||
if (string.IsNullOrEmpty(action))
|
||||
string newActionKeyword = tbActionword.Text.Trim();
|
||||
if (string.IsNullOrEmpty(newActionKeyword))
|
||||
{
|
||||
string warning = context.API.GetTranslation("wox_plugin_websearch_input_action_keyword");
|
||||
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_action_keyword");
|
||||
MessageBox.Show(warning);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (!update)
|
||||
if (_isUpdate)
|
||||
{
|
||||
if (WebSearchStorage.Instance.WebSearches.Exists(o => o.ActionKeyword == action))
|
||||
try
|
||||
{
|
||||
string warning = context.API.GetTranslation("wox_plugin_websearch_action_keyword_exist");
|
||||
MessageBox.Show(warning);
|
||||
_plguin.NotifyActionKeywordsUpdated(_updateWebSearch.ActionKeyword, newActionKeyword);
|
||||
}
|
||||
catch (WoxPluginException exception)
|
||||
{
|
||||
MessageBox.Show(exception.Message);
|
||||
return;
|
||||
}
|
||||
|
||||
_updateWebSearch.ActionKeyword = newActionKeyword;
|
||||
_updateWebSearch.IconPath = tbIconPath.Text;
|
||||
_updateWebSearch.Enabled = cbEnable.IsChecked ?? false;
|
||||
_updateWebSearch.Url = url;
|
||||
_updateWebSearch.Title = title;
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
_plguin.NotifyActionKeywordsAdded(newActionKeyword);
|
||||
}
|
||||
catch (WoxPluginException exception)
|
||||
{
|
||||
MessageBox.Show(exception.Message);
|
||||
return;
|
||||
}
|
||||
WebSearchStorage.Instance.WebSearches.Add(new WebSearch()
|
||||
{
|
||||
ActionKeyword = action,
|
||||
ActionKeyword = newActionKeyword,
|
||||
Enabled = cbEnable.IsChecked ?? false,
|
||||
IconPath = tbIconPath.Text,
|
||||
Url = url,
|
||||
Title = title
|
||||
});
|
||||
|
||||
//save the action keywords, the order is not metters. Wox will read this metadata when save settings.
|
||||
context.CurrentPluginMetadata.ActionKeywords.Add(action);
|
||||
|
||||
string msg = context.API.GetTranslation("wox_plugin_websearch_succeed");
|
||||
MessageBox.Show(msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (WebSearchStorage.Instance.WebSearches.Exists(o => o.ActionKeyword == action && o != updateWebSearch))
|
||||
{
|
||||
string warning = context.API.GetTranslation("wox_plugin_websearch_action_keyword_exist");
|
||||
MessageBox.Show(warning);
|
||||
return;
|
||||
}
|
||||
updateWebSearch.ActionKeyword = action;
|
||||
updateWebSearch.IconPath = tbIconPath.Text;
|
||||
updateWebSearch.Enabled = cbEnable.IsChecked ?? false;
|
||||
updateWebSearch.Url = url;
|
||||
updateWebSearch.Title= title;
|
||||
|
||||
//save the action keywords, the order is not metters. Wox will read this metadata when save settings.
|
||||
context.CurrentPluginMetadata.ActionKeywords.Add(action);
|
||||
|
||||
string msg = context.API.GetTranslation("wox_plugin_websearch_succeed");
|
||||
MessageBox.Show(msg);
|
||||
}
|
||||
WebSearchStorage.Instance.Save();
|
||||
|
||||
settingWindow.ReloadWebSearchView();
|
||||
_settingWindow.ReloadWebSearchView();
|
||||
Close();
|
||||
}
|
||||
|
||||
private void BtnSelectIcon_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if(!Directory.Exists(defaultWebSearchImageDirectory))
|
||||
if (!Directory.Exists(_defaultWebSearchImageDirectory))
|
||||
{
|
||||
defaultWebSearchImageDirectory =
|
||||
_defaultWebSearchImageDirectory =
|
||||
Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
|
||||
}
|
||||
|
||||
var dlg = new OpenFileDialog
|
||||
{
|
||||
InitialDirectory = defaultWebSearchImageDirectory,
|
||||
Filter ="Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"
|
||||
InitialDirectory = _defaultWebSearchImageDirectory,
|
||||
Filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"
|
||||
};
|
||||
|
||||
bool? result = dlg.ShowDialog();
|
||||
|
||||
Reference in New Issue
Block a user