// -----------------------------------------------------------------------
//
// Copyright 2014 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex
{
using System;
using System.Threading;
using Perspex.Controls;
using Perspex.Input;
using Perspex.Layout;
using Perspex.Rendering;
using Perspex.Styling;
using Perspex.Threading;
using Splat;
///
/// Encapsulates a Perspex application.
///
///
/// The class encapsulates Perspex application-specific
/// functionality, including:
/// - A global set of .
/// - A global set of .
/// - A .
/// - An .
/// - Registers services needed by the rest of Perspex in the
/// method.
/// - Tracks the lifetime of the application.
///
public class Application : IGlobalDataTemplates, IGlobalStyles
{
///
/// The application-global data templates.
///
private DataTemplates dataTemplates;
///
/// The styler that will be used to apply styles to controls.
///
private Styler styler = new Styler();
private ICloseable mainWindow;
///
/// Initializes a new instance of the class.
///
/// The theme to use.
public Application(Styles theme)
{
if (Current != null)
{
throw new InvalidOperationException("Cannot create more than one Application instance.");
}
Current = this;
this.Styles = theme;
this.RegisterServices();
}
///
/// Gets the current instance of the class.
///
///
/// The current instance of the class.
///
public static Application Current
{
get;
private set;
}
///
/// Gets or sets the application's global data templates.
///
///
/// The application's global data templates.
///
public DataTemplates DataTemplates
{
get
{
if (this.dataTemplates == null)
{
this.dataTemplates = new DataTemplates();
}
return this.dataTemplates;
}
set
{
this.dataTemplates = value;
}
}
///
/// Gets the application's focus manager.
///
///
/// The application's focus manager.
///
public IFocusManager FocusManager
{
get;
private set;
}
///
/// Gets the application's input manager.
///
///
/// The application's input manager.
///
public InputManager InputManager
{
get;
private set;
}
///
/// Gets the application's global styles.
///
///
/// The application's global styles.
///
///
/// Global styles apply to all windows in the application.
///
public Styles Styles
{
get;
private set;
}
///
/// Runs the application's main loop until the is closed.
///
/// The closable to track
public void Run(ICloseable closable)
{
var source = new CancellationTokenSource();
closable.Closed += (s, e) => source.Cancel();
Dispatcher.UIThread.MainLoop(source.Token);
}
///
/// Register's the services needed by Perspex.
///
protected virtual void RegisterServices()
{
this.FocusManager = new FocusManager();
this.InputManager = new InputManager();
Locator.CurrentMutable.Register(() => new AccessKeyHandler(), typeof(IAccessKeyHandler));
Locator.CurrentMutable.Register(() => this, typeof(IGlobalDataTemplates));
Locator.CurrentMutable.Register(() => this, typeof(IGlobalStyles));
Locator.CurrentMutable.Register(() => this.FocusManager, typeof(IFocusManager));
Locator.CurrentMutable.Register(() => this.InputManager, typeof(IInputManager));
Locator.CurrentMutable.Register(() => new KeyboardNavigationHandler(), typeof(IKeyboardNavigationHandler));
Locator.CurrentMutable.Register(() => this.styler, typeof(IStyler));
Locator.CurrentMutable.Register(() => new LayoutManager(), typeof(ILayoutManager));
Locator.CurrentMutable.Register(() => new RenderManager(), typeof(IRenderManager));
}
}
}