more widget host work
This commit is contained in:
@@ -47,12 +47,11 @@ public partial class App :
|
||||
services.AddSingleton<DesktopBar>();
|
||||
services.AddContentTemplate<WidgetBarViewModel, WidgetBarView>();
|
||||
|
||||
services.AddTransient<IWidgetServiceCollection>(provider =>
|
||||
new WidgetServiceCollection(services =>
|
||||
services.AddTransient<IProxyServiceCollection<IWidgetBuilder>>(provider =>
|
||||
new ProxyServiceCollection<IWidgetBuilder>(services =>
|
||||
{
|
||||
services.AddSingleton<IDispatcher, Dispatcher>();
|
||||
services.AddTransient<ITemplateFactory, TemplateFactory>();
|
||||
|
||||
services.AddScoped<IVirtualKeyboard, VirtualKeyboard>();
|
||||
|
||||
services.AddHandler<KeyAcceleratorHandler>();
|
||||
|
||||
@@ -12,9 +12,12 @@ public static class IServiceCollectionExtensions
|
||||
public static IServiceCollection AddDefault(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IServiceFactory>(provider =>
|
||||
new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!)));
|
||||
new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!)));
|
||||
|
||||
services.AddSingleton<IMediator, Mediator>();
|
||||
services.AddSingleton<IProxyService<IMediator>>(provider =>
|
||||
new ProxyService<IMediator>(provider.GetRequiredService<IMediator>()));
|
||||
|
||||
services.AddSingleton<IDisposer, Disposer>();
|
||||
|
||||
services.AddTransient<IInitializer, WidgetInitializer>();
|
||||
@@ -23,6 +26,7 @@ public static class IServiceCollectionExtensions
|
||||
services.AddHandler<WidgetEnumerationHandler>();
|
||||
services.AddHandler<WidgetAssemblyHandler>();
|
||||
services.AddHandler<WidgetHandler>();
|
||||
services.AddHandler<WidgetHostHander>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IProxyService<TService>
|
||||
{
|
||||
TService Proxy { get; }
|
||||
}
|
||||
+2
-2
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IWidgetServiceCollection
|
||||
public interface IProxyServiceCollection<T>
|
||||
{
|
||||
IServiceCollection Services { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ProxyService<TService>(TService proxy) :
|
||||
IProxyService<TService>
|
||||
{
|
||||
public TService Proxy { get; private set; } = proxy;
|
||||
}
|
||||
+3
-3
@@ -2,10 +2,10 @@
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class WidgetServiceCollection :
|
||||
IWidgetServiceCollection
|
||||
public class ProxyServiceCollection<T> :
|
||||
IProxyServiceCollection<T>
|
||||
{
|
||||
public WidgetServiceCollection(Action<IServiceCollection> configureDelegate)
|
||||
public ProxyServiceCollection(Action<IServiceCollection> configureDelegate)
|
||||
{
|
||||
Services = new ServiceCollection();
|
||||
configureDelegate.Invoke(Services);
|
||||
@@ -4,9 +4,9 @@ namespace Hyperbar;
|
||||
|
||||
public interface IWidgetBuilder
|
||||
{
|
||||
IWidgetBuilder ConfigureServices(Action<IServiceCollection> configureDelegate);
|
||||
|
||||
IWidgetHost Build();
|
||||
|
||||
IWidgetBuilder ConfigureServices(Action<IServiceCollection> configureDelegate);
|
||||
}
|
||||
|
||||
public interface IWidgetBuilder<TConfiguration> :
|
||||
|
||||
@@ -2,5 +2,9 @@
|
||||
|
||||
public interface IWidgetHost
|
||||
{
|
||||
IServiceProvider Services { get; }
|
||||
|
||||
Task StartAsync();
|
||||
|
||||
Task StopAsync();
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IWidgetServiceBuilder
|
||||
{
|
||||
void ConfigureWidgetServices(IWidgetServiceCollection widgetServices);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public record Started<TValue>(TValue Value) : INotification;
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public record Stopped<TValue>(TValue Value) : INotification;
|
||||
@@ -5,37 +5,32 @@ using System.Reflection;
|
||||
|
||||
namespace Hyperbar.Widgets;
|
||||
|
||||
public class WidgetBuilder<TConfiguration> :
|
||||
IWidgetBuilder<TConfiguration>,
|
||||
IWidgetServiceBuilder
|
||||
public class WidgetBuilder<TConfiguration>(TConfiguration configuration) :
|
||||
IWidgetBuilder<TConfiguration>
|
||||
where TConfiguration :
|
||||
WidgetConfiguration,
|
||||
new()
|
||||
{
|
||||
private readonly IHostBuilder hostBuilder;
|
||||
|
||||
public WidgetBuilder(TConfiguration configuration)
|
||||
{
|
||||
hostBuilder = new HostBuilder()
|
||||
.UseContentRoot(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||
private readonly IHostBuilder hostBuilder = new HostBuilder()
|
||||
.UseContentRoot(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||
Assembly.GetEntryAssembly()?.GetName().Name!), true)
|
||||
.ConfigureAppConfiguration(config =>
|
||||
{
|
||||
config.AddJsonFile("Settings.json", true, true);
|
||||
})
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
services.AddScoped<IServiceFactory>(provider =>
|
||||
new ServiceFactory((type, parameters) =>
|
||||
ActivatorUtilities.CreateInstance(provider, type, parameters!)));
|
||||
.ConfigureAppConfiguration(config =>
|
||||
{
|
||||
config.AddJsonFile("Settings.json", true, true);
|
||||
})
|
||||
.ConfigureServices((context, services) =>
|
||||
{
|
||||
services.AddScoped<IServiceFactory>(provider =>
|
||||
new ServiceFactory((type, parameters) =>
|
||||
ActivatorUtilities.CreateInstance(provider, type, parameters!)));
|
||||
|
||||
services.AddHostedService<WidgetService>();
|
||||
services.AddHostedService<WidgetService>();
|
||||
|
||||
services.AddScoped<IMediator, Mediator>();
|
||||
services.AddScoped<IDisposer, Disposer>();
|
||||
services.AddConfiguration<TConfiguration>(configuration);
|
||||
});
|
||||
}
|
||||
services.AddScoped<IMediator, Mediator>();
|
||||
services.AddScoped<IDisposer, Disposer>();
|
||||
|
||||
services.AddConfiguration(configuration);
|
||||
});
|
||||
|
||||
public static IWidgetBuilder Configure(Action<TConfiguration> configurationDelegate)
|
||||
{
|
||||
@@ -57,7 +52,8 @@ public class WidgetBuilder<TConfiguration> :
|
||||
.GetResult();
|
||||
}
|
||||
|
||||
return new WidgetHost(host);
|
||||
return (IWidgetHost)ActivatorUtilities.CreateInstance(host.Services,
|
||||
typeof(WidgetHost), host);
|
||||
}
|
||||
|
||||
public IWidgetBuilder ConfigureServices(Action<IServiceCollection> configureDelegate)
|
||||
@@ -65,9 +61,4 @@ public class WidgetBuilder<TConfiguration> :
|
||||
hostBuilder.ConfigureServices(configureDelegate.Invoke);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void ConfigureWidgetServices(IWidgetServiceCollection widgetServices)
|
||||
{
|
||||
hostBuilder.ConfigureServices(services => services.AddRange(widgetServices.Services));
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Hyperbar;
|
||||
public class WidgetHost :
|
||||
IWidgetHost
|
||||
{
|
||||
public WidgetHost(IHost host)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,30 @@
|
||||
namespace Hyperbar;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public class WidgetHandler(IWidgetServiceCollection serviceCollection,
|
||||
namespace Hyperbar;
|
||||
|
||||
public class WidgetHandler(IProxyServiceCollection<IWidgetBuilder> typedServices,
|
||||
IServiceProvider provider,
|
||||
IMediator mediator) :
|
||||
INotificationHandler<Created<IWidget>>
|
||||
{
|
||||
public Task Handle(Created<IWidget> notification,
|
||||
public async Task Handle(Created<IWidget> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if(notification.Value is IWidget widget)
|
||||
{
|
||||
IWidgetBuilder widgetBuilder = widget.Create();
|
||||
if (widgetBuilder is IWidgetServiceBuilder serviceBuilder)
|
||||
IWidgetBuilder builder = widget.Create();
|
||||
|
||||
builder.ConfigureServices(args =>
|
||||
{
|
||||
serviceBuilder.ConfigureWidgetServices(serviceCollection);
|
||||
}
|
||||
args.AddTransient(_ => provider.GetRequiredService<IProxyService<IMediator>>());
|
||||
args.AddRange(typedServices.Services);
|
||||
});
|
||||
|
||||
widgetBuilder.Build();
|
||||
IWidgetHost host = builder.Build();
|
||||
|
||||
await host.StartAsync();
|
||||
await mediator.PublishAsync(new Created<IWidgetHost>(host),
|
||||
cancellationToken);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Hyperbar;
|
||||
public class WidgetHost(IHost host,
|
||||
IProxyService<IMediator> proxyMediator) :
|
||||
IWidgetHost
|
||||
{
|
||||
public IServiceProvider Services => host.Services;
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
if (proxyMediator.Proxy is IMediator mediator)
|
||||
{
|
||||
await mediator.PublishAsync(new Started<IWidgetHost>(this));
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StopAsync()
|
||||
{
|
||||
if (proxyMediator.Proxy is IMediator mediator)
|
||||
{
|
||||
await mediator.SendAsync(new Started<IWidgetHost>(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public class WidgetHostHander :
|
||||
INotificationHandler<Started<IWidgetHost>>,
|
||||
INotificationHandler<Stopped<IWidgetHost>>
|
||||
{
|
||||
public Task Handle(Started<IWidgetHost> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task Handle(Stopped<IWidgetHost> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user