2025-05-16 15:36:08 +02:00
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
using Timer = System.Timers.Timer;
|
|
|
|
|
|
|
|
|
|
namespace AIStudio.Components;
|
|
|
|
|
|
2025-08-15 21:16:51 +02:00
|
|
|
public partial class DebouncedTextField : MudComponentBase, IDisposable
|
2025-05-16 15:36:08 +02:00
|
|
|
{
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Label { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Text { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<string> TextChanged { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<string, Task> WhenTextChangedAsync { get; set; } = _ => Task.CompletedTask;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Action<string> WhenTextCanged { get; set; } = _ => { };
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public int Lines { get; set; } = 1;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public int MaxLines { get; set; } = 1;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Dictionary<string, object?> Attributes { get; set; } = [];
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Func<string, string?> ValidationFunc { get; set; } = _ => null;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string HelpText { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Placeholder { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Icon { get; set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public TimeSpan DebounceTime { get; set; } = TimeSpan.FromMilliseconds(800);
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Disabled { get; set; }
|
|
|
|
|
|
|
|
|
|
private readonly Timer debounceTimer = new();
|
|
|
|
|
private string text = string.Empty;
|
2025-08-15 20:07:05 +02:00
|
|
|
private string lastParameterText = string.Empty;
|
2025-08-15 21:16:51 +02:00
|
|
|
private bool isInitialized;
|
2025-05-16 15:36:08 +02:00
|
|
|
|
|
|
|
|
#region Overrides of ComponentBase
|
|
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
|
|
|
|
this.text = this.Text;
|
2025-08-15 20:07:05 +02:00
|
|
|
this.lastParameterText = this.Text;
|
2025-05-16 15:36:08 +02:00
|
|
|
this.debounceTimer.AutoReset = false;
|
|
|
|
|
this.debounceTimer.Interval = this.DebounceTime.TotalMilliseconds;
|
|
|
|
|
this.debounceTimer.Elapsed += (_, _) =>
|
|
|
|
|
{
|
|
|
|
|
this.debounceTimer.Stop();
|
2026-11-14 14:11:34 +01:00
|
|
|
this.InvokeAsync(async () =>
|
|
|
|
|
{
|
|
|
|
|
// The parent hands this text back with its next render, which is no change of its own, cf. OnParametersSetAsync:
|
|
|
|
|
this.lastParameterText = this.text;
|
|
|
|
|
await this.TextChanged.InvokeAsync(this.text);
|
|
|
|
|
}).Observe($"{nameof(DebouncedTextField)}: notifying about changed text");
|
2026-08-25 18:53:09 +02:00
|
|
|
this.InvokeAsync(async () => await this.WhenTextChangedAsync(this.text)).Observe($"{nameof(DebouncedTextField)}: handling changed text asynchronously");
|
|
|
|
|
this.InvokeAsync(() => this.WhenTextCanged(this.text)).Observe($"{nameof(DebouncedTextField)}: handling changed text");
|
2025-05-16 15:36:08 +02:00
|
|
|
};
|
|
|
|
|
|
2025-08-15 21:16:51 +02:00
|
|
|
this.isInitialized = true;
|
2026-11-14 14:11:34 +01:00
|
|
|
|
|
|
|
|
// A text the field starts with, e.g., one sent from another assistant, is handled like a typed one:
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(this.text))
|
|
|
|
|
this.debounceTimer.Start();
|
|
|
|
|
|
2025-05-16 15:36:08 +02:00
|
|
|
await base.OnInitializedAsync();
|
|
|
|
|
}
|
2025-08-15 20:07:05 +02:00
|
|
|
|
2025-08-15 21:16:51 +02:00
|
|
|
protected override async Task OnParametersSetAsync()
|
2025-08-15 20:07:05 +02:00
|
|
|
{
|
2025-08-15 21:16:51 +02:00
|
|
|
// Ensure the timer uses the latest debouncing interval:
|
|
|
|
|
if (!this.isInitialized)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if(Math.Abs(this.debounceTimer.Interval - this.DebounceTime.TotalMilliseconds) > 1)
|
|
|
|
|
this.debounceTimer.Interval = this.DebounceTime.TotalMilliseconds;
|
|
|
|
|
|
2026-11-14 14:11:34 +01:00
|
|
|
//
|
|
|
|
|
// Only a text which the parent changed itself, e.g., by a reset, is synced and handled like a typed one. Every
|
|
|
|
|
// other render of the parent leaves the timer alone: restarting it there let its notification render the parent
|
|
|
|
|
// again, which restarted the timer, and so on, as long as the assistant stayed open.
|
|
|
|
|
//
|
2025-08-15 20:07:05 +02:00
|
|
|
if (this.Text != this.lastParameterText)
|
|
|
|
|
{
|
|
|
|
|
this.text = this.Text;
|
|
|
|
|
this.lastParameterText = this.Text;
|
2026-11-14 14:11:34 +01:00
|
|
|
this.debounceTimer.Stop();
|
|
|
|
|
this.debounceTimer.Start();
|
2025-08-15 20:07:05 +02:00
|
|
|
}
|
2026-11-14 14:11:34 +01:00
|
|
|
|
2025-08-15 21:16:51 +02:00
|
|
|
await base.OnParametersSetAsync();
|
2025-08-15 20:07:05 +02:00
|
|
|
}
|
2025-05-16 15:36:08 +02:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2026-10-31 10:48:20 +01:00
|
|
|
private void OnTextChanged(string? value)
|
2025-05-16 15:36:08 +02:00
|
|
|
{
|
2026-10-31 10:48:20 +01:00
|
|
|
this.text = value ?? string.Empty;
|
2025-05-16 15:36:08 +02:00
|
|
|
this.debounceTimer.Stop();
|
|
|
|
|
this.debounceTimer.Start();
|
|
|
|
|
}
|
2025-08-15 21:16:51 +02:00
|
|
|
|
|
|
|
|
#region IDisposable
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this.debounceTimer.Stop();
|
|
|
|
|
this.debounceTimer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2025-05-16 15:36:08 +02:00
|
|
|
}
|