Completed instance handling for browser and taskbar button implementation.
This commit is contained in:
parent
9125b41361
commit
af7e1e6a6e
9 changed files with 56 additions and 42 deletions
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,8 @@ using SafeExamBrowser.Contracts.UserInterface;
|
|||
|
||||
namespace SafeExamBrowser.Contracts.Configuration
|
||||
{
|
||||
public delegate void TerminationEventHandler(Guid id);
|
||||
|
||||
public interface IApplicationInstance
|
||||
{
|
||||
/// <summary>
|
||||
|
@ -23,6 +25,11 @@ namespace SafeExamBrowser.Contracts.Configuration
|
|||
/// </summary>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Event fired when the application instance has been terminated.
|
||||
/// </summary>
|
||||
event TerminationEventHandler OnTerminated;
|
||||
|
||||
/// <summary>
|
||||
/// The main window of the application instance.
|
||||
/// </summary>
|
||||
|
|
|
@ -22,15 +22,8 @@ namespace SafeExamBrowser.Contracts.UserInterface
|
|||
event TaskbarButtonClickHandler OnClick;
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
void RegisterInstance(IApplicationInstance instance);
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters an application instance, e.g. if it gets closed.
|
||||
/// </summary>
|
||||
/// <param name="id">The identifier for the application instance.</param>
|
||||
void UnregisterInstance(Guid id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,8 +8,15 @@
|
|||
|
||||
namespace SafeExamBrowser.Contracts.UserInterface
|
||||
{
|
||||
public delegate void WindowCloseHandler();
|
||||
|
||||
public interface IWindow
|
||||
{
|
||||
/// <summary>
|
||||
/// Event fired when the window is closing.
|
||||
/// </summary>
|
||||
event WindowCloseHandler OnClose;
|
||||
|
||||
/// <summary>
|
||||
/// Brings the window to the foreground.
|
||||
/// </summary>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue