Improve sidebar nav

This commit is contained in:
TheXamlGuy
2024-06-23 17:25:59 +01:00
parent 574a9d0983
commit 590bc61d70
6 changed files with 45 additions and 17 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ public class Component :
this.builder = builder;
}
public static TComponent? Create<TComponent>(IServiceProvider provider,
public static TComponent? Register<TComponent>(IServiceProvider provider,
Action<IComponentBuilder> builderDelegate)
where TComponent : class, IComponent
{
+23 -13
View File
@@ -9,16 +9,27 @@ public class ComponentBuilder :
{
private readonly IHostBuilder hostBuilder;
private bool configurationRegistered;
private ContentRootConfiguration rootConfiguration = new();
public void SetRootConfiguration(Action<ContentRootConfiguration> configurationDelegate)
{
ContentRootConfiguration rootConfiguration = new();
configurationDelegate.Invoke(rootConfiguration);
this.rootConfiguration = rootConfiguration;
}
public void SetAppConfiguration(Action<ContentRootConfiguration> rootConfigurationDelegate)
{
ContentRootConfiguration rootConfiguration = new();
rootConfigurationDelegate.Invoke(rootConfiguration);
this.rootConfiguration = rootConfiguration;
}
private ComponentBuilder()
{
hostBuilder = new HostBuilder()
.UseContentRoot("Local", true)
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("Settings.json", true, true);
})
.ConfigureServices((context, services) =>
{
services.AddScoped<IComponentHost, ComponentHost>();
@@ -72,13 +83,6 @@ public class ComponentBuilder :
where TConfiguration :
ComponentConfiguration, new()
{
if (configurationRegistered)
{
return this;
}
configurationRegistered = true;
hostBuilder.AddConfiguration(section: section,
defaultConfiguration: configuration);
@@ -100,6 +104,12 @@ public class ComponentBuilder :
public IComponentHost Build()
{
hostBuilder.UseContentRoot(rootConfiguration.ContentRoot, true)
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile(rootConfiguration.JsonFileName, true, true);
});
IHost host = hostBuilder.Build();
return host.Services.GetRequiredService<IComponentHost>();
}
+2 -1
View File
@@ -8,7 +8,7 @@ public class ComponentFactory(IServiceProvider provider,
IComponentFactory
{
public IComponentHost? Create<TComponent, TConfiguration>(string name,
TConfiguration configuration,
TConfiguration? configuration = null,
Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent
where TConfiguration : ComponentConfiguration, new()
@@ -16,6 +16,7 @@ public class ComponentFactory(IServiceProvider provider,
if (provider.GetRequiredService<TComponent>() is TComponent component)
{
IComponentBuilder builder = component.Create();
builder.AddServices(services =>
{
services.AddTransient(_ =>
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public record ContentRootConfiguration
{
public string ContentRoot { get; set; } = "Local";
public string JsonFileName { get; set; } = "Settings.json";
}
+2 -1
View File
@@ -5,7 +5,8 @@ namespace Toolkit.Foundation;
public interface IComponentFactory
{
IComponentHost? Create<TComponent, TConfiguration>(string name,
TConfiguration configuration, Action<IServiceCollection>? servicesDelegate = null)
TConfiguration? configuration = null,
Action<IServiceCollection>? servicesDelegate = null)
where TComponent : IComponent
where TConfiguration : ComponentConfiguration, new();
}
@@ -6,3 +6,11 @@ public class NavigationViewItem :
protected override Type StyleKeyOverride =>
typeof(FluentAvalonia.UI.Controls.NavigationViewItem);
}
public class NavigationViewItemSeparator :
FluentAvalonia.UI.Controls.NavigationViewItemSeparator
{
protected override Type StyleKeyOverride =>
typeof(FluentAvalonia.UI.Controls.NavigationViewItemSeparator);
}