2016-02-23 05:43:37 +08:00
|
|
|
|
using System;
|
2016-02-18 20:56:53 +08:00
|
|
|
|
using System.Collections.Generic;
|
2014-02-22 15:52:20 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows;
|
2016-02-23 05:43:37 +08:00
|
|
|
|
using NHotkey;
|
|
|
|
|
using NHotkey.Wpf;
|
2016-01-08 04:04:37 +08:00
|
|
|
|
using Wox.Core.Resource;
|
2015-01-05 22:41:17 +08:00
|
|
|
|
using Wox.Core.UserSettings;
|
2016-02-18 20:56:53 +08:00
|
|
|
|
using Wox.Infrastructure.Hotkey;
|
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
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
private SettingWindow _settingWidow;
|
2014-02-22 15:52:20 +08:00
|
|
|
|
private bool update;
|
|
|
|
|
private CustomPluginHotkey updateCustomHotkey;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
private Settings _settings;
|
2014-02-22 15:52:20 +08:00
|
|
|
|
|
2016-04-21 08:53:21 +08:00
|
|
|
|
public CustomQueryHotkeySetting(SettingWindow settingWidow, Settings settings)
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settingWidow = settingWidow;
|
2014-02-22 15:52:20 +08:00
|
|
|
|
InitializeComponent();
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settings = settings;
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 10:09:57 +08:00
|
|
|
|
if (_settings.CustomPluginHotkeys == null)
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settings.CustomPluginHotkeys = new List<CustomPluginHotkey>();
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-07 05:34:42 +08:00
|
|
|
|
var pluginHotkey = new CustomPluginHotkey
|
2014-02-22 15:52:20 +08:00
|
|
|
|
{
|
|
|
|
|
Hotkey = ctlHotkey.CurrentHotkey.ToString(),
|
|
|
|
|
ActionKeyword = tbAction.Text
|
|
|
|
|
};
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settings.CustomPluginHotkeys.Add(pluginHotkey);
|
2016-02-18 20:56:53 +08:00
|
|
|
|
|
|
|
|
|
SetHotkey(ctlHotkey.CurrentHotkey, delegate
|
|
|
|
|
{
|
|
|
|
|
App.API.ChangeQuery(pluginHotkey.ActionKeyword);
|
|
|
|
|
App.API.ShowApp();
|
|
|
|
|
});
|
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
|
2016-02-18 20:56:53 +08:00
|
|
|
|
RemoveHotkey(oldHotkey);
|
|
|
|
|
SetHotkey(new HotkeyModel(updateCustomHotkey.Hotkey), delegate
|
|
|
|
|
{
|
|
|
|
|
App.API.ShowApp();
|
|
|
|
|
App.API.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
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 10:09:57 +08:00
|
|
|
|
_settingWidow.ReloadCustomPluginHotkeyView();
|
2014-02-22 15:52:20 +08:00
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateItem(CustomPluginHotkey item)
|
|
|
|
|
{
|
2016-03-28 10:09:57 +08:00
|
|
|
|
updateCustomHotkey = _settings.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)
|
|
|
|
|
{
|
2016-02-18 20:56:53 +08:00
|
|
|
|
App.API.ShowApp();
|
|
|
|
|
App.API.ChangeQuery(tbAction.Text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RemoveHotkey(string hotkeyStr)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(hotkeyStr))
|
|
|
|
|
{
|
|
|
|
|
HotkeyManager.Current.Remove(hotkeyStr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
|
|
|
|
{
|
|
|
|
|
string hotkeyStr = hotkey.ToString();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
|
|
|
|
|
MessageBox.Show(errorMsg);
|
|
|
|
|
}
|
2014-02-22 15:52:20 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|