Flow.Launcher/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs

113 lines
4 KiB
C#
Raw Normal View History

using Flow.Launcher.Core.Resource;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.UserSettings;
using System.Collections.ObjectModel;
2014-02-22 07:52:20 +00:00
using System.Linq;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher
2014-02-22 07:52:20 +00:00
{
2015-01-02 15:07:49 +00:00
public partial class CustomQueryHotkeySetting : Window
2014-02-22 07:52:20 +00:00
{
private SettingWindow _settingWidow;
2014-02-22 07:52:20 +00:00
private bool update;
private CustomPluginHotkey updateCustomHotkey;
private Settings _settings;
2014-02-22 07:52:20 +00:00
public CustomQueryHotkeySetting(SettingWindow settingWidow, Settings settings)
2014-02-22 07:52:20 +00:00
{
_settingWidow = settingWidow;
2014-02-22 07:52:20 +00:00
InitializeComponent();
_settings = settings;
2014-02-22 07:52:20 +00: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 15:00:56 +00:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("hotkeyIsNotUnavailable"));
2014-02-22 07:52:20 +00:00
return;
}
if (_settings.CustomPluginHotkeys == null)
2014-02-22 07:52:20 +00:00
{
_settings.CustomPluginHotkeys = new ObservableCollection<CustomPluginHotkey>();
2014-02-22 07:52:20 +00:00
}
2016-01-06 21:34:42 +00:00
var pluginHotkey = new CustomPluginHotkey
2014-02-22 07:52:20 +00:00
{
Hotkey = ctlHotkey.CurrentHotkey.ToString(),
ActionKeyword = tbAction.Text
};
_settings.CustomPluginHotkeys.Add(pluginHotkey);
HotKeyMapper.SetCustomQueryHotkey(pluginHotkey);
2014-02-22 07:52:20 +00:00
}
else
{
if (updateCustomHotkey.Hotkey != ctlHotkey.CurrentHotkey.ToString() && !ctlHotkey.CurrentHotkeyAvailable)
{
2015-01-21 15:00:56 +00:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("hotkeyIsNotUnavailable"));
2014-02-22 07:52:20 +00:00
return;
}
var oldHotkey = updateCustomHotkey.Hotkey;
updateCustomHotkey.ActionKeyword = tbAction.Text;
updateCustomHotkey.Hotkey = ctlHotkey.CurrentHotkey.ToString();
//remove origin hotkey
HotKeyMapper.RemoveHotkey(oldHotkey);
HotKeyMapper.SetCustomQueryHotkey(updateCustomHotkey);
2014-02-22 07:52:20 +00:00
}
Close();
}
public void UpdateItem(CustomPluginHotkey item)
{
updateCustomHotkey = _settings.CustomPluginHotkeys.FirstOrDefault(o => o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey);
2014-02-22 07:52:20 +00:00
if (updateCustomHotkey == null)
{
2015-01-21 15:00:56 +00:00
MessageBox.Show(InternationalizationManager.Instance.GetTranslation("invalidPluginHotkey"));
2014-02-22 07:52:20 +00:00
Close();
return;
}
tbAction.Text = updateCustomHotkey.ActionKeyword;
2022-08-10 03:18:37 +00:00
_ = ctlHotkey.SetHotkeyAsync(updateCustomHotkey.Hotkey, false);
2014-02-22 07:52:20 +00:00
update = true;
2015-01-21 15:00:56 +00:00
lblAdd.Text = InternationalizationManager.Instance.GetTranslation("update");
2014-02-22 07:52:20 +00:00
}
private void BtnTestActionKeyword_OnClick(object sender, RoutedEventArgs e)
{
App.API.ChangeQuery(tbAction.Text);
Application.Current.MainWindow.Show();
Application.Current.MainWindow.Opacity = 1;
Application.Current.MainWindow.Focus();
}
private void cmdEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
{
Close();
}
private void window_MouseDown(object sender, MouseButtonEventArgs e) /* for close hotkey popup */
{
TextBox textBox = Keyboard.FocusedElement as TextBox;
if (textBox != null)
{
TraversalRequest tRequest = new TraversalRequest(FocusNavigationDirection.Next);
textBox.MoveFocus(tRequest);
}
}
2014-02-22 07:52:20 +00:00
}
}