more widget host work
This commit is contained in:
@@ -47,12 +47,11 @@ public partial class App :
|
|||||||
services.AddSingleton<DesktopBar>();
|
services.AddSingleton<DesktopBar>();
|
||||||
services.AddContentTemplate<WidgetBarViewModel, WidgetBarView>();
|
services.AddContentTemplate<WidgetBarViewModel, WidgetBarView>();
|
||||||
|
|
||||||
services.AddTransient<IWidgetServiceCollection>(provider =>
|
services.AddTransient<IProxyServiceCollection<IWidgetBuilder>>(provider =>
|
||||||
new WidgetServiceCollection(services =>
|
new ProxyServiceCollection<IWidgetBuilder>(services =>
|
||||||
{
|
{
|
||||||
services.AddSingleton<IDispatcher, Dispatcher>();
|
services.AddSingleton<IDispatcher, Dispatcher>();
|
||||||
services.AddTransient<ITemplateFactory, TemplateFactory>();
|
services.AddTransient<ITemplateFactory, TemplateFactory>();
|
||||||
|
|
||||||
services.AddScoped<IVirtualKeyboard, VirtualKeyboard>();
|
services.AddScoped<IVirtualKeyboard, VirtualKeyboard>();
|
||||||
|
|
||||||
services.AddHandler<KeyAcceleratorHandler>();
|
services.AddHandler<KeyAcceleratorHandler>();
|
||||||
|
|||||||
@@ -12,9 +12,12 @@ public static class IServiceCollectionExtensions
|
|||||||
public static IServiceCollection AddDefault(this IServiceCollection services)
|
public static IServiceCollection AddDefault(this IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddSingleton<IServiceFactory>(provider =>
|
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<IMediator, Mediator>();
|
||||||
|
services.AddSingleton<IProxyService<IMediator>>(provider =>
|
||||||
|
new ProxyService<IMediator>(provider.GetRequiredService<IMediator>()));
|
||||||
|
|
||||||
services.AddSingleton<IDisposer, Disposer>();
|
services.AddSingleton<IDisposer, Disposer>();
|
||||||
|
|
||||||
services.AddTransient<IInitializer, WidgetInitializer>();
|
services.AddTransient<IInitializer, WidgetInitializer>();
|
||||||
@@ -23,6 +26,7 @@ public static class IServiceCollectionExtensions
|
|||||||
services.AddHandler<WidgetEnumerationHandler>();
|
services.AddHandler<WidgetEnumerationHandler>();
|
||||||
services.AddHandler<WidgetAssemblyHandler>();
|
services.AddHandler<WidgetAssemblyHandler>();
|
||||||
services.AddHandler<WidgetHandler>();
|
services.AddHandler<WidgetHandler>();
|
||||||
|
services.AddHandler<WidgetHostHander>();
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Hyperbar;
|
||||||
|
|
||||||
|
public interface IProxyService<TService>
|
||||||
|
{
|
||||||
|
TService Proxy { get; }
|
||||||
|
}
|
||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Hyperbar;
|
namespace Hyperbar;
|
||||||
|
|
||||||
public interface IWidgetServiceCollection
|
public interface IProxyServiceCollection<T>
|
||||||
{
|
{
|
||||||
IServiceCollection Services { get; }
|
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;
|
namespace Hyperbar;
|
||||||
|
|
||||||
public class WidgetServiceCollection :
|
public class ProxyServiceCollection<T> :
|
||||||
IWidgetServiceCollection
|
IProxyServiceCollection<T>
|
||||||
{
|
{
|
||||||
public WidgetServiceCollection(Action<IServiceCollection> configureDelegate)
|
public ProxyServiceCollection(Action<IServiceCollection> configureDelegate)
|
||||||
{
|
{
|
||||||
Services = new ServiceCollection();
|
Services = new ServiceCollection();
|
||||||
configureDelegate.Invoke(Services);
|
configureDelegate.Invoke(Services);
|
||||||
@@ -4,9 +4,9 @@ namespace Hyperbar;
|
|||||||
|
|
||||||
public interface IWidgetBuilder
|
public interface IWidgetBuilder
|
||||||
{
|
{
|
||||||
IWidgetBuilder ConfigureServices(Action<IServiceCollection> configureDelegate);
|
|
||||||
|
|
||||||
IWidgetHost Build();
|
IWidgetHost Build();
|
||||||
|
|
||||||
|
IWidgetBuilder ConfigureServices(Action<IServiceCollection> configureDelegate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IWidgetBuilder<TConfiguration> :
|
public interface IWidgetBuilder<TConfiguration> :
|
||||||
|
|||||||
@@ -2,5 +2,9 @@
|
|||||||
|
|
||||||
public interface IWidgetHost
|
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;
|
namespace Hyperbar.Widgets;
|
||||||
|
|
||||||
public class WidgetBuilder<TConfiguration> :
|
public class WidgetBuilder<TConfiguration>(TConfiguration configuration) :
|
||||||
IWidgetBuilder<TConfiguration>,
|
IWidgetBuilder<TConfiguration>
|
||||||
IWidgetServiceBuilder
|
|
||||||
where TConfiguration :
|
where TConfiguration :
|
||||||
WidgetConfiguration,
|
WidgetConfiguration,
|
||||||
new()
|
new()
|
||||||
{
|
{
|
||||||
private readonly IHostBuilder hostBuilder;
|
private readonly IHostBuilder hostBuilder = new HostBuilder()
|
||||||
|
.UseContentRoot(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||||
public WidgetBuilder(TConfiguration configuration)
|
|
||||||
{
|
|
||||||
hostBuilder = new HostBuilder()
|
|
||||||
.UseContentRoot(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
||||||
Assembly.GetEntryAssembly()?.GetName().Name!), true)
|
Assembly.GetEntryAssembly()?.GetName().Name!), true)
|
||||||
.ConfigureAppConfiguration(config =>
|
.ConfigureAppConfiguration(config =>
|
||||||
{
|
{
|
||||||
config.AddJsonFile("Settings.json", true, true);
|
config.AddJsonFile("Settings.json", true, true);
|
||||||
})
|
})
|
||||||
.ConfigureServices((context, services) =>
|
.ConfigureServices((context, services) =>
|
||||||
{
|
{
|
||||||
services.AddScoped<IServiceFactory>(provider =>
|
services.AddScoped<IServiceFactory>(provider =>
|
||||||
new ServiceFactory((type, parameters) =>
|
new ServiceFactory((type, parameters) =>
|
||||||
ActivatorUtilities.CreateInstance(provider, type, parameters!)));
|
ActivatorUtilities.CreateInstance(provider, type, parameters!)));
|
||||||
|
|
||||||
services.AddHostedService<WidgetService>();
|
services.AddHostedService<WidgetService>();
|
||||||
|
|
||||||
services.AddScoped<IMediator, Mediator>();
|
services.AddScoped<IMediator, Mediator>();
|
||||||
services.AddScoped<IDisposer, Disposer>();
|
services.AddScoped<IDisposer, Disposer>();
|
||||||
services.AddConfiguration<TConfiguration>(configuration);
|
|
||||||
});
|
services.AddConfiguration(configuration);
|
||||||
}
|
});
|
||||||
|
|
||||||
public static IWidgetBuilder Configure(Action<TConfiguration> configurationDelegate)
|
public static IWidgetBuilder Configure(Action<TConfiguration> configurationDelegate)
|
||||||
{
|
{
|
||||||
@@ -57,7 +52,8 @@ public class WidgetBuilder<TConfiguration> :
|
|||||||
.GetResult();
|
.GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new WidgetHost(host);
|
return (IWidgetHost)ActivatorUtilities.CreateInstance(host.Services,
|
||||||
|
typeof(WidgetHost), host);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IWidgetBuilder ConfigureServices(Action<IServiceCollection> configureDelegate)
|
public IWidgetBuilder ConfigureServices(Action<IServiceCollection> configureDelegate)
|
||||||
@@ -65,9 +61,4 @@ public class WidgetBuilder<TConfiguration> :
|
|||||||
hostBuilder.ConfigureServices(configureDelegate.Invoke);
|
hostBuilder.ConfigureServices(configureDelegate.Invoke);
|
||||||
return this;
|
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) :
|
IMediator mediator) :
|
||||||
INotificationHandler<Created<IWidget>>
|
INotificationHandler<Created<IWidget>>
|
||||||
{
|
{
|
||||||
public Task Handle(Created<IWidget> notification,
|
public async Task Handle(Created<IWidget> notification,
|
||||||
CancellationToken cancellationToken)
|
CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if(notification.Value is IWidget widget)
|
if(notification.Value is IWidget widget)
|
||||||
{
|
{
|
||||||
IWidgetBuilder widgetBuilder = widget.Create();
|
IWidgetBuilder builder = widget.Create();
|
||||||
if (widgetBuilder is IWidgetServiceBuilder serviceBuilder)
|
|
||||||
|
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