InkForge/app/InkForge.Desktop/App.axaml.cs

88 lines
2.5 KiB
C#
Raw Permalink Normal View History

2024-02-08 00:54:45 +01:00
using Avalonia;
2024-03-17 22:27:01 +01:00
using Avalonia.Controls.Templates;
2024-02-08 00:54:45 +01:00
using Avalonia.Markup.Xaml;
2024-03-17 22:27:01 +01:00
using DynamicData;
2024-02-09 01:23:38 +01:00
2024-03-17 22:27:01 +01:00
using Microsoft.Extensions.Configuration;
2024-02-09 01:23:38 +01:00
using Microsoft.Extensions.DependencyInjection;
2024-03-17 22:27:01 +01:00
using Microsoft.Extensions.FileProviders;
2024-02-09 01:23:38 +01:00
2024-02-11 02:39:36 +01:00
using Splat;
using Splat.Microsoft.Extensions.DependencyInjection;
2024-02-16 02:23:58 +01:00
namespace InkForge.Desktop;
2024-02-08 00:54:45 +01:00
public partial class App : Application
{
2024-03-17 22:27:01 +01:00
public static readonly StyledProperty<IDataTemplate> AppDataTemplateProperty
= AvaloniaProperty.Register<App, IDataTemplate>(
name: nameof(AppDataTemplate),
coerce: OnAppDataTemplateChanged);
2024-02-11 02:39:36 +01:00
public static readonly StyledProperty<IServiceProvider> ServiceProviderProperty
= AvaloniaProperty.Register<App, IServiceProvider>(
name: nameof(ServiceProvider),
coerce: OnServiceProviderChanged);
2024-02-08 00:54:45 +01:00
2024-03-17 22:27:01 +01:00
public IDataTemplate AppDataTemplate => GetValue(AppDataTemplateProperty);
public IServiceProvider ServiceProvider => GetValue(ServiceProviderProperty);
2024-02-08 00:54:45 +01:00
2024-03-17 22:27:01 +01:00
public static void Configure(IServiceCollection services, ConfigurationManager configuration)
2024-02-11 02:39:36 +01:00
{
2024-03-17 22:27:01 +01:00
configuration.SetBasePath(AppContext.BaseDirectory);
configuration.AddJsonFile(
new ManifestEmbeddedFileProvider(typeof(App).Assembly),
"Properties/appsettings.json", false, false);
configuration.AddJsonFile(
Path.Combine(
Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData,
Environment.SpecialFolderOption.DoNotVerify),
"InkForge",
"usersettings.json"), true, true);
configuration.AddJsonFile("appsettings.json", true, true);
2024-02-11 02:39:36 +01:00
services.UseMicrosoftDependencyResolver();
Locator.CurrentMutable.InitializeSplat();
services.AddInkForge();
}
2024-02-08 00:54:45 +01:00
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
2024-03-17 22:27:01 +01:00
private static IDataTemplate OnAppDataTemplateChanged(AvaloniaObject @object, IDataTemplate dataTemplate)
2024-02-08 00:54:45 +01:00
{
2024-03-17 22:27:01 +01:00
var host = (IDataTemplateHost)@object;
var original = @object.GetValue(AppDataTemplateProperty);
if (original is null && dataTemplate is not null)
2024-02-09 01:23:38 +01:00
{
2024-03-17 22:27:01 +01:00
host.DataTemplates.Add(dataTemplate);
}
else if (original is not null)
{
if (dataTemplate is null)
{
host.DataTemplates.Remove(original);
}
else
{
host.DataTemplates.ReplaceOrAdd(original, dataTemplate);
}
}
return dataTemplate!;
2024-02-08 00:54:45 +01:00
}
2024-02-11 02:39:36 +01:00
private static IServiceProvider OnServiceProviderChanged(AvaloniaObject @object, IServiceProvider provider)
{
provider.UseMicrosoftDependencyResolver();
2024-03-17 22:27:01 +01:00
@object.SetValue(AppDataTemplateProperty, provider.GetRequiredService<IDataTemplate>());
2024-02-11 02:39:36 +01:00
return provider;
}
2024-02-08 00:54:45 +01:00
}