From 834114249ff53298e9e3280138ae3afd08e7eee4 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 27 Jul 2024 21:07:40 +0200 Subject: [PATCH] Added configuration component for numeric sliders --- .../Blocks/ConfigurationSlider.razor | 8 ++ .../Blocks/ConfigurationSlider.razor.cs | 95 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor create mode 100644 app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor.cs diff --git a/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor b/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor new file mode 100644 index 00000000..a76bc21a --- /dev/null +++ b/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor @@ -0,0 +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 new file mode 100644 index 00000000..8b34fc6a --- /dev/null +++ b/app/MindWork AI Studio/Components/Blocks/ConfigurationSlider.razor.cs @@ -0,0 +1,95 @@ +using System.Numerics; + +using AIStudio.Tools; + +using Microsoft.AspNetCore.Components; + +namespace AIStudio.Components.Blocks; + +public partial class ConfigurationSlider : ConfigurationBase, IMessageBusReceiver where T : struct, INumber +{ + /// + /// The minimum value for the slider. + /// + [Parameter] + public T Min { get; set; } = T.Zero; + + /// + /// The maximum value for the slider. + /// + [Parameter] + public T Max { get; set; } = T.One; + + /// + /// The step size for the slider. + /// + [Parameter] + public T Step { get; set; } = T.One; + + /// + /// The unit to display next to the slider's value. + /// + [Parameter] + public string Unit { get; set; } = string.Empty; + + /// + /// The value used for the slider. + /// + [Parameter] + public Func Value { get; set; } = () => T.Zero; + + /// + /// An action which is called when the option is changed. + /// + [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