Added components for configuration options

This commit is contained in:
Thorsten Sommer 2024-05-04 11:08:18 +02:00
parent 0541e1b18e
commit 5bc0f7f7df
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108
7 changed files with 146 additions and 0 deletions

View File

@ -0,0 +1,29 @@
using AIStudio.Settings;
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
/// <summary>
/// A base class for configuration options.
/// </summary>
public partial class ConfigurationBase : ComponentBase
{
/// <summary>
/// The description of the option, i.e., the name. Should be
/// as short as possible.
/// </summary>
[Parameter]
public string OptionDescription { get; set; } = string.Empty;
/// <summary>
/// A helpful text that explains the option in more detail.
/// </summary>
[Parameter]
public string OptionHelp { get; set; } = string.Empty;
[Inject]
public SettingsManager SettingsManager { get; init; } = null!;
protected const string MARGIN_CLASS = "mb-6";
}

View File

@ -0,0 +1,7 @@
@inherits ConfigurationBase
<MudField Label="@this.OptionDescription" Variant="Variant.Outlined" HelperText="@this.OptionHelp" Class="@MARGIN_CLASS">
<MudSwitch T="bool" Value="@this.State()" ValueChanged="@(updatedState => this.OptionChanged(updatedState))">
@(this.State() ? this.LabelOn : this.LabelOff)
</MudSwitch>
</MudField>

View File

@ -0,0 +1,39 @@
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
/// <summary>
/// Configuration component for any boolean option.
/// </summary>
public partial class ConfigurationOption : ConfigurationBase
{
/// <summary>
/// Text to display when the option is true.
/// </summary>
[Parameter]
public string LabelOn { get; set; } = string.Empty;
/// <summary>
/// Text to display when the option is false.
/// </summary>
[Parameter]
public string LabelOff { get; set; } = string.Empty;
/// <summary>
/// The boolean state of the option.
/// </summary>
[Parameter]
public Func<bool> State { get; set; } = () => false;
/// <summary>
/// An action which is called when the option is changed.
/// </summary>
[Parameter]
public Action<bool> StateUpdate { get; set; } = _ => { };
private async Task OptionChanged(bool updatedState)
{
this.StateUpdate(updatedState);
await this.SettingsManager.StoreSettings();
}
}

View File

@ -0,0 +1,11 @@
@inherits ConfigurationBase
@typeparam T
<MudSelect T="T" Value="@this.SelectedValue()" Margin="Margin.Dense" Label="@this.OptionDescription" Class="@GetClass" Variant="Variant.Outlined" HelperText="@this.OptionHelp" ValueChanged="selectedValue => this.OptionChanged(selectedValue)">
@foreach (var data in this.Data)
{
<MudSelectItem Value="@data.Value">
@data.Name
</MudSelectItem>
}
</MudSelect>

View File

@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
/// <summary>
/// Configuration component for selecting a value from a list.
/// </summary>
/// <typeparam name="T">The type of the value to select.</typeparam>
public partial class ConfigurationSelect<T> : ConfigurationBase
{
/// <summary>
/// The data to select from.
/// </summary>
[Parameter]
public IEnumerable<ConfigurationSelectData<T>> Data { get; set; } = [];
/// <summary>
/// The selected value.
/// </summary>
[Parameter]
public Func<T> SelectedValue { get; set; } = () => default!;
/// <summary>
/// An action that is called when the selection changes.
/// </summary>
[Parameter]
public Action<T> SelectionUpdate { get; set; } = _ => { };
private async Task OptionChanged(T updatedValue)
{
this.SelectionUpdate(updatedValue);
await this.SettingsManager.StoreSettings();
}
private static string GetClass => $"{MARGIN_CLASS} rounded-lg";
}

View File

@ -0,0 +1,24 @@
using AIStudio.Settings;
namespace AIStudio.Components;
/// <summary>
/// A data structure to map a name to a value.
/// </summary>
/// <param name="Name">The name of the value, to be displayed in the UI.</param>
/// <param name="Value">The value to be stored.</param>
/// <typeparam name="T">The type of the value to store.</typeparam>
public readonly record struct ConfigurationSelectData<T>(string Name, T Value);
/// <summary>
/// A static factory class to get the lists of selectable values.
/// </summary>
public static class ConfigurationSelectDataFactory
{
public static IEnumerable<ConfigurationSelectData<SendBehavior>> GetSendBehaviorData()
{
yield return new("No key is sending the input", SendBehavior.NO_KEY_IS_SENDING);
yield return new("Modifier key + enter is sending the input", SendBehavior.MODIFER_ENTER_IS_SENDING);
yield return new("Enter is sending the input", SendBehavior.ENTER_IS_SENDING);
}
}