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:
bao-qian
2016-06-21 00:14:32 +01:00
parent 3efeb4a0a6
commit 6e13440f1f
30 changed files with 495 additions and 518 deletions

View File

@@ -5,6 +5,7 @@
<system:String x:Key="wox_plugin_websearch_delete">Löschen</system:String>
<system:String x:Key="wox_plugin_websearch_edit">Bearbeiten</system:String>
<system:String x:Key="wox_plugin_websearch_add">Hinzufügen</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">Confirm</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">Aktionsschlüsselwort</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">Suche</system:String>
@@ -12,7 +13,7 @@
<system:String x:Key="wox_plugin_websearch_pls_select_web_search">Bitte wähle einen Suchdienst</system:String>
<system:String x:Key="wox_plugin_websearch_delete_warning">Willst du wirklich {0} löschen?</system:String>
<!--web search edit-->
<system:String x:Key="wox_plugin_websearch_title">Titel</system:String>
<system:String x:Key="wox_plugin_websearch_enable">Aktivieren</system:String>

View File

@@ -5,13 +5,14 @@
<system:String x:Key="wox_plugin_websearch_delete">Delete</system:String>
<system:String x:Key="wox_plugin_websearch_edit">Edit</system:String>
<system:String x:Key="wox_plugin_websearch_add">Add</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">Confirm</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">Action Keyword</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">Search</system:String>
<system:String x:Key="wox_plugin_websearch_enable_suggestion">Enable search suggestions</system:String>
<system:String x:Key="wox_plugin_websearch_pls_select_web_search">Please select a web search</system:String>
<system:String x:Key="wox_plugin_websearch_delete_warning">Are you sure you want to delete {0}?</system:String>
<!--web search edit-->
<system:String x:Key="wox_plugin_websearch_title">Title</system:String>
<system:String x:Key="wox_plugin_websearch_enable">Enable</system:String>

View File

@@ -5,13 +5,14 @@
<system:String x:Key="wox_plugin_websearch_delete">Usuń</system:String>
<system:String x:Key="wox_plugin_websearch_edit">Edytuj</system:String>
<system:String x:Key="wox_plugin_websearch_add">Dodaj</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">Confirm</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">Wyzwalacz</system:String>
<system:String x:Key="wox_plugin_websearch_url">Adres URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">Szukaj</system:String>
<system:String x:Key="wox_plugin_websearch_enable_suggestion">Pokazuj podpowiedzi wyszukiwania</system:String>
<system:String x:Key="wox_plugin_websearch_pls_select_web_search">Musisz wybrać coś z listy</system:String>
<system:String x:Key="wox_plugin_websearch_delete_warning">Czy jesteś pewnie że chcesz usunąć {0}?</system:String>
<!--web search edit-->
<system:String x:Key="wox_plugin_websearch_title">Tytuł</system:String>
<system:String x:Key="wox_plugin_websearch_enable">Aktywne</system:String>

View File

@@ -5,6 +5,7 @@
<system:String x:Key="wox_plugin_websearch_delete">删除</system:String>
<system:String x:Key="wox_plugin_websearch_edit">编辑</system:String>
<system:String x:Key="wox_plugin_websearch_add">添加</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">确认</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">触发关键字</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">搜索</system:String>

View File

@@ -5,6 +5,7 @@
<system:String x:Key="wox_plugin_websearch_delete">刪除</system:String>
<system:String x:Key="wox_plugin_websearch_edit">編輯</system:String>
<system:String x:Key="wox_plugin_websearch_add">添加</system:String>
<system:String x:Key="wox_plugin_websearch_confirm">确认</system:String>
<system:String x:Key="wox_plugin_websearch_action_keyword">觸發關鍵字</system:String>
<system:String x:Key="wox_plugin_websearch_url">URL</system:String>
<system:String x:Key="wox_plugin_websearch_search">搜索</system:String>
@@ -28,5 +29,5 @@
<system:String x:Key="wox_plugin_websearch_plugin_name">網頁搜索</system:String>
<system:String x:Key="wox_plugin_websearch_plugin_description">提供網頁搜索能力</system:String>
</ResourceDictionary>

View File

