Skip to content

Commit

Permalink
Fix drag and drop preview
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Feb 13, 2025
1 parent bab283c commit 3e67814
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 149 deletions.
51 changes: 18 additions & 33 deletions src/PicView.Avalonia/DragAndDrop/DragAndDropHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Text;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media.Imaging;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using PicView.Avalonia.ImageHandling;
Expand Down Expand Up @@ -102,25 +101,16 @@ private static async Task HandleDropFromUrl(DragEventArgs e, MainViewModel vm)
public static async Task DragEnter(DragEventArgs e, MainViewModel vm, Control control)
{
var files = e.Data.GetFiles();
if (files == null)
{
var handledFromUrl = await HandleDragEnterFromUrl(e, vm);
if (!handledFromUrl)
{
RemoveDragDropView();
}
}

await HandleDragEnter(files, e, vm, control);
}

private static async Task HandleDragEnter(IEnumerable<IStorageItem> files, DragEventArgs e, MainViewModel vm, Control control)
{
var fileArray = files as IStorageItem[] ?? files.ToArray();
if (fileArray is null || fileArray.Length < 1)
IStorageItem[]? fileArray = null;
if (files is not null)
{
RemoveDragDropView();
return;
fileArray = files as IStorageItem[] ?? files.ToArray();
}

await Dispatcher.UIThread.InvokeAsync(() =>
Expand All @@ -141,6 +131,15 @@ await Dispatcher.UIThread.InvokeAsync(() =>
_dragDropView.RemoveThumbnail();
}
});
if (fileArray is null)
{
var handledFromUrl = await HandleDragEnterFromUrl(e, vm);
if (!handledFromUrl)
{
RemoveDragDropView();
}
return;
}
var firstFile = fileArray[0];
var path = RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
? firstFile.Path.AbsolutePath
Expand Down Expand Up @@ -179,15 +178,7 @@ await Dispatcher.UIThread.InvokeAsync(() =>
var thumb = await GetThumbnails.GetThumbAsync(path, SizeDefaults.WindowMinSize - 30)
.ConfigureAwait(false);

await Dispatcher.UIThread.InvokeAsync(() => { _dragDropView?.UpdateThumbnail(thumb as Bitmap); });
}
}
else
{
var handledFromUrl = await HandleDragEnterFromUrl(e, vm);
if (!handledFromUrl)
{
RemoveDragDropView();
await Dispatcher.UIThread.InvokeAsync(() => { _dragDropView?.UpdateThumbnail(thumb); });
}
}
}
Expand All @@ -197,30 +188,24 @@ private static async Task<bool> HandleDragEnterFromUrl(object? urlObject, MainVi
{
if (urlObject is null)
{
_dragDropView.RemoveThumbnail();
return false;
}
await Dispatcher.UIThread.InvokeAsync(() =>
{
if (_dragDropView == null)
_dragDropView ??= new DragDropView { DataContext = vm };
if (!_dragDropView.IsLinkChainVisible)
{
_dragDropView = new DragDropView { DataContext = vm };
_dragDropView.AddLinkChain();
UIHelper.GetMainView.MainGrid.Children.Add(_dragDropView);
}
else
if (!UIHelper.GetMainView.MainGrid.Children.Contains(_dragDropView))
{
_dragDropView.RemoveThumbnail();
UIHelper.GetMainView.MainGrid.Children.Add(_dragDropView);
}
});
return true;
}

private static async Task<bool> HandleDragEnterFromUrl(DragEventArgs e, MainViewModel vm)
{
var urlObject = e.Data.Get("text/x-moz-url");
return await HandleDragEnterFromUrl(urlObject, vm);
}

public static void DragLeave(DragEventArgs e, Control control)
{
if (!control.IsPointerOver)
Expand Down
4 changes: 4 additions & 0 deletions src/PicView.Avalonia/Views/UC/DragDrogView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public partial class DragDropView : UserControl

private const int MaxSize = SizeDefaults.WindowMinSize - 30;

public bool IsLinkChainVisible => _linkChain != null && ParentPanel.Children.Contains(_linkChain);
public bool IsDirectoryIconVisible => _directoryIcon != null && ParentPanel.Children.Contains(_directoryIcon);
public bool IsZipIconVisible => _zipIcon != null && ParentPanel.Children.Contains(_zipIcon);

public DragDropView()
{
InitializeComponent();
Expand Down
Loading

0 comments on commit 3e67814

Please sign in to comment.