using AIStudio.Tools.Pauses; using Microsoft.AspNetCore.Components; using Timer = System.Timers.Timer; namespace AIStudio.Components; /// /// One slider with two handles for a random pause, e.g., between two indexing steps: the shortest and the longest pause. /// /// /// The slider shows the bounds which apply, as PauseRange.GetEffective works them out, and stores /// them that way: pushing the lower handle past the upper one takes the upper one along. A bound /// which a configuration plugin locks keeps its value, its handle cannot move, and the option shows /// a lock. The handles move in steps of five seconds; a bound off these steps, e.g., one which a /// configuration plugin sets, applies as it is, and only its handle shows the nearest step. /// public partial class ConfigurationPauseRange : ConfigurationBaseCore { /// /// The seconds between two tick marks, and the step of the handles. /// private const int STEP_SECONDS = 5; /// /// Initializes the component, which shows the lock of the option while either bound is locked. /// public ConfigurationPauseRange() { this.IsLocked = () => this.IsMinimumLocked() || this.IsMaximumLocked(); } /// /// The shortest and the longest pause the feature allows. /// [Parameter, EditorRequired] public PauseLimits Limits { get; set; } = null!; /// /// The lower bound as set. /// [Parameter] public Func Minimum { get; set; } = () => 0; /// /// Sets the lower bound. /// [Parameter] public Action MinimumUpdate { get; set; } = _ => { }; /// /// Whether a configuration plugin locks the lower bound. /// [Parameter] public Func IsMinimumLocked { get; set; } = () => false; /// /// The upper bound as set. /// [Parameter] public Func Maximum { get; set; } = () => 0; /// /// Sets the upper bound. /// [Parameter] public Action MaximumUpdate { get; set; } = _ => { }; /// /// Whether a configuration plugin locks the upper bound. /// [Parameter] public Func IsMaximumLocked { get; set; } = () => false; /// /// Whether the bounds are settings, which a change stores and announces to the other options. /// /// /// Defaults to true. False for bounds which live elsewhere, e.g., in the session of an assistant. /// [Parameter] public bool IsSetting { get; set; } = true; /// /// Stores the settings once the user stopped moving a handle: storing them with every step /// would make the slider stutter, since the store runs a while. /// private readonly Timer storeTimer = new(TimeSpan.FromMilliseconds(500)) { AutoReset = false }; /// /// Whether a change waits for storeTimer. /// private bool isStorePending; private PauseRange EffectiveRange => PauseRange.GetEffective(this.Limits, this.Minimum(), this.IsMinimumLocked, this.Maximum(), this.IsMaximumLocked); /// /// The start of the track: the shortest pause, or the step right below it. /// private int TrackMin => this.Limits.ShortestSeconds / STEP_SECONDS * STEP_SECONDS; /// /// The end of the track: the longest pause, or the step right above it. /// private int TrackMax => (this.Limits.LongestSeconds + STEP_SECONDS - 1) / STEP_SECONDS * STEP_SECONDS; #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() { // Off the renderer, which stays free while the settings are stored: this.storeTimer.Elapsed += (_, _) => this.StoreAsync().Observe($"{nameof(ConfigurationPauseRange)}: storing the changed pause"); await base.OnInitializedAsync(); } #endregion #region Overrides of MSGComponentBase protected override void DisposeResources() { this.storeTimer.Stop(); this.storeTimer.Dispose(); // A change made right before closing still reaches the disk: if (this.isStorePending) this.StoreAsync().Observe($"{nameof(ConfigurationPauseRange)}: storing the changed pause while closing"); base.DisposeResources(); } #endregion private string GetRangeText(PauseRange range) { if (range.IsNone) return T("No pause"); return range.MinimumSeconds == range.MaximumSeconds ? string.Format(T("{0} seconds"), range.MinimumSeconds) : string.Format(T("{0} to {1} seconds"), range.MinimumSeconds, range.MaximumSeconds); } private void UpdateMinimum(int seconds) => this.ApplyBounds(seconds, this.Maximum()); private void UpdateMaximum(int seconds) => this.ApplyBounds(this.Minimum(), seconds); /// /// Sets both bounds as they apply, so that the handles show what is stored. /// /// /// A locked bound keeps its value, even when the slider reports another one, e.g., while it /// puts its handles in order. /// private void ApplyBounds(int minimumSeconds, int maximumSeconds) { var range = PauseRange.GetEffective(this.Limits, minimumSeconds, this.IsMinimumLocked, maximumSeconds, this.IsMaximumLocked); if (!this.IsMinimumLocked()) this.MinimumUpdate(range.MinimumSeconds); if (!this.IsMaximumLocked()) this.MaximumUpdate(range.MaximumSeconds); if (!this.IsSetting) return; this.isStorePending = true; this.storeTimer.Stop(); this.storeTimer.Start(); } private async Task StoreAsync() { this.isStorePending = false; await this.SettingsManager.StoreSettings(); await this.InformAboutChange(); } }