@@ -6,28 +6,26 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;
using JetBrains.Annotations;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin.WebSearch.SuggestionSources;
namespace Wox.Plugin.WebSearch
{
public class Main : IPlugin, ISettingProvider, IPluginI18n, IMultipleActionKeywords, ISavable, IResultUpdated
public class Main : IPlugin, ISettingProvider, IPluginI18n, ISavable, IResultUpdated
{
public PluginInitContext Context { get; private set; }
private PluginInitContext _context;
private PluginJsonStorage<Settings> _storage;
private Settings _settings;
private readonly Settings _settings;
private readonly SettingsViewModel _viewModel;
private CancellationTokenSource _updateSource;
private CancellationToken _updateToken;
public const string Images = "Images";
public static string ImagesDirectory;
public static readonly string ImagesDirectory;
public void Save()
{
_storage.Save();
_viewModel.Save();
}
public List<Result> Query(Query query)
@@ -36,21 +34,21 @@ namespace Wox.Plugin.WebSearch
_updateSource = new CancellationTokenSource();
_updateToken = _updateSource.Token;
WebSearch webSearch =
_settings.WebSearches.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled);
SearchSource searchSource =
_settings.SearchSources.FirstOrDefault(o => o.ActionKeyword == query.ActionKeyword && o.Enabled);
if (webSearch != null)
if (searchSource != null)
{
string keyword = query.Search;
string title = keyword;
string subtitle = Context.API.GetTranslation("wox_plugin_websearch_search") + " " + webSearch.Title;
string subtitle = _context.API.GetTranslation("wox_plugin_websearch_search") + " " + searchSource.Title;
if (string.IsNullOrEmpty(keyword))
{
var result = new Result
{
Title = subtitle,
SubTitle = string.Empty,
IcoPath = webSearch.IconPath
IcoPath = searchSource.IconPath
};
return new List<Result> { result };
}
@@ -62,15 +60,15 @@ namespace Wox.Plugin.WebSearch
Title = title,
SubTitle = subtitle,
Score = 6,
IcoPath = webSearch.IconPath,
IcoPath = searchSource.IconPath,
Action = c =>
{
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(keyword)));
return true;
}
};
results.Add(result);
UpdateResultsFromSuggestion(results, keyword, subtitle, webSearch, query);
UpdateResultsFromSuggestion(results, keyword, subtitle, searchSource, query);
return results;
}
}
@@ -80,14 +78,15 @@ namespace Wox.Plugin.WebSearch
}
}
private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle, WebSearch webSearch, Query query)
private void UpdateResultsFromSuggestion(List<Result> results, string keyword, string subtitle,
SearchSource searchSource, Query query)
{
if (_settings.EnableWebSearchSuggestion)
if (_settings.EnableSuggestion)
{
const int waittime = 300;
var task = Task.Run(async () =>
{
var suggestions = await Suggestions(keyword, subtitle, webSearch);
var suggestions = await Suggestions(keyword, subtitle, searchSource);
results.AddRange(suggestions);
}, _updateToken);
@@ -102,21 +101,21 @@ namespace Wox.Plugin.WebSearch
}
}
private async Task<IEnumerable<Result>> Suggestions(string keyword, string subtitle, WebSearch webSearch)
private async Task<IEnumerable<Result>> Suggestions(string keyword, string subtitle, SearchSource searchSource)
{
var source = SuggestionSource.GetSuggestionSource(_settings.WebSearchSuggestionSource);
var source = _settings.SelectedSuggestion;
if (source != null)
{
var suggestions = await source.GetSuggestions(keyword);
var suggestions = await source.Suggestions(keyword);
var resultsFromSuggestion = suggestions.Select(o => new Result
{
Title = o,
SubTitle = subtitle,
Score = 5,
IcoPath = webSearch.IconPath,
IcoPath = searchSource.IconPath,
Action = c =>
{
Process.Start(webSearch.Url.Replace("{q}", Uri.EscapeDataString(o)));
Process.Start(searchSource.Url.Replace("{q}", Uri.EscapeDataString(o)));
return true;
}
});
@@ -127,18 +126,21 @@ namespace Wox.Plugin.WebSearch
static Main()
{
var plugins = Infrastructure.Constant.Plugins;
var plugins = Constant.Plugins;
var assemblyName = typeof(Main).Assembly.GetName().Name;
var pluginDirectory = Path.Combine(Infrastructure.Constant.SettingsPath, plugins, assemblyName);
var pluginDirectory = Path.Combine(Constant.SettingsPath, plugins, assemblyName);
ImagesDirectory = Path.Combine(pluginDirectory, Images);
}
public Main()
{
_viewModel = new SettingsViewModel();
_settings = _viewModel.Settings;
}
public void Init(PluginInitContext context)
{
Context = context;
_storage = new PluginJsonStorage<Settings>();
_settings = _storage.Load();
_context = context;
var pluginDirectory = context.CurrentPluginMetadata.PluginDirectory;
var bundledImagesDirectory = Path.Combine(pluginDirectory, Images);
@@ -149,43 +151,21 @@ namespace Wox.Plugin.WebSearch
public Control CreateSettingPanel()
{
return new WebSearchesSetting(this, _settings);
return new SettingsControl(_context, _viewModel);
}
#endregion
public string GetTranslatedPluginTitle()
{
return Context.API.GetTranslation("wox_plugin_websearch_plugin_name");
return _context.API.GetTranslation("wox_plugin_websearch_plugin_name");
}
public string GetTranslatedPluginDescription()
{
return Context.API.GetTranslation("wox_plugin_websearch_plugin_description");
return _context.API.GetTranslation("wox_plugin_websearch_plugin_description");
}
public bool IsInstantQuery(string query) => false;
[NotifyPropertyChangedInvocator]
public void NotifyActionKeywordsUpdated(string oldActionKeywords, string newActionKeywords)
{
ActionKeywordsChanged?.Invoke(this, new ActionKeywordsChangedEventArgs
{
OldActionKeyword = oldActionKeywords,
NewActionKeyword = newActionKeywords
});
}
[NotifyPropertyChangedInvocator]
public void NotifyActionKeywordsAdded(string newActionKeywords)
{
ActionKeywordsChanged?.Invoke(this, new ActionKeywordsChangedEventArgs
{
NewActionKeyword = newActionKeywords
});
}
public event ActionKeywordsChangedEventHandler ActionKeywordsChanged;
public event ResultUpdatedEventHandler ResultsUpdated;
}
}
}

