Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Drag-n'-Drop support #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

using Avalonia.Markup.Xaml;
using Lumper.UI.ViewModels;
using Lumper.UI.Views;
Expand All @@ -17,10 +18,12 @@ public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime
desktop)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just revert this file so the drag and drop commit only touches file it has to

desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel()
};
}

base.OnFrameworkInitializationCompleted();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lumper.UI/ViewModels/MainWindowViewModel.IO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private async Task LoadBsp(IStorageFile file)
if(!file.TryGetUri(out var path))
{
throw new Exception("Failed to get file path");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, revert or move formatting stuff to a separate commit


}
LoadBsp(path.AbsolutePath);
}
Expand Down
53 changes: 53 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;
Riven-Spell marked this conversation as resolved.
Show resolved Hide resolved
using Lumper.UI.ViewModels.Bsp;
using ReactiveUI;

Expand All @@ -27,6 +31,11 @@ public MainWindowViewModel()
SearchInit();
TabsInit();
IOInit();

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

public IClassicDesktopStyleApplicationLifetime Desktop
Expand All @@ -45,4 +54,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
16 changes: 16 additions & 0 deletions src/Lumper.UI/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
using System;
using System.ComponentModel;
using Avalonia.Controls;
using Avalonia.Input;
using Lumper.UI.ViewModels;

namespace Lumper.UI.Views;

public partial class MainWindow : Window
{
// Quietly shadow DataContext to add initializer
public new object? DataContext
{
init // DataContext is never overwritten-- It's ok to not defensively remove the Drop handler, and do DragOver here.
{
var dc = value as MainWindowViewModel; // It must be (as of 3/16/23)
AddHandler(DragDrop.DropEvent, dc!.Drop);
AddHandler(DragDrop.DragOverEvent, MainWindowViewModel.DragOver);

base.DataContext = value;
}

get => base.DataContext;
}

public MainWindow()
{
InitializeComponent();
Expand Down