using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Threading;
using XFEToolBox.Tools.VideoStudio;
namespace VideoStudio.Validation;
internal static partial class Program
{
[StructLayout(LayoutKind.Sequential)] private struct DropPoint { public int X, Y; }
[DllImport("kernel32.dll")] private static extern nint GlobalAlloc(uint flags, nuint bytes);
[DllImport("kernel32.dll")] private static extern nint GlobalLock(nint memory);
[DllImport("kernel32.dll")] private static extern bool GlobalUnlock(nint memory);
[DllImport("kernel32.dll")] private static extern nint GlobalFree(nint memory);
[DllImport("user32.dll")] private static extern nint SendMessage(nint hwnd, int message, nint wParam, nint lParam);
[DllImport("user32.dll")] private static extern bool ScreenToClient(nint hwnd, ref DropPoint point);
private static nint DropPayload(string[] paths, DropPoint point = default)
{
byte[] names = Encoding.Unicode.GetBytes(string.Join('\0', paths) + "\0\0");
nint memory = GlobalAlloc(0x42, (nuint)(20 + names.Length));
if (memory == 0) throw new OutOfMemoryException();
nint data = GlobalLock(memory);
if (data == 0) { GlobalFree(memory); throw new InvalidOperationException("Cannot lock test HDROP"); }
try
{
Marshal.WriteInt32(data, 0, 20); Marshal.WriteInt32(data, 4, point.X); Marshal.WriteInt32(data, 8, point.Y);
Marshal.WriteInt32(data, 12, 0); Marshal.WriteInt32(data, 16, 1);
Marshal.Copy(names, 0, data + 20, names.Length);
}
finally { GlobalUnlock(memory); }
return memory;
}
private static async Task ExplorerDropTests(string[] paths)
{
nint payload = DropPayload([paths[0], paths[0], paths[2]]);
try { Check(ExplorerFileDrop.ReadPaths(payload).SequenceEqual(new[] { paths[0], paths[2] }), "native HDROP preserves Unicode paths and deduplicates files"); }
finally { GlobalFree(payload); }
foreach (var invalid in new[] { new[] { "relative.mp4" }, Enumerable.Repeat(paths[0], 513).ToArray() })
{
payload = DropPayload(invalid);
try { await Reject(() => { ExplorerFileDrop.ReadPaths(payload); return Task.CompletedTask; }, "native HDROP rejects relative paths or excessive file counts"); }
finally { GlobalFree(payload); }
}
var page = new MainPage(); var vm = (MainPageViewModel)page.DataContext;
vm.FfmpegPath = ffmpeg; vm.FfprobePath = ffprobe; page.Playback.IsMuted = true;
var window = TestWindow(page); window.Show(); await Dispatcher.Yield(DispatcherPriority.ApplicationIdle);
try
{
Check(page.ExplorerDropEnabled, "loaded editor registers the native Explorer drop channel");
var timeline = (TimelineSurface)page.FindName("Timeline");
void DropAt(string[] files, FrameworkElement target, Point localPoint)
{
var screen = target.PointToScreen(localPoint); var point = new DropPoint { X = (int)Math.Round(screen.X), Y = (int)Math.Round(screen.Y) };
nint hwnd = new WindowInteropHelper(window).Handle;
if (!ScreenToClient(hwnd, ref point)) throw new InvalidOperationException("Cannot map test drop point");
// Send a Shell-format file message to this test-owned HWND only. The
// production hook owns and releases the payload; no external UI input.
SendMessage(hwnd, ExplorerFileDrop.DropFilesMessage, DropPayload(files, point), 0);
}
DropAt([paths[0]], (FrameworkElement)page.FindName("SourcePreview"), new(35, 35));
await WaitUntil(() => vm.Assets.Count == 1 && vm.Clips.Count == 2 && !vm.IsBusy, "native monitor-area drop imports first video with independent original audio");
DropAt([paths[2], paths[3]], (FrameworkElement)page.FindName("AssetPane"), new(35, 35));
await WaitUntil(() => vm.Assets.Count == 3 && !vm.IsBusy, "native multi-file bin drop imports audio and picture");
Check(vm.Clips.Count == 2, "native non-timeline drop keeps later media out of tracks");
var v2 = vm.Tracks.Single(t => t.Name == "V2");
DropAt([paths[1]], timeline, new(timeline.XAt(3.5), timeline.YAt(v2) + 30));
await WaitUntil(() => vm.Assets.Count == 4 && vm.Clips.Count == 3 && !vm.IsBusy, "native timeline drop routes to the actual target video lane");
Check(vm.SelectedClip?.TrackId == v2.Id && Near(vm.SelectedClip.TimelineStart, 3.5), "native physical drop coordinates preserve target lane and frame-aligned time");
v2.IsLocked = true;
DropAt([paths[1]], timeline, new(timeline.XAt(5), timeline.YAt(v2) + 30));
await WaitUntil(() => vm.Status.Contains("锁定"), "native drop rejects locked target lane");
Check(vm.Clips.Count == 3, "rejected native drop leaves all clips unchanged"); v2.IsLocked = false;
vm.IsBusy = true; DropAt([paths[3]], page, new(15, 15));
await WaitUntil(() => vm.Status.Contains("当前任务尚未完成"), "native drop during busy operation gives actionable feedback"); vm.IsBusy = false;
DropAt(["relative.mp4"], page, new(15, 15));
await WaitUntil(() => vm.Status.Contains("无法读取拖入文件"), "invalid native payload reports failure without crashing editor");
Check(vm.Assets.Count == 4 && vm.Clips.Count == 3, "bad native payload preserves existing project");
await vm.WaitForAssetPreviewsAsync();
}
finally { vm.IsBusy = false; vm.HasUnsavedChanges = false; window.Close(); }
await Dispatcher.Yield(DispatcherPriority.ApplicationIdle);
Check(!page.ExplorerDropEnabled, "closing editor releases the native drop hook");
}
}
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Threading;
using XFEToolBox.Tools.VideoStudio;
namespace VideoStudio.Validation;
internal static partial class Program
{
[StructLayout(LayoutKind.Sequential)] private struct DropPoint { public int X, Y; }
[DllImport("kernel32.dll")] private static extern nint GlobalAlloc(uint flags, nuint bytes);
[DllImport("kernel32.dll")] private static extern nint GlobalLock(nint memory);
[DllImport("kernel32.dll")] private static extern bool GlobalUnlock(nint memory);
[DllImport("kernel32.dll")] private static extern nint GlobalFree(nint memory);
[DllImport("user32.dll")] private static extern nint SendMessage(nint hwnd, int message, nint wParam, nint lParam);
[DllImport("user32.dll")] private static extern bool ScreenToClient(nint hwnd, ref DropPoint point);
private static nint DropPayload(string[] paths, DropPoint point = default)
{
byte[] names = Encoding.Unicode.GetBytes(string.Join('\0', paths) + "\0\0");
nint memory = GlobalAlloc(0x42, (nuint)(20 + names.Length));
if (memory == 0) throw new OutOfMemoryException();
nint data = GlobalLock(memory);
if (data == 0) { GlobalFree(memory); throw new InvalidOperationException("Cannot lock test HDROP"); }
try
{
Marshal.WriteInt32(data, 0, 20); Marshal.WriteInt32(data, 4, point.X); Marshal.WriteInt32(data, 8, point.Y);
Marshal.WriteInt32(data, 12, 0); Marshal.WriteInt32(data, 16, 1);
Marshal.Copy(names, 0, data + 20, names.Length);
}
finally { GlobalUnlock(memory); }
return memory;
}
private static async Task ExplorerDropTests(string[] paths)
{
nint payload = DropPayload([paths[0], paths[0], paths[2]]);
try { Check(ExplorerFileDrop.ReadPaths(payload).SequenceEqual(new[] { paths[0], paths[2] }), "native HDROP preserves Unicode paths and deduplicates files"); }
finally { GlobalFree(payload); }
foreach (var invalid in new[] { new[] { "relative.mp4" }, Enumerable.Repeat(paths[0], 513).ToArray() })
{
payload = DropPayload(invalid);
try { await Reject(() => { ExplorerFileDrop.ReadPaths(payload); return Task.CompletedTask; }, "native HDROP rejects relative paths or excessive file counts"); }
finally { GlobalFree(payload); }
}
var page = new MainPage(); var vm = (MainPageViewModel)page.DataContext;
vm.FfmpegPath = ffmpeg; vm.FfprobePath = ffprobe; page.Playback.IsMuted = true;
var window = TestWindow(page); window.Show(); await Dispatcher.Yield(DispatcherPriority.ApplicationIdle);
try
{
Check(page.ExplorerDropEnabled, "loaded editor registers the native Explorer drop channel");
var timeline = (TimelineSurface)page.FindName("Timeline");
void DropAt(string[] files, FrameworkElement target, Point localPoint)
{
var screen = target.PointToScreen(localPoint); var point = new DropPoint { X = (int)Math.Round(screen.X), Y = (int)Math.Round(screen.Y) };
nint hwnd = new WindowInteropHelper(window).Handle;
if (!ScreenToClient(hwnd, ref point)) throw new InvalidOperationException("Cannot map test drop point");
// Send a Shell-format file message to this test-owned HWND only. The
// production hook owns and releases the payload; no external UI input.
SendMessage(hwnd, ExplorerFileDrop.DropFilesMessage, DropPayload(files, point), 0);
}
DropAt([paths[0]], (FrameworkElement)page.FindName("SourcePreview"), new(35, 35));
await WaitUntil(() => vm.Assets.Count == 1 && vm.Clips.Count == 2 && !vm.IsBusy, "native monitor-area drop imports first video with independent original audio");
DropAt([paths[2], paths[3]], (FrameworkElement)page.FindName("AssetPane"), new(35, 35));
await WaitUntil(() => vm.Assets.Count == 3 && !vm.IsBusy, "native multi-file bin drop imports audio and picture");
Check(vm.Clips.Count == 2, "native non-timeline drop keeps later media out of tracks");
var v2 = vm.Tracks.Single(t => t.Name == "V2");
DropAt([paths[1]], timeline, new(timeline.XAt(3.5), timeline.YAt(v2) + 30));
await WaitUntil(() => vm.Assets.Count == 4 && vm.Clips.Count == 3 && !vm.IsBusy, "native timeline drop routes to the actual target video lane");
Check(vm.SelectedClip?.TrackId == v2.Id && Near(vm.SelectedClip.TimelineStart, 3.5), "native physical drop coordinates preserve target lane and frame-aligned time");
v2.IsLocked = true;
DropAt([paths[1]], timeline, new(timeline.XAt(5), timeline.YAt(v2) + 30));
await WaitUntil(() => vm.Status.Contains("锁定"), "native drop rejects locked target lane");
Check(vm.Clips.Count == 3, "rejected native drop leaves all clips unchanged"); v2.IsLocked = false;
vm.IsBusy = true; DropAt([paths[3]], page, new(15, 15));
await WaitUntil(() => vm.Status.Contains("当前任务尚未完成"), "native drop during busy operation gives actionable feedback"); vm.IsBusy = false;
DropAt(["relative.mp4"], page, new(15, 15));
await WaitUntil(() => vm.Status.Contains("无法读取拖入文件"), "invalid native payload reports failure without crashing editor");
Check(vm.Assets.Count == 4 && vm.Clips.Count == 3, "bad native payload preserves existing project");
await vm.WaitForAssetPreviewsAsync();
}
finally { vm.IsBusy = false; vm.HasUnsavedChanges = false; window.Close(); }
await Dispatcher.Yield(DispatcherPriority.ApplicationIdle);
Check(!page.ExplorerDropEnabled, "closing editor releases the native drop hook");
}
}