View File

@@ -0,0 +1,7 @@
namespace Wox.Plugin.WebSearch
{
public class SearchSourceViewModel
{
public SearchSource SearchSource { get; set; }
}
}

View File

@@ -1,12 +1,23 @@
using System.Collections.Generic;
using System;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using Wox.Plugin.WebSearch.SuggestionSources;
namespace Wox.Plugin.WebSearch
{
public class Settings
public class Settings : BaseModel
{
public List<WebSearch> WebSearches { get; set; } = new List<WebSearch>
public Settings()
{
SelectedSuggestion = Suggestions[0];
if (SearchSources.Count > 0)
{
new WebSearch
SelectedSearchSource = SearchSources[0];
}
}
public ObservableCollection<SearchSource> SearchSources { get; set; } = new ObservableCollection<SearchSource>
{
new SearchSource
{
Title = "Google",
ActionKeyword = "g",
@@ -14,7 +25,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://www.google.com/search?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Google Scholar",
ActionKeyword = "sc",
@@ -22,7 +33,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://scholar.google.com/scholar?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Wikipedia",
ActionKeyword = "wiki",
@@ -30,7 +41,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://en.wikipedia.org/wiki/{q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "FindIcon",
ActionKeyword = "findicon",
@@ -38,7 +49,7 @@ namespace Wox.Plugin.WebSearch
Url = "http://findicons.com/search/{q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Facebook",
ActionKeyword = "facebook",
@@ -46,7 +57,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://www.facebook.com/search/?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Twitter",
ActionKeyword = "twitter",
@@ -54,7 +65,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://twitter.com/search?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Google Maps",
ActionKeyword = "maps",
@@ -62,7 +73,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://maps.google.com/maps?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Google Translate",
ActionKeyword = "translate",
@@ -70,7 +81,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://translate.google.com/#auto|en|{q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Duckduckgo",
ActionKeyword = "duckduckgo",
@@ -78,7 +89,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://duckduckgo.com/?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Github",
ActionKeyword = "github",
@@ -86,7 +97,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://github.com/search?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Github Gist",
ActionKeyword = "gist",
@@ -94,7 +105,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://gist.github.com/search?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Gmail",
ActionKeyword = "gmail",
@@ -102,7 +113,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://mail.google.com/mail/ca/u/0/#apps/{q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Google Drive",
ActionKeyword = "drive",
@@ -110,7 +121,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://drive.google.com/?hl=en&tab=bo#search/{q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Wolframalpha",
ActionKeyword = "wolframalpha",
@@ -118,7 +129,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://www.wolframalpha.com/input/?i={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Stackoverflow",
ActionKeyword = "stackoverflow",
@@ -126,7 +137,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://stackoverflow.com/search?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "I'm Feeling Lucky",
ActionKeyword = "lucky",
@@ -134,7 +145,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://google.com/search?q={q}&btnI=I",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Google Image",
ActionKeyword = "image",
@@ -142,7 +153,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://www.google.com/search?q={q}&tbm=isch",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Youtube",
ActionKeyword = "youtube",
@@ -150,7 +161,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://www.youtube.com/results?search_query={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Bing",
ActionKeyword = "bing",
@@ -158,7 +169,7 @@ namespace Wox.Plugin.WebSearch
Url = "https://www.bing.com/search?q={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title = "Yahoo",
ActionKeyword = "yahoo",
@@ -166,18 +177,50 @@ namespace Wox.Plugin.WebSearch
Url = "https://www.search.yahoo.com/search?p={q}",
Enabled = true
},
new WebSearch
new SearchSource
{
Title= "Baidu",
ActionKeyword= "bd",
Icon= "baidu.png",
Url="https://www.baidu.com/#ie=UTF-8&wd={q}",
Enabled= true
Title = "Baidu",
ActionKeyword = "bd",
Icon = "baidu.png",
Url = "https://www.baidu.com/#ie=UTF-8&wd={q}",
Enabled = true
}
};
public bool EnableWebSearchSuggestion { get; set; }
[JsonIgnore]
public SearchSource SelectedSearchSource { get; set; }
public bool EnableSuggestion { get; set; }
[JsonIgnore]
public SuggestionSource[] Suggestions { get; set; } = {
new Google(),
new Baidu()
};
[JsonIgnore]
public SuggestionSource SelectedSuggestion { get; set; }
/// <summary>
/// used to store Settings.json only
/// </summary>
public string Suggestion
{
get
{
return SelectedSuggestion.ToString();
}
set
{
foreach (var s in Suggestions)
{
if (string.Equals(s.ToString(), value, StringComparison.OrdinalIgnoreCase))
{
SelectedSuggestion = s;
}
}
}
}
public string WebSearchSuggestionSource { get; set; }
}
}

