[Settings]Fix null exception on shortcut control (#25398)

* [Settings]Fix null exception on shortcut control

* Also add ifs around getting the settings window
This commit is contained in:
Jaime Bernardo 2023-04-11 17:47:17 +01:00 committed by GitHub
parent 1387c3a9a9
commit d0a1e40b5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -89,7 +89,10 @@ namespace Microsoft.PowerToys.Settings.UI.Controls
hook = new HotkeySettingsControlHook(Hotkey_KeyDown, Hotkey_KeyUp, Hotkey_IsActive, FilterAccessibleKeyboardEvents);
ResourceLoader resourceLoader = ResourceLoader.GetForViewIndependentUse();
App.GetSettingsWindow().Activated += ShortcutDialog_SettingsWindow_Activated;
if (App.GetSettingsWindow() != null)
{
App.GetSettingsWindow().Activated += ShortcutDialog_SettingsWindow_Activated;
}
// We create the Dialog in C# because doing it in XAML is giving WinUI/XAML Island bugs when using dark theme.
shortcutDialog = new ContentDialog
@ -113,10 +116,18 @@ namespace Microsoft.PowerToys.Settings.UI.Controls
shortcutDialog.Opened -= ShortcutDialog_Opened;
shortcutDialog.Closing -= ShortcutDialog_Closing;
App.GetSettingsWindow().Activated -= ShortcutDialog_SettingsWindow_Activated;
if (App.GetSettingsWindow() != null)
{
App.GetSettingsWindow().Activated -= ShortcutDialog_SettingsWindow_Activated;
}
// Dispose the HotkeySettingsControlHook object to terminate the hook threads when the textbox is unloaded
hook.Dispose();
if (hook != null)
{
hook.Dispose();
}
hook = null;
}
private void KeyEventHandler(int key, bool matchValue, int matchValueCode)
@ -371,15 +382,16 @@ namespace Microsoft.PowerToys.Settings.UI.Controls
private void ShortcutDialog_SettingsWindow_Activated(object sender, WindowActivatedEventArgs args)
{
args.Handled = true;
if (args.WindowActivationState != WindowActivationState.Deactivated && hook.GetDisposedState() == true)
if (args.WindowActivationState != WindowActivationState.Deactivated && (hook == null || hook.GetDisposedState() == true))
{
// If the PT settings window gets focussed/activated again, we enable the keyboard hook to catch the keyboard input.
hook = new HotkeySettingsControlHook(Hotkey_KeyDown, Hotkey_KeyUp, Hotkey_IsActive, FilterAccessibleKeyboardEvents);
}
else if (args.WindowActivationState == WindowActivationState.Deactivated && hook.GetDisposedState() == false)
else if (args.WindowActivationState == WindowActivationState.Deactivated && hook != null && hook.GetDisposedState() == false)
{
// If the PT settings window lost focus/activation, we disable the keyboard hook to allow keyboard input on other windows.
hook.Dispose();
hook = null;
}
}
@ -394,7 +406,12 @@ namespace Microsoft.PowerToys.Settings.UI.Controls
{
if (disposing)
{
hook.Dispose();
if (hook != null)
{
hook.Dispose();
}
hook = null;
}
disposedValue = true;