using CommunityToolkit.Mvvm.Input; namespace Hyperbar.Widget.Primary.Windows; public class WidgetComponentFactory(IServiceFactory factory, IMediator mediator, ICache cache) : IFactory { public IWidgetComponentViewModel? Create(PrimaryCommandConfiguration configuration) { WidgetComponentViewModel? viewModel = default; if (configuration is KeyAcceleratorCommandConfiguration keyAcceleratorCommandConfiguration) { viewModel = factory.Create(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 childViewModels = []; foreach (PrimaryCommandConfiguration childCommandConfiguration in childCommandConfigurations) { WidgetComponentViewModel? childViewModel = null; if (childCommandConfiguration is ProcessCommandConfiguration childProcessCommandConfiguration) { childViewModel = factory.Create(childProcessCommandConfiguration.Id, childProcessCommandConfiguration.Icon, childProcessCommandConfiguration.Text, new RelayCommand(async () => await mediator.SendAsync(new StartProcess(childProcessCommandConfiguration.Path)))); } if (childCommandConfiguration is KeyAcceleratorCommandConfiguration childKeyAcceleratorCommandConfiguration) { childViewModel = factory.Create(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(childViewModels, processCommandConfiguration.Id, processCommandConfiguration.Text, processCommandConfiguration.Icon, new RelayCommand(async () => await mediator.SendAsync(new StartProcess(processCommandConfiguration.Path)))); } else { viewModel = factory.Create(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; } }