View File

@@ -0,0 +1,22 @@
using Wox.Infrastructure.Storage;
namespace Wox.Plugin.WebSearch
{
public class SettingsViewModel
{
private readonly JsonStrorage<Settings> _storage;
public SettingsViewModel()
{
_storage = new PluginJsonStorage<Settings>();
Settings = _storage.Load();
}
public Settings Settings { get; set; }
public void Save()
{
_storage.Save();
}
}
}

View File

@@ -13,11 +13,9 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
{
public class Baidu : SuggestionSource
{
public override string Domain { get; set; } = "www.baidu.com";
private readonly Regex _reg = new Regex("window.baidu.sug\\((.*)\\)");
Regex reg = new Regex("window.baidu.sug\\((.*)\\)");
public override async Task<List<string>> GetSuggestions(string query)
public override async Task<List<string>> Suggestions(string query)
{
string result;
@@ -30,12 +28,12 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
{
Log.Warn("Can't get suggestion from baidu");
Log.Exception(e);
return new List<string>(); ;
return new List<string>();
;
}
if (string.IsNullOrEmpty(result)) return new List<string>();
Match match = reg.Match(result);
Match match = _reg.Match(result);
if (match.Success)
{
JContainer json;
@@ -61,5 +59,10 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
return new List<string>();
}
public override string ToString()
{
return "Baidu";
}
}
}
}

View File

@@ -12,8 +12,7 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
{
public class Google : SuggestionSource
{
public override string Domain { get; set; } = "www.google.com";
public override async Task<List<string>> GetSuggestions(string query)
public override async Task<List<string>> Suggestions(string query)
{
string result;
try
@@ -25,7 +24,8 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
{
Log.Warn("Can't get suggestion from google");
Log.Exception(e);
return new List<string>(); ;
return new List<string>();
;
}
if (string.IsNullOrEmpty(result)) return new List<string>();
JContainer json;
@@ -48,5 +48,10 @@ namespace Wox.Plugin.WebSearch.SuggestionSources
}
return new List<string>();
}
public override string ToString()
{
return "Google";
}
}
}
}

View File

