Skip to content

Commit

Permalink
feat: enable drag-n-drop
Browse files Browse the repository at this point in the history
  • Loading branch information
Riven-Spell committed Mar 13, 2023
1 parent 065e7d9 commit 1832b25
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
14 changes: 13 additions & 1 deletion src/Lumper.UI/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input;
using Avalonia.Markup.Xaml;
using Lumper.UI.ViewModels;
using Lumper.UI.Views;
Expand All @@ -17,11 +18,22 @@ public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime
desktop)
{
var vm = new MainWindowViewModel();
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel()
DataContext = vm,
};

desktop.MainWindow.AddHandler(DragDrop.DragOverEvent, MainWindowViewModel.DragOver);
desktop.MainWindow.AddHandler(DragDrop.DropEvent, vm.Drop);

if (desktop.Args?.Length > 0)
{
vm.LoadBsp(desktop.Args[0]);
}
}

base.OnFrameworkInitializationCompleted();
}
}
6 changes: 3 additions & 3 deletions src/Lumper.UI/ViewModels/MainWindowViewModel.IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ private async void Save(string path)
_bspModel.FilePath = path;
}

private void LoadBsp(string path)
public void LoadBsp(string path)
{
var bspFile = new BspFile(path);
BspModel = new BspViewModel(this, bspFile);
}

private async Task LoadBsp(IStorageFile file)
public async Task LoadBsp(IStorageFile file)
{
if (!file.CanOpenRead)
return;
Expand All @@ -163,7 +163,7 @@ private async Task LoadBsp(IStorageFile file)
if(!file.TryGetUri(out var path))
{
throw new Exception("Failed to get file path");

}
LoadBsp(path.AbsolutePath);
}
Expand Down
48 changes: 48 additions & 0 deletions src/Lumper.UI/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input;
using Lumper.UI.ViewModels.Bsp;
using ReactiveUI;

Expand Down Expand Up @@ -45,4 +49,48 @@ public BspNodeBase? SelectedNode
get => _selectedNode;
set => this.RaiseAndSetIfChanged(ref _selectedNode, value);
}

internal static void DragOver(object? sender, DragEventArgs e)
{
e.DragEffects = DragDropEffects.Link;

var names = e.Data.GetFileNames() ?? new List<string>();

if (!e.Data.Contains(DataFormats.FileNames) || !names.FirstOrDefault("").ToLower().EndsWith(".bsp"))
e.DragEffects = DragDropEffects.None;
}

internal void Drop(object? sender, DragEventArgs e)
{
if (!e.Data.Contains(DataFormats.FileNames))
return;

var names = e.Data.GetFileNames() ?? new List<string>();
foreach (string target in names)
{
if (!target.ToLower().EndsWith(".bsp"))
continue;

if (BspModel == null)
{
// if nothing is open, open it.
LoadBsp(target);
}
else
{
// Otherwise, open a brand new Lumper instance with it.
string? executableFileName = Process.GetCurrentProcess().MainModule?.FileName;
if (executableFileName == null)
return;

ProcessStartInfo startInfo = new()
{
ArgumentList = { $"{target}" },
FileName = executableFileName,
};

Process.Start(startInfo);
}
}
}
}
1 change: 1 addition & 0 deletions src/Lumper.UI/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
TransparencyLevelHint="AcrylicBlur"
ExtendClientAreaToDecorationsHint="True"
WindowStartupLocation="CenterScreen"
DragDrop.AllowDrop="True"
Background="Transparent">

<Design.DataContext>
Expand Down

0 comments on commit 1832b25

Please sign in to comment.