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.ReactiveUI;
|
|
|
|
|
using Avalonia.Threading;
|
|
|
|
|
|
2024-02-16 02:23:58 +01:00
|
|
|
using InkForge.Desktop;
|
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()
|
|
|
|
|
.UseReactiveUI()
|
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!;
|
2024-02-08 01:19:26 +01:00
|
|
|
services
|
2024-02-08 00:54:45 +01:00
|
|
|
.AddSingleton(app)
|
|
|
|
|
.AddSingleton(app.ApplicationLifetime!)
|
|
|
|
|
.AddSingleton(app.PlatformSettings!)
|
|
|
|
|
.AddSingleton(dispatcher);
|
|
|
|
|
|
2024-02-08 01:19:26 +01:00
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
|
|
|
app.SetValue(App.ServiceProviderProperty, serviceProvider);
|
2024-02-16 02:23:58 +01:00
|
|
|
_ = new ServiceProviderDisposer(serviceProvider, dispatcher);
|
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();
|
2024-02-08 01:19:26 +01:00
|
|
|
ServiceCollection services = [];
|
2024-02-16 02:23:58 +01:00
|
|
|
services.AddSingleton<IConfiguration>(configuration);
|
|
|
|
|
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;
|
|
|
|
|
private readonly TaskCompletionSource<ValueTask> _shutdownTask = new();
|
|
|
|
|
|
|
|
|
|
public ServiceProviderDisposer(ServiceProvider serviceProvider, Dispatcher dispatcher)
|
|
|
|
|
{
|
|
|
|
|
dispatcher.ShutdownStarted += OnShutdownStarted;
|
|
|
|
|
dispatcher.ShutdownFinished += OnShutdownFinished;
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnShutdownFinished(object? sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_shutdownTask.Task.Result is { IsCompleted: false } disposeTask)
|
|
|
|
|
{
|
|
|
|
|
disposeTask.GetAwaiter().GetResult();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnShutdownStarted(object? sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
#pragma warning disable CA2012 // This will only ever be awaited once in ShutdownFinished
|
|
|
|
|
_shutdownTask.SetResult(_serviceProvider.DisposeAsync());
|
|
|
|
|
#pragma warning restore CA2012
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-08 00:54:45 +01:00
|
|
|
}
|