// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Reflection;
using System.Threading;
using Perspex.Controls;
using Perspex.Controls.Templates;
using Perspex.Input;
using Perspex.Input.Platform;
using Perspex.Layout;
using Perspex.Rendering;
using Perspex.Styling;
using Perspex.Threading;
namespace Perspex
{
///
/// Encapsulates a Perspex application.
///
///
/// The class encapsulates Perspex application-specific
/// functionality, including:
/// - A global set of .
/// - A global set of .
/// - A .
/// - An .
/// - Loads and initializes rendering and windowing subsystems with
/// and .
/// - 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;
private readonly Lazy _clipboard =
new Lazy(() => (IClipboard)PerspexLocator.Current.GetService(typeof(IClipboard)));
///
/// The styler that will be used to apply styles to controls.
///
private readonly Styler _styler = new Styler();
///
/// Initializes a new instance of the class.
///
public Application()
{
if (Current != null)
{
throw new InvalidOperationException("Cannot create more than one Application instance.");
}
Current = this;
}
///
/// 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 (_dataTemplates == null)
{
_dataTemplates = new DataTemplates();
}
return _dataTemplates;
}
set
{
_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 clipboard.
///
public IClipboard Clipboard => _clipboard.Value;
///
/// Gets the application's global styles.
///
///
/// The application's global styles.
///
///
/// Global styles apply to all windows in the application.
///
public Styles Styles
{
get;
protected 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()
{
PerspexSynchronizationContext.InstallIfNeeded();
FocusManager = new FocusManager();
InputManager = new InputManager();
PerspexLocator.CurrentMutable
.Bind().ToTransient()
.Bind().ToConstant(this)
.Bind().ToConstant(this)
.Bind().ToConstant(FocusManager)
.Bind().ToConstant(InputManager)
.Bind().ToTransient()
.Bind().ToConstant(_styler)
.Bind().ToTransient()
.Bind().ToTransient();
}
///
/// Initializes the rendering and windowing subsystems according to platform.
///
/// The value of Environment.OSVersion.Platform.
protected void InitializeSubsystems(int platformID)
{
if (platformID == 4 || platformID == 6)
{
InitializeSubsystem("Perspex.Cairo");
InitializeSubsystem("Perspex.Gtk");
}
else
{
InitializeSubsystem("Perspex.Direct2D1");
InitializeSubsystem("Perspex.Win32");
}
}
///
/// Initializes the rendering or windowing subsystem defined by the specified assemblt.
///
/// The name of the assembly.
protected void InitializeSubsystem(string assemblyName)
{
var assembly = Assembly.Load(new AssemblyName(assemblyName));
var platformClassName = assemblyName.Replace("Perspex.", string.Empty) + "Platform";
var platformClassFullName = assemblyName + "." + platformClassName;
var platformClass = assembly.GetType(platformClassFullName);
var init = platformClass.GetRuntimeMethod("Initialize", new Type[0]);
init.Invoke(null, null);
}
}
}