@@ -3,25 +3,9 @@ using System.Threading.Tasks;
namespace Wox.Plugin.WebSearch.SuggestionSources
{
//todo rename file
public abstract class SuggestionSource
{
public virtual string Domain { get; set; }
public abstract Task<List<string>> GetSuggestions(string query);
public static SuggestionSource GetSuggestionSource(string name)
{
switch (name.ToLower())
{
case "google":
return new Google();
case "baidu":
return new Baidu();
default:
return null;
}
}
public abstract Task<List<string>> Suggestions(string query);
}
}
}

View File

@@ -1,40 +1,43 @@
using System.IO;
using System.Windows.Media;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Wox.Infrastructure.Image;
namespace Wox.Plugin.WebSearch
{
public class WebSearch
public class SearchSource : BaseModel
{
public const string DefaultIcon = "web_search.png";
public string Title { get; set; }
public string ActionKeyword { get; set; }
[NotNull]
private string _icon = DefaultIcon;
[NotNull]
public string Icon
{
get { return _icon; }
set
{
_icon = value;
IconPath = Path.Combine(Main.ImagesDirectory, value);
}
}
public string Icon { private get; set; } = DefaultIcon;
/// <summary>
/// All icon should be put under Images directory
/// </summary>
[NotNull]
[JsonIgnore]
internal string IconPath { get; private set; } = Path.Combine
(
Main.ImagesDirectory, DefaultIcon
);
internal string IconPath => Path.Combine(Main.ImagesDirectory, Icon);
[JsonIgnore]
public ImageSource Image => ImageLoader.Load(IconPath);
public string Url { get; set; }
public bool Enabled { get; set; }
public SearchSource DeepCopy()
{
var webSearch = new SearchSource
{
Title = string.Copy(Title),
ActionKeyword = string.Copy(ActionKeyword),
Url = string.Copy(Url),
Icon = string.Copy(Icon),
Enabled = Enabled
};
return webSearch;
}
}
}

View File

@@ -1,42 +1,60 @@
<Window x:Class="Wox.Plugin.WebSearch.WebSearchSetting"
<Window x:Class="Wox.Plugin.WebSearch.SearchSourceSettingWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:Wox.Plugin.WebSearch"
mc:Ignorable="d"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
Title="WebSearchSetting" Height="350" Width="674.766">
Title="Search Source Setting" Height="300" Width="500"
d:DataContext="{d:DesignInstance vm:SearchSourceViewModel}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="60"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="60" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="120" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Margin="10" FontSize="14" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_title}"></TextBlock>
<TextBox x:Name="WebSearchName" Margin="10" Grid.Row="0" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center"
HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_title}" />
<TextBox Text="{Binding SearchSource.Title}" Margin="10" Grid.Row="0" Width="300" Grid.Column="1" VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBlock Margin="10" FontSize="14" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_url}"></TextBlock>
<TextBox x:Name="Url" Margin="10" Grid.Row="1" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center"
HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_url}" />
<TextBox Text="{Binding SearchSource.Url}" Margin="10" Grid.Row="1" Width="300" Grid.Column="1" VerticalAlignment="Center"
HorizontalAlignment="Left" />
<TextBlock Margin="10" FontSize="14" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_action_keyword}"></TextBlock>
<TextBox x:Name="Actionword" Margin="10" Grid.Row="2" Width="400" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></TextBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center"
HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_action_keyword}" />
<TextBox Text="{Binding SearchSource.ActionKeyword}" Margin="10" Grid.Row="2" Width="300" Grid.Column="1"
VerticalAlignment="Center" HorizontalAlignment="Left" />
<TextBlock Margin="10" FontSize="14" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_enable}"></TextBlock>
<CheckBox x:Name="EnableCheckBox" IsChecked="True" Margin="10" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center"></CheckBox>
<TextBlock Margin="10" FontSize="14" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center"
HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_enable}" />
<CheckBox IsChecked="{Binding SearchSource.Enabled}" Margin="10" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" />
<TextBlock Margin="10" FontSize="14" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_icon}"></TextBlock>
<TextBlock Margin="10" FontSize="14" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center"
HorizontalAlignment="Right" Text="{DynamicResource wox_plugin_websearch_icon}" />
<StackPanel Orientation="Horizontal" Grid.Row="4" Grid.Column="1" Margin="10">
<Image x:Name="WebSearchIcon" Width="24" Height="24" Margin="0 0 10 0" />
<Button x:Name="SelectIconButton" Height="24" Click="SelectIconButtonOnClick" Content="{DynamicResource wox_plugin_websearch_select_icon}"></Button>
<Image Source="{Binding SearchSource.Image ,IsAsync=True}" Width="24" Height="24" Margin="0 0 10 0" />
<Button Click="OnSelectIconClick" Height="24"
Content="{DynamicResource wox_plugin_websearch_select_icon}" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Grid.Row="5" Grid.Column="1">
<Button x:Name="CancelButton" Click="CancelButtonOnClick" Margin="10 0 10 0" Width="80" Height="25" Content="{DynamicResource wox_plugin_websearch_cancel}"/>
<Button x:Name="ConfirmButton" Margin="10 0 10 0" Width="80" Height="25" Click="ConfirmButtonOnClick" Content="{DynamicResource wox_plugin_websearch_add}"/>
<Button Click="OnCancelButtonClick"
Margin="10 0 10 0" Width="80" Height="25"
Content="{DynamicResource wox_plugin_websearch_cancel}" />
<Button Click="OnConfirmButtonClick"
Margin="10 0 10 0" Width="80" Height="25"
Content="{DynamicResource wox_plugin_websearch_confirm}" />
</StackPanel>
</Grid>
</Window>
</Window>

