Add KeyBindingBehavior
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
@@ -35,12 +36,14 @@ public class ComponentInitializer(IEnumerable<IComponent> components,
|
||||
provider.GetRequiredService<IComponentScopeProvider>());
|
||||
|
||||
services.AddRange(typedServices.Services);
|
||||
|
||||
services.AddSingleton(new ComponentScope(component.GetType().Name));
|
||||
});
|
||||
|
||||
IComponentHost host = builder.Build();
|
||||
|
||||
scopes.Add(component.GetType().Name,
|
||||
host.Services.GetRequiredService<IServiceProvider>());
|
||||
scopes.Add(new ComponentScopeDescriptor(component.GetType().Name,
|
||||
provider.GetRequiredService<IServiceProvider>()));
|
||||
|
||||
hosts.Add(host);
|
||||
await host.StartAsync();
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record ComponentScope(string Name);
|
||||
@@ -1,4 +1,4 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class ComponentScopeCollection : Dictionary<string, IServiceProvider>,
|
||||
public class ComponentScopeCollection : List<ComponentScopeDescriptor>,
|
||||
IComponentScopeCollection;
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public record ComponentScopeDescriptor(string Key, IServiceProvider Services);
|
||||
@@ -3,10 +3,9 @@
|
||||
public class ComponentScopeProvider(IComponentScopeCollection scopes) :
|
||||
IComponentScopeProvider
|
||||
{
|
||||
public IServiceProvider? Get(string name)
|
||||
public ComponentScopeDescriptor? Get(string key)
|
||||
{
|
||||
return scopes.TryGetValue(name,
|
||||
out IServiceProvider? scope) ? scope : default;
|
||||
return scopes.FirstOrDefault(x => x.Key == key) is ComponentScopeDescriptor
|
||||
descriptor ? descriptor : default;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class Configuration<TConfiguration>(string section,
|
||||
public class ConfigurationDescriptor<TConfiguration>(string section,
|
||||
IConfigurationReader<TConfiguration> reader) :
|
||||
IConfiguration<TConfiguration>
|
||||
IConfigurationDescriptor<TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
{
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Xml.Linq;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class ContentTemplateDescriptorProvider(IEnumerable<IContentTemplateDescriptor> contentTemplates) :
|
||||
IContentTemplateDescriptorProvider
|
||||
|
||||
@@ -50,9 +50,10 @@ public class DefaultBuilder :
|
||||
|
||||
services.AddTransient<INavigationScope, NavigationScope>();
|
||||
|
||||
services.AddSingleton(new ComponentScope("Root"));
|
||||
services.AddScoped<IComponentScopeCollection, ComponentScopeCollection>(provider => new ComponentScopeCollection
|
||||
{
|
||||
{ "Default", provider.GetRequiredService<IServiceProvider>() }
|
||||
new ComponentScopeDescriptor("Root", provider.GetRequiredService<IServiceProvider>())
|
||||
});
|
||||
|
||||
services.AddTransient<IComponentScopeProvider, ComponentScopeProvider>();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IComponentScopeCollection :
|
||||
IDictionary<string, IServiceProvider>;
|
||||
IList<ComponentScopeDescriptor>;
|
||||
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
|
||||
public interface IComponentScopeProvider
|
||||
{
|
||||
IServiceProvider? Get(string name);
|
||||
ComponentScopeDescriptor? Get(string key);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public interface IConfiguration<out TConfiguration>
|
||||
public interface IConfigurationDescriptor<out TConfiguration>
|
||||
where TConfiguration :
|
||||
class
|
||||
{
|
||||
@@ -3,9 +3,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.Extensions.FileProviders.Physical;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using Toolkit.Foundation;
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
@@ -145,11 +143,11 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
|
||||
|
||||
services.TryAddKeyedTransient<IConfiguration<TConfiguration>>(section, (provider, key) =>
|
||||
new Configuration<TConfiguration>(section, provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(key)));
|
||||
services.TryAddKeyedTransient<IConfigurationDescriptor<TConfiguration>>(section, (provider, key) =>
|
||||
new ConfigurationDescriptor<TConfiguration>(section, provider.GetRequiredKeyedService<IConfigurationReader<TConfiguration>>(key)));
|
||||
|
||||
services.AddTransient(provider => provider.GetRequiredKeyedService<IConfiguration<TConfiguration>>(section));
|
||||
services.AddTransient(provider => provider.GetRequiredKeyedService<IConfiguration<TConfiguration>>(section).Value);
|
||||
services.AddTransient(provider => provider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section));
|
||||
services.AddTransient(provider => provider.GetRequiredKeyedService<IConfigurationDescriptor<TConfiguration>>(section).Value);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -8,10 +8,10 @@ public class NavigateBackHandler(IComponentScopeProvider provider) :
|
||||
public async Task Handle(NavigateBack args,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (provider.Get(args.Scope ?? "Default")
|
||||
is IServiceProvider scope)
|
||||
if (provider.Get(args.Scope ?? "Root")
|
||||
is ComponentScopeDescriptor descriptor)
|
||||
{
|
||||
if (scope.GetService<INavigationScope>() is INavigationScope navigationScope)
|
||||
if (descriptor?.Services?.GetService<INavigationScope>() is INavigationScope navigationScope)
|
||||
{
|
||||
await navigationScope.NavigateBackAsync(args.Context, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -2,16 +2,17 @@
|
||||
|
||||
namespace Toolkit.Foundation;
|
||||
|
||||
public class NavigateHandler(IComponentScopeProvider provider) :
|
||||
public class NavigateHandler(ComponentScope scope,
|
||||
IComponentScopeProvider provider) :
|
||||
INotificationHandler<Navigate>
|
||||
{
|
||||
public async Task Handle(Navigate args,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (provider.Get(args.Scope ?? "Default")
|
||||
is IServiceProvider scope)
|
||||
if (provider.Get(args.Scope ?? scope.Name)
|
||||
is ComponentScopeDescriptor descriptor)
|
||||
{
|
||||
if (scope.GetService<INavigationScope>() is INavigationScope navigationScope)
|
||||
if (descriptor?.Services?.GetService<INavigationScope>() is INavigationScope navigationScope)
|
||||
{
|
||||
await navigationScope.NavigateAsync(args.Route, args.Sender,
|
||||
args.Context, args.Navigated, args.Parameters, cancellationToken);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Avalonia.Xaml.Interactivity;
|
||||
|
||||
namespace Toolkit.UI.Avalonia;
|
||||
|
||||
public class ComparisonCondition :
|
||||
AvaloniaObject,
|
||||
ICondition
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Xaml.Interactivity;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Toolkit.UI.Avalonia;
|
||||
|
||||
public class KeyBindingBehavior :
|
||||
Trigger<InputElement>,
|
||||
ICommand
|
||||
{
|
||||
public static readonly StyledProperty<KeyBinding> KeyBindingProperty =
|
||||
AvaloniaProperty.Register<KeyBindingBehavior, KeyBinding>(nameof(KeyBinding));
|
||||
|
||||
public KeyBinding KeyBinding
|
||||
{
|
||||
get => GetValue(KeyBindingProperty);
|
||||
set => SetValue(KeyBindingProperty, value);
|
||||
}
|
||||
|
||||
public event EventHandler? CanExecuteChanged;
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
if (KeyBinding != null)
|
||||
{
|
||||
KeyBinding.Command = this;
|
||||
AssociatedObject?.KeyBindings.Add(KeyBinding);
|
||||
}
|
||||
|
||||
base.OnAttached();
|
||||
}
|
||||
|
||||
public bool CanExecute(object? parameter) => true;
|
||||
|
||||
public void Execute(object? parameter) =>
|
||||
Interaction.ExecuteActions(AssociatedObject, Actions, null);
|
||||
}
|
||||
Reference in New Issue
Block a user