using System.Numerics; using Microsoft.AspNetCore.Components; namespace AIStudio.Components; /// /// A number typed into a field, for a range too wide for a slider, e.g., a size limit in megabytes. /// public partial class ConfigurationNumber : ConfigurationBaseCore where T : struct, INumber, IMinMaxValue { /// /// The smallest allowed value. /// [Parameter] public T Min { get; set; } = T.MinValue; /// /// The largest allowed value. /// [Parameter] public T Max { get; set; } = T.MaxValue; /// /// The unit to display next to the value. /// [Parameter] public string Unit { get; set; } = string.Empty; /// /// The value used for the field. /// [Parameter] public Func Value { get; set; } = () => T.Zero; /// /// An action which is called when the option is changed. /// [Parameter] public Action ValueUpdate { get; set; } = _ => { }; #region Overrides of ConfigurationBase /// protected override bool Stretch => true; /// protected override Variant Variant => Variant.Outlined; protected override string Label => this.OptionDescription; #endregion #region Overrides of ComponentBase protected override async Task OnInitializedAsync() { await this.EnsureMinMax(); await base.OnInitializedAsync(); } protected override async Task OnParametersSetAsync() { await this.EnsureMinMax(); await base.OnParametersSetAsync(); } #endregion private async Task OptionChanged(T updatedValue) { this.ValueUpdate(updatedValue); await this.SettingsManager.StoreSettings(); await this.InformAboutChange(); } private async Task EnsureMinMax() { if (this.Value() < this.Min) await this.OptionChanged(this.Min); else if (this.Value() > this.Max) await this.OptionChanged(this.Max); } }