mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
add exception handling for register & unregister hotkey (#3263)
This commit is contained in:
parent
54a49d68f3
commit
70396bccd9
2 changed files with 46 additions and 22 deletions
|
|
@ -8,6 +8,7 @@ using Flow.Launcher.ViewModel;
|
||||||
using Flow.Launcher.Core;
|
using Flow.Launcher.Core;
|
||||||
using ChefKeys;
|
using ChefKeys;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using Flow.Launcher.Infrastructure.Logger;
|
||||||
|
|
||||||
namespace Flow.Launcher.Helper;
|
namespace Flow.Launcher.Helper;
|
||||||
|
|
||||||
|
|
@ -39,38 +40,49 @@ internal static class HotKeyMapper
|
||||||
|
|
||||||
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
|
private static void SetHotkey(string hotkeyStr, EventHandler<HotkeyEventArgs> action)
|
||||||
{
|
{
|
||||||
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
|
|
||||||
{
|
|
||||||
SetWithChefKeys(hotkeyStr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var hotkey = new HotkeyModel(hotkeyStr);
|
var hotkey = new HotkeyModel(hotkeyStr);
|
||||||
SetHotkey(hotkey, action);
|
SetHotkey(hotkey, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void SetWithChefKeys(string hotkeyStr)
|
private static void SetWithChefKeys(string hotkeyStr)
|
||||||
{
|
{
|
||||||
ChefKeysManager.RegisterHotkey(hotkeyStr, hotkeyStr, OnToggleHotkeyWithChefKeys);
|
try
|
||||||
ChefKeysManager.Start();
|
{
|
||||||
|
ChefKeysManager.RegisterHotkey(hotkeyStr, hotkeyStr, OnToggleHotkeyWithChefKeys);
|
||||||
|
ChefKeysManager.Start();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Error(
|
||||||
|
string.Format("|HotkeyMapper.SetWithChefKeys|Error registering hotkey: {0} \nStackTrace:{1}",
|
||||||
|
e.Message,
|
||||||
|
e.StackTrace));
|
||||||
|
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
|
||||||
|
string errorMsgTitle = InternationalizationManager.Instance.GetTranslation("MessageBoxTitle");
|
||||||
|
MessageBoxEx.Show(errorMsg, errorMsgTitle);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
internal static void SetHotkey(HotkeyModel hotkey, EventHandler<HotkeyEventArgs> action)
|
||||||
{
|
{
|
||||||
string hotkeyStr = hotkey.ToString();
|
string hotkeyStr = hotkey.ToString();
|
||||||
|
|
||||||
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
|
|
||||||
{
|
|
||||||
SetWithChefKeys(hotkeyStr);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
|
||||||
|
{
|
||||||
|
SetWithChefKeys(hotkeyStr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
HotkeyManager.Current.AddOrReplace(hotkeyStr, hotkey.CharKey, hotkey.ModifierKeys, action);
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
Log.Error(
|
||||||
|
string.Format("|HotkeyMapper.SetHotkey|Error registering hotkey {2}: {0} \nStackTrace:{1}",
|
||||||
|
e.Message,
|
||||||
|
e.StackTrace,
|
||||||
|
hotkeyStr));
|
||||||
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
|
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("registerHotkeyFailed"), hotkeyStr);
|
||||||
string errorMsgTitle = InternationalizationManager.Instance.GetTranslation("MessageBoxTitle");
|
string errorMsgTitle = InternationalizationManager.Instance.GetTranslation("MessageBoxTitle");
|
||||||
MessageBoxEx.Show(errorMsg, errorMsgTitle);
|
MessageBoxEx.Show(errorMsg, errorMsgTitle);
|
||||||
|
|
@ -79,15 +91,26 @@ internal static class HotKeyMapper
|
||||||
|
|
||||||
internal static void RemoveHotkey(string hotkeyStr)
|
internal static void RemoveHotkey(string hotkeyStr)
|
||||||
{
|
{
|
||||||
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
|
try
|
||||||
{
|
{
|
||||||
RemoveWithChefKeys(hotkeyStr);
|
if (hotkeyStr == "LWin" || hotkeyStr == "RWin")
|
||||||
return;
|
{
|
||||||
}
|
RemoveWithChefKeys(hotkeyStr);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(hotkeyStr))
|
if (!string.IsNullOrEmpty(hotkeyStr))
|
||||||
|
HotkeyManager.Current.Remove(hotkeyStr);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
HotkeyManager.Current.Remove(hotkeyStr);
|
Log.Error(
|
||||||
|
string.Format("|HotkeyMapper.RemoveHotkey|Error removing hotkey: {0} \nStackTrace:{1}",
|
||||||
|
e.Message,
|
||||||
|
e.StackTrace));
|
||||||
|
string errorMsg = string.Format(InternationalizationManager.Instance.GetTranslation("unregisterHotkeyFailed"), hotkeyStr);
|
||||||
|
string errorMsgTitle = InternationalizationManager.Instance.GetTranslation("MessageBoxTitle");
|
||||||
|
MessageBoxEx.Show(errorMsg, errorMsgTitle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
|
|
||||||
<!-- MainWindow -->
|
<!-- MainWindow -->
|
||||||
<system:String x:Key="registerHotkeyFailed">Failed to register hotkey "{0}". The hotkey may be in use by another program. Change to a different hotkey, or exit another program.</system:String>
|
<system:String x:Key="registerHotkeyFailed">Failed to register hotkey "{0}". The hotkey may be in use by another program. Change to a different hotkey, or exit another program.</system:String>
|
||||||
|
<system:String x:Key="unregisterHotkeyFailed">Failed to unregister hotkey "{0}". Please try again or see log for details</system:String>
|
||||||
<system:String x:Key="MessageBoxTitle">Flow Launcher</system:String>
|
<system:String x:Key="MessageBoxTitle">Flow Launcher</system:String>
|
||||||
<system:String x:Key="couldnotStartCmd">Could not start {0}</system:String>
|
<system:String x:Key="couldnotStartCmd">Could not start {0}</system:String>
|
||||||
<system:String x:Key="invalidFlowLauncherPluginFileFormat">Invalid Flow Launcher plugin file format</system:String>
|
<system:String x:Key="invalidFlowLauncherPluginFileFormat">Invalid Flow Launcher plugin file format</system:String>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue