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)
|
public void RegisterApplicationButton(ITaskbarButton button)
|
||||||
{
|
{
|
||||||
this.button = button;
|
this.button = button;
|
||||||
this.button.OnClick += ButtonClick;
|
this.button.OnClick += Button_OnClick;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Terminate()
|
public void Terminate()
|
||||||
{
|
{
|
||||||
foreach (var instance in instances)
|
foreach (var instance in instances)
|
||||||
{
|
{
|
||||||
|
instance.OnTerminated -= Instance_OnTerminated;
|
||||||
instance.Window.Close();
|
instance.Window.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
Cef.Shutdown();
|
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)
|
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");
|
instances.Remove(instances.FirstOrDefault(i => i.Id == id));
|
||||||
var window = uiFactory.CreateBrowserWindow(control);
|
|
||||||
var instance = new BrowserApplicationInstance("DuckDuckGo");
|
|
||||||
|
|
||||||
instances.Add(instance);
|
|
||||||
instance.RegisterWindow(window);
|
|
||||||
button.RegisterInstance(instance);
|
|
||||||
|
|
||||||
window.Show();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ namespace SafeExamBrowser.Browser
|
||||||
public string Name { get; private set; }
|
public string Name { get; private set; }
|
||||||
public IWindow Window { get; private set; }
|
public IWindow Window { get; private set; }
|
||||||
|
|
||||||
|
public event TerminationEventHandler OnTerminated;
|
||||||
|
|
||||||
public BrowserApplicationInstance(string name)
|
public BrowserApplicationInstance(string name)
|
||||||
{
|
{
|
||||||
Id = Guid.NewGuid();
|
Id = Guid.NewGuid();
|
||||||
|
@ -27,6 +29,7 @@ namespace SafeExamBrowser.Browser
|
||||||
public void RegisterWindow(IWindow window)
|
public void RegisterWindow(IWindow window)
|
||||||
{
|
{
|
||||||
Window = window;
|
Window = window;
|
||||||
|
Window.OnClose += () => OnTerminated?.Invoke(Id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,8 @@ using SafeExamBrowser.Contracts.UserInterface;
|
||||||
|
|
||||||
namespace SafeExamBrowser.Contracts.Configuration
|
namespace SafeExamBrowser.Contracts.Configuration
|
||||||
{
|
{
|
||||||
|
public delegate void TerminationEventHandler(Guid id);
|
||||||
|
|
||||||
public interface IApplicationInstance
|
public interface IApplicationInstance
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -23,6 +25,11 @@ namespace SafeExamBrowser.Contracts.Configuration
|
||||||
/// </summary>
|
/// </summary>
|
||||||
string Name { get; }
|
string Name { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Event fired when the application instance has been terminated.
|
||||||
|
/// </summary>
|
||||||
|
event TerminationEventHandler OnTerminated;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The main window of the application instance.
|
/// The main window of the application instance.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -22,15 +22,8 @@ namespace SafeExamBrowser.Contracts.UserInterface
|
||||||
event TaskbarButtonClickHandler OnClick;
|
event TaskbarButtonClickHandler OnClick;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers a new instance of an application, to be displayed if the user clicks the taskbar button
|
/// 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.
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void RegisterInstance(IApplicationInstance instance);
|
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
|
namespace SafeExamBrowser.Contracts.UserInterface
|
||||||
{
|
{
|
||||||
|
public delegate void WindowCloseHandler();
|
||||||
|
|
||||||
public interface IWindow
|
public interface IWindow
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Event fired when the window is closing.
|
||||||
|
/// </summary>
|
||||||
|
event WindowCloseHandler OnClose;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Brings the window to the foreground.
|
/// Brings the window to the foreground.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading;
|
|
||||||
using SafeExamBrowser.Contracts.Behaviour;
|
using SafeExamBrowser.Contracts.Behaviour;
|
||||||
using SafeExamBrowser.Contracts.Configuration;
|
using SafeExamBrowser.Contracts.Configuration;
|
||||||
using SafeExamBrowser.Contracts.I18n;
|
using SafeExamBrowser.Contracts.I18n;
|
||||||
|
@ -54,9 +53,6 @@ namespace SafeExamBrowser.Core.Behaviour
|
||||||
{
|
{
|
||||||
operation.SplashScreen = splashScreen;
|
operation.SplashScreen = splashScreen;
|
||||||
operation.Revert();
|
operation.Revert();
|
||||||
|
|
||||||
// TODO: Remove!
|
|
||||||
Thread.Sleep(250);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
|
||||||
using SafeExamBrowser.Contracts.Behaviour;
|
using SafeExamBrowser.Contracts.Behaviour;
|
||||||
using SafeExamBrowser.Contracts.Configuration;
|
using SafeExamBrowser.Contracts.Configuration;
|
||||||
using SafeExamBrowser.Contracts.I18n;
|
using SafeExamBrowser.Contracts.I18n;
|
||||||
|
@ -66,9 +65,6 @@ namespace SafeExamBrowser.Core.Behaviour
|
||||||
operation.Perform();
|
operation.Perform();
|
||||||
|
|
||||||
splashScreen.Progress();
|
splashScreen.Progress();
|
||||||
|
|
||||||
// TODO: Remove!
|
|
||||||
Thread.Sleep(250);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,9 +76,6 @@ namespace SafeExamBrowser.Core.Behaviour
|
||||||
|
|
||||||
operation.Revert();
|
operation.Revert();
|
||||||
splashScreen.Regress();
|
splashScreen.Regress();
|
||||||
|
|
||||||
// TODO: Remove!
|
|
||||||
Thread.Sleep(250);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,8 @@ namespace SafeExamBrowser.UserInterface
|
||||||
InitializeBrowserWindow(browserControl);
|
InitializeBrowserWindow(browserControl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public event WindowCloseHandler OnClose;
|
||||||
|
|
||||||
public void BringToForeground()
|
public void BringToForeground()
|
||||||
{
|
{
|
||||||
if (WindowState == WindowState.Minimized)
|
if (WindowState == WindowState.Minimized)
|
||||||
|
@ -35,6 +37,8 @@ namespace SafeExamBrowser.UserInterface
|
||||||
{
|
{
|
||||||
BrowserControlHost.Child = browserControl as System.Windows.Forms.Control;
|
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);
|
var instanceButton = new ApplicationInstanceButton(instance, info);
|
||||||
|
|
||||||
instances.Add(instance);
|
|
||||||
instanceButton.Click += (id) => OnClick?.Invoke(id);
|
instanceButton.Click += (id) => OnClick?.Invoke(id);
|
||||||
|
instance.OnTerminated += (id) => Instance_OnTerminated(id, instanceButton);
|
||||||
|
|
||||||
|
instances.Add(instance);
|
||||||
InstanceStackPanel.Children.Add(instanceButton);
|
InstanceStackPanel.Children.Add(instanceButton);
|
||||||
|
|
||||||
ActiveBar.Visibility = Visibility.Visible;
|
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()
|
private void InitializeApplicationButton()
|
||||||
{
|
{
|
||||||
Button.ToolTip = info.Tooltip;
|
Button.ToolTip = info.Tooltip;
|
||||||
|
@ -84,5 +76,16 @@ namespace SafeExamBrowser.UserInterface.Controls
|
||||||
InstancePopup.IsOpen = true;
|
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