Bladeren bron

Added command for opening files from clipboard and fixed not being able to paste .pixi files

CPKreuz 2 jaren geleden
bovenliggende
commit
843d2480f8

+ 15 - 2
src/PixiEditor/Models/Controllers/ClipboardController.cs

@@ -13,6 +13,7 @@ using PixiEditor.DrawingApi.Core.Surface.ImageData;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.IO;
+using PixiEditor.Parser;
 using PixiEditor.ViewModels.SubViewModels.Document;
 
 namespace PixiEditor.Models.Controllers;
@@ -132,8 +133,19 @@ internal static class ClipboardController
                     continue;
                 try
                 {
-                    Surface imported = Surface.Load(path);
-                    string filename = Path.GetFileName(path);
+                    Surface imported;
+                    
+                    if (Path.GetExtension(path) == ".pixi")
+                    {
+                        using var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
+                        imported = Surface.Load(PixiParser.ReadPreview(stream));
+                    }
+                    else
+                    {
+                        imported = Surface.Load(path);
+                    }
+
+                    string filename = Path.GetFullPath(path);
                     surfaces.Add((filename, imported));
                 }
                 catch
@@ -145,6 +157,7 @@ internal static class ClipboardController
         return surfaces;
     }
 
+    [Evaluator.CanExecute("PixiEditor.Clipboard.HasImageInClipboard")]
     public static bool IsImageInClipboard() => IsImage(ClipboardHelper.TryGetDataObject());
     
     public static bool IsImage(DataObject? dataObject)

+ 4 - 0
src/PixiEditor/Models/Dialogs/ImportFileDialog.cs

@@ -55,6 +55,10 @@ internal class ImportFileDialog : CustomDialog
         {
             FilePath = FilePath
         };
+
+        if (FileWidth != 0) popup.ImportWidth = FileWidth;
+        if (FileHeight != 0) popup.ImportHeight = FileHeight;
+        
         popup.ShowDialog();
         if (popup.DialogResult == true)
         {

+ 2 - 1
src/PixiEditor/Models/DocumentModels/Public/DocumentOperationsModule.cs

@@ -1,4 +1,5 @@
 using System.Collections.Immutable;
+using System.IO;
 using ChunkyImageLib;
 using ChunkyImageLib.DataHolders;
 using PixiEditor.ChangeableDocument.Actions.Undo;
@@ -120,7 +121,7 @@ internal class DocumentOperationsModule
 
         foreach (var imageWithName in images)
         {
-            var layerGuid = Internals.StructureHelper.CreateNewStructureMember(StructureMemberType.Layer, imageWithName.name);
+            var layerGuid = Internals.StructureHelper.CreateNewStructureMember(StructureMemberType.Layer, Path.GetFileName(imageWithName.name));
             DrawImage(imageWithName.image, new ShapeCorners(new RectD(VecD.Zero, imageWithName.image.Size)), layerGuid, true, false, false);
         }
         Internals.ActionAccumulator.AddFinishedActions();

+ 13 - 3
src/PixiEditor/Models/IO/Importer.cs

@@ -61,12 +61,17 @@ internal class Importer : NotifyableObject
         }
     }
 
-    public static DocumentViewModel ImportDocument(string path)
+    public static DocumentViewModel ImportDocument(string path, bool associatePath = true)
     {
         try
         {
             var doc = PixiParser.Deserialize(path).ToDocument();
-            doc.FullFilePath = path;
+            
+            if (associatePath)
+            {
+                doc.FullFilePath = path;
+            }
+
             return doc;
         }
         catch (InvalidFileException)
@@ -74,7 +79,12 @@ internal class Importer : NotifyableObject
             try
             {
                 var doc = DepractedPixiParser.Deserialize(path).ToDocument();
-                doc.FullFilePath = path;
+                
+                if (associatePath)
+                {
+                    doc.FullFilePath = path;
+                }
+
                 return doc;
             }
             catch (InvalidFileException e)

+ 70 - 14
src/PixiEditor/ViewModels/SubViewModels/Main/FileViewModel.cs

@@ -138,6 +138,23 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
         OpenFromPath(dialog.FileName);
     }
 