View File

@@ -1,150 +1,150 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using Microsoft.Win32;
using Wox.Infrastructure.Exception;
using Wox.Infrastructure.Image;
using Wox.Core.Plugin;
namespace Wox.Plugin.WebSearch
{
public partial class WebSearchSetting
public partial class SearchSourceSettingWindow
{
private readonly WebSearchesSetting _settingWindow;
private bool _isUpdate;
private WebSearch _webSearch;
private readonly PluginInitContext _context;
private readonly Main _plugin;
private readonly Settings _settings;
private readonly SearchSource _oldSearchSource;
private SearchSource _searchSource;
private IList<SearchSource> _searchSources;
private Action _action;
private PluginInitContext _context;
private IPublicAPI _api;
private SearchSourceViewModel _viewModel;
public WebSearchSetting(WebSearchesSetting settingWidow, Settings settings)
public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context, SearchSource old)
{
_oldSearchSource = old;
_viewModel = new SearchSourceViewModel { SearchSource = old.DeepCopy() };
Initilize(sources, context, Action.Edit);
}
public SearchSourceSettingWindow(IList<SearchSource> sources, PluginInitContext context)
{
_viewModel = new SearchSourceViewModel { SearchSource = new SearchSource() };
Initilize(sources, context, Action.Add);
}
private void Initilize(IList<SearchSource> sources, PluginInitContext context, Action action)
{
InitializeComponent();
WebSearchName.Focus();
_plugin = settingWidow.Plugin;
_context = settingWidow.Context;
_settingWindow = settingWidow;
_settings = settings;
DataContext = _viewModel;
_searchSource = _viewModel.SearchSource;
_searchSources = sources;
_action = action;
_context = context;
_api = _context.API;
}
public void UpdateItem(WebSearch webSearch)
{
_webSearch = _settings.WebSearches.FirstOrDefault(o => o == webSearch);
if (_webSearch == null || string.IsNullOrEmpty(_webSearch.Url))
{
string warning = _context.API.GetTranslation("wox_plugin_websearch_invalid_web_search");
MessageBox.Show(warning);
Close();
return;
}
_isUpdate = true;
ConfirmButton.Content = "Update";
WebSearchIcon.Source = ImageLoader.Load(webSearch.IconPath);
EnableCheckBox.IsChecked = webSearch.Enabled;
WebSearchName.Text = webSearch.Title;
Url.Text = webSearch.Url;
Actionword.Text = webSearch.ActionKeyword;
}
public void AddItem(WebSearch webSearch)
{
_webSearch = webSearch;
WebSearchIcon.Source = ImageLoader.Load(webSearch.IconPath);
}
private void CancelButtonOnClick(object sender, RoutedEventArgs e)
private void OnCancelButtonClick(object sender, RoutedEventArgs e)
{
Close();
}
/// <summary>
/// Confirm button for both add and update
/// </summary>
private void ConfirmButtonOnClick(object sender, RoutedEventArgs e)
private void OnConfirmButtonClick(object sender, RoutedEventArgs e)
{
string title = WebSearchName.Text;
if (string.IsNullOrEmpty(title))
{
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_title");
MessageBox.Show(warning);
return;
}
string url = Url.Text;
if (string.IsNullOrEmpty(url))
if (string.IsNullOrEmpty(_searchSource.Title))
{
string warning = _context.API.GetTranslation("wox_plugin_websearch_input_url");
var warning = _api.GetTranslation("wox_plugin_websearch_input_title");
MessageBox.Show(warning);
return;
}
string newActionKeyword = Actionword.Text.Trim();
if (_isUpdate)
else if (string.IsNullOrEmpty(_searchSource.Url))
{
try
{
_plugin.NotifyActionKeywordsUpdated(_webSearch.ActionKeyword, newActionKeyword);
}
catch (WoxPluginException exception)
{
MessageBox.Show(exception.Message);
return;
}
var warning = _api.GetTranslation("wox_plugin_websearch_input_url");
MessageBox.Show(warning);
}
else if (string.IsNullOrEmpty(_searchSource.ActionKeyword))
{
var warning = _api.GetTranslation("wox_plugin_websearch_input_action_keyword");
MessageBox.Show(warning);
}
else if (_action == Action.Add)
{
AddSearchSource();
}
else if (_action == Action.Edit)
{
EditSearchSource();
}
}
private void AddSearchSource()
{
var keyword = _searchSource.ActionKeyword;
if (!PluginManager.ActionKeywordRegistered(keyword))
{
var id = _context.CurrentPluginMetadata.ID;
PluginManager.AddActionKeyword(id, keyword);
_searchSources.Add(_searchSource);
var info = _api.GetTranslation("succeed");
MessageBox.Show(info);
Close();
}
else
{
try
{
_plugin.NotifyActionKeywordsAdded(newActionKeyword);
}
catch (WoxPluginException exception)
{
MessageBox.Show(exception.Message);
return;
}
_settings.WebSearches.Add(_webSearch);
var warning = _api.GetTranslation("newActionKeywordsHasBeenAssigned");
MessageBox.Show(warning);
}
_webSearch.ActionKeyword = newActionKeyword;
_webSearch.Enabled = EnableCheckBox.IsChecked ?? false;
_webSearch.Url = url;
_webSearch.Title = title;
_settingWindow.ReloadWebSearchView();
Close();
}
private void SelectIconButtonOnClick(object sender, RoutedEventArgs e)
private void EditSearchSource()
{
var keyword = _searchSource.ActionKeyword;
if (!PluginManager.ActionKeywordRegistered(keyword))
{
var newKeyword = keyword;
var oldKeyword = _oldSearchSource.ActionKeyword;
var id = _context.CurrentPluginMetadata.ID;
PluginManager.ReplaceActionKeyword(id, oldKeyword, newKeyword);
var index = _searchSources.IndexOf(_oldSearchSource);
_searchSources[index] = _searchSource;
var info = _api.GetTranslation("succeed");
MessageBox.Show(info);
Close();
}
else
{
var warning = _api.GetTranslation("newActionKeywordsHasBeenAssigned");
MessageBox.Show(warning);
}
}
private void OnSelectIconClick(object sender, RoutedEventArgs e)
{
var directory = Path.Combine(Main.ImagesDirectory, Main.Images);
var dlg = new OpenFileDialog
{
InitialDirectory = directory,
Filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp"
};
const string filter = "Image files (*.jpg, *.jpeg, *.gif, *.png, *.bmp) |*.jpg; *.jpeg; *.gif; *.png; *.bmp";
var dialog = new OpenFileDialog { InitialDirectory = directory, Filter = filter };
bool? result = dlg.ShowDialog();
if (result != null && result == true)
var result = dialog.ShowDialog();
if (result == true)
{
string fullpath = dlg.FileName;
if (fullpath != null)
var fullpath = dialog.FileName;
if (!string.IsNullOrEmpty(fullpath))
{
_webSearch.Icon = Path.GetFileName(fullpath);
if (File.Exists(_webSearch.IconPath))
_searchSource.Icon = Path.GetFileName(fullpath);
if (!File.Exists(_searchSource.IconPath))
{
WebSearchIcon.Source = ImageLoader.Load(_webSearch.IconPath);
}
else
{
_webSearch.Icon = WebSearch.DefaultIcon;
_searchSource.Icon = SearchSource.DefaultIcon;
MessageBox.Show($"The file should be put under {directory}");
}
}
}
}
}
}
public enum Action
{
Add,
Edit
}
}

