Files
AI-Studio/app/MindWork AI Studio/Components/SelectDirectory.razor.cs
T

104 lines
3.2 KiB
C#
Raw Normal View History

using AIStudio.Tools.Services;
2025-01-01 15:49:27 +01:00
using Microsoft.AspNetCore.Components;
namespace AIStudio.Components;
public partial class SelectDirectory : MSGComponentBase
2025-01-01 15:49:27 +01:00
{
[Parameter]
public string Directory { get; set; } = string.Empty;
[Parameter]
public EventCallback<string> DirectoryChanged { get; set; }
[Parameter]
public bool Disabled { get; set; }
[Parameter]
public string Label { get; set; } = string.Empty;
[Parameter]
public string DirectoryDialogTitle { get; set; } = "Select Directory";
2025-01-13 19:51:26 +01:00
[Parameter]
public Func<string, string?> Validation { get; set; } = _ => null;
2025-01-01 15:49:27 +01:00
2026-09-09 18:43:37 +02:00
/// <summary>
/// When true, the folder can also be chosen by dropping it onto this component.
/// </summary>
[Parameter]
public bool EnableDragDrop { get; set; }
/// <summary>
/// Makes this component the default target of its area, meaning of its page, assistant, or
/// dialog: it then also takes the drops which land anywhere in that area without hitting a zone
/// of their own.
2026-09-09 18:43:37 +02:00
/// </summary>
[Parameter]
public bool CatchAllDocuments { get; set; }
2025-01-01 15:49:27 +01:00
[Inject]
public RustService RustService { get; set; } = null!;
[Inject]
protected ILogger<SelectDirectory> Logger { get; init; } = null!;
private static readonly Dictionary<string, object?> SPELLCHECK_ATTRIBUTES = new();
private bool isDirectoryDialogOpen;
2025-01-01 15:49:27 +01:00
#region Overrides of ComponentBase
protected override async Task OnInitializedAsync()
{
// Configure the spellchecking for the instance name input:
this.SettingsManager.InjectSpellchecking(SPELLCHECK_ATTRIBUTES);
await base.OnInitializedAsync();
}
#endregion
private void InternalDirectoryChanged(string directory)
{
this.Directory = directory;
this.DirectoryChanged.InvokeAsync(directory);
}
private async Task OpenDirectoryDialog()
{
if (this.isDirectoryDialogOpen)
return;
2025-01-01 15:49:27 +01:00
this.isDirectoryDialogOpen = true;
try
{
var response = await this.RustService.SelectDirectory(this.DirectoryDialogTitle, string.IsNullOrWhiteSpace(this.Directory) ? null : this.Directory);
this.Logger.LogInformation("The user selected the directory '{SelectedDirectory}'.", response.SelectedDirectory);
if (!response.UserCancelled)
this.InternalDirectoryChanged(response.SelectedDirectory);
}
finally
{
this.isDirectoryDialogOpen = false;
}
2025-01-01 15:49:27 +01:00
}
2026-09-09 18:43:37 +02:00
/// <summary>
/// Takes the first dropped path which leads to a folder.
/// </summary>
/// <param name="paths">The dropped paths.</param>
private async Task PathsDropped(List<string> paths)
{
var path = DroppedFolder.FirstUsable(paths);
if (path is null)
2026-09-09 18:43:37 +02:00
{
this.Logger.LogWarning("None of the {Count} dropped path(s) could be used as a directory.", paths.Count);
await this.MessageBus.SendWarning(new(Icons.Material.Filled.Warning, DroppedFolder.WarningFor(paths)));
2026-09-09 18:43:37 +02:00
return;
}
this.Logger.LogInformation("The user dropped the directory '{DroppedDirectory}'.", path);
this.InternalDirectoryChanged(path);
2026-09-09 18:43:37 +02:00
}
2025-01-01 15:49:27 +01:00
}