More refactoring

This commit is contained in:
TheXamlGuy
2024-01-11 19:52:48 +00:00
parent 7ccfedb5e2
commit 814c806240
16 changed files with 125 additions and 78 deletions
@@ -6,5 +6,7 @@ namespace Hyperbar.Windows.Primary;
[JsonDerivedType(typeof(ProcessCommandConfiguration), typeDiscriminator: "ProcessCommand")]
public class PrimaryCommandConfiguration
{
public string? Icon { get; set; }
public required string Id { get; set; }
public required string Icon { get; set; }
}
@@ -3,7 +3,7 @@
public class KeyAcceleratorCommandConfiguration :
PrimaryCommandConfiguration
{
public int Key { get; set; }
public required int Key { get; set; }
public int[]? Modifiers { get; set; }
}
@@ -5,6 +5,6 @@ public class PrimaryWidgetConfiguration :
{
public static PrimaryWidgetConfiguration Defaults => new()
{
new KeyAcceleratorCommandConfiguration { Icon = "\uE720", Key = 91, Modifiers = [] }
new KeyAcceleratorCommandConfiguration { Id = $"{Guid.NewGuid()}", Icon = "\uE720", Key = 91, Modifiers = [] }
};
}
@@ -1,22 +0,0 @@
namespace Hyperbar.Windows.Primary;
public class PrimaryWidgetConfigurationChangedHandler :
INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>>
{
private readonly IMediator mediator;
private readonly IEnumerable<IWidgetComponentViewModel> items;
public PrimaryWidgetConfigurationChangedHandler(IMediator mediator,
IEnumerable<IWidgetComponentViewModel> items)
{
this.mediator = mediator;
this.items = items;
}
public async ValueTask Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
CancellationToken cancellationToken)
{
await mediator.PublishAsync(new CollectionChanged<IEnumerable<IWidgetComponentViewModel>>(items),
cancellationToken);
}
}
@@ -6,9 +6,11 @@ namespace Hyperbar.Windows.Primary;
public class PrimaryWidgetProvider :
IWidgetProvider
{
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
services.AddConfiguration<PrimaryWidgetConfiguration>()
.AddHandler<WidgetComponentMapping>()
.AddHandler<PrimaryWidgetConfigurationChangedHandler>()
.AddWidgetTemplate<PrimaryWidgetViewModel>();
.AddTransient<IFactory<IEnumerable<IWidgetComponentViewModel>>, WidgetComponentViewModelFactory>()
.AddWidgetTemplate<PrimaryWidgetViewModel>()
.AddNotificationPipeline<ConfigurationChanged<PrimaryWidgetConfiguration>,
ValueChanging<IEnumerable<IWidgetComponentViewModel>>>();
}
@@ -4,8 +4,8 @@ namespace Hyperbar.Windows.Primary;
public class PrimaryWidgetViewModel(ITemplateFactory templateFactory,
IServiceFactory serviceFactory,
IMediator mediator,
IEnumerable<IWidgetComponentViewModel> items) :
ObservableCollectionViewModel<IWidgetComponentViewModel>(serviceFactory, mediator, items),
IFactory<IEnumerable<IWidgetComponentViewModel>> factory) :
ObservableCollectionViewModel<IWidgetComponentViewModel>(serviceFactory, mediator, factory),
IWidgetViewModel,
ITemplatedViewModel
{
@@ -3,5 +3,5 @@
public class ProcessCommandConfiguration :
PrimaryCommandConfiguration
{
public string? Path { get; set; }
public required string Path { get; set; }
}
@@ -1,25 +0,0 @@
namespace Hyperbar.Windows.Primary;
public class WidgetComponentMapping(PrimaryWidgetConfiguration configuration,
IServiceFactory service,
IMediator mediator) :
IMappingHandler<PrimaryWidgetConfiguration, IEnumerable<IWidgetComponentViewModel>>
{
public IEnumerable<IWidgetComponentViewModel> Handle()
{
foreach (var item in configuration)
{
if (item is KeyAcceleratorCommandConfiguration keyAcceleratorCommandConfiguration)
{
yield return service.Create<WidgetButtonViewModel>(keyAcceleratorCommandConfiguration.Icon, new Action(async () =>
await mediator.SendAsync(new KeyAcceleratorRequest((VirtualKey)keyAcceleratorCommandConfiguration.Key,
keyAcceleratorCommandConfiguration.Modifiers?.Select(modifier => (VirtualKey)modifier).ToArray()))));
}
if (item is ProcessCommandConfiguration processCommandConfiguration)
{
}
}
}
}
@@ -0,0 +1,26 @@
namespace Hyperbar.Windows.Primary;
public class WidgetComponentViewModelFactory(PrimaryWidgetConfiguration configuration,
IServiceFactory service,
IMediator mediator) :
IFactory<IEnumerable<IWidgetComponentViewModel>>
{
public IEnumerable<IWidgetComponentViewModel> Create()
{
foreach (PrimaryCommandConfiguration item in configuration)
{
if (item is KeyAcceleratorCommandConfiguration keyAcceleratorCommandConfiguration)
{
yield return service.Create<WidgetButtonViewModel>(keyAcceleratorCommandConfiguration.Icon, new Action(async () =>
await mediator.SendAsync(new KeyAcceleratorRequest((VirtualKey)keyAcceleratorCommandConfiguration.Key,
keyAcceleratorCommandConfiguration.Modifiers?.Select(modifier => (VirtualKey)modifier).ToArray()))));
}
if (item is ProcessCommandConfiguration processCommandConfiguration)
{
yield return service.Create<WidgetButtonViewModel>(processCommandConfiguration.Icon, new Action(async () =>
await mediator.SendAsync(new ProcessRequest(processCommandConfiguration.Path))));
}
}
}
}