2026-10-30 20:01:57 +01:00
|
|
|
using System.Globalization;
|
2024-07-28 11:20:00 +02:00
|
|
|
using System.Numerics;
|
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
2024-08-21 08:30:01 +02:00
|
|
|
namespace AIStudio.Components;
|
2024-07-28 11:20:00 +02:00
|
|
|
|
2025-08-09 19:29:43 +02:00
|
|
|
public partial class ConfigurationSlider<T> : ConfigurationBaseCore where T : struct, INumber<T>
|
2024-07-28 11:20:00 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The minimum value for the slider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public T Min { get; set; } = T.Zero;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The maximum value for the slider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public T Max { get; set; } = T.One;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The step size for the slider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public T Step { get; set; } = T.One;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The unit to display next to the slider's value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Unit { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The value used for the slider.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<T> Value { get; set; } = () => T.Zero;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An action which is called when the option is changed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Action<T> ValueUpdate { get; set; } = _ => { };
|
|
|
|
|
|
2026-10-30 20:01:57 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Whether the option changes while the user drags the slider, or only once they release it.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Defaults to true. An option which changes the layout of the page, such as the interface size,
|
|
|
|
|
/// should wait for the release: a page which changes under the pointer moves the slider away from
|
|
|
|
|
/// it. The slider shows the value it is dragged to either way.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Immediate { get; set; } = true;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The value the user drags the slider to, while the option waits for the release, see Immediate.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private T? draggedValue;
|
|
|
|
|
|
2025-08-09 19:29:43 +02:00
|
|
|
#region Overrides of ConfigurationBase
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override bool Stretch => true;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected override Variant Variant => Variant.Outlined;
|
|
|
|
|
|
|
|
|
|
protected override string Label => this.OptionDescription;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-08-05 21:12:52 +02:00
|
|
|
#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
|
|
|
|
|
|
2024-07-28 11:20:00 +02:00
|
|
|
private async Task OptionChanged(T updatedValue)
|
|
|
|
|
{
|
2026-10-30 20:01:57 +01:00
|
|
|
this.draggedValue = null;
|
2024-07-28 11:20:00 +02:00
|
|
|
this.ValueUpdate(updatedValue);
|
|
|
|
|
await this.SettingsManager.StoreSettings();
|
|
|
|
|
await this.InformAboutChange();
|
|
|
|
|
}
|
2026-10-30 20:01:57 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Shows the value the user drags the slider to, while the option waits for the release.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void ShowDraggedValue(ChangeEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (this.Immediate)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// The range input reports its value with a dot as decimal separator, whatever the language:
|
|
|
|
|
if (T.TryParse(args.Value?.ToString(), CultureInfo.InvariantCulture, out var value))
|
|
|
|
|
this.draggedValue = value;
|
|
|
|
|
}
|
2024-08-05 21:12:52 +02:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-07-28 11:20:00 +02:00
|
|
|
}
|