Skip to content

Commit

Permalink
Allow drag'n'drop of .gcdump on the main window and launching without…
Browse files Browse the repository at this point in the history
… parameter
  • Loading branch information
filipnavara committed Aug 10, 2023
1 parent be57156 commit e7f3f74
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
14 changes: 10 additions & 4 deletions src/OneHub.Tools.HeapView/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/OneHub.Tools.HeapView/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<heapView:HeapView Name="heapView">
</heapView:HeapView>
</Window>
46 changes: 40 additions & 6 deletions src/OneHub.Tools.HeapView/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -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)

Check warning on line 28 in src/OneHub.Tools.HeapView/MainWindow.axaml.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used

Check warning on line 28 in src/OneHub.Tools.HeapView/MainWindow.axaml.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'ex' is declared but never used
{
// 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<IStorageFile>().ToArray();
if (files?.FirstOrDefault()?.TryGetLocalPath() is string path)
{
Open(path);
}
}
}
}
6 changes: 3 additions & 3 deletions src/OneHub.Tools.HeapView/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ class Program
[STAThread]
public static int Main(string[] args)
{
var fileNameArgument = new Argument<string>("filename", "Path to .gcdump file");
var fileNameArgument = new Argument<string?>("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<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace()
.StartWithClassicDesktopLifetime(new[] { inputFileName });
.StartWithClassicDesktopLifetime(inputFileName != null ? new[] { inputFileName } : Array.Empty<string>());
}
}
7 changes: 7 additions & 0 deletions src/OneHub.Tools.HeapView/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"profiles": {
"dotnet-heapview": {
"commandName": "Project"
}
}
}

0 comments on commit e7f3f74

Please sign in to comment.