restructure project for part 2

This commit is contained in:
TheXamlGuy
2024-01-27 10:55:53 +00:00
parent a322893166
commit 48925b89ff
96 changed files with 383 additions and 351 deletions
@@ -0,0 +1,77 @@
using CommunityToolkit.Mvvm.Input;
using Hyperbar.Widget;
namespace Hyperbar.Widget.Primary.Windows;
public class WidgetComponentFactory(IServiceFactory factory,
IMediator mediator,
ICache<Guid, IWidgetComponentViewModel> cache) :
IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>
{
public IWidgetComponentViewModel? Create(PrimaryCommandConfiguration configuration)
{
WidgetComponentViewModel? viewModel = default;
if (configuration is KeyAcceleratorCommandConfiguration keyAcceleratorCommandConfiguration)
{
viewModel = factory.Create<WidgetButtonViewModel>(keyAcceleratorCommandConfiguration.Id,
keyAcceleratorCommandConfiguration.Text, keyAcceleratorCommandConfiguration.Icon,
new RelayCommand(async () => await mediator.SendAsync(new KeyAccelerator((VirtualKey)
keyAcceleratorCommandConfiguration.Key, keyAcceleratorCommandConfiguration.Modifiers?
.Select(modifier => (VirtualKey)modifier).ToArray()))));
}
if (configuration is ProcessCommandConfiguration processCommandConfiguration)
{
if (processCommandConfiguration.Commands is { Count: > 0 } childCommandConfigurations)
{
List<IWidgetComponentViewModel> childViewModels = [];
foreach (PrimaryCommandConfiguration childCommandConfiguration in childCommandConfigurations)
{
WidgetComponentViewModel? childViewModel = null;
if (childCommandConfiguration is ProcessCommandConfiguration childProcessCommandConfiguration)
{
childViewModel = factory.Create<WidgetMenuViewModel>(childProcessCommandConfiguration.Id,
childProcessCommandConfiguration.Icon, childProcessCommandConfiguration.Text,
new RelayCommand(async () => await mediator.SendAsync(new StartProcess(childProcessCommandConfiguration.Path))));
}
if (childCommandConfiguration is KeyAcceleratorCommandConfiguration childKeyAcceleratorCommandConfiguration)
{
childViewModel = factory.Create<WidgetMenuViewModel>(childKeyAcceleratorCommandConfiguration.Id,
childKeyAcceleratorCommandConfiguration.Text, childKeyAcceleratorCommandConfiguration.Icon,
new RelayCommand(async () =>
await mediator.SendAsync(new KeyAccelerator((VirtualKey)childKeyAcceleratorCommandConfiguration.Key,
childKeyAcceleratorCommandConfiguration.Modifiers?.Select(modifier => (VirtualKey)modifier).ToArray()))));
}
if (childViewModel is not null)
{
childViewModels.Add(childViewModel);
cache.Add(childCommandConfiguration.Id, childViewModel);
}
}
viewModel = factory.Create<WidgetSplitButtonViewModel>(childViewModels,
processCommandConfiguration.Id, processCommandConfiguration.Text,
processCommandConfiguration.Icon, new RelayCommand(async () =>
await mediator.SendAsync(new StartProcess(processCommandConfiguration.Path))));
}
else
{
viewModel = factory.Create<WidgetButtonViewModel>(processCommandConfiguration.Id,
processCommandConfiguration.Text, processCommandConfiguration.Icon, new RelayCommand(async () =>
await mediator.SendAsync(new StartProcess(processCommandConfiguration.Path))));
}
}
if (viewModel is not null)
{
cache.Add(configuration.Id, viewModel);
}
return viewModel;
}
}