Skip to content

Commit

Permalink
Add Drag&Drop handler for dropping Folders/ISO-Files onto the main wi…
Browse files Browse the repository at this point in the history
…ndow
  • Loading branch information
UniqProject committed Feb 28, 2023
1 parent 30bbf60 commit b722243
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion BDInfoAV/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
SizeChanged="OnSizeChanged"
Background="{DynamicResource SystemControlBackgroundAltHighBrush}"
UseLayoutRounding="True"
ExtendClientAreaChromeHints="PreferSystemChrome">
ExtendClientAreaChromeHints="PreferSystemChrome"
DragDrop.AllowDrop="True">
<!--
ExtendClientAreaToDecorationsHint="True"
ExtendClientAreaChromeHints="PreferSystemChrome"
Expand Down
29 changes: 29 additions & 0 deletions BDInfoAV/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
//=============================================================================

using Avalonia.Controls;
using Avalonia.Input;
using System.Linq;
using BDInfo.ViewModels;

namespace BDInfo.Views;

Expand All @@ -29,6 +32,32 @@ public MainWindow()
InitializeComponent();
Instance = this;
this.Position = BDInfoSettings.WindowLocation;

AddHandler(DragDrop.DropEvent, DropHandler);
AddHandler(DragDrop.DragOverEvent, DragOverHandler);
}

private void DragOverHandler(object sender, DragEventArgs e)
{
// Only allow Copy or Link as Drop Operations.
e.DragEffects = e.DragEffects & (DragDropEffects.Copy | DragDropEffects.Link);

// Only allow if the dragged data contains text or filenames.
if (!e.Data.Contains(DataFormats.FileNames))
e.DragEffects = DragDropEffects.None;
}

private void DropHandler(object sender, DragEventArgs e)
{
if (e.Data.Contains(DataFormats.FileNames))
{
if (DataContext is MainWindowViewModel dataContext)
{
dataContext.Folder = e.Data.GetFileNames()!.First();
dataContext.Rescan();
}
}

}

private void OnPositionChanged(object sender, PixelPointEventArgs e)
Expand Down

0 comments on commit b722243

Please sign in to comment.