2026-09-09 16:43:37 +00:00
using Microsoft.AspNetCore.Components ;
namespace AIStudio.Components ;
/// <summary>
/// A drop zone which reports the paths of whatever was dropped on it, and nothing else.
/// </summary>
/// <remarks>
/// Dropping is a native matter in AI Studio: the Tauri runtime reports real paths, which is why
/// this zone can hand out folders just as well as files. What those paths mean is the consumer's
/// business — this component reads no content and does not care whether a path leads to a file or
/// to a folder.
/// </remarks>
public partial class PathDropZone : MSGComponentBase
{
/// <summary>
/// The content shown inside the zone.
/// </summary>
[Parameter]
public RenderFragment ? ChildContent { get ; set ; }
/// <summary>
/// Reports the dropped paths, in the order the runtime delivered them.
/// </summary>
[Parameter]
public EventCallback < List < string > > OnPathsDropped { get ; set ; }
/// <summary>
2026-09-10 09:32:08 +00:00
/// Makes this zone the default target of its area, meaning of its page, assistant, or dialog.
2026-09-09 16:43:37 +00:00
/// </summary>
/// <remarks>
2026-09-10 09:32:08 +00:00
/// A drop aimed at this zone arrives here in any case. What this flag decides is the fate of the
/// drops aimed anywhere else in the surrounding area which hit no zone of their own: with the
/// flag, they arrive here as well. Only one zone per area can hold that role, and if several ask
/// for it, the first one in the markup gets it.
2026-09-09 16:43:37 +00:00
/// </remarks>
[Parameter]
public bool CatchAllDocuments { get ; set ; }
/// <summary>
/// When true, the zone ignores drops and is not highlighted.
/// </summary>
/// <remarks>
2026-09-10 09:32:08 +00:00
/// It keeps its ID in the DOM nevertheless and therefore swallows the drops aimed at it. That is
/// what the pointer says: it rests on a switched-off field, so nothing happens. Letting the drop
/// fall through to the area behind it would deliver the files somewhere else entirely.
2026-09-09 16:43:37 +00:00
/// </remarks>
[Parameter]
public bool Disabled { get ; set ; }
2026-09-10 09:32:08 +00:00
/// <summary>
/// The area this zone lives in, if it lives in one at all.
/// </summary>
[CascadingParameter]
private DropZoneScopeState ? Scope { get ; set ; }
2026-09-09 16:43:37 +00:00
[Inject]
private ILogger < PathDropZone > Logger { get ; init ; } = null ! ;
private const string DEFAULT_DRAG_CLASS = "relative rounded-lg border-2 border-dashed pa-3 mb-3 mud-width-full" ;
2026-09-10 09:32:08 +00:00
private readonly string dropZoneId = $"path-drop-zone-{Guid.NewGuid():N}" ;
2026-09-09 16:43:37 +00:00
private string dragClass = DEFAULT_DRAG_CLASS ;
2026-09-10 09:32:08 +00:00
private bool isDefaultZone ;
private bool isHighlighted ;
2026-09-09 16:43:37 +00:00
#region Overrides of MSGComponentBase
protected override async Task OnInitializedAsync ( )
{
2026-09-10 09:32:08 +00:00
this . ApplyFilters ( [ ] , [ Event . HIGHLIGHT_DROP_ZONE , Event . PATHS_DROPPED ] ) ;
this . ClaimDefaultZoneRole ( ) ;
2026-09-09 16:43:37 +00:00
await base . OnInitializedAsync ( ) ;
}
/// <summary>
2026-09-10 09:32:08 +00:00
/// Hands the role of the default target back to the area.
2026-09-09 16:43:37 +00:00
/// </summary>
protected override void DisposeResources ( )
{
2026-09-10 09:32:08 +00:00
if ( this . isDefaultZone )
this . Scope ? . ReleaseDefaultZone ( this ) ;
2026-09-09 16:43:37 +00:00
base . DisposeResources ( ) ;
}
protected override async Task ProcessIncomingMessage < T > ( ComponentBase ? sendingComponent , Event triggeredEvent , T ? data ) where T : default
{
switch ( triggeredEvent )
{
2026-09-10 09:32:08 +00:00
case Event . HIGHLIGHT_DROP_ZONE when data is DropZoneHighlight highlight :
this . ApplyHighlight ( this . IsThisZone ( highlight . ZoneId ) ) ;
2026-09-09 16:43:37 +00:00
break ;
2026-09-10 09:32:08 +00:00
case Event . PATHS_DROPPED when data is DroppedPaths dropped :
// Whoever the drop was meant for, the drag is over and no zone stays highlighted:
this . ApplyHighlight ( false ) ;
2026-09-09 16:43:37 +00:00
2026-09-10 09:32:08 +00:00
if ( ! this . IsThisZone ( dropped . ZoneId ) )
2026-09-09 16:43:37 +00:00
return ;
2026-09-10 09:32:08 +00:00
if ( this . Disabled )
{
this . Logger . LogDebug ( "The path drop zone '{ZoneId}' is disabled and swallowed {Count} dropped path(s)." , this . dropZoneId , dropped . Paths . Count ) ;
2026-09-09 16:43:37 +00:00
return ;
2026-09-10 09:32:08 +00:00
}
2026-09-09 16:43:37 +00:00
2026-09-10 09:32:08 +00:00
this . Logger . LogDebug ( "The path drop zone '{ZoneId}' caught {Count} path(s)." , this . dropZoneId , dropped . Paths . Count ) ;
await this . OnPathsDropped . InvokeAsync ( dropped . Paths ) ;
2026-09-09 16:43:37 +00:00
break ;
}
}
#endregion
2026-09-10 09:32:08 +00:00
/// <summary>
/// Asks the area for the role of its default target, if this zone wants it.
/// </summary>
private void ClaimDefaultZoneRole ( )
2026-09-09 16:43:37 +00:00
{
2026-09-10 09:32:08 +00:00
if ( ! this . CatchAllDocuments | | this . Scope is null )
2026-09-09 16:43:37 +00:00
return ;
2026-09-10 09:32:08 +00:00
this . isDefaultZone = this . Scope . TryBecomeDefaultZone ( this ) ;
if ( ! this . isDefaultZone )
this . Logger . LogDebug ( "The path drop zone '{ZoneId}' asked to be the default target of its area, which another zone already is. It now takes only the drops aimed at itself." , this . dropZoneId ) ;
2026-09-09 16:43:37 +00:00
}
2026-09-10 09:32:08 +00:00
/// <summary>
/// Decides whether the named zone is this one.
/// </summary>
/// <remarks>
/// The area counts as this zone as long as this zone is its default target. That is the whole
/// mechanism behind dropping anywhere in a page and still landing here.
/// </remarks>
/// <param name="zoneId">The ID the hit test reported, or null when it hit nothing.</param>
private bool IsThisZone ( string? zoneId ) = > zoneId is not null & & ( zoneId = = this . dropZoneId | | ( this . isDefaultZone & & zoneId = = this . Scope ? . ScopeId ) ) ;
/// <summary>
/// Highlights the zone, or takes the highlight away.
/// </summary>
/// <remarks>
/// The comparison is not for tidiness: a throttled drag-over event arrives about ten times per
/// second, and without it every one of them would render every zone on the page anew.
/// </remarks>
private void ApplyHighlight ( bool shouldBeHighlighted )
2026-09-09 16:43:37 +00:00
{
2026-09-10 09:32:08 +00:00
var highlighted = shouldBeHighlighted & & ! this . Disabled ;
if ( highlighted = = this . isHighlighted )
2026-09-09 16:43:37 +00:00
return ;
2026-09-10 09:32:08 +00:00
this . isHighlighted = highlighted ;
this . dragClass = highlighted ? $"{DEFAULT_DRAG_CLASS} mud-border-primary border-2" : DEFAULT_DRAG_CLASS ;
2026-09-09 16:43:37 +00:00
this . StateHasChanged ( ) ;
}
}