Only store hotkeys when successfully set using Settings window.

This commit is contained in:
Peter Kirmeier 2023-06-03 16:56:17 +02:00
parent 33e1d9decf
commit dda3d94e7b
2 changed files with 26 additions and 13 deletions

View file

@ -36,7 +36,7 @@ namespace SystemTrayMenu.UserInterface
// Disable right-clicking
ContextMenu = new()
{
Visibility = System.Windows.Visibility.Collapsed,
Visibility = Visibility.Collapsed,
IsEnabled = false,
};
@ -50,9 +50,11 @@ namespace SystemTrayMenu.UserInterface
LostFocus += (_, _) => GlobalHotkeys.IsEnabled = true;
PopulateModifierLists();
SetHotkeyRegistration((IHotkeyFunction?)null);
SetHotkeyRegistration(null);
}
internal bool WasHotkeyChanged { get; private set; }
internal IHotkeyFunction? HotkeyFunction { get; private set; }
public static string HotkeyToString(ModifierKeys modifierKeyCode, Key key)
@ -90,6 +92,7 @@ namespace SystemTrayMenu.UserInterface
internal void SetHotkeyRegistration(IHotkeyFunction? hotkeyFunction)
{
HotkeyFunction = hotkeyFunction;
WasHotkeyChanged = false;
UpdateHotkeyRegistration();
}
@ -123,7 +126,17 @@ namespace SystemTrayMenu.UserInterface
/// <param name="hotkeyString">Hotkey combination string.</param>
internal void ChangeHotkey(string hotkeyString)
{
HotkeyFunction?.Register(hotkeyString);
try
{
HotkeyFunction?.Register(hotkeyString);
WasHotkeyChanged = true;
}
catch (InvalidOperationException ex)
{
Log.Warn($"Hotkey failed resetting to default: '{hotkeyString}'", ex);
return;
}
UpdateHotkeyRegistration();
}
@ -138,12 +151,14 @@ namespace SystemTrayMenu.UserInterface
if (key == Key.None)
{
HotkeyFunction?.Unregister();
WasHotkeyChanged = true;
}
else
{
try
{
HotkeyFunction?.Register(modifiers, key);
WasHotkeyChanged = true;
}
catch (InvalidOperationException ex)
{
@ -152,15 +167,6 @@ namespace SystemTrayMenu.UserInterface
}
}
/// <summary>
/// Clears the current hotkey and resets the TextBox.
/// </summary>
private void ResetHotkey()
{
HotkeyFunction?.Unregister();
UpdateHotkeyRegistration();
}
private void HandlePreviewKeyDown(object sender, KeyEventArgs e)
{
ModifierKeys modifiers = Keyboard.Modifiers;
@ -169,6 +175,7 @@ namespace SystemTrayMenu.UserInterface
case Key.Back:
case Key.Delete:
HotkeyFunction?.Unregister();
WasHotkeyChanged = true;
UpdateHotkeyRegistration();
e.Handled = true;
break;
@ -290,6 +297,7 @@ namespace SystemTrayMenu.UserInterface
if (e.Key == Key.Back || e.Key == Key.Delete)
{
HotkeyFunction?.Unregister();
WasHotkeyChanged = true;
}
else
{
@ -320,6 +328,7 @@ namespace SystemTrayMenu.UserInterface
else if (hotkey == Key.None)
{
HotkeyFunction?.Unregister();
WasHotkeyChanged = true;
UpdateHotkeyRegistration();
}
}

View file

@ -504,7 +504,11 @@ namespace SystemTrayMenu.UserInterface
Settings.Default.CheckForUpdates = checkBoxCheckForUpdates.IsChecked ?? false;
Settings.Default.HotKey = textBoxHotkey.HotkeyFunction?.GetHotkeyInvariantString() ?? string.Empty;
if (textBoxHotkey.WasHotkeyChanged)
{
Settings.Default.HotKey = textBoxHotkey.HotkeyFunction?.GetHotkeyInvariantString() ?? string.Empty;
}
Settings.Default.CurrentCultureInfoName = comboBoxLanguage.SelectedValue.ToString();
Settings.Default.SizeInPercent = numericUpDownSizeInPercent.Value;
Settings.Default.IconSizeInPercent = numericUpDownIconSizeInPercent.Value;