From dab86a31f54d4c2fe4beca1ed8f4ebfa3bed2ee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damian=20B=C3=BCchel?= Date: Mon, 27 Mar 2023 16:55:17 +0200 Subject: [PATCH] SEBWIN-668, #589: Fixed crash when using SEB without browser and pressing the Windows key. --- .../Windows/Taskbar.xaml.cs | 6 ++- .../Windows/Taskbar.xaml.cs | 40 +++++++------------ 2 files changed, 18 insertions(+), 28 deletions(-) diff --git a/SafeExamBrowser.UserInterface.Desktop/Windows/Taskbar.xaml.cs b/SafeExamBrowser.UserInterface.Desktop/Windows/Taskbar.xaml.cs index b5f57203..40490c16 100644 --- a/SafeExamBrowser.UserInterface.Desktop/Windows/Taskbar.xaml.cs +++ b/SafeExamBrowser.UserInterface.Desktop/Windows/Taskbar.xaml.cs @@ -198,7 +198,7 @@ namespace SafeExamBrowser.UserInterface.Desktop.Windows private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { isQuitButtonFocusedAtKeyDown = QuitButton.IsKeyboardFocusWithin; - isFirstChildFocusedAtKeyDown = ApplicationStackPanel.Children[0].IsKeyboardFocusWithin; + isFirstChildFocusedAtKeyDown = ApplicationStackPanel.Children.Count > 0 && ApplicationStackPanel.Children[0].IsKeyboardFocusWithin; } private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) @@ -206,7 +206,9 @@ namespace SafeExamBrowser.UserInterface.Desktop.Windows if (e.Key == System.Windows.Input.Key.Tab) { var shift = System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftShift); - if (!shift && ApplicationStackPanel.Children[0].IsKeyboardFocusWithin && isQuitButtonFocusedAtKeyDown) + var hasFocus = ApplicationStackPanel.Children.Count > 0 && ApplicationStackPanel.Children[0].IsKeyboardFocusWithin; + + if (!shift && hasFocus && isQuitButtonFocusedAtKeyDown) { LoseFocusRequested?.Invoke(true); e.Handled = true; diff --git a/SafeExamBrowser.UserInterface.Mobile/Windows/Taskbar.xaml.cs b/SafeExamBrowser.UserInterface.Mobile/Windows/Taskbar.xaml.cs index 0c07f359..5b56b589 100644 --- a/SafeExamBrowser.UserInterface.Mobile/Windows/Taskbar.xaml.cs +++ b/SafeExamBrowser.UserInterface.Mobile/Windows/Taskbar.xaml.cs @@ -196,8 +196,8 @@ namespace SafeExamBrowser.UserInterface.Mobile.Windows private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { - this.isQuitButtonFocusedAtKeyDown = this.QuitButton.IsKeyboardFocusWithin; - this.isFirstChildFocusedAtKeyDown = this.ApplicationStackPanel.Children[0].IsKeyboardFocusWithin; + isQuitButtonFocusedAtKeyDown = QuitButton.IsKeyboardFocusWithin; + isFirstChildFocusedAtKeyDown = ApplicationStackPanel.Children.Count > 0 && ApplicationStackPanel.Children[0].IsKeyboardFocusWithin; } private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) @@ -205,36 +205,22 @@ namespace SafeExamBrowser.UserInterface.Mobile.Windows if (e.Key == System.Windows.Input.Key.Tab) { var shift = System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.LeftShift); - if (!shift && this.ApplicationStackPanel.Children[0].IsKeyboardFocusWithin && this.isQuitButtonFocusedAtKeyDown) + var hasFocus = ApplicationStackPanel.Children.Count > 0 && ApplicationStackPanel.Children[0].IsKeyboardFocusWithin; + + if (!shift && hasFocus && isQuitButtonFocusedAtKeyDown) { - this.LoseFocusRequested?.Invoke(true); + LoseFocusRequested?.Invoke(true); e.Handled = true; } - else if (shift && this.QuitButton.IsKeyboardFocusWithin && this.isFirstChildFocusedAtKeyDown) + else if (shift && QuitButton.IsKeyboardFocusWithin && isFirstChildFocusedAtKeyDown) { - this.LoseFocusRequested?.Invoke(false); + LoseFocusRequested?.Invoke(false); e.Handled = true; } } - this.isQuitButtonFocusedAtKeyDown = false; - this.isFirstChildFocusedAtKeyDown = false; - } - - void ITaskbar.Focus(bool forward) - { - this.Dispatcher.BeginInvoke((Action) (() => - { - base.Activate(); - if (forward) - { - this.SetFocusWithin(this.ApplicationStackPanel.Children[0]); - } - else - { - this.QuitButton.Focus(); - } - })); + isQuitButtonFocusedAtKeyDown = false; + isFirstChildFocusedAtKeyDown = false; } private bool SetFocusWithin(UIElement uIElement) @@ -248,9 +234,10 @@ namespace SafeExamBrowser.UserInterface.Mobile.Windows if (uIElement is System.Windows.Controls.Panel) { var panel = uIElement as System.Windows.Controls.Panel; + for (var i = 0; i < panel.Children.Count; i++) { - if (this.SetFocusWithin(panel.Children[i])) + if (SetFocusWithin(panel.Children[i])) { return true; } @@ -262,9 +249,10 @@ namespace SafeExamBrowser.UserInterface.Mobile.Windows { var control = uIElement as System.Windows.Controls.ContentControl; var content = control.Content as UIElement; + if (content != null) { - return this.SetFocusWithin(content); + return SetFocusWithin(content); } }