allow new items to be added to sub menus at runtime, from a config file

This commit is contained in:
TheXamlGuy
2024-01-18 22:28:53 +00:00
parent 78cedcdeb8
commit 2fed876182
8 changed files with 110 additions and 47 deletions
@@ -1,11 +1,28 @@
namespace Hyperbar.Windows.Primary;
public class WidgetComponentViewModelEnumerator(PrimaryWidgetConfiguration configuration,
IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory) :
IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory,
ICache<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> cache) :
IViewModelEnumerator<IWidgetComponentViewModel>
{
public IEnumerable<IWidgetComponentViewModel?> Next()
{
Stack<(Guid, List<PrimaryCommandConfiguration>)> stack = new();
stack.Push((Guid.Empty, configuration.Commands));
while (stack.Count > 0)
{
(Guid currentParentId, List<PrimaryCommandConfiguration> currentConfigurations) = stack.Pop();
foreach (PrimaryCommandConfiguration configuration in currentConfigurations)
{
cache.Add((currentParentId, configuration.Id), configuration);
if (configuration.Commands is not null && configuration.Commands.Count > 0)
{
stack.Push((configuration.Id, configuration.Commands));
}
}
}
foreach (PrimaryCommandConfiguration item in configuration.Commands.OrderBy(x => x.Order))
{
yield return factory.Create(item);