PowerToys/Wox/SettingWindow.xaml.cs

356 lines
13 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2014-03-11 22:17:10 +08:00
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
2014-03-19 22:17:01 +08:00
using System.Windows.Media;
using IWshRuntimeLibrary;
2014-03-23 16:17:41 +08:00
using Microsoft.VisualBasic.ApplicationServices;
using Wox.Infrastructure;
2014-03-23 16:17:41 +08:00
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.Storage.UserSettings;
2014-03-19 22:17:01 +08:00
using Wox.Plugin;
using Application = System.Windows.Forms.Application;
using File = System.IO.File;
using MessageBox = System.Windows.MessageBox;
2014-01-29 18:33:24 +08:00
namespace Wox
{
2014-03-20 02:26:00 +08:00
public partial class SettingWindow : Window
{
string woxLinkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "wox.lnk");
2014-02-22 15:52:20 +08:00
public MainWindow MainWindow;
2014-03-20 02:26:00 +08:00
public SettingWindow()
{
InitializeComponent();
}
2014-03-20 02:26:00 +08:00
public SettingWindow(MainWindow mainWindow)
{
2014-02-22 15:52:20 +08:00
this.MainWindow = mainWindow;
InitializeComponent();
Loaded += Setting_Loaded;
2014-02-22 15:52:20 +08:00
}
private void Setting_Loaded(object sender, RoutedEventArgs ev)
{
2014-02-22 11:55:48 +08:00
ctlHotkey.OnHotkeyChanged += ctlHotkey_OnHotkeyChanged;
2014-03-23 16:17:41 +08:00
ctlHotkey.SetHotkey(UserSettingStorage.Instance.Hotkey, false);
2014-01-26 00:37:15 +08:00
cbReplaceWinR.Checked += (o, e) =>
{
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.ReplaceWinR = true;
UserSettingStorage.Instance.Save();
2014-01-26 00:37:15 +08:00
};
cbReplaceWinR.Unchecked += (o, e) =>
{
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.ReplaceWinR = false;
UserSettingStorage.Instance.Save();
2014-01-26 00:37:15 +08:00
};
2014-02-22 11:55:48 +08:00
2014-03-19 01:27:59 +08:00
cbEnablePythonPlugins.Checked += (o, e) =>
{
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.EnablePythonPlugins = true;
UserSettingStorage.Instance.Save();
2014-03-19 01:27:59 +08:00
};
cbEnablePythonPlugins.Unchecked += (o, e) =>
{
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.EnablePythonPlugins = false;
UserSettingStorage.Instance.Save();
2014-03-19 01:27:59 +08:00
};
2014-03-19 22:17:01 +08:00
#region Load Theme Data
2014-03-23 16:17:41 +08:00
if (!string.IsNullOrEmpty(UserSettingStorage.Instance.QueryBoxFont) &&
Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.QueryBoxFont)) > 0)
2014-03-19 22:17:01 +08:00
{
2014-03-23 16:17:41 +08:00
cbQueryBoxFont.Text = UserSettingStorage.Instance.QueryBoxFont;
2014-03-19 22:17:01 +08:00
}
2014-03-23 16:17:41 +08:00
if (!string.IsNullOrEmpty(UserSettingStorage.Instance.ResultItemFont) &&
Fonts.SystemFontFamilies.Count(o => o.FamilyNames.Values.Contains(UserSettingStorage.Instance.ResultItemFont)) > 0)
2014-03-19 22:17:01 +08:00
{
2014-03-23 16:17:41 +08:00
cbResultItemFont.Text = UserSettingStorage.Instance.ResultItemFont;
2014-03-19 22:17:01 +08:00
}
resultPanelPreview.AddResults(new List<Result>()
{
new Result()
{
Title = "Wox is an effective launcher for windows",
SubTitle = "Wox provide bundles of features let you access infomations quickly.",
IcoPath = "Images/work.png",
PluginDirectory = AppDomain.CurrentDomain.BaseDirectory
},
new Result()
{
Title = "Search applications",
SubTitle = "Search applications, files (via everything plugin) and chrome bookmarks",
IcoPath = "Images/work.png",
PluginDirectory = AppDomain.CurrentDomain.BaseDirectory
},
new Result()
{
Title = "Search web contents with shortcuts",
SubTitle = "e.g. search google with g keyword or youtube keyword)",
IcoPath = "Images/work.png",
PluginDirectory = AppDomain.CurrentDomain.BaseDirectory
},
new Result()
{
Title = "clipboard history ",
IcoPath = "Images/work.png",
PluginDirectory = AppDomain.CurrentDomain.BaseDirectory
},
new Result()
{
Title = "Themes support",
SubTitle = "get more themes from http://www.getwox.com/theme",
IcoPath = "Images/work.png",
PluginDirectory = AppDomain.CurrentDomain.BaseDirectory
}
});
#endregion
2014-03-19 01:20:51 +08:00
foreach (string theme in LoadAvailableThemes())
{
2014-03-19 20:16:20 +08:00
string themeName = System.IO.Path.GetFileNameWithoutExtension(theme);
themeComboBox.Items.Add(themeName);
}
2014-03-23 16:17:41 +08:00
themeComboBox.SelectedItem = UserSettingStorage.Instance.Theme;
cbReplaceWinR.IsChecked = UserSettingStorage.Instance.ReplaceWinR;
webSearchView.ItemsSource = UserSettingStorage.Instance.WebSearches;
programSourceView.ItemsSource = UserSettingStorage.Instance.ProgramSources;
lvCustomHotkey.ItemsSource = UserSettingStorage.Instance.CustomPluginHotkeys;
cbEnablePythonPlugins.IsChecked = UserSettingStorage.Instance.EnablePythonPlugins;
cbStartWithWindows.IsChecked = File.Exists(woxLinkPath);
}
2014-02-02 16:15:34 +08:00
public void ReloadWebSearchView()
{
webSearchView.Items.Refresh();
}
2014-03-19 20:16:20 +08:00
public void ReloadProgramSourceView()
{
programSourceView.Items.Refresh();
}
private List<string> LoadAvailableThemes()
{
string themePath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Themes");
2014-03-15 00:17:37 +08:00
return Directory.GetFiles(themePath).Where(filePath => filePath.EndsWith(".xaml") && !filePath.EndsWith("Default.xaml")).ToList();
}
2014-03-19 22:17:01 +08:00
2014-02-02 16:15:34 +08:00
private void btnAddWebSearch_OnClick(object sender, RoutedEventArgs e)
{
2014-02-02 16:15:34 +08:00
WebSearchSetting webSearch = new WebSearchSetting(this);
2014-02-22 15:52:20 +08:00
webSearch.ShowDialog();
}
2014-02-02 16:15:34 +08:00
private void btnDeleteWebSearch_OnClick(object sender, RoutedEventArgs e)
{
WebSearch seletedWebSearch = webSearchView.SelectedItem as WebSearch;
if (seletedWebSearch != null &&
MessageBox.Show("Are your sure to delete " + seletedWebSearch.Title, "Delete WebSearch",
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.WebSearches.Remove(seletedWebSearch);
2014-02-02 16:15:34 +08:00
webSearchView.Items.Refresh();
}
else
{
MessageBox.Show("Please select a web search");
}
}
private void btnEditWebSearch_OnClick(object sender, RoutedEventArgs e)
{
WebSearch seletedWebSearch = webSearchView.SelectedItem as WebSearch;
if (seletedWebSearch != null)
{
WebSearchSetting webSearch = new WebSearchSetting(this);
webSearch.UpdateItem(seletedWebSearch);
2014-02-22 15:52:20 +08:00
webSearch.ShowDialog();
2014-02-02 16:15:34 +08:00
}
else
{
MessageBox.Show("Please select a web search");
}
}
2014-03-19 20:16:20 +08:00
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)
{
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.ProgramSources.Remove(seletedProgramSource);
2014-03-19 20:16:20 +08:00
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();
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.StartWoxOnSystemStartup = true;
UserSettingStorage.Instance.Save();
}
private void CbStartWithWindows_OnUnchecked(object sender, RoutedEventArgs e)
{
if (File.Exists(woxLinkPath))
{
File.Delete(woxLinkPath);
}
2014-03-19 01:20:51 +08:00
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.StartWoxOnSystemStartup = false;
UserSettingStorage.Instance.Save();
}
private void CreateStartupFolderShortcut()
{
WshShellClass wshShell = new WshShellClass();
IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(woxLinkPath);
shortcut.TargetPath = Application.ExecutablePath;
shortcut.Arguments = "hideStart";
shortcut.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
shortcut.Description = "Launch Wox";
shortcut.IconLocation = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "App.ico");
shortcut.Save();
}
2014-02-22 15:52:20 +08:00
void ctlHotkey_OnHotkeyChanged(object sender, System.EventArgs e)
{
if (ctlHotkey.CurrentHotkeyAvailable)
{
MainWindow.SetHotkey(ctlHotkey.CurrentHotkey.ToString(), delegate
{
if (!MainWindow.IsVisible)
{
MainWindow.ShowApp();
}
else
{
MainWindow.HideApp();
}
});
2014-03-23 16:17:41 +08:00
MainWindow.RemoveHotkey(UserSettingStorage.Instance.Hotkey);
UserSettingStorage.Instance.Hotkey = ctlHotkey.CurrentHotkey.ToString();
UserSettingStorage.Instance.Save();
2014-02-22 15:52:20 +08:00
}
}
#region Custom Plugin Hotkey
private void BtnDeleteCustomHotkey_OnClick(object sender, RoutedEventArgs e)
{
CustomPluginHotkey item = lvCustomHotkey.SelectedItem as CustomPluginHotkey;
if (item != null &&
MessageBox.Show("Are your sure to delete " + item.Hotkey + " plugin hotkey?", "Delete Custom Plugin Hotkey",
2014-02-22 15:52:20 +08:00
MessageBoxButton.YesNo) == MessageBoxResult.Yes)
{
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.CustomPluginHotkeys.Remove(item);
2014-02-22 15:52:20 +08:00
lvCustomHotkey.Items.Refresh();
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.Save();
2014-02-22 15:52:20 +08:00
MainWindow.RemoveHotkey(item.Hotkey);
}
else
{
MessageBox.Show("Please select an item");
}
}
private void BtnEditCustomHotkey_OnClick(object sender, RoutedEventArgs e)
{
CustomPluginHotkey item = lvCustomHotkey.SelectedItem as CustomPluginHotkey;
if (item != null)
{
CustomPluginHotkeySetting window = new CustomPluginHotkeySetting(this);
window.UpdateItem(item);
window.ShowDialog();
}
else
{
MessageBox.Show("Please select an item");
}
}
private void BtnAddCustomeHotkey_OnClick(object sender, RoutedEventArgs e)
{
new CustomPluginHotkeySetting(this).ShowDialog();
}
public void ReloadCustomPluginHotkeyView()
{
lvCustomHotkey.Items.Refresh();
}
#endregion
2014-03-11 22:17:10 +08:00
private void BtnEnableInstaller_OnClick(object sender, RoutedEventArgs e)
{
Process.Start("Wox.UAC.exe", "AssociatePluginInstaller");
}
2014-03-19 22:17:01 +08:00
#region Theme
private void ThemeComboBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
string themeName = themeComboBox.SelectedItem.ToString();
MainWindow.SetTheme(themeName);
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.Theme = themeName;
UserSettingStorage.Instance.Save();
2014-03-19 22:17:01 +08:00
}
private void CbQueryBoxFont_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
string queryBoxFontName = cbQueryBoxFont.SelectedItem.ToString();
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.QueryBoxFont = queryBoxFontName;
UserSettingStorage.Instance.Save();
App.Window.SetTheme(UserSettingStorage.Instance.Theme);
2014-03-19 22:17:01 +08:00
}
private void CbResultItemFont_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
string resultItemFont = cbResultItemFont.SelectedItem.ToString();
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.ResultItemFont = resultItemFont;
UserSettingStorage.Instance.Save();
App.Window.SetTheme(UserSettingStorage.Instance.Theme);
2014-03-19 22:17:01 +08:00
}
#endregion
}
}