+    [Command.Basic("PixiEditor.File.OpenFileFromClipboard", "Open from clipboard", "Open from clipboard", CanExecute = "PixiEditor.Clipboard.HasImageInClipboard")]
+    public void OpenFromClipboard()
+    {
+        var images = ClipboardController.GetImagesFromClipboard();
+
+        foreach (var (name, image) in images)
+        {
+            if (name == null)
+            {
+                OpenRegularImage(image, null);
+                continue;
+            }
+            
+            OpenFromPath(name, false);
+        }
+    }
+
     private bool MakeExistingDocumentActiveIfOpened(string path)
     {
         foreach (DocumentViewModel document in Owner.DocumentManagerSubViewModel.Documents)
@@ -154,8 +171,7 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
     /// <summary>
     /// Tries to open the passed file if it isn't already open
     /// </summary>
-    /// <param name="path"></param>
-    public void OpenFromPath(string path)
+    public void OpenFromPath(string path, bool associatePath = true)
     {
         if (MakeExistingDocumentActiveIfOpened(path))
             return;
@@ -164,11 +180,11 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
         {
             if (path.EndsWith(".pixi"))
             {
-                OpenDotPixi(path);
+                OpenDotPixi(path, associatePath);
             }
             else
             {
-                OpenRegularImage(path);
+                OpenRegularImage(path, associatePath);
             }
         }
         catch (CorruptedFileException ex)
@@ -184,9 +200,9 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
     /// <summary>
     /// Opens a .pixi file from path, creates a document from it, and adds it to the system
     /// </summary>
-    private void OpenDotPixi(string path)
+    private void OpenDotPixi(string path, bool associatePath = true)
     {
-        DocumentViewModel document = Importer.ImportDocument(path);
+        DocumentViewModel document = Importer.ImportDocument(path, associatePath);
         AddDocumentViewModelToTheSystem(document);
         AddRecentlyOpened(document.FullFilePath);
     }
@@ -204,7 +220,7 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
     /// <summary>
     /// Opens a regular image file from path, creates a document from it, and adds it to the system.
     /// </summary>
-    private void OpenRegularImage(string path)
+    private void OpenRegularImage(string path, bool associatePath)
     {
         ImportFileDialog dialog = new ImportFileDialog();
 
@@ -213,17 +229,57 @@ internal class FileViewModel : SubViewModel<ViewModelMain>
             dialog.FilePath = path;
         }
 
-        if (dialog.ShowDialog())
+        if (!dialog.ShowDialog())
         {
-            DocumentViewModel doc = NewDocument(b => b
+            return;
+        }
+
+        DocumentViewModel doc = NewDocument(b => b
+            .WithSize(dialog.FileWidth, dialog.FileHeight)
+            .WithLayer(l => l
+                .WithName("Image")
                 .WithSize(dialog.FileWidth, dialog.FileHeight)
-                .WithLayer(l => l
-                    .WithName("Image")
-                    .WithSize(dialog.FileWidth, dialog.FileHeight)
-                    .WithSurface(Importer.ImportImage(dialog.FilePath, new VecI(dialog.FileWidth, dialog.FileHeight)))));
+                .WithSurface(Importer.ImportImage(dialog.FilePath, new VecI(dialog.FileWidth, dialog.FileHeight)))));
+
+        if (associatePath)
+        {
             doc.FullFilePath = path;
-            AddRecentlyOpened(path);
         }
+
+        AddRecentlyOpened(path);
+    }
+
+    /// <summary>
+    /// Opens a regular image file from path, creates a document from it, and adds it to the system.
+    /// </summary>
+    private void OpenRegularImage(Surface surface, string path)
+    {
+        ImportFileDialog dialog = new ImportFileDialog( );
+
+        dialog.FileWidth = surface.Size.X;
+        dialog.FileHeight = surface.Size.Y;
+        
+        if (!dialog.ShowDialog())
+        {
+            return;
+        }
+
+        surface.ResizeNearestNeighbor(new VecI(dialog.FileWidth, dialog.FileHeight));
+            
+        DocumentViewModel doc = NewDocument(b => b
+            .WithSize(dialog.FileWidth, dialog.FileHeight)
+            .WithLayer(l => l
+                .WithName("Image")
+                .WithSize(dialog.FileWidth, dialog.FileHeight)
+                .WithSurface(surface)));
+
+        if (path == null)
+        {
+            return;
+        }
+
+        doc.FullFilePath = path;
+        AddRecentlyOpened(path);
     }
 
     [Command.Basic("PixiEditor.File.New", "New image", "Create new image", Key = Key.N, Modifiers = ModifierKeys.Control)]

+ 1 - 1
src/PixiEditor/Views/Dialogs/ImportFilePopup.xaml.cs

@@ -19,7 +19,7 @@ internal partial class ImportFilePopup : Window
     public int ImportHeight
     {
         get => dc.ImportHeight;
-        set => dc.ImportWidth = value;
+        set => dc.ImportHeight = value;
     }