From c032d0fd141206d869f35cb0ad98ef56483aad30 Mon Sep 17 00:00:00 2001 From: Fred Emmott Date: Sun, 13 Jul 2025 10:35:38 -0500 Subject: [PATCH] Allow file drag-and-drop Support dropping files onto SelectSingleBinaryAndPDBControl, both in 'Examine a binary' and 'Start a diff' windows --- .../SelectSingleBinaryAndPDBControl.xaml | 10 ++- .../SelectSingleBinaryAndPDBControl.xaml.cs | 90 +++++++++++++++++++ 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/SizeBench.GUI/Windows/SelectSingleBinaryAndPDBControl.xaml b/src/SizeBench.GUI/Windows/SelectSingleBinaryAndPDBControl.xaml index eb5d13a..c3e959e 100644 --- a/src/SizeBench.GUI/Windows/SelectSingleBinaryAndPDBControl.xaml +++ b/src/SizeBench.GUI/Windows/SelectSingleBinaryAndPDBControl.xaml @@ -1,11 +1,13 @@  - + diff --git a/src/SizeBench.GUI/Windows/SelectSingleBinaryAndPDBControl.xaml.cs b/src/SizeBench.GUI/Windows/SelectSingleBinaryAndPDBControl.xaml.cs index 5b48243..67be39a 100644 --- a/src/SizeBench.GUI/Windows/SelectSingleBinaryAndPDBControl.xaml.cs +++ b/src/SizeBench.GUI/Windows/SelectSingleBinaryAndPDBControl.xaml.cs @@ -1,4 +1,5 @@ using System.Diagnostics.CodeAnalysis; +using System.IO; using System.Windows; using System.Windows.Controls; using Microsoft.Win32; @@ -56,4 +57,93 @@ private void btnBinaryPathBrowse_Click(object sender, RoutedEventArgs e) this._viewModel!.BinaryPath = ofd.FileName; } } + + private void OnDragOver(object sender, DragEventArgs e) + { + e.Effects = GetDroppedExeAndPdb(e.Data) is null ? DragDropEffects.None : DragDropEffects.Copy; + e.Handled = true; + } + + private static (string, string)? GetDroppedExeAndPdb(IDataObject data) + { + if (!data.GetDataPresent(DataFormats.FileDrop)) + { + return null; + } + + var files = (data.GetData(DataFormats.FileDrop) as string[])!; + + if (files.Length is < 0 or > 2) + { + return null; + } + + string? exePath = null; + string? pdbPath = null; + foreach (var file in files) + { + if (file.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) + { + exePath = file; + } + else if (file.EndsWith(".pdb", StringComparison.OrdinalIgnoreCase)) + { + pdbPath = file; + } + } + + if (exePath is null && pdbPath is null) + { + return null; + } + + if (exePath is not null && pdbPath is not null) + { + return (exePath, pdbPath); + } + + if (files.Length != 1) + { + return null; + } + + if (exePath is not null) + { + pdbPath = exePath[..^3] + "pdb"; + if (File.Exists(pdbPath)) + { + return (exePath, pdbPath); + } + + return null; + } + + if (pdbPath is not null) + { + exePath = pdbPath[..^3] + "exe"; + if (File.Exists(exePath)) + { + return (exePath, pdbPath); + } + } + + return null; + } + + private void OnDrop(object sender, DragEventArgs e) + { + var exeAndPdb = GetDroppedExeAndPdb(e.Data); + if (exeAndPdb is null) + { + e.Effects = DragDropEffects.None; + e.Handled = true; + return; + } + + var (exePath, pdbPath) = exeAndPdb.Value; + this._viewModel!.PDBPath = pdbPath; + this._viewModel!.BinaryPath = exePath; + e.Effects = DragDropEffects.Copy; + e.Handled = true; + } }