From cd5bbfcb47611bf0695d6d3c4430c932bd21e66a Mon Sep 17 00:00:00 2001 From: dbuechel Date: Thu, 21 Jun 2018 08:54:43 +0200 Subject: [PATCH] SEBWIN-220: Implemented mechanism to prevent (accidental) closing of taskbar by ALT+F4. --- .../Behaviour/ClientController.cs | 7 ++++++- .../Browser/DownloadRequestedEventHandler.cs | 3 +++ .../SafeExamBrowser.Contracts.csproj | 1 + .../UserInterface/Taskbar/ITaskbar.cs | 2 -- .../Taskbar/QuitButtonClickedEventHandler.cs | 17 +++++++++++++++++ .../Controls/QuitButton.xaml.cs | 5 +++-- .../Taskbar.xaml.cs | 16 +++++++++++++++- .../Controls/QuitButton.xaml.cs | 3 ++- .../Taskbar.xaml.cs | 17 +++++++++++++---- 9 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 SafeExamBrowser.Contracts/UserInterface/Taskbar/QuitButtonClickedEventHandler.cs diff --git a/SafeExamBrowser.Client/Behaviour/ClientController.cs b/SafeExamBrowser.Client/Behaviour/ClientController.cs index d1fae0ac..92762797 100644 --- a/SafeExamBrowser.Client/Behaviour/ClientController.cs +++ b/SafeExamBrowser.Client/Behaviour/ClientController.cs @@ -7,6 +7,7 @@ */ using System; +using System.ComponentModel; using System.IO; using SafeExamBrowser.Contracts.Behaviour; using SafeExamBrowser.Contracts.Behaviour.OperationModel; @@ -250,7 +251,7 @@ namespace SafeExamBrowser.Client.Behaviour shutdown.Invoke(); } - private void Taskbar_QuitButtonClicked() + private void Taskbar_QuitButtonClicked(CancelEventArgs args) { var result = messageBox.Show(TextKey.MessageBox_Quit, TextKey.MessageBox_QuitTitle, MessageBoxAction.YesNo, MessageBoxIcon.Question); @@ -266,6 +267,10 @@ namespace SafeExamBrowser.Client.Behaviour messageBox.Show(TextKey.MessageBox_QuitError, TextKey.MessageBox_QuitErrorTitle, icon: MessageBoxIcon.Error); } } + else + { + args.Cancel = true; + } } private void WindowMonitor_WindowChanged(IntPtr window) diff --git a/SafeExamBrowser.Contracts/Browser/DownloadRequestedEventHandler.cs b/SafeExamBrowser.Contracts/Browser/DownloadRequestedEventHandler.cs index a96f730b..561764be 100644 --- a/SafeExamBrowser.Contracts/Browser/DownloadRequestedEventHandler.cs +++ b/SafeExamBrowser.Contracts/Browser/DownloadRequestedEventHandler.cs @@ -8,5 +8,8 @@ namespace SafeExamBrowser.Contracts.Browser { + /// + /// Event handler used to control (e.g. allow or prohibit) download requests. + /// public delegate void DownloadRequestedEventHandler(string fileName, DownloadEventArgs args); } diff --git a/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj b/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj index c726f27d..a5465de4 100644 --- a/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj +++ b/SafeExamBrowser.Contracts/SafeExamBrowser.Contracts.csproj @@ -138,6 +138,7 @@ + diff --git a/SafeExamBrowser.Contracts/UserInterface/Taskbar/ITaskbar.cs b/SafeExamBrowser.Contracts/UserInterface/Taskbar/ITaskbar.cs index d24d16b2..e289f5ba 100644 --- a/SafeExamBrowser.Contracts/UserInterface/Taskbar/ITaskbar.cs +++ b/SafeExamBrowser.Contracts/UserInterface/Taskbar/ITaskbar.cs @@ -8,8 +8,6 @@ namespace SafeExamBrowser.Contracts.UserInterface.Taskbar { - public delegate void QuitButtonClickedEventHandler(); - /// /// Defines the functionality of the application taskbar. The taskbar is the main user interface element via which the user can access /// the browser, third-party applications, system controls and so on. diff --git a/SafeExamBrowser.Contracts/UserInterface/Taskbar/QuitButtonClickedEventHandler.cs b/SafeExamBrowser.Contracts/UserInterface/Taskbar/QuitButtonClickedEventHandler.cs new file mode 100644 index 00000000..8e7afa59 --- /dev/null +++ b/SafeExamBrowser.Contracts/UserInterface/Taskbar/QuitButtonClickedEventHandler.cs @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET) + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +using System.ComponentModel; + +namespace SafeExamBrowser.Contracts.UserInterface.Taskbar +{ + /// + /// Event handler used to define the control flow when the 's quit button is clicked. + /// + public delegate void QuitButtonClickedEventHandler(CancelEventArgs args); +} diff --git a/SafeExamBrowser.UserInterface.Classic/Controls/QuitButton.xaml.cs b/SafeExamBrowser.UserInterface.Classic/Controls/QuitButton.xaml.cs index b9e4f3c6..605da386 100644 --- a/SafeExamBrowser.UserInterface.Classic/Controls/QuitButton.xaml.cs +++ b/SafeExamBrowser.UserInterface.Classic/Controls/QuitButton.xaml.cs @@ -7,6 +7,7 @@ */ using System; +using System.ComponentModel; using System.Windows; using System.Windows.Controls; using SafeExamBrowser.Contracts.UserInterface.Taskbar; @@ -16,7 +17,7 @@ namespace SafeExamBrowser.UserInterface.Classic.Controls { public partial class QuitButton : UserControl { - public QuitButtonClickedEventHandler Clicked; + public event QuitButtonClickedEventHandler Clicked; public QuitButton() { @@ -26,7 +27,7 @@ namespace SafeExamBrowser.UserInterface.Classic.Controls private void Button_Click(object sender, RoutedEventArgs e) { - Clicked?.Invoke(); + Clicked?.Invoke(new CancelEventArgs()); } private void LoadIcon() diff --git a/SafeExamBrowser.UserInterface.Classic/Taskbar.xaml.cs b/SafeExamBrowser.UserInterface.Classic/Taskbar.xaml.cs index cbaded3e..0ed4af16 100644 --- a/SafeExamBrowser.UserInterface.Classic/Taskbar.xaml.cs +++ b/SafeExamBrowser.UserInterface.Classic/Taskbar.xaml.cs @@ -16,6 +16,7 @@ namespace SafeExamBrowser.UserInterface.Classic { public partial class Taskbar : Window, ITaskbar { + private bool allowClose; private ILogger logger; public event QuitButtonClickedEventHandler QuitButtonClicked; @@ -28,7 +29,7 @@ namespace SafeExamBrowser.UserInterface.Classic Closing += Taskbar_Closing; Loaded += (o, args) => InitializeBounds(); - QuitButton.Clicked += () => QuitButtonClicked?.Invoke(); + QuitButton.Clicked += QuitButton_Clicked; } public void AddApplication(IApplicationButton button) @@ -87,8 +88,21 @@ namespace SafeExamBrowser.UserInterface.Classic }); } + private void QuitButton_Clicked(CancelEventArgs args) + { + QuitButtonClicked?.Invoke(args); + allowClose = !args.Cancel; + } + private void Taskbar_Closing(object sender, CancelEventArgs e) { + if (!allowClose) + { + e.Cancel = true; + + return; + } + foreach (var child in SystemControlStackPanel.Children) { if (child is ISystemControl systemControl) diff --git a/SafeExamBrowser.UserInterface.Windows10/Controls/QuitButton.xaml.cs b/SafeExamBrowser.UserInterface.Windows10/Controls/QuitButton.xaml.cs index dacf1717..6859c194 100644 --- a/SafeExamBrowser.UserInterface.Windows10/Controls/QuitButton.xaml.cs +++ b/SafeExamBrowser.UserInterface.Windows10/Controls/QuitButton.xaml.cs @@ -6,6 +6,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +using System.ComponentModel; using System.Windows; using System.Windows.Controls; using SafeExamBrowser.Contracts.UserInterface.Taskbar; @@ -23,7 +24,7 @@ namespace SafeExamBrowser.UserInterface.Windows10.Controls private void Button_Click(object sender, RoutedEventArgs e) { - Clicked?.Invoke(); + Clicked?.Invoke(new CancelEventArgs()); } } } diff --git a/SafeExamBrowser.UserInterface.Windows10/Taskbar.xaml.cs b/SafeExamBrowser.UserInterface.Windows10/Taskbar.xaml.cs index 72e3ed3f..160d7ce9 100644 --- a/SafeExamBrowser.UserInterface.Windows10/Taskbar.xaml.cs +++ b/SafeExamBrowser.UserInterface.Windows10/Taskbar.xaml.cs @@ -16,6 +16,7 @@ namespace SafeExamBrowser.UserInterface.Windows10 { public partial class Taskbar : Window, ITaskbar { + private bool allowClose; private ILogger logger; public event QuitButtonClickedEventHandler QuitButtonClicked; @@ -26,9 +27,9 @@ namespace SafeExamBrowser.UserInterface.Windows10 InitializeComponent(); - Loaded += (o, args) => InitializeBounds(); Closing += Taskbar_Closing; - QuitButtonClicked += Taskbar_QuitButtonClicked; + Loaded += (o, args) => InitializeBounds(); + QuitButtonClicked += QuitButton_Clicked; } public void AddApplication(IApplicationButton button) @@ -87,13 +88,21 @@ namespace SafeExamBrowser.UserInterface.Windows10 }); } - private void Taskbar_QuitButtonClicked() + private void QuitButton_Clicked(CancelEventArgs args) { - QuitButtonClicked?.Invoke(); + QuitButtonClicked?.Invoke(args); + allowClose = !args.Cancel; } private void Taskbar_Closing(object sender, CancelEventArgs e) { + if (!allowClose) + { + e.Cancel = true; + + return; + } + foreach (var child in SystemControlStackPanel.Children) { if (child is ISystemControl systemControl)