Rip out Microsoft's configuration and replace because it simply doesn't support polymorphism.... we are getting advanced boys!!!

This commit is contained in:
TheXamlGuy
2024-01-09 20:32:07 +00:00
parent e72d997563
commit 05b404d504
19 changed files with 247 additions and 135 deletions
@@ -1,5 +1,10 @@
namespace Hyperbar.Windows.Primary;
using System.Text.Json.Serialization;
public interface IPrimaryCommandConfiguration
namespace Hyperbar.Windows.Primary;
[JsonDerivedType(typeof(KeyAcceleratorCommandConfiguration), typeDiscriminator: "KeyAcceleratorCommand")]
[JsonDerivedType(typeof(ProcessCommandConfiguration), typeDiscriminator: "ProcessCommand")]
public class PrimaryCommandConfiguration
{
public string? Icon { get; set; }
}
@@ -1,11 +1,9 @@
namespace Hyperbar.Windows.Primary;
public class KeyAcceleratorCommandConfiguration :
IPrimaryCommandConfiguration
PrimaryCommandConfiguration
{
public string? Icon { get; set; }
public int Key { get; set; }
public string? Key { get; set; }
public string[]? Modifiers { get; set; }
}
public int[]? Modifiers { get; set; }
}
@@ -1,10 +1,10 @@
namespace Hyperbar.Windows.Primary;
public class PrimaryWidgetConfiguration :
List<KeyAcceleratorCommandConfiguration>
List<PrimaryCommandConfiguration>
{
public static PrimaryWidgetConfiguration Defaults => new()
{
new KeyAcceleratorCommandConfiguration { Icon = "\uE720", Key = "Test", Modifiers = ["Test", "Test"] }
new KeyAcceleratorCommandConfiguration { Icon = "\uE720", Key = 91, Modifiers = [] }
};
}
@@ -7,7 +7,7 @@ public class PrimaryWidgetProvider :
IWidgetProvider
{
public void Create(HostBuilderContext comtext, IServiceCollection services) =>
services.AddConfiguration< PrimaryWidgetConfiguration>(comtext.Configuration.GetSection(nameof(PrimaryWidgetConfiguration)))
services.AddConfiguration<PrimaryWidgetConfiguration>()
.AddHandler<WidgetComponentMapping>()
.AddHandler<PrimaryWidgetConfigurationChangedHandler>()
.AddWidgetTemplate<PrimaryWidgetViewModel>();
@@ -0,0 +1,7 @@
namespace Hyperbar.Windows.Primary;
public class ProcessCommandConfiguration :
PrimaryCommandConfiguration
{
public string? Path { get; set; }
}
@@ -1,5 +1,4 @@
namespace Hyperbar.Windows.Primary;
namespace Hyperbar.Windows.Primary;
public class WidgetComponentMapping(PrimaryWidgetConfiguration configuration,
IServiceFactory service,
@@ -8,12 +7,18 @@ public class WidgetComponentMapping(PrimaryWidgetConfiguration configuration,
{
public IEnumerable<IWidgetComponentViewModel> Handle()
{
foreach (IPrimaryCommandConfiguration item in configuration)
foreach (var item in configuration)
{
if (item is KeyAcceleratorCommandConfiguration keyAcceleratorCommand)
if (item is KeyAcceleratorCommandConfiguration keyAcceleratorCommandConfiguration)
{
yield return service.Create<WidgetButtonViewModel>(keyAcceleratorCommand.Icon, new Action(async () =>
await mediator.SendAsync(new KeyAcceleratorCommand(VirtualKey.LeftWindows))));
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)
{
}
}
}