diff --git a/SafeExamBrowser.Browser/BrowserApplicationController.cs b/SafeExamBrowser.Browser/BrowserApplicationController.cs index bca41ec3..fa304cd5 100644 --- a/SafeExamBrowser.Browser/BrowserApplicationController.cs +++ b/SafeExamBrowser.Browser/BrowserApplicationController.cs @@ -48,20 +48,36 @@ namespace SafeExamBrowser.Browser public void RegisterApplicationButton(ITaskbarButton button) { this.button = button; - this.button.OnClick += ButtonClick; + this.button.OnClick += Button_OnClick; } public void Terminate() { foreach (var instance in instances) { + instance.OnTerminated -= Instance_OnTerminated; instance.Window.Close(); } Cef.Shutdown(); } - private void ButtonClick(Guid? instanceId = null) + private void CreateNewInstance() + { + var control = new BrowserControl("www.duckduckgo.com"); + var window = uiFactory.CreateBrowserWindow(control); + var instance = new BrowserApplicationInstance("DuckDuckGo"); + + instance.RegisterWindow(window); + instance.OnTerminated += Instance_OnTerminated; + + button.RegisterInstance(instance); + instances.Add(instance); + + window.Show(); + } + + private void Button_OnClick(Guid? instanceId = null) { if (instanceId.HasValue) { @@ -73,17 +89,9 @@ namespace SafeExamBrowser.Browser } } - private void CreateNewInstance() + private void Instance_OnTerminated(Guid id) { - var control = new BrowserControl("www.duckduckgo.com"); - var window = uiFactory.CreateBrowserWindow(control); - var instance = new BrowserApplicationInstance("DuckDuckGo"); - - instances.Add(instance); - instance.RegisterWindow(window); - button.RegisterInstance(instance); - - window.Show(); + instances.Remove(instances.FirstOrDefault(i => i.Id == id)); } } } diff --git a/SafeExamBrowser.Browser/BrowserApplicationInstance.cs b/SafeExamBrowser.Browser/BrowserApplicationInstance.cs index ec58c8fb..afe99d3c 100644 --- a/SafeExamBrowser.Browser/BrowserApplicationInstance.cs +++ b/SafeExamBrowser.Browser/BrowserApplicationInstance.cs @@ -18,6 +18,8 @@ namespace SafeExamBrowser.Browser public string Name { get; private set; } public IWindow Window { get; private set; } + public event TerminationEventHandler OnTerminated; + public BrowserApplicationInstance(string name) { Id = Guid.NewGuid(); @@ -27,6 +29,7 @@ namespace SafeExamBrowser.Browser public void RegisterWindow(IWindow window) { Window = window; + Window.OnClose += () => OnTerminated?.Invoke(Id); } } } diff --git a/SafeExamBrowser.Contracts/Configuration/IApplicationInstance.cs b/SafeExamBrowser.Contracts/Configuration/IApplicationInstance.cs index 160ae56a..af69fcea 100644 --- a/SafeExamBrowser.Contracts/Configuration/IApplicationInstance.cs +++ b/SafeExamBrowser.Contracts/Configuration/IApplicationInstance.cs @@ -11,6 +11,8 @@ using SafeExamBrowser.Contracts.UserInterface; namespace SafeExamBrowser.Contracts.Configuration { + public delegate void TerminationEventHandler(Guid id); + public interface IApplicationInstance { /// @@ -23,6 +25,11 @@ namespace SafeExamBrowser.Contracts.Configuration /// string Name { get; } + /// + /// Event fired when the application instance has been terminated. + /// + event TerminationEventHandler OnTerminated; + /// /// The main window of the application instance. /// diff --git a/SafeExamBrowser.Contracts/UserInterface/ITaskbarButton.cs b/SafeExamBrowser.Contracts/UserInterface/ITaskbarButton.cs index 5e9da8a3..c2edd52d 100644 --- a/SafeExamBrowser.Contracts/UserInterface/ITaskbarButton.cs +++ b/SafeExamBrowser.Contracts/UserInterface/ITaskbarButton.cs @@ -22,15 +22,8 @@ namespace SafeExamBrowser.Contracts.UserInterface event TaskbarButtonClickHandler OnClick; /// - /// Registers a new instance of an application, to be displayed if the user clicks the taskbar button - /// when there are already one or more instances of the same application running. + /// Registers a new instance of an application, to be displayed if the user clicks the taskbar button. /// void RegisterInstance(IApplicationInstance instance); - - /// - /// Unregisters an application instance, e.g. if it gets closed. - /// - /// The identifier for the application instance. - void UnregisterInstance(Guid id); } } diff --git a/SafeExamBrowser.Contracts/UserInterface/IWindow.cs b/SafeExamBrowser.Contracts/UserInterface/IWindow.cs index b6423f7e..986d16b0 100644 --- a/SafeExamBrowser.Contracts/UserInterface/IWindow.cs +++ b/SafeExamBrowser.Contracts/UserInterface/IWindow.cs @@ -8,8 +8,15 @@ namespace SafeExamBrowser.Contracts.UserInterface { + public delegate void WindowCloseHandler(); + public interface IWindow { + /// + /// Event fired when the window is closing. + /// + event WindowCloseHandler OnClose; + /// /// Brings the window to the foreground. /// diff --git a/SafeExamBrowser.Core/Behaviour/ShutdownController.cs b/SafeExamBrowser.Core/Behaviour/ShutdownController.cs index 183bc7ee..7a21295e 100644 --- a/SafeExamBrowser.Core/Behaviour/ShutdownController.cs +++ b/SafeExamBrowser.Core/Behaviour/ShutdownController.cs @@ -8,7 +8,6 @@ using System; using System.Collections.Generic; -using System.Threading; using SafeExamBrowser.Contracts.Behaviour; using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.I18n; @@ -54,9 +53,6 @@ namespace SafeExamBrowser.Core.Behaviour { operation.SplashScreen = splashScreen; operation.Revert(); - - // TODO: Remove! - Thread.Sleep(250); } } diff --git a/SafeExamBrowser.Core/Behaviour/StartupController.cs b/SafeExamBrowser.Core/Behaviour/StartupController.cs index 4e14a326..692a7c2c 100644 --- a/SafeExamBrowser.Core/Behaviour/StartupController.cs +++ b/SafeExamBrowser.Core/Behaviour/StartupController.cs @@ -9,7 +9,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Threading; using SafeExamBrowser.Contracts.Behaviour; using SafeExamBrowser.Contracts.Configuration; using SafeExamBrowser.Contracts.I18n; @@ -66,9 +65,6 @@ namespace SafeExamBrowser.Core.Behaviour operation.Perform(); splashScreen.Progress(); - - // TODO: Remove! - Thread.Sleep(250); } } @@ -80,9 +76,6 @@ namespace SafeExamBrowser.Core.Behaviour operation.Revert(); splashScreen.Regress(); - - // TODO: Remove! - Thread.Sleep(250); } } diff --git a/SafeExamBrowser.UserInterface/BrowserWindow.xaml.cs b/SafeExamBrowser.UserInterface/BrowserWindow.xaml.cs index a8581053..197587ee 100644 --- a/SafeExamBrowser.UserInterface/BrowserWindow.xaml.cs +++ b/SafeExamBrowser.UserInterface/BrowserWindow.xaml.cs @@ -19,6 +19,8 @@ namespace SafeExamBrowser.UserInterface InitializeBrowserWindow(browserControl); } + public event WindowCloseHandler OnClose; + public void BringToForeground() { if (WindowState == WindowState.Minimized) @@ -35,6 +37,8 @@ namespace SafeExamBrowser.UserInterface { BrowserControlHost.Child = browserControl as System.Windows.Forms.Control; } + + Closing += (o, args) => OnClose?.Invoke(); } } } diff --git a/SafeExamBrowser.UserInterface/Controls/ApplicationButton.xaml.cs b/SafeExamBrowser.UserInterface/Controls/ApplicationButton.xaml.cs index 3a1db4b2..6a7b0be6 100644 --- a/SafeExamBrowser.UserInterface/Controls/ApplicationButton.xaml.cs +++ b/SafeExamBrowser.UserInterface/Controls/ApplicationButton.xaml.cs @@ -36,23 +36,15 @@ namespace SafeExamBrowser.UserInterface.Controls { var instanceButton = new ApplicationInstanceButton(instance, info); - instances.Add(instance); instanceButton.Click += (id) => OnClick?.Invoke(id); + instance.OnTerminated += (id) => Instance_OnTerminated(id, instanceButton); + + instances.Add(instance); InstanceStackPanel.Children.Add(instanceButton); ActiveBar.Visibility = Visibility.Visible; } - public void UnregisterInstance(Guid id) - { - instances.Remove(instances.FirstOrDefault(i => i.Id == id)); - - if (!instances.Any()) - { - ActiveBar.Visibility = Visibility.Collapsed; - } - } - private void InitializeApplicationButton() { Button.ToolTip = info.Tooltip; @@ -84,5 +76,16 @@ namespace SafeExamBrowser.UserInterface.Controls InstancePopup.IsOpen = true; } } + + private void Instance_OnTerminated(Guid id, ApplicationInstanceButton instanceButton) + { + instances.Remove(instances.FirstOrDefault(i => i.Id == id)); + InstanceStackPanel.Children.Remove(instanceButton); + + if (!instances.Any()) + { + ActiveBar.Visibility = Visibility.Collapsed; + } + } } }