diff --git a/src/OneHub.Tools.HeapView/App.axaml.cs b/src/OneHub.Tools.HeapView/App.axaml.cs index 2d4735d..9a67599 100644 --- a/src/OneHub.Tools.HeapView/App.axaml.cs +++ b/src/OneHub.Tools.HeapView/App.axaml.cs @@ -1,7 +1,9 @@ using Avalonia; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; +using Avalonia.Threading; using System; +using System.IO; namespace OneHub.Tools.HeapView; @@ -16,10 +18,14 @@ public override void OnFrameworkInitializationCompleted() { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { - if (desktop.Args?.Length > 0) - desktop.MainWindow = new MainWindow(desktop.Args[0]); - else - Environment.Exit(1); + var mainWindow = new MainWindow(); + desktop.MainWindow = mainWindow; + switch (desktop.Args) + { + case [string fileName, ..] when File.Exists(fileName): + mainWindow.Open(fileName); + break; + } } base.OnFrameworkInitializationCompleted(); diff --git a/src/OneHub.Tools.HeapView/MainWindow.axaml b/src/OneHub.Tools.HeapView/MainWindow.axaml index 2f1ba94..7214ee6 100644 --- a/src/OneHub.Tools.HeapView/MainWindow.axaml +++ b/src/OneHub.Tools.HeapView/MainWindow.axaml @@ -6,7 +6,8 @@ xmlns:heapView="using:OneHub.Diagnostics.HeapView" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="OneHub.Tools.HeapView.MainWindow" - Title="Heap View"> + Title="Heap View" + DragDrop.AllowDrop="True"> diff --git a/src/OneHub.Tools.HeapView/MainWindow.axaml.cs b/src/OneHub.Tools.HeapView/MainWindow.axaml.cs index 15b7a07..2543c97 100644 --- a/src/OneHub.Tools.HeapView/MainWindow.axaml.cs +++ b/src/OneHub.Tools.HeapView/MainWindow.axaml.cs @@ -1,19 +1,53 @@ using Avalonia.Controls; +using Avalonia.Input; +using Avalonia.Platform.Storage; using OneHub.Diagnostics.HeapView; +using System.Linq; +using System; namespace OneHub.Tools.HeapView; public partial class MainWindow : Window { - HeapSnapshot heapSnapshot; - - public MainWindow(string fileName) + public MainWindow() { InitializeComponent(); - var heapDump = new GCHeapDump(fileName); - heapSnapshot = new HeapSnapshot(heapDump); + AddHandler(DragDrop.DragOverEvent, DragOver); + AddHandler(DragDrop.DropEvent, Drop); + } + + public void Open(string fileName) + { + try + { + var heapDump = new GCHeapDump(fileName); + var heapSnapshot = new HeapSnapshot(heapDump); + heapView.Snapshot = heapSnapshot; + } + catch (Exception ex) + { + // TODO: Show error + } + } + + private void DragOver(object? sender, DragEventArgs e) + { + if (!e.Data.Contains(DataFormats.Files) || e.Data.GetFiles()?.FirstOrDefault() is not IStorageFile) + e.DragEffects = DragDropEffects.None; + else + e.DragEffects = DragDropEffects.Move; + } - heapView.Snapshot = heapSnapshot; + private void Drop(object? sender, DragEventArgs e) + { + if (e.Data.Contains(DataFormats.Files)) + { + var files = e.Data.GetFiles()?.OfType().ToArray(); + if (files?.FirstOrDefault()?.TryGetLocalPath() is string path) + { + Open(path); + } + } } } \ No newline at end of file diff --git a/src/OneHub.Tools.HeapView/Program.cs b/src/OneHub.Tools.HeapView/Program.cs index 7af03eb..4dd6046 100644 --- a/src/OneHub.Tools.HeapView/Program.cs +++ b/src/OneHub.Tools.HeapView/Program.cs @@ -9,19 +9,19 @@ class Program [STAThread] public static int Main(string[] args) { - var fileNameArgument = new Argument("filename", "Path to .gcdump file"); + var fileNameArgument = new Argument("filename", () => null, "Path to .gcdump file"); var cmd = new RootCommand { fileNameArgument }; cmd.SetHandler(HandleView, fileNameArgument); return cmd.Invoke(args); } - static void HandleView(string inputFileName) + static void HandleView(string? inputFileName) { AppBuilder .Configure() .UsePlatformDetect() .WithInterFont() .LogToTrace() - .StartWithClassicDesktopLifetime(new[] { inputFileName }); + .StartWithClassicDesktopLifetime(inputFileName != null ? new[] { inputFileName } : Array.Empty()); } } diff --git a/src/OneHub.Tools.HeapView/Properties/launchSettings.json b/src/OneHub.Tools.HeapView/Properties/launchSettings.json new file mode 100644 index 0000000..22e7ba5 --- /dev/null +++ b/src/OneHub.Tools.HeapView/Properties/launchSettings.json @@ -0,0 +1,7 @@ +{ + "profiles": { + "dotnet-heapview": { + "commandName": "Project" + } + } +} \ No newline at end of file