From d0a1e40b5db8c0ccb35b04f8ac446e600fa43663 Mon Sep 17 00:00:00 2001 From: Jaime Bernardo Date: Tue, 11 Apr 2023 17:47:17 +0100 Subject: [PATCH] [Settings]Fix null exception on shortcut control (#25398) * [Settings]Fix null exception on shortcut control * Also add ifs around getting the settings window --- .../ShortcutControl/ShortcutControl.xaml.cs | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/settings-ui/Settings.UI/Controls/ShortcutControl/ShortcutControl.xaml.cs b/src/settings-ui/Settings.UI/Controls/ShortcutControl/ShortcutControl.xaml.cs index 38ae3adb59..c9b9035049 100644 --- a/src/settings-ui/Settings.UI/Controls/ShortcutControl/ShortcutControl.xaml.cs +++ b/src/settings-ui/Settings.UI/Controls/ShortcutControl/ShortcutControl.xaml.cs @@ -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;