From 08ac5ca654033a08ab407408373ee02edce77031 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 27 Jul 2024 22:29:26 +0200 Subject: [PATCH] Refactored message bus handling to the base component --- .../Blocks/ConfigurationOption.razor.cs | 42 +---------------- .../Blocks/ConfigurationSelect.razor.cs | 42 +---------------- .../Blocks/ConfigurationSlider.razor | 4 +- .../Blocks/ConfigurationSlider.razor.cs | 46 +------------------ .../Components/ConfigurationBase.razor.cs | 46 ++++++++++++++++++- 5 files changed, 50 insertions(+), 130 deletions(-) diff --git a/app/MindWork AI Studio/Components/Blocks/ConfigurationOption.razor.cs b/app/MindWork AI Studio/Components/Blocks/ConfigurationOption.razor.cs index 874fbd38..4ace2801 100644 --- a/app/MindWork AI Studio/Components/Blocks/ConfigurationOption.razor.cs +++ b/app/MindWork AI Studio/Components/Blocks/ConfigurationOption.razor.cs @@ -7,7 +7,7 @@ namespace AIStudio.Components.Blocks; /// /// Configuration component for any boolean option. /// -public partial class ConfigurationOption : ConfigurationBase, IMessageBusReceiver +public partial class ConfigurationOption : ConfigurationBase { /// /// Text to display when the option is true. @@ -32,25 +32,6 @@ public partial class ConfigurationOption : ConfigurationBase, IMessageBusReceive /// [Parameter] public Action StateUpdate { get; set; } = _ => { }; - - /// - /// Is the option disabled? - /// - [Parameter] - public Func Disabled { get; set; } = () => false; - - #region Overrides of ComponentBase - - protected override async Task OnInitializedAsync() - { - // Register this component with the message bus: - this.MessageBus.RegisterComponent(this); - this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]); - - await base.OnInitializedAsync(); - } - - #endregion private async Task OptionChanged(bool updatedState) { @@ -58,25 +39,4 @@ public partial class ConfigurationOption : ConfigurationBase, IMessageBusReceive await this.SettingsManager.StoreSettings(); await this.InformAboutChange(); } - - #region Implementation of IMessageBusReceiver - - public Task ProcessMessage(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data) - { - switch (triggeredEvent) - { - case Event.CONFIGURATION_CHANGED: - this.StateHasChanged(); - break; - } - - return Task.CompletedTask; - } - - public Task ProcessMessageWithResult(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data) - { - return Task.FromResult(default); - } - - #endregion } \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Blocks/ConfigurationSelect.razor.cs b/app/MindWork AI Studio/Components/Blocks/ConfigurationSelect.razor.cs index 616a4864..b2cc4f49 100644 --- a/app/MindWork AI Studio/Components/Blocks/ConfigurationSelect.razor.cs +++ b/app/MindWork AI Studio/Components/Blocks/ConfigurationSelect.razor.cs @@ -9,7 +9,7 @@ namespace AIStudio.Components.Blocks; /// Configuration component for selecting a value from a list. /// /// The type of the value to select. -public partial class ConfigurationSelect : ConfigurationBase, IMessageBusReceiver +public partial class ConfigurationSelect : ConfigurationBase { /// /// The data to select from. @@ -29,25 +29,6 @@ public partial class ConfigurationSelect : ConfigurationBase, IMessageBusRece [Parameter] public Action SelectionUpdate { get; set; } = _ => { }; - /// - /// Is the selection component disabled? - /// - [Parameter] - public Func Disabled { get; set; } = () => false; - - #region Overrides of ComponentBase - - protected override async Task OnInitializedAsync() - { - // Register this component with the message bus: - this.MessageBus.RegisterComponent(this); - this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]); - - await base.OnInitializedAsync(); - } - - #endregion - private async Task OptionChanged(T updatedValue) { this.SelectionUpdate(updatedValue); @@ -56,25 +37,4 @@ public partial class ConfigurationSelect : ConfigurationBase, IMessageBusRece } private static string GetClass => $"{MARGIN_CLASS} rounded-lg"; - - #region Implementation of IMessageBusReceiver - - public Task ProcessMessage(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data) - { - switch (triggeredEvent) - { - case Event.CONFIGURATION_CHANGED: - this.StateHasChanged(); - break; - } - - return Task.CompletedTask; - } - - public Task ProcessMessageWithResult(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data) - { - return Task.FromResult(default); - } - - #endregion } \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor b/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor index a76bc21a..b42f4a4d 100644 --- a/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor +++ b/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor @@ -1,8 +1,8 @@ @typeparam T @inherits ConfigurationBase - - + + @this.Value() @this.Unit \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor.cs b/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor.cs index 8b34fc6a..0df46f3d 100644 --- a/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor.cs +++ b/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor.cs @@ -1,12 +1,10 @@ using System.Numerics; -using AIStudio.Tools; - using Microsoft.AspNetCore.Components; namespace AIStudio.Components.Blocks; -public partial class ConfigurationSlider : ConfigurationBase, IMessageBusReceiver where T : struct, INumber +public partial class ConfigurationSlider : ConfigurationBase where T : struct, INumber { /// /// The minimum value for the slider. @@ -44,52 +42,10 @@ public partial class ConfigurationSlider : ConfigurationBase, IMessageBusRece [Parameter] public Action ValueUpdate { get; set; } = _ => { }; - /// - /// Is the option disabled? - /// - [Parameter] - public Func Disabled { get; set; } = () => false; - - private MudSlider slider = null!; - - #region Overrides of ComponentBase - - protected override async Task OnInitializedAsync() - { - // Register this component with the message bus: - this.MessageBus.RegisterComponent(this); - this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]); - - await base.OnInitializedAsync(); - } - - #endregion - private async Task OptionChanged(T updatedValue) { this.ValueUpdate(updatedValue); await this.SettingsManager.StoreSettings(); await this.InformAboutChange(); } - - #region Implementation of IMessageBusReceiver - - public Task ProcessMessage(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data) - { - switch (triggeredEvent) - { - case Event.CONFIGURATION_CHANGED: - this.StateHasChanged(); - break; - } - - return Task.CompletedTask; - } - - public Task ProcessMessageWithResult(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data) - { - return Task.FromResult(default); - } - - #endregion } \ No newline at end of file diff --git a/app/MindWork AI Studio/Components/ConfigurationBase.razor.cs b/app/MindWork AI Studio/Components/ConfigurationBase.razor.cs index 03dc7dbc..bb1a7033 100644 --- a/app/MindWork AI Studio/Components/ConfigurationBase.razor.cs +++ b/app/MindWork AI Studio/Components/ConfigurationBase.razor.cs @@ -8,7 +8,7 @@ namespace AIStudio.Components; /// /// A base class for configuration options. /// -public partial class ConfigurationBase : ComponentBase +public partial class ConfigurationBase : ComponentBase, IMessageBusReceiver { /// /// The description of the option, i.e., the name. Should be @@ -23,6 +23,12 @@ public partial class ConfigurationBase : ComponentBase [Parameter] public string OptionHelp { get; set; } = string.Empty; + /// + /// Is the option disabled? + /// + [Parameter] + public Func Disabled { get; set; } = () => false; + [Inject] protected SettingsManager SettingsManager { get; init; } = null!; @@ -30,6 +36,44 @@ public partial class ConfigurationBase : ComponentBase protected MessageBus MessageBus { get; init; } = null!; protected const string MARGIN_CLASS = "mb-6"; + protected static readonly Dictionary SPELLCHECK_ATTRIBUTES = new(); + + #region Overrides of ComponentBase + + protected override async Task OnInitializedAsync() + { + // Configure the spellchecking for the instance name input: + this.SettingsManager.InjectSpellchecking(SPELLCHECK_ATTRIBUTES); + + // Register this component with the message bus: + this.MessageBus.RegisterComponent(this); + this.MessageBus.ApplyFilters(this, [], [ Event.CONFIGURATION_CHANGED ]); + + await base.OnInitializedAsync(); + } + + #endregion protected async Task InformAboutChange() => await this.MessageBus.SendMessage(this, Event.CONFIGURATION_CHANGED); + + #region Implementation of IMessageBusReceiver + + public Task ProcessMessage(ComponentBase? sendingComponent, Event triggeredEvent, TMsg? data) + { + switch (triggeredEvent) + { + case Event.CONFIGURATION_CHANGED: + this.StateHasChanged(); + break; + } + + return Task.CompletedTask; + } + + public Task ProcessMessageWithResult(ComponentBase? sendingComponent, Event triggeredEvent, TPayload? data) + { + return Task.FromResult(default); + } + + #endregion } \ No newline at end of file