Skip to content

Open files by drag&drop to exe #92

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

Merged
merged 1 commit into from
Oct 9, 2024
Merged
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
6 changes: 3 additions & 3 deletions Pixed/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using Microsoft.Extensions.DependencyInjection;
using Pixed.Controls;
using Pixed.DependencyInjection;
using Pixed.ViewModels;
using Pixed.Windows;

namespace Pixed;

public partial class App : Application
{
internal static IPixedServiceProvider ServiceProvider { get; private set; }

Check warning on line 13 in Pixed/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'ServiceProvider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 13 in Pixed/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Non-nullable property 'ServiceProvider' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
Expand All @@ -29,7 +28,8 @@

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
PixedWindow<MainViewModel> window = provider.Get<PixedWindow<MainViewModel>>();
MainWindow window = provider.Get<MainWindow>();
window.OpenFromArgs(desktop.Args);

Check warning on line 32 in Pixed/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Possible null reference argument for parameter 'args' in 'void MainWindow.OpenFromArgs(string[] args)'.

Check warning on line 32 in Pixed/App.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference argument for parameter 'args' in 'void MainWindow.OpenFromArgs(string[] args)'.
desktop.MainWindow = window;
}

Expand Down
2 changes: 1 addition & 1 deletion Pixed/DependencyInjection/DependencyInjectionRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void Register(ref IServiceCollection collection)
collection.AddSingleton<ProjectsSectionViewModel>();
collection.AddSingleton<ToolsSectionViewModel>();

collection.AddSingleton<PixedWindow<MainViewModel>, MainWindow>();
collection.AddSingleton<MainWindow>();

collection.AddScoped<ToolBucket>();
collection.AddScoped<ToolCircle>();
Expand Down
30 changes: 24 additions & 6 deletions Pixed/IO/PixedProjectMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,32 @@ public async Task Open(RecentFilesService recentFilesService)
public void Open(string path)
{
FileInfo info = new(path);
PixedProjectSerializer serializer = new();
IPixedProjectSerializer serializer;
Stream stream = File.OpenRead(path);
PixedModel model = serializer.Deserialize(stream, _applicationData);
stream?.Dispose();

model.FileName = info.Name;
model.FilePath = path;
if(path.EndsWith(".pixed"))
{
serializer = new PixedProjectSerializer();
}
else if(path.EndsWith(".piskel"))
{
serializer = new PiskelProjectSerializer();
}
else
{
serializer = new PngProjectSerializer();
}

PixedModel model = serializer.Deserialize(stream, _applicationData);
stream.Dispose();
model.FileName = info.Name.Replace(".png", ".pixed");
model.AddHistory();
model.UnsavedChanges = false;

if (info.Name.EndsWith(".pixed"))
{
model.FilePath = info.FullName;
model.UnsavedChanges = false;
}

if (_applicationData.CurrentModel.IsEmpty)
{
Expand All @@ -122,6 +139,7 @@ public void Open(string path)
{
_applicationData.Models.Add(model);
}

Subjects.ProjectAdded.OnNext(model);
}

Expand Down
12 changes: 12 additions & 0 deletions Pixed/Windows/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Pixed.Services.Keyboard;
using Pixed.Tools;
using Pixed.ViewModels;
using System.IO;
using System.Windows.Input;

namespace Pixed.Windows;
Expand Down Expand Up @@ -51,6 +52,17 @@ public override void OnLoaded()
Subjects.FrameChanged.OnNext(_applicationData.CurrentFrame);
}

public void OpenFromArgs(string[] args)
{
foreach(var arg in args)
{
if(File.Exists(arg))
{
_pixedProjectMethods.Open(arg);
}
}
}

protected override void OnInitialized()
{
base.OnInitialized();
Expand Down
Loading