restructure project for part 2
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<UseRidGraph>true</UseRidGraph>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="System.Runtime.Caching" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Hyperbar.Widget\Hyperbar.Widget.csproj" />
|
||||
<ProjectReference Include="..\Hyperbar\Hyperbar.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Hyperbar.Widget.Primary.Windows;
|
||||
|
||||
public class KeyAcceleratorCommandConfiguration :
|
||||
PrimaryCommandConfiguration
|
||||
{
|
||||
public required int Key { get; set; }
|
||||
|
||||
public int[]? Modifiers { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Hyperbar.Widget.Primary.Windows;
|
||||
|
||||
[JsonDerivedType(typeof(KeyAcceleratorCommandConfiguration), typeDiscriminator: "KeyAcceleratorCommand")]
|
||||
[JsonDerivedType(typeof(ProcessCommandConfiguration), typeDiscriminator: "ProcessCommand")]
|
||||
public class PrimaryCommandConfiguration
|
||||
{
|
||||
public List<PrimaryCommandConfiguration> Commands { get; set; } = [];
|
||||
|
||||
public required string Icon { get; set; }
|
||||
|
||||
public required Guid Id { get; set; }
|
||||
|
||||
public required int Order { get; set; }
|
||||
|
||||
public required string Text { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Hyperbar.Widget;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Hyperbar.Widget.Primary.Windows;
|
||||
|
||||
public class PrimaryWidget :
|
||||
IWidget
|
||||
{
|
||||
public IWidgetBuilder Create() =>
|
||||
WidgetBuilder<PrimaryWidgetConfiguration>.Configure(args =>
|
||||
{
|
||||
args.Name = "Primary commands";
|
||||
args.Commands =
|
||||
[
|
||||
new KeyAcceleratorCommandConfiguration
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Order = 0,
|
||||
Text = "Test",
|
||||
Icon = "dd",
|
||||
Key = 1
|
||||
}
|
||||
];
|
||||
}).ConfigureServices(services =>
|
||||
{
|
||||
services.AddCache<(Guid ParentId, Guid Id), PrimaryCommandConfiguration>()
|
||||
.AddCache<Guid, IWidgetComponentViewModel>()
|
||||
.AddTransient<IProvider<PrimaryCommandConfiguration, IWidgetComponentViewModel?>, WidgetComponentProvider>()
|
||||
.AddTransient<IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?>, WidgetComponentFactory>()
|
||||
.AddWidgetTemplate<PrimaryWidgetViewModel>()
|
||||
.AddHandler<WidgetComponentViewModelEnumerator>()
|
||||
.AddHandler<PrimaryWidgetConfigurationHandler>();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using Hyperbar.Widget;
|
||||
|
||||
namespace Hyperbar.Widget.Primary.Windows;
|
||||
|
||||
public class PrimaryWidgetConfiguration :
|
||||
WidgetConfiguration
|
||||
{
|
||||
public List<PrimaryCommandConfiguration> Commands { get; set; } = [];
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using Hyperbar.Widget;
|
||||
|
||||
namespace Hyperbar.Widget.Primary.Windows;
|
||||
|
||||
public class PrimaryWidgetConfigurationHandler(IMediator mediator,
|
||||
PrimaryWidgetConfiguration configuration,
|
||||
IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory,
|
||||
IProvider<PrimaryCommandConfiguration, IWidgetComponentViewModel?> provider,
|
||||
ICache<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> cache) :
|
||||
INotificationHandler<ConfigurationChanged<PrimaryWidgetConfiguration>>
|
||||
{
|
||||
public async Task Handle(ConfigurationChanged<PrimaryWidgetConfiguration> notification,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration>> items = [];
|
||||
|
||||
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)
|
||||
{
|
||||
var key = (currentParentId, configuration.Id);
|
||||
items.Add(new KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration>(key, configuration));
|
||||
|
||||
if (configuration.Commands?.Count > 0)
|
||||
{
|
||||
stack.Push((configuration.Id, configuration.Commands));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HashSet<Guid> cacheIds = new(cache.Select(x => x.Key.Id));
|
||||
HashSet<Guid> itemIds = new(items.Select(x => x.Key.Id));
|
||||
|
||||
List<KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration>> movedItems =
|
||||
items.ExceptBy(cache.Select(x => new { x.Value.Order, x.Value.Id }), x =>
|
||||
new { x.Value.Order, x.Value.Id }).ToList();
|
||||
|
||||
foreach (KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> moved in movedItems)
|
||||
{
|
||||
if (moved.Value is PrimaryCommandConfiguration configuration &&
|
||||
provider.Get(configuration) is IWidgetComponentViewModel viewModel)
|
||||
{
|
||||
await mediator.PublishAsync(new Moved<IWidgetComponentViewModel>(configuration.Order, viewModel),
|
||||
moved.Key.ParentId == Guid.Empty ? nameof(PrimaryWidgetViewModel) : moved.Key.ParentId,
|
||||
cancellationToken);
|
||||
|
||||
cache.Remove(moved.Key);
|
||||
cache.Add(moved.Key, moved.Value);
|
||||
}
|
||||
}
|
||||
|
||||
List<KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration>> addedItems =
|
||||
items.ExceptBy(cacheIds.Select(x => x), x => x.Key.Id).ToList();
|
||||
|
||||
foreach (KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> added in addedItems)
|
||||
{
|
||||
if (added.Value is PrimaryCommandConfiguration configuration &&
|
||||
factory.Create(configuration) is IWidgetComponentViewModel viewModel)
|
||||
{
|
||||
await mediator.PublishAsync(
|
||||
new Inserted<IWidgetComponentViewModel>(configuration.Order, viewModel),
|
||||
added.Key.ParentId == Guid.Empty ? nameof(PrimaryWidgetViewModel) : added.Key.ParentId,
|
||||
cancellationToken);
|
||||
|
||||
cache.Add(added.Key, added.Value);
|
||||
}
|
||||
}
|
||||
|
||||
List<KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration>> removedItems =
|
||||
cache.ExceptBy(itemIds.Select(x => x), x => x.Key.Id).ToList();
|
||||
|
||||
foreach (KeyValuePair<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> removed in removedItems)
|
||||
{
|
||||
if (removed.Value is PrimaryCommandConfiguration configuration &&
|
||||
provider.Get(configuration) is IWidgetComponentViewModel viewModel)
|
||||
{
|
||||
await mediator.PublishAsync(
|
||||
new Removed<IWidgetComponentViewModel>(viewModel),
|
||||
removed.Key.ParentId == Guid.Empty ? nameof(PrimaryWidgetViewModel) : removed.Key.ParentId,
|
||||
cancellationToken);
|
||||
|
||||
cache.Remove(removed.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using Hyperbar.Widget;
|
||||
|
||||
namespace Hyperbar.Widget.Primary.Windows;
|
||||
|
||||
[NotificationHandler(nameof(PrimaryWidgetViewModel))]
|
||||
public class PrimaryWidgetViewModel(ITemplateFactory templateFactory,
|
||||
IServiceFactory serviceFactory,
|
||||
IMediator mediator,
|
||||
IDisposer disposer) :
|
||||
ObservableCollectionViewModel<IWidgetComponentViewModel>(serviceFactory, mediator, disposer),
|
||||
IWidgetViewModel,
|
||||
ITemplatedViewModel
|
||||
{
|
||||
public ITemplateFactory TemplateFactory => templateFactory;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Hyperbar.Widget.Primary.Windows;
|
||||
|
||||
public class ProcessCommandConfiguration :
|
||||
PrimaryCommandConfiguration
|
||||
{
|
||||
public required string Path { get; set; }
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Hyperbar.Widget;
|
||||
|
||||
namespace Hyperbar.Widget.Primary.Windows;
|
||||
|
||||
public class WidgetComponentProvider(ICache<Guid, IWidgetComponentViewModel> cache) :
|
||||
IProvider<PrimaryCommandConfiguration, IWidgetComponentViewModel?>
|
||||
{
|
||||
public IWidgetComponentViewModel? Get(PrimaryCommandConfiguration value)
|
||||
{
|
||||
if (cache.TryGetValue(value.Id, out IWidgetComponentViewModel? viewModel))
|
||||
{
|
||||
return viewModel;
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Hyperbar.Widget;
|
||||
|
||||
namespace Hyperbar.Widget.Primary.Windows;
|
||||
|
||||
public class WidgetComponentViewModelEnumerator(PrimaryWidgetConfiguration configuration,
|
||||
IMediator mediator,
|
||||
IFactory<PrimaryCommandConfiguration, IWidgetComponentViewModel?> factory,
|
||||
ICache<(Guid ParentId, Guid Id), PrimaryCommandConfiguration> cache) :
|
||||
INotificationHandler<Enumerate<IWidgetComponentViewModel>>
|
||||
{
|
||||
public async Task Handle(Enumerate<IWidgetComponentViewModel> notification, CancellationToken cancellationToken)
|
||||
{
|
||||
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))
|
||||
{
|
||||
if (factory.Create(item) is IWidgetComponentViewModel viewModel)
|
||||
{
|
||||
await mediator.PublishAsync(new Created<IWidgetComponentViewModel>(viewModel), nameof(PrimaryWidgetViewModel),
|
||||
cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user