InkForge/app/InkForge.Desktop/Program.cs

89 lines
2.6 KiB
C#
Raw Normal View History

2024-02-09 01:23:38 +01:00
using Avalonia;
2024-02-11 02:39:36 +01:00
using Avalonia.Controls.ApplicationLifetimes;
2024-02-08 00:54:45 +01:00
using Avalonia.Threading;
2024-02-16 02:23:58 +01:00
using InkForge.Desktop;
2024-03-17 22:27:01 +01:00
using InkForge.Desktop.Views;
2024-02-08 00:54:45 +01:00
2024-02-11 02:39:36 +01:00
using Microsoft.Extensions.Configuration;
2024-02-08 00:54:45 +01:00
using Microsoft.Extensions.DependencyInjection;
static class Program
{
[STAThread]
public static void Main(string[] args)
=> BuildAvaloniaApp()
2024-02-16 02:23:58 +01:00
.UseMicrosoftDependencyInjection(out var configuration)
.StartWithClassicDesktopLifetime(args, configuration.WithMicrosoftDependencyInjection);
2024-02-08 00:54:45 +01:00
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
2024-02-09 01:23:38 +01:00
.WithInterFont()
2024-02-08 00:54:45 +01:00
.LogToTrace();
2024-02-16 02:23:58 +01:00
private static void SetupApp(this IServiceCollection services, AppBuilder appBuilder)
2024-02-08 00:54:45 +01:00
{
var dispatcher = Dispatcher.UIThread;
var app = appBuilder.Instance!;
services
2024-02-08 00:54:45 +01:00
.AddSingleton(app)
.AddSingleton(app.ApplicationLifetime!)
.AddSingleton(app.PlatformSettings!)
.AddSingleton(dispatcher);
var serviceProvider = services.BuildServiceProvider();
app.SetValue(App.ServiceProviderProperty, serviceProvider);
2024-02-16 02:23:58 +01:00
_ = new ServiceProviderDisposer(serviceProvider, dispatcher);
2024-03-17 22:27:01 +01:00
_ = app.ApplicationLifetime switch
{
IClassicDesktopStyleApplicationLifetime desktop => desktop.MainWindow = new MainWindow(),
2024-05-02 21:44:13 +02:00
_ => throw new NotSupportedException(),
2024-03-17 22:27:01 +01:00
};
2024-02-08 00:54:45 +01:00
}
2024-02-16 02:23:58 +01:00
private static AppBuilder UseMicrosoftDependencyInjection(this AppBuilder builder, out ConfigurationManager configuration)
2024-02-08 00:54:45 +01:00
{
2024-02-16 02:23:58 +01:00
configuration = new();
ServiceCollection services = [];
2024-02-16 02:23:58 +01:00
services.AddSingleton<IConfiguration>(configuration);
2024-03-17 22:27:01 +01:00
App.Configure(services, configuration);
2024-02-11 02:39:36 +01:00
2024-02-16 02:23:58 +01:00
builder.AfterSetup(services.SetupApp);
2024-02-08 00:54:45 +01:00
return builder;
}
2024-02-16 02:23:58 +01:00
private static void WithMicrosoftDependencyInjection(this ConfigurationManager configuration, IClassicDesktopStyleApplicationLifetime lifetime)
{
configuration.AddCommandLine(lifetime.Args ?? []);
}
private class ServiceProviderDisposer
{
private readonly ServiceProvider _serviceProvider;
2024-02-26 18:08:18 +01:00
private ValueTask? _shutdownTask;
2024-02-16 02:23:58 +01:00
public ServiceProviderDisposer(ServiceProvider serviceProvider, Dispatcher dispatcher)
{
dispatcher.ShutdownFinished += OnShutdownFinished;
2024-02-26 18:08:18 +01:00
dispatcher.ShutdownStarted += OnShutdownStarted;
2024-02-16 02:23:58 +01:00
_serviceProvider = serviceProvider;
}
private void OnShutdownFinished(object? sender, EventArgs e)
{
2024-02-26 18:08:18 +01:00
if (_shutdownTask is { IsCompleted: false } disposeTask)
2024-02-16 02:23:58 +01:00
{
disposeTask.GetAwaiter().GetResult();
}
}
private void OnShutdownStarted(object? sender, EventArgs e)
{
#pragma warning disable CA2012 // This will only ever be awaited once in ShutdownFinished
2024-02-26 18:08:18 +01:00
_shutdownTask = _serviceProvider.DisposeAsync();
2024-02-16 02:23:58 +01:00
#pragma warning restore CA2012
}
}
2024-02-08 00:54:45 +01:00
}