Skip to content

Commit

Permalink
Feature: Supported files can now be opened using drag and drop into t…
Browse files Browse the repository at this point in the history
…he app window.

Issue #17
  • Loading branch information
deanthecoder committed Feb 3, 2024
1 parent 030bc50 commit 7f027d5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Speculator/CSharp.Utils/Commands/FileOpenCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ await TopLevel
}
}
});
var selectedFile = files.FirstOrDefault();
var selectedFile = files.FirstOrDefault()?.ToFileInfo();
if (selectedFile != null)
FileSelected?.Invoke(this, new FileInfo(selectedFile.Path.LocalPath));
FileSelected?.Invoke(this, selectedFile);
else
Cancelled?.Invoke(this, null);
}
Expand Down
23 changes: 23 additions & 0 deletions Speculator/CSharp.Utils/Extensions/IStorageItemExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Code authored by Dean Edis (DeanTheCoder).
// Anyone is free to copy, modify, use, compile, or distribute this software,
// either in source code form or as a compiled binary, for any non-commercial
// purpose.
//
// If you modify the code, please retain this copyright header,
// and consider contributing back to the repository or letting us know
// about your modifications. Your contributions are valued!
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND.

using Avalonia.Platform.Storage;

namespace CSharp.Utils.Extensions;

public static class IStorageItemExtensions
{
public static FileInfo ToFileInfo(this IStorageFile storageFile) =>
storageFile != null ? new FileInfo(storageFile.Path.LocalPath) : null;

public static DirectoryInfo ToDirectoryInfo(this IStorageFolder storageFolder) =>
storageFolder != null ? new DirectoryInfo(storageFolder.Path.LocalPath) : null;
}
10 changes: 8 additions & 2 deletions Speculator/Speculator/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND.

using System;
using System.IO;
using Avalonia;
using Avalonia.Threading;
using CSharp.Utils.Commands;
Expand Down Expand Up @@ -67,8 +68,7 @@ public void LoadGameRom()
{
try
{
Speccy.LoadRom(info);
Mru.Add(info);
LoadGameRomDirect(info);
}
finally
{
Expand All @@ -79,6 +79,12 @@ public void LoadGameRom()
command.Execute(null);
}

public void LoadGameRomDirect(FileInfo info)
{
Speccy.LoadRom(info);
Mru.Add(info);
}

public void SaveGameRom()
{
var keyBlocker = Speccy.PortHandler.CreateKeyBlocker();
Expand Down
1 change: 1 addition & 0 deletions Speculator/Speculator/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Title="ZX Speculator"
Width="1066" Height="634"
MinWidth="520" MinHeight="320"
DragDrop.AllowDrop="True"
x:Name="Self">
<Design.DataContext>
<vm:MainWindowViewModel />
Expand Down
13 changes: 11 additions & 2 deletions Speculator/Speculator/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND.

using System;
using System.Threading.Tasks;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Avalonia.Platform.Storage;
using CSharp.Utils.Extensions;
using CSharp.Utils.UI;
using Material.Icons.Avalonia;
using Speculator.ViewModels;

// ReSharper disable UnusedParameter.Local

namespace Speculator.Views;
Expand All @@ -31,6 +33,7 @@ public MainWindow()
{
InitializeComponent();

AddHandler(DragDrop.DropEvent, OnDrop);
Closing += (_, args) => AppCloseHandler.Instance.OnMainWindowClosing(args);
Closed += (_, _) => (DataContext as IDisposable)?.Dispose();
}
Expand Down Expand Up @@ -77,4 +80,10 @@ private void OnKeyboardIconLoaded(object sender, RoutedEventArgs e)
};
icon.PointerExited += (_, _) => Keyboard.Opacity = 0.0;
}

private void OnDrop(object sender, DragEventArgs e)
{
if (e.Data.GetFiles()?.FirstOrDefault() is IStorageFile file)
((MainWindowViewModel)DataContext)?.LoadGameRomDirect(file.ToFileInfo());
}
}

0 comments on commit 7f027d5

Please sign in to comment.