2021-07-12 12:04:08 +00:00
|
|
|
using Flow.Launcher.Infrastructure.Hotkey;
|
2021-07-09 12:05:43 +00:00
|
|
|
using Flow.Launcher.Infrastructure.UserSettings;
|
|
|
|
|
using System;
|
|
|
|
|
using NHotkey;
|
|
|
|
|
using NHotkey.Wpf;
|
|
|
|
|
using Flow.Launcher.Core.Resource;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using Flow.Launcher.ViewModel;
|
|
|
|
|
|
|
|
|
|
namespace Flow.Launcher.Helper
|
|
|
|
|
{
|
|
|
|
|
internal static class HotKeyMapper
|
|
|
|
|
{
|
|
|
|
|
private static Settings settings;
|
|
|
|
|
private static MainViewModel mainViewModel;
|
|
|
|
|
|
|
|
|
|
internal static void Initialize(MainViewModel mainVM)
|
|
|
|
|
{
|
|
|
|
|
mainViewModel = mainVM;
|
|
|
|
|
settings = mainViewModel._settings;
|
|
|
|
|
|
2021-10-20 03:10:37 +00:00
|
|
|
SetHotkey(settings.Hotkey, OnToggleHotkey);
|
2021-07-13 11:53:02 +00:00
|
|
|
LoadCustomPluginHotkey();
|
2021-07-09 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 03:10:37 +00:00
|
|
|
internal static void OnToggleHotkey(object sender, HotkeyEventArgs args)
|
|
|
|
|
{
|
2022-02-25 22:43:27 +00:00
|
|
|
if (!mainViewModel.ShouldIgnoreHotkeys() && !mainViewModel.GameModeStatus)
|
2021-11-19 06:43:47 +00:00
|
|
|
mainViewModel.ToggleFlowLauncher();
|
2021-10-20 03:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-09 12:05:43 +00:00
|
|
|
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
|
|
|
|
|
{
|
|
|
|
|
var hotkey = new HotkeyModel(hotkeyStr);
|
|
|
|
|
SetHotkey(hotkey, action);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static 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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void RemoveHotkey(string hotkeyStr)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(hotkeyStr))
|
|
|
|
|
{
|
|
|
|
|
HotkeyManager.Current.Remove(hotkeyStr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-13 11:53:02 +00:00
|
|
|
internal static void LoadCustomPluginHotkey()
|
2021-07-09 12:05:43 +00:00
|
|
|
{
|
2021-07-13 11:53:02 +00:00
|
|
|
if (settings.CustomPluginHotkeys == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-07-09 12:05:43 +00:00
|
|
|
foreach (CustomPluginHotkey hotkey in settings.CustomPluginHotkeys)
|
|
|
|
|
{
|
2021-07-13 11:53:02 +00:00
|
|
|
SetCustomQueryHotkey(hotkey);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void SetCustomQueryHotkey(CustomPluginHotkey hotkey)
|
|
|
|
|
{
|
|
|
|
|
SetHotkey(hotkey.Hotkey, (s, e) =>
|
|
|
|
|
{
|
2021-11-19 12:06:02 +00:00
|
|
|
if (mainViewModel.ShouldIgnoreHotkeys() || mainViewModel.GameModeStatus)
|
2021-07-13 11:53:02 +00:00
|
|
|
return;
|
2021-07-12 21:46:39 +00:00
|
|
|
|
2021-11-14 09:13:03 +00:00
|
|
|
mainViewModel.Show();
|
2021-09-25 01:05:55 +00:00
|
|
|
mainViewModel.ChangeQueryText(hotkey.ActionKeyword, true);
|
2021-07-13 11:53:02 +00:00
|
|
|
});
|
2021-07-09 12:05:43 +00:00
|
|
|
}
|
2021-07-13 21:43:09 +00:00
|
|
|
|
|
|
|
|
internal static bool CheckAvailability(HotkeyModel currentHotkey)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
HotkeyManager.Current.AddOrReplace("HotkeyAvailabilityTest", currentHotkey.CharKey, currentHotkey.ModifierKeys, (sender, e) => { });
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
HotkeyManager.Current.Remove("HotkeyAvailabilityTest");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-07-09 12:05:43 +00:00
|
|
|
}
|
|
|
|
|
}
|