mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
MVVM refactoring for web search plugin, part 1
1. #486 2. fix #778 #763 #742 3. MVVM refactoring 4. remove IMultipleActionKeywords interface, use PluginManager directly
This commit is contained in:
@@ -1,120 +1,54 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Wox.Core.Plugin;
|
||||
|
||||
namespace Wox.Plugin.WebSearch
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for WebSearchesSetting.xaml
|
||||
/// </summary>
|
||||
public partial class WebSearchesSetting : UserControl
|
||||
public partial class SettingsControl : UserControl
|
||||
{
|
||||
private Settings _settings;
|
||||
public PluginInitContext Context { get; }
|
||||
public Main Plugin { get; }
|
||||
private readonly Settings _settings;
|
||||
private readonly PluginInitContext _context;
|
||||
|
||||
public WebSearchesSetting(Main plugin, Settings settings)
|
||||
public SettingsControl(PluginInitContext context, SettingsViewModel viewModel)
|
||||
{
|
||||
Context = plugin.Context;
|
||||
Plugin = plugin;
|
||||
InitializeComponent();
|
||||
Loaded += Setting_Loaded;
|
||||
_settings = settings;
|
||||
_context = context;
|
||||
_settings = viewModel.Settings;
|
||||
DataContext = viewModel;
|
||||
}
|
||||
|
||||
private void Setting_Loaded(object sender, RoutedEventArgs e)
|
||||
private void OnAddSearchSearchClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
webSearchView.ItemsSource = _settings.WebSearches;
|
||||
cbEnableWebSearchSuggestion.IsChecked = _settings.EnableWebSearchSuggestion;
|
||||
comboBoxSuggestionSource.Visibility = _settings.EnableWebSearchSuggestion
|
||||
? Visibility.Visible
|
||||
: Visibility.Collapsed;
|
||||
|
||||
List<ComboBoxItem> items = new List<ComboBoxItem>
|
||||
{
|
||||
new ComboBoxItem {Content = "Google"},
|
||||
new ComboBoxItem {Content = "Baidu"}
|
||||
};
|
||||
ComboBoxItem selected = items.FirstOrDefault(o => o.Content.ToString() == _settings.WebSearchSuggestionSource);
|
||||
if (selected == null)
|
||||
{
|
||||
selected = items[0];
|
||||
}
|
||||
comboBoxSuggestionSource.ItemsSource = items;
|
||||
comboBoxSuggestionSource.SelectedItem = selected;
|
||||
}
|
||||
|
||||
public void ReloadWebSearchView()
|
||||
{
|
||||
webSearchView.Items.Refresh();
|
||||
}
|
||||
|
||||
|
||||
private void btnAddWebSearch_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var setting = new WebSearchSetting(this, _settings);
|
||||
var webSearch = new WebSearch();
|
||||
|
||||
|
||||
setting.AddItem(webSearch);
|
||||
var setting = new SearchSourceSettingWindow(_settings.SearchSources, _context);
|
||||
setting.ShowDialog();
|
||||
}
|
||||
|
||||
private void btnDeleteWebSearch_OnClick(object sender, RoutedEventArgs e)
|
||||
private void OnDeleteSearchSearchClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
WebSearch selectedWebSearch = webSearchView.SelectedItem as WebSearch;
|
||||
if (selectedWebSearch != null)
|
||||
{
|
||||
string msg = string.Format(Context.API.GetTranslation("wox_plugin_websearch_delete_warning"), selectedWebSearch.Title);
|
||||
var selected = _settings.SelectedSearchSource;
|
||||
var warning = _context.API.GetTranslation("wox_plugin_websearch_delete_warning");
|
||||
var formated = string.Format(warning, selected.Title);
|
||||
|
||||
if (MessageBox.Show(msg, string.Empty, MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
_settings.WebSearches.Remove(selectedWebSearch);
|
||||
webSearchView.Items.Refresh();
|
||||
}
|
||||
}
|
||||
else
|
||||
var result = MessageBox.Show(formated, string.Empty, MessageBoxButton.YesNo);
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
string warning = Context.API.GetTranslation("wox_plugin_websearch_pls_select_web_search");
|
||||
MessageBox.Show(warning);
|
||||
var id = _context.CurrentPluginMetadata.ID;
|
||||
PluginManager.RemoveActionKeyword(id, selected.ActionKeyword);
|
||||
_settings.SearchSources.Remove(selected);
|
||||
}
|
||||
}
|
||||
|
||||
private void btnEditWebSearch_OnClick(object sender, RoutedEventArgs e)
|
||||
private void OnEditSearchSourceClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
WebSearch selectedWebSearch = webSearchView.SelectedItem as WebSearch;
|
||||
if (selectedWebSearch != null)
|
||||
{
|
||||
WebSearchSetting webSearch = new WebSearchSetting(this, _settings);
|
||||
webSearch.UpdateItem(selectedWebSearch);
|
||||
webSearch.ShowDialog();
|
||||
}
|
||||
else
|
||||
{
|
||||
string warning = Context.API.GetTranslation("wox_plugin_websearch_pls_select_web_search");
|
||||
MessageBox.Show(warning);
|
||||
}
|
||||
}
|
||||
|
||||
private void CbEnableWebSearchSuggestion_OnChecked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
comboBoxSuggestionSource.Visibility = Visibility.Visible;
|
||||
_settings.EnableWebSearchSuggestion = true;
|
||||
}
|
||||
|
||||
private void CbEnableWebSearchSuggestion_OnUnchecked(object sender, RoutedEventArgs e)
|
||||
{
|
||||
comboBoxSuggestionSource.Visibility = Visibility.Collapsed;
|
||||
_settings.EnableWebSearchSuggestion = false;
|
||||
}
|
||||
|
||||
private void ComboBoxSuggestionSource_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
if (e.AddedItems.Count > 0)
|
||||
{
|
||||
_settings.WebSearchSuggestionSource = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
|
||||
}
|
||||
var selected = _settings.SelectedSearchSource;
|
||||
var webSearch = new SearchSourceSettingWindow
|
||||
(
|
||||
_settings.SearchSources, _context, selected
|
||||
);
|
||||
webSearch.ShowDialog();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user