Skip to content

Commit

Permalink
Implement opening new lumper instance
Browse files Browse the repository at this point in the history
  • Loading branch information
Riven-Spell committed Mar 12, 2023
1 parent 359ae2c commit 486cf53
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 78 deletions.
61 changes: 34 additions & 27 deletions src/Lumper.UI/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Lumper.UI.ViewModels;
using Lumper.UI.Views;

namespace Lumper.UI;

public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime
desktop)
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel()
};

base.OnFrameworkInitializationCompleted();
}
}
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Lumper.UI.ViewModels;
using Lumper.UI.Views;

namespace Lumper.UI;

public class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime
desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel()
};

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

base.OnFrameworkInitializationCompleted();
}
}
125 changes: 74 additions & 51 deletions src/Lumper.UI/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,74 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Input;
using Lumper.UI.ViewModels;

namespace Lumper.UI.Views;

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

AddHandler(DragDrop.DragOverEvent, DragOver);
AddHandler(DragDrop.DropEvent, Drop);
}

private async void Window_OnClosing(object? sender, CancelEventArgs e)
{
if (DataContext is not MainWindowViewModel model)
throw new ArgumentOutOfRangeException();
await model.OnClose(e);
}

private 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;
}

private void Drop(object? sender, DragEventArgs e)
{
if (e.Data.Contains(DataFormats.FileNames))
{
var names = e.Data.GetFileNames() ?? new List<String>();
var target = names.FirstOrDefault("");

if (!target.ToLower().EndsWith(".bsp"))
return;

((MainWindowViewModel)DataContext!).LoadBsp(target);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Lumper.UI.ViewModels;

namespace Lumper.UI.Views;

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

AddHandler(DragDrop.DragOverEvent, DragOver);
AddHandler(DragDrop.DropEvent, Drop);
}

private async void Window_OnClosing(object? sender, CancelEventArgs e)
{
if (DataContext is not MainWindowViewModel model)
throw new ArgumentOutOfRangeException();
await model.OnClose(e);
}

private 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;
}

private void Drop(object? sender, DragEventArgs e)
{
if (e.Data.Contains(DataFormats.FileNames))
{
var names = e.Data.GetFileNames() ?? new List<String>();
foreach (string target in names)
{
if (!target.ToLower().EndsWith(".bsp"))
continue;

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

string? executablePath = mainModuleFileName;
ProcessStartInfo startInfo = new ProcessStartInfo()
{
ArgumentList = { $"{target}" },
FileName = executablePath,
};

Process.Start(startInfo);
}
}
}
}
}

0 comments on commit 486cf53

Please sign in to comment.