From 1fa4be9ebb25fa6505ba02d67be560713ae18db8 Mon Sep 17 00:00:00 2001 From: Yeechan Lu Date: Wed, 19 Mar 2014 20:16:20 +0800 Subject: [PATCH] Close #42 Program Source settings GUI --- .../UserSettings/ProgramSource.cs | 7 +- .../UserSettings/UserSetting.cs | 6 +- Wox.Plugin.System/AppPathsProgramSource.cs | 7 +- .../CommonStartMenuProgramSource.cs | 6 +- Wox.Plugin.System/FileSystemProgramSource.cs | 7 +- .../PortableAppsProgramSource.cs | 8 +- Wox.Plugin.System/Programs.cs | 4 +- .../UserStartMenuProgramSource.cs | 7 +- Wox/ProgramSourceSetting.xaml | 40 ++++++ Wox/ProgramSourceSetting.xaml.cs | 129 ++++++++++++++++++ Wox/SettingWindow.xaml | 51 +++++++ Wox/SettingWindow.xaml.cs | 45 +++++- Wox/StringEmptyConverter.cs | 29 ++++ Wox/WebSearchSetting.xaml.cs | 5 + Wox/Wox.csproj | 8 ++ 15 files changed, 347 insertions(+), 12 deletions(-) create mode 100644 Wox/ProgramSourceSetting.xaml create mode 100644 Wox/ProgramSourceSetting.xaml.cs create mode 100644 Wox/StringEmptyConverter.cs diff --git a/Wox.Infrastructure/UserSettings/ProgramSource.cs b/Wox.Infrastructure/UserSettings/ProgramSource.cs index 97b1933e0c..eb8223cba5 100644 --- a/Wox.Infrastructure/UserSettings/ProgramSource.cs +++ b/Wox.Infrastructure/UserSettings/ProgramSource.cs @@ -10,8 +10,13 @@ namespace Wox.Infrastructure.UserSettings { public string Location { get; set; } public string Type { get; set; } - public int BounsPoints { get; set; } + public int BonusPoints { get; set; } public bool Enabled { get; set; } public Dictionary Meta { get; set; } + + public override string ToString() + { + return (this.Type ?? "") + ":" + this.Location ?? ""; + } } } diff --git a/Wox.Infrastructure/UserSettings/UserSetting.cs b/Wox.Infrastructure/UserSettings/UserSetting.cs index c3ac94da77..6ea989b07b 100644 --- a/Wox.Infrastructure/UserSettings/UserSetting.cs +++ b/Wox.Infrastructure/UserSettings/UserSetting.cs @@ -52,19 +52,19 @@ namespace Wox.Infrastructure.UserSettings var list = new List(); list.Add(new ProgramSource() { - BounsPoints = 0, + BonusPoints = 0, Enabled = true, Type = "CommonStartMenuProgramSource" }); list.Add(new ProgramSource() { - BounsPoints = 0, + BonusPoints = 0, Enabled = true, Type = "UserStartMenuProgramSource" }); list.Add(new ProgramSource() { - BounsPoints = -10, + BonusPoints = -10, Enabled = true, Type = "AppPathsProgramSource" }); diff --git a/Wox.Plugin.System/AppPathsProgramSource.cs b/Wox.Plugin.System/AppPathsProgramSource.cs index 35b47ed0b8..93bc6420b0 100644 --- a/Wox.Plugin.System/AppPathsProgramSource.cs +++ b/Wox.Plugin.System/AppPathsProgramSource.cs @@ -16,7 +16,7 @@ namespace Wox.Plugin.System public AppPathsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) : this() { - this.BonusPoints = source.BounsPoints; + this.BonusPoints = source.BonusPoints; } public override List LoadPrograms() @@ -46,5 +46,10 @@ namespace Wox.Plugin.System } } } + + public override string ToString() + { + return typeof(AppPathsProgramSource).Name; + } } } diff --git a/Wox.Plugin.System/CommonStartMenuProgramSource.cs b/Wox.Plugin.System/CommonStartMenuProgramSource.cs index dd973f4b31..37169e008c 100644 --- a/Wox.Plugin.System/CommonStartMenuProgramSource.cs +++ b/Wox.Plugin.System/CommonStartMenuProgramSource.cs @@ -30,8 +30,12 @@ namespace Wox.Plugin.System public CommonStartMenuProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) : this() { - this.BonusPoints = source.BounsPoints; + this.BonusPoints = source.BonusPoints; } + public override string ToString() + { + return typeof(CommonStartMenuProgramSource).Name; + } } } diff --git a/Wox.Plugin.System/FileSystemProgramSource.cs b/Wox.Plugin.System/FileSystemProgramSource.cs index 0ecd03fc15..5f3a142f4b 100644 --- a/Wox.Plugin.System/FileSystemProgramSource.cs +++ b/Wox.Plugin.System/FileSystemProgramSource.cs @@ -25,7 +25,7 @@ namespace Wox.Plugin.System public FileSystemProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) : this(source.Location) { - this.BonusPoints = source.BounsPoints; + this.BonusPoints = source.BonusPoints; } public override List LoadPrograms() @@ -52,5 +52,10 @@ namespace Wox.Plugin.System GetAppFromDirectory(subDirectory, list); } } + + public override string ToString() + { + return typeof(FileSystemProgramSource).Name + ":" + this.BaseDirectory; + } } } diff --git a/Wox.Plugin.System/PortableAppsProgramSource.cs b/Wox.Plugin.System/PortableAppsProgramSource.cs index 17be681ffe..a2600116c3 100644 --- a/Wox.Plugin.System/PortableAppsProgramSource.cs +++ b/Wox.Plugin.System/PortableAppsProgramSource.cs @@ -18,7 +18,7 @@ namespace Wox.Plugin.System public PortableAppsProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) : this(source.Location) { - this.BonusPoints = source.BounsPoints; + this.BonusPoints = source.BonusPoints; } public override List LoadPrograms() @@ -121,5 +121,11 @@ namespace Wox.Plugin.System return list; } + + + public override string ToString() + { + return typeof(PortableAppsProgramSource).Name + ":" + this.BaseDirectory; + } } } diff --git a/Wox.Plugin.System/Programs.cs b/Wox.Plugin.System/Programs.cs index 8188880671..5d05bff18e 100644 --- a/Wox.Plugin.System/Programs.cs +++ b/Wox.Plugin.System/Programs.cs @@ -44,7 +44,7 @@ namespace Wox.Plugin.System { List installedList = new List(); List sources = new List(); - Dictionary sourceTypes = new Dictionary() { + public static Dictionary SourceTypes = new Dictionary() { {"CommonStartMenuProgramSource", typeof(CommonStartMenuProgramSource)}, {"UserStartMenuProgramSource", typeof(UserStartMenuProgramSource)}, {"AppPathsProgramSource", typeof(AppPathsProgramSource)}, @@ -116,7 +116,7 @@ namespace Wox.Plugin.System if (source.Enabled) { Type sourceClass; - if (sourceTypes.TryGetValue(source.Type, out sourceClass)) + if (SourceTypes.TryGetValue(source.Type, out sourceClass)) { sources.Add(sourceClass.GetConstructor( new Type[] { typeof(Wox.Infrastructure.UserSettings.ProgramSource) } diff --git a/Wox.Plugin.System/UserStartMenuProgramSource.cs b/Wox.Plugin.System/UserStartMenuProgramSource.cs index ae828ee138..bdb902d569 100644 --- a/Wox.Plugin.System/UserStartMenuProgramSource.cs +++ b/Wox.Plugin.System/UserStartMenuProgramSource.cs @@ -16,7 +16,12 @@ namespace Wox.Plugin.System public UserStartMenuProgramSource(Wox.Infrastructure.UserSettings.ProgramSource source) : this() { - this.BonusPoints = source.BounsPoints; + this.BonusPoints = source.BonusPoints; + } + + public override string ToString() + { + return typeof(UserStartMenuProgramSource).Name; } } } diff --git a/Wox/ProgramSourceSetting.xaml b/Wox/ProgramSourceSetting.xaml new file mode 100644 index 0000000000..114e54a092 --- /dev/null +++ b/Wox/ProgramSourceSetting.xaml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + Type: + + + Location: + + + BonusPoints: + + + Enable: + + + + + + + + diff --git a/Wox/ProgramSourceSetting.xaml.cs b/Wox/ProgramSourceSetting.xaml.cs new file mode 100644 index 0000000000..e08e80d426 --- /dev/null +++ b/Wox/ProgramSourceSetting.xaml.cs @@ -0,0 +1,129 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Forms; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Shapes; +using Wox.Helper; +using Wox.Infrastructure; +using Wox.Infrastructure.UserSettings; +using MessageBox = System.Windows.MessageBox; + +namespace Wox +{ + public partial class ProgramSourceSetting : Window + { + private SettingWidow settingWidow; + private bool update; + private ProgramSource updateProgramSource; + + public ProgramSourceSetting(SettingWidow settingWidow) + { + this.settingWidow = settingWidow; + InitializeComponent(); + + this.cbType.ItemsSource = Wox.Plugin.System.Programs.SourceTypes.Select(o => o.Key).ToList(); + } + + public void UpdateItem(ProgramSource programSource) + { + updateProgramSource = CommonStorage.Instance.UserSetting.ProgramSources.FirstOrDefault(o => o == programSource); + if (updateProgramSource == null) + { + MessageBox.Show("Invalid program source"); + Close(); + return; + } + + update = true; + lblAdd.Text = "Update"; + cbEnable.IsChecked = programSource.Enabled; + cbType.SelectedItem = programSource.Type; + cbType.IsEnabled = false; + tbLocation.Text = programSource.Location; + tbBonusPoints.Text = programSource.BonusPoints.ToString(); + } + + private void BtnCancel_OnClick(object sender, RoutedEventArgs e) + { + Close(); + } + + private void btnAdd_OnClick(object sender, RoutedEventArgs e) + { + string location = tbLocation.Text; + if (this.tbLocation.IsEnabled == true && string.IsNullOrEmpty(location)) + { + MessageBox.Show("Please input Type field"); + return; + } + + string type = cbType.SelectedItem as string; + if (string.IsNullOrEmpty(type)) + { + MessageBox.Show("Please input Type field"); + return; + } + + int bonusPoint = 0; + int.TryParse(this.tbBonusPoints.Text, out bonusPoint); + + if (!update) + { + ProgramSource p = new ProgramSource() + { + Location = this.tbLocation.IsEnabled ? location : null, + Enabled = cbEnable.IsChecked ?? false, + Type = type, + BonusPoints = bonusPoint + }; + if (CommonStorage.Instance.UserSetting.ProgramSources.Exists(o => o.ToString() == p.ToString() && o != p)) + { + MessageBox.Show("Program source already exists!"); + return; + } + CommonStorage.Instance.UserSetting.ProgramSources.Add(p); + MessageBox.Show(string.Format("Add {0} program source successfully!", p.ToString())); + } + else + { + if (CommonStorage.Instance.UserSetting.ProgramSources.Exists(o => o.ToString() == updateProgramSource.ToString() && o != updateProgramSource)) + { + MessageBox.Show("Program source already exists!"); + return; + } + updateProgramSource.Location = this.tbLocation.IsEnabled ? location : null; + updateProgramSource.Type = type; + updateProgramSource.Enabled = cbEnable.IsChecked ?? false; + updateProgramSource.BonusPoints = bonusPoint; + MessageBox.Show(string.Format("Update {0} program source successfully!", updateProgramSource.ToString())); + } + CommonStorage.Instance.Save(); + settingWidow.ReloadProgramSourceView(); + Close(); + } + + private void cbType_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + string item = cbType.SelectedItem as String; + Type type; + if (item != null && Wox.Plugin.System.Programs.SourceTypes.TryGetValue(item, out type)) + { + var attrs = type.GetCustomAttributes(typeof(System.ComponentModel.BrowsableAttribute), false); + if (attrs.Length > 0 && (attrs[0] as System.ComponentModel.BrowsableAttribute).Browsable == false) + { + this.tbLocation.IsEnabled = false; + return; + } + } + this.tbLocation.IsEnabled = true; + } + } +} diff --git a/Wox/SettingWindow.xaml b/Wox/SettingWindow.xaml index 41fdbe14a2..5295102e91 100644 --- a/Wox/SettingWindow.xaml +++ b/Wox/SettingWindow.xaml @@ -69,6 +69,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index 1bdb638bdd..06bac66a18 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -60,13 +60,14 @@ namespace Wox foreach (string theme in LoadAvailableThemes()) { - string themeName = theme.Substring(theme.LastIndexOf('\\') + 1).Replace(".xaml", ""); + string themeName = System.IO.Path.GetFileNameWithoutExtension(theme); themeComboBox.Items.Add(themeName); } themeComboBox.SelectedItem = CommonStorage.Instance.UserSetting.Theme; cbReplaceWinR.IsChecked = CommonStorage.Instance.UserSetting.ReplaceWinR; webSearchView.ItemsSource = CommonStorage.Instance.UserSetting.WebSearches; + programSourceView.ItemsSource = CommonStorage.Instance.UserSetting.ProgramSources; lvCustomHotkey.ItemsSource = CommonStorage.Instance.UserSetting.CustomPluginHotkeys; cbEnablePythonPlugins.IsChecked = CommonStorage.Instance.UserSetting.EnablePythonPlugins; cbStartWithWindows.IsChecked = File.Exists(woxLinkPath); @@ -77,6 +78,11 @@ namespace Wox webSearchView.Items.Refresh(); } + public void ReloadProgramSourceView() + { + programSourceView.Items.Refresh(); + } + private List LoadAvailableThemes() { string themePath = Directory.GetCurrentDirectory() + "\\Themes\\"; @@ -128,6 +134,43 @@ namespace Wox } } + private void btnAddProgramSource_OnClick(object sender, RoutedEventArgs e) + { + ProgramSourceSetting programSource = new ProgramSourceSetting(this); + programSource.ShowDialog(); + } + + private void btnDeleteProgramSource_OnClick(object sender, RoutedEventArgs e) + { + ProgramSource seletedProgramSource = programSourceView.SelectedItem as ProgramSource; + if (seletedProgramSource != null && + MessageBox.Show("Are your sure to delete " + seletedProgramSource.ToString(), "Delete ProgramSource", + MessageBoxButton.YesNo) == MessageBoxResult.Yes) + { + CommonStorage.Instance.UserSetting.ProgramSources.Remove(seletedProgramSource); + programSourceView.Items.Refresh(); + } + else + { + MessageBox.Show("Please select a program source"); + } + } + + private void btnEditProgramSource_OnClick(object sender, RoutedEventArgs e) + { + ProgramSource seletedProgramSource = programSourceView.SelectedItem as ProgramSource; + if (seletedProgramSource != null) + { + ProgramSourceSetting programSource = new ProgramSourceSetting(this); + programSource.UpdateItem(seletedProgramSource); + programSource.ShowDialog(); + } + else + { + MessageBox.Show("Please select a program source"); + } + } + private void CbStartWithWindows_OnChecked(object sender, RoutedEventArgs e) { CreateStartupFolderShortcut(); diff --git a/Wox/StringEmptyConverter.cs b/Wox/StringEmptyConverter.cs new file mode 100644 index 0000000000..b9b148ef3d --- /dev/null +++ b/Wox/StringEmptyConverter.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Windows.Data; +using System.Windows.Markup; + +namespace Wox +{ + public class StringEmptyConverter : MarkupExtension, IValueConverter + { + public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + { + return string.IsNullOrEmpty((string)value) ? parameter : value; + } + + public object ConvertBack( + object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + { + throw new NotSupportedException(); + } + + + public override object ProvideValue(IServiceProvider serviceProvider) + { + return this; + } + } +} diff --git a/Wox/WebSearchSetting.xaml.cs b/Wox/WebSearchSetting.xaml.cs index 23d8ceceb2..e1f07f65c1 100644 --- a/Wox/WebSearchSetting.xaml.cs +++ b/Wox/WebSearchSetting.xaml.cs @@ -109,6 +109,11 @@ namespace Wox } else { + if (CommonStorage.Instance.UserSetting.WebSearches.Exists(o => o.ActionWord == action && o != updateWebSearch)) + { + MessageBox.Show("ActionWord has existed, please input a new one."); + return; + } updateWebSearch.ActionWord = action; updateWebSearch.IconPath = tbIconPath.Text; updateWebSearch.Enabled = cbEnable.IsChecked ?? false; diff --git a/Wox/Wox.csproj b/Wox/Wox.csproj index 50a48365a6..e2c64949b2 100644 --- a/Wox/Wox.csproj +++ b/Wox/Wox.csproj @@ -107,6 +107,9 @@ + + ProgramSourceSetting.xaml + CustomPluginHotkeySetting.xaml @@ -135,10 +138,15 @@ SettingWindow.xaml + WebSearchSetting.xaml + + MSBuild:Compile + Designer + Designer MSBuild:Compile