PowerToys/Wox/CustomQueryHotkeySetting.xaml.cs

102 lines
3.9 KiB
C#
Raw Normal View History

2014-06-30 22:27:56 +08:00
using System.Collections.Generic;
2014-02-22 15:52:20 +08:00
using System.Linq;
using System.Windows;
2015-01-02 23:07:49 +08:00
using Wox.Core.i18n;
using Wox.Core.UserSettings;
2014-02-22 15:52:20 +08:00
namespace Wox
{
2015-01-02 23:07:49 +08:00
public partial class CustomQueryHotkeySetting : Window
2014-02-22 15:52:20 +08:00
{
2014-03-20 02:26:00 +08:00
private SettingWindow settingWidow;
2014-02-22 15:52:20 +08:00
private bool update;
private CustomPluginHotkey updateCustomHotkey;
2015-01-02 23:07:49 +08:00
public CustomQueryHotkeySetting(SettingWindow settingWidow)
2014-02-22 15:52:20 +08:00
{
this.settingWidow = settingWidow;
InitializeComponent();
}
private void BtnCancel_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private void btnAdd_OnClick(object sender, RoutedEventArgs e)
{
if (!update)
{
if (!ctlHotkey.CurrentHotkeyAvailable)
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("hotkeyIsNotUnavailable"));
2014-02-22 15:52:20 +08:00
return;
}
2014-03-23 16:17:41 +08:00
if (UserSettingStorage.Instance.CustomPluginHotkeys == null)
2014-02-22 15:52:20 +08:00
{
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.CustomPluginHotkeys = new List<CustomPluginHotkey>();
2014-02-22 15:52:20 +08:00
}
var pluginHotkey = new CustomPluginHotkey()
{
Hotkey = ctlHotkey.CurrentHotkey.ToString(),
ActionKeyword = tbAction.Text
};
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.CustomPluginHotkeys.Add(pluginHotkey);
2014-02-22 15:52:20 +08:00
settingWidow.MainWindow.SetHotkey(ctlHotkey.CurrentHotkey.ToString(), delegate
{
settingWidow.MainWindow.ChangeQuery(pluginHotkey.ActionKeyword);
2014-02-28 23:21:01 +08:00
settingWidow.MainWindow.ShowApp();
2014-02-22 15:52:20 +08:00
});
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed"));
2014-02-22 15:52:20 +08:00
}
else
{
if (updateCustomHotkey.Hotkey != ctlHotkey.CurrentHotkey.ToString() && !ctlHotkey.CurrentHotkeyAvailable)
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("hotkeyIsNotUnavailable"));
2014-02-22 15:52:20 +08:00
return;
}
var oldHotkey = updateCustomHotkey.Hotkey;
updateCustomHotkey.ActionKeyword = tbAction.Text;
updateCustomHotkey.Hotkey = ctlHotkey.CurrentHotkey.ToString();
//remove origin hotkey
settingWidow.MainWindow.RemoveHotkey(oldHotkey);
settingWidow.MainWindow.SetHotkey(updateCustomHotkey.Hotkey, delegate
{
settingWidow.MainWindow.ShowApp();
settingWidow.MainWindow.ChangeQuery(updateCustomHotkey.ActionKeyword);
});
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("succeed"));
2014-02-22 15:52:20 +08:00
}
2014-03-23 16:17:41 +08:00
UserSettingStorage.Instance.Save();
2014-02-22 15:52:20 +08:00
settingWidow.ReloadCustomPluginHotkeyView();
Close();
}
public void UpdateItem(CustomPluginHotkey item)
{
2014-03-23 16:17:41 +08:00
updateCustomHotkey = UserSettingStorage.Instance.CustomPluginHotkeys.FirstOrDefault(o => o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey);
2014-02-22 15:52:20 +08:00
if (updateCustomHotkey == null)
{
2015-01-21 23:00:56 +08:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("invalidPluginHotkey"));
2014-02-22 15:52:20 +08:00
Close();
return;
}
tbAction.Text = updateCustomHotkey.ActionKeyword;
ctlHotkey.SetHotkey(updateCustomHotkey.Hotkey, false);
update = true;
2015-01-21 23:00:56 +08:00
lblAdd.Text = InternationalizationManager.Instance.GetTranslation("update");
2014-02-22 15:52:20 +08:00
}
private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
{
settingWidow.MainWindow.ShowApp();
settingWidow.MainWindow.ChangeQuery(tbAction.Text);
}
}
}