|
|
|
@ -3,6 +3,7 @@ |
|
|
|
|
|
|
|
using System; |
|
|
|
using System.Reflection; |
|
|
|
using System.Linq; |
|
|
|
using Avalonia.Platform; |
|
|
|
|
|
|
|
namespace Avalonia.Controls |
|
|
|
@ -21,33 +22,48 @@ namespace Avalonia.Controls |
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a method to call the initialize the runtime platform services (e. g. AssetLoader)
|
|
|
|
/// </summary>
|
|
|
|
public Action RuntimePlatformServices { get; set; } |
|
|
|
public Action RuntimePlatformServicesInitializer { get; private set; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the <see cref="Application"/> instance being initialized.
|
|
|
|
/// </summary>
|
|
|
|
public Application Instance { get; set; } |
|
|
|
public Application Instance { get; protected set; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a method to call the initialize the windowing subsystem.
|
|
|
|
/// </summary>
|
|
|
|
public Action WindowingSubsystem { get; set; } |
|
|
|
public Action WindowingSubsystemInitializer { get; private set; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the name of the currently selected windowing subsystem.
|
|
|
|
/// </summary>
|
|
|
|
public string WindowingSubsystemName { get; private set; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a method to call the initialize the windowing subsystem.
|
|
|
|
/// </summary>
|
|
|
|
public Action RenderingSubsystem { get; set; } |
|
|
|
public Action RenderingSubsystemInitializer { get; private set; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the name of the currently selected rendering subsystem.
|
|
|
|
/// </summary>
|
|
|
|
public string RenderingSubsystemName { get; private set; } |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a method to call after the <see cref="Application"/> is setup.
|
|
|
|
/// </summary>
|
|
|
|
public Action<TAppBuilder> AfterSetupCallback { get; private set; } = builder => { }; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets a method to call before <see cref="Start{TMainWindow}"/> is called on the
|
|
|
|
/// <see cref="Application"/>.
|
|
|
|
/// </summary>
|
|
|
|
public Action<TAppBuilder> BeforeStartCallback { get; set; } |
|
|
|
public Action<TAppBuilder> BeforeStartCallback { get; private set; } = builder => { }; |
|
|
|
|
|
|
|
protected AppBuilderBase(IRuntimePlatform platform, Action platformSevices) |
|
|
|
{ |
|
|
|
RuntimePlatform = platform; |
|
|
|
RuntimePlatformServices = platformSevices; |
|
|
|
RuntimePlatformServicesInitializer = platformSevices; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -85,7 +101,13 @@ namespace Avalonia.Controls |
|
|
|
/// <returns>An <typeparamref name="TAppBuilder"/> instance.</returns>
|
|
|
|
public TAppBuilder BeforeStarting(Action<TAppBuilder> callback) |
|
|
|
{ |
|
|
|
BeforeStartCallback = callback; |
|
|
|
BeforeStartCallback = (Action<TAppBuilder>)Delegate.Combine(BeforeStartCallback, callback); |
|
|
|
return Self; |
|
|
|
} |
|
|
|
|
|
|
|
public TAppBuilder AfterSetup(Action<TAppBuilder> callback) |
|
|
|
{ |
|
|
|
AfterSetupCallback = (Action<TAppBuilder>)Delegate.Combine(AfterSetupCallback, callback); |
|
|
|
return Self; |
|
|
|
} |
|
|
|
|
|
|
|
@ -97,7 +119,7 @@ namespace Avalonia.Controls |
|
|
|
where TMainWindow : Window, new() |
|
|
|
{ |
|
|
|
Setup(); |
|
|
|
BeforeStartCallback?.Invoke(Self); |
|
|
|
BeforeStartCallback(Self); |
|
|
|
|
|
|
|
var window = new TMainWindow(); |
|
|
|
window.Show(); |
|
|
|
@ -119,9 +141,10 @@ namespace Avalonia.Controls |
|
|
|
/// </summary>
|
|
|
|
/// <param name="initializer">The method to call to initialize the windowing subsystem.</param>
|
|
|
|
/// <returns>An <typeparamref name="TAppBuilder"/> instance.</returns>
|
|
|
|
public TAppBuilder UseWindowingSubsystem(Action initializer) |
|
|
|
public TAppBuilder UseWindowingSubsystem(Action initializer, string name = "") |
|
|
|
{ |
|
|
|
WindowingSubsystem = initializer; |
|
|
|
WindowingSubsystemInitializer = initializer; |
|
|
|
WindowingSubsystemName = name; |
|
|
|
return Self; |
|
|
|
} |
|
|
|
|
|
|
|
@ -130,16 +153,17 @@ namespace Avalonia.Controls |
|
|
|
/// </summary>
|
|
|
|
/// <param name="dll">The dll in which to look for subsystem.</param>
|
|
|
|
/// <returns>An <typeparamref name="TAppBuilder"/> instance.</returns>
|
|
|
|
public TAppBuilder UseWindowingSubsystem(string dll) => UseWindowingSubsystem(GetInitializer(dll)); |
|
|
|
public TAppBuilder UseWindowingSubsystem(string dll) => UseWindowingSubsystem(GetInitializer(dll), dll.Replace("Avalonia.", string.Empty)); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Specifies a rendering subsystem to use.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="initializer">The method to call to initialize the rendering subsystem.</param>
|
|
|
|
/// <returns>An <typeparamref name="TAppBuilder"/> instance.</returns>
|
|
|
|
public TAppBuilder UseRenderingSubsystem(Action initializer) |
|
|
|
public TAppBuilder UseRenderingSubsystem(Action initializer, string name = "") |
|
|
|
{ |
|
|
|
RenderingSubsystem = initializer; |
|
|
|
RenderingSubsystemInitializer = initializer; |
|
|
|
RenderingSubsystemName = name; |
|
|
|
return Self; |
|
|
|
} |
|
|
|
|
|
|
|
@ -160,6 +184,28 @@ namespace Avalonia.Controls |
|
|
|
init.Invoke(null, null); |
|
|
|
}; |
|
|
|
|
|
|
|
public TAppBuilder UseAvaloniaModules() => AfterSetup(builder => SetupAvaloniaModules()); |
|
|
|
|
|
|
|
private void SetupAvaloniaModules() |
|
|
|
{ |
|
|
|
var moduleInitializers = from assembly in AvaloniaLocator.Current.GetService<IRuntimePlatform>().GetLoadedAssemblies() |
|
|
|
from attribute in assembly.GetCustomAttributes<ExportAvaloniaModuleAttribute>() |
|
|
|
where attribute.ForWindowingSubsystem == "" |
|
|
|
|| attribute.ForWindowingSubsystem == WindowingSubsystemName |
|
|
|
where attribute.ForRenderingSubsystem == "" |
|
|
|
|| attribute.ForRenderingSubsystem == RenderingSubsystemName |
|
|
|
group attribute by attribute.Name into exports |
|
|
|
select (from export in exports |
|
|
|
orderby export.ForWindowingSubsystem.Length descending |
|
|
|
orderby export.ForRenderingSubsystem.Length descending |
|
|
|
select export).First().ModuleType into moduleType |
|
|
|
select (from constructor in moduleType.GetTypeInfo().DeclaredConstructors |
|
|
|
where constructor.GetParameters().Length == 0 && !constructor.IsStatic |
|
|
|
select constructor).Single() into constructor |
|
|
|
select (Action)(() => constructor.Invoke(new object[0])); |
|
|
|
Delegate.Combine(moduleInitializers.ToArray()).DynamicInvoke(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Sets up the platform-speciic services for the <see cref="Application"/>.
|
|
|
|
/// </summary>
|
|
|
|
@ -170,26 +216,27 @@ namespace Avalonia.Controls |
|
|
|
throw new InvalidOperationException("No App instance configured."); |
|
|
|
} |
|
|
|
|
|
|
|
if (RuntimePlatformServices == null) |
|
|
|
if (RuntimePlatformServicesInitializer == null) |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("No runtime platform services configured."); |
|
|
|
} |
|
|
|
|
|
|
|
if (WindowingSubsystem == null) |
|
|
|
if (WindowingSubsystemInitializer == null) |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("No windowing system configured."); |
|
|
|
} |
|
|
|
|
|
|
|
if (RenderingSubsystem == null) |
|
|
|
if (RenderingSubsystemInitializer == null) |
|
|
|
{ |
|
|
|
throw new InvalidOperationException("No rendering system configured."); |
|
|
|
} |
|
|
|
|
|
|
|
Instance.RegisterServices(); |
|
|
|
RuntimePlatformServices(); |
|
|
|
WindowingSubsystem(); |
|
|
|
RenderingSubsystem(); |
|
|
|
RuntimePlatformServicesInitializer(); |
|
|
|
WindowingSubsystemInitializer(); |
|
|
|
RenderingSubsystemInitializer(); |
|
|
|
Instance.Initialize(); |
|
|
|
AfterSetupCallback(Self); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|