using Microsoft.AspNetCore.Components; namespace AIStudio.Components; /// /// Configuration component for any boolean option. /// public partial class ConfigurationOption : ConfigurationBaseCore { /// /// Text to display when the option is true. /// [Parameter] public string LabelOn { get; set; } = string.Empty; /// /// Text to display when the option is false. /// [Parameter] public string LabelOff { get; set; } = string.Empty; /// /// The boolean state of the option. /// [Parameter] public Func State { get; set; } = () => false; /// /// An action which is called when the option is changed. /// [Parameter] public Action StateUpdate { get; set; } = _ => { }; /// /// Asks before the option takes the new state, e.g., whether the user really wants to turn off a protection. /// /// /// It gets the new state and returns false to keep the old one. /// [Parameter] public Func> ConfirmChange { get; set; } = _ => Task.FromResult(true); /// /// The key of the switch, which changes whenever a change was rejected. /// /// /// The switch shows the state the user clicked, until its value changes. A rejected change leaves the value /// as it was, so the switch is built anew to show it. /// private int switchKey; #region Overrides of ConfigurationBase /// protected override bool Stretch => true; /// protected override Variant Variant => Variant.Outlined; /// protected override string Label => this.OptionDescription; #endregion private async Task OptionChanged(bool updatedState) { if (!await this.ConfirmChange(updatedState)) { this.switchKey++; return; } this.StateUpdate(updatedState); await this.SettingsManager.StoreSettings(); await this.InformAboutChange(); } }