emby-toolbox/EmbyToolbox/Behaviors/FileDropBehavior.cs
Emby Toolbox 6264b487fe Initial commit: Emby Toolbox (conversion scroll fix, bulk Del for tracks).
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-12 21:33:47 +05:00

73 lines
1.9 KiB
C#

using System.Windows;
using System.Windows.Input;
using EmbyToolbox.ViewModels;
namespace EmbyToolbox.Behaviors;
public static class FileDropBehavior
{
public static readonly DependencyProperty DropCommandProperty =
DependencyProperty.RegisterAttached(
"DropCommand",
typeof(RelayCommand),
typeof(FileDropBehavior),
new PropertyMetadata(null, OnDropCommandChanged));
public static void SetDropCommand(DependencyObject element, RelayCommand? value)
{
element.SetValue(DropCommandProperty, value);
}
public static RelayCommand? GetDropCommand(DependencyObject element)
{
return (RelayCommand?)element.GetValue(DropCommandProperty);
}
private static void OnDropCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is not UIElement element)
{
return;
}
element.AllowDrop = e.NewValue is not null;
element.PreviewDragOver -= OnPreviewDragOver;
element.Drop -= OnDrop;
if (e.NewValue is not null)
{
element.PreviewDragOver += OnPreviewDragOver;
element.Drop += OnDrop;
}
}
private static void OnPreviewDragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Copy;
e.Handled = true;
}
}
private static void OnDrop(object sender, DragEventArgs e)
{
if (sender is not DependencyObject d)
{
return;
}
var command = GetDropCommand(d);
if (command is null)
{
return;
}
if (e.Data.GetData(DataFormats.FileDrop) is string[] paths && command.CanExecute(paths))
{
command.Execute(paths);
e.Handled = true;
}
}
}