View File

@@ -1,35 +1,45 @@
<UserControl x:Class="Wox.Plugin.WebSearch.WebSearchesSetting"
<UserControl x:Class="Wox.Plugin.WebSearch.SettingsControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:Wox.Plugin.WebSearch"
mc:Ignorable="d"
Background="White"
d:DataContext="{d:DesignInstance vm:SettingsViewModel}"
d:DesignHeight="300" d:DesignWidth="500">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="42"/>
<RowDefinition/>
<RowDefinition Height="50"/>
<RowDefinition Height="40" />
<RowDefinition />
<RowDefinition Height="40" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<CheckBox x:Name="cbEnableWebSearchSuggestion" Unchecked="CbEnableWebSearchSuggestion_OnUnchecked" Checked="CbEnableWebSearchSuggestion_OnChecked" Margin="0 10 10 10" Content="{DynamicResource wox_plugin_websearch_enable_suggestion}"></CheckBox>
<ComboBox x:Name="comboBoxSuggestionSource" SelectionChanged="ComboBoxSuggestionSource_OnSelectionChanged" Margin="10">
</ComboBox>
<CheckBox IsChecked="{Binding Settings.EnableSuggestion}"
Margin="0 10 10 10"
Name="EnableSuggestion"
Content="{DynamicResource wox_plugin_websearch_enable_suggestion}" />
<ComboBox ItemsSource="{Binding Settings.Suggestions}"
SelectedItem="{Binding Settings.SelectedSuggestion}"
IsEnabled="{Binding ElementName=EnableSuggestion, Path=IsChecked}" Margin="10" />
<!-- Not sure why binding IsEnabled directly to Settings.EnableWebSaerchSuggestion is not working -->
</StackPanel>
<ListView x:Name="webSearchView" Grid.Row="1">
<ListView ItemsSource="{Binding Settings.SearchSources}"
SelectedItem="{Binding Settings.SelectedSearchSource}"
Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Header="{DynamicResource wox_plugin_websearch_action_keyword}" Width="180">
<GridViewColumn Header="{DynamicResource wox_plugin_websearch_action_keyword}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding ActionKeyword}"/>
<TextBlock Text="{Binding ActionKeyword}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{DynamicResource wox_plugin_websearch_url}" Width="500">
<GridViewColumn Header="{DynamicResource wox_plugin_websearch_url}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Url}"/>
<TextBlock Text="{Binding Url}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
@@ -37,9 +47,12 @@
</ListView.View>
</ListView>
<StackPanel Grid.Row="2" HorizontalAlignment="Right" Orientation="Horizontal">
<Button x:Name="btnDeleteWebSearch" Click="btnDeleteWebSearch_OnClick" Width="100" Margin="10" Content="{DynamicResource wox_plugin_websearch_delete}"/>
<Button x:Name="btnEditWebSearch" Click="btnEditWebSearch_OnClick" Width="100" Margin="10" Content="{DynamicResource wox_plugin_websearch_edit}"/>
<Button x:Name="btnAddWebSearch" Click="btnAddWebSearch_OnClick" Width="100" Margin="10" Content="{DynamicResource wox_plugin_websearch_add}"/>
<Button Click="OnDeleteSearchSearchClick" Width="100" Margin="10"
Content="{DynamicResource wox_plugin_websearch_delete}" />
<Button Click="OnEditSearchSourceClick" Width="100" Margin="10"
Content="{DynamicResource wox_plugin_websearch_edit}" />
<Button Click="OnAddSearchSearchClick" Width="100" Margin="10"
Content="{DynamicResource wox_plugin_websearch_add}" />
</StackPanel>
</Grid>
</UserControl>
</UserControl>

View File

@@ -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();
}
}
}
}

View File

@@ -59,6 +59,8 @@
<Link>Properties\SolutionAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SearchSourceViewModel.cs" />
<Compile Include="SettingsViewModel.cs" />
<Compile Include="SuggestionSources\Baidu.cs" />
<Compile Include="SuggestionSources\Google.cs" />
<Compile Include="SuggestionSources\ISuggestionSource.cs" />
@@ -162,6 +164,10 @@
</Page>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Wox.Core\Wox.Core.csproj">
<Project>{B749F0DB-8E75-47DB-9E5E-265D16D0C0D2}</Project>
<Name>Wox.Core</Name>
</ProjectReference>
<ProjectReference Include="..\..\Wox.Infrastructure\Wox.Infrastructure.csproj">
<Project>{4fd29318-a8ab-4d8f-aa47-60bc241b8da3}</Project>
<Name>Wox.Infrastructure</Name>

View File

@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="JetBrains.Annotations" version="10.1.4" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net452" />