2024-07-28 09:20:00 +00:00
using Microsoft.AspNetCore.Components ;
2024-09-15 19:14:56 +00:00
using Timer = System . Timers . Timer ;
2024-08-21 06:30:01 +00:00
namespace AIStudio.Components ;
2024-07-28 09:20:00 +00:00
2025-08-09 17:29:43 +00:00
public partial class ConfigurationText : ConfigurationBaseCore
2024-07-28 09:20:00 +00:00
{
/// <summary>
/// The text used for the textfield.
/// </summary>
[Parameter]
public Func < string > Text { get ; set ; } = ( ) = > string . Empty ;
/// <summary>
2024-09-15 19:14:56 +00:00
/// An action which is called when the text was changed.
2024-07-28 09:20:00 +00:00
/// </summary>
[Parameter]
public Action < string > TextUpdate { get ; set ; } = _ = > { } ;
/// <summary>
/// The icon to display next to the textfield.
/// </summary>
[Parameter]
public string Icon { get ; set ; } = Icons . Material . Filled . Info ;
/// <summary>
/// The color of the icon to use.
/// </summary>
[Parameter]
public Color IconColor { get ; set ; } = Color . Default ;
2024-09-15 19:14:56 +00:00
/// <summary>
/// How many lines should the textfield have?
/// </summary>
[Parameter]
public int NumLines { get ; set ; } = 1 ;
/// <summary>
/// What is the maximum number of lines?
/// </summary>
[Parameter]
public int MaxLines { get ; set ; } = 12 ;
2026-08-11 18:14:21 +00:00
/// <summary>
/// When configured, displays a button which restores this value.
/// </summary>
[Parameter]
public Func < string > ? ResetValue { get ; set ; }
/// <summary>
/// The text displayed on the optional reset button.
/// </summary>
[Parameter]
public string ResetButtonText { get ; set ; } = string . Empty ;
/// <summary>
/// Validates the configured text before it is stored.
/// </summary>
[Parameter]
public Func < string , string? > ? Validation { get ; set ; }
2024-09-15 19:14:56 +00:00
private string internalText = string . Empty ;
2025-08-09 17:29:43 +00:00
private readonly Timer timer = new ( TimeSpan . FromMilliseconds ( 500 ) )
2024-09-15 19:14:56 +00:00
{
AutoReset = false
} ;
2025-08-09 17:29:43 +00:00
#region Overrides of ConfigurationBase
/// <inheritdoc />
protected override bool Stretch = > true ;
protected override Variant Variant = > Variant . Outlined ;
protected override string Label = > this . OptionDescription ;
2024-09-15 19:14:56 +00:00
protected override async Task OnInitializedAsync ( )
{
2026-08-25 16:53:09 +00:00
this . timer . Elapsed + = ( _ , _ ) = > this . InvokeAsync ( async ( ) = > await this . OptionChanged ( this . internalText ) ) . Observe ( $"{nameof(ConfigurationText)}: applying the changed text" ) ;
2024-09-15 19:14:56 +00:00
await base . OnInitializedAsync ( ) ;
}
protected override async Task OnParametersSetAsync ( )
{
this . internalText = this . Text ( ) ;
await base . OnParametersSetAsync ( ) ;
}
#endregion
private bool AutoGrow = > this . NumLines > 1 ;
private int GetMaxLines = > this . AutoGrow ? this . MaxLines : 1 ;
private void InternalUpdate ( string text )
{
this . timer . Stop ( ) ;
this . internalText = text ;
this . timer . Start ( ) ;
}
2026-08-11 18:14:21 +00:00
private async Task ResetTextAsync ( )
{
if ( this . ResetValue is null | | this . IsDisabled )
return ;
this . timer . Stop ( ) ;
this . internalText = this . ResetValue ( ) ;
await this . OptionChanged ( this . internalText ) ;
}
2024-07-28 09:20:00 +00:00
private async Task OptionChanged ( string updatedText )
{
2026-08-11 18:14:21 +00:00
if ( this . Validation ? . Invoke ( updatedText ) is not null )
return ;
2024-07-28 09:20:00 +00:00
this . TextUpdate ( updatedText ) ;
await this . SettingsManager . StoreSettings ( ) ;
await this . InformAboutChange ( ) ;
}
2025-08-15 19:16:51 +00:00
#region Overrides of MSGComponentBase
protected override void DisposeResources ( )
{
try
{
this . timer . Stop ( ) ;
this . timer . Dispose ( ) ;
}
catch
{
// ignore
}
base . DisposeResources ( ) ;
}
#endregion
2024-07-28 09:20:00 +00:00
}