Browse Source

Fix crashes

Equbuxu 3 years ago
parent
commit
aa5b107be4

+ 7 - 0
PixiEditor/Helpers/Converters/DockingManagerActiveContentConverter.cs

@@ -8,12 +8,16 @@ namespace PixiEditor.Helpers.Converters
 {
 {
     class DockingManagerActiveContentConverter : IValueConverter
     class DockingManagerActiveContentConverter : IValueConverter
     {
     {
+        private Document cachedDocument = null;
         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
         {
         {
             if (value == null)
             if (value == null)
                 return DependencyProperty.UnsetValue;
                 return DependencyProperty.UnsetValue;
             if (value is Document document)
             if (value is Document document)
+            {
+                cachedDocument = document;
                 return document;
                 return document;
+            }
             return DependencyProperty.UnsetValue;
             return DependencyProperty.UnsetValue;
         }
         }
 
 
@@ -21,6 +25,9 @@ namespace PixiEditor.Helpers.Converters
         {
         {
             if (value is Document document)
             if (value is Document document)
                 return document;
                 return document;
+            if (value != null && cachedDocument != null && !cachedDocument.Disposed)
+                return cachedDocument;
+            cachedDocument = null;
             return DependencyProperty.UnsetValue;
             return DependencyProperty.UnsetValue;
         }
         }
     }
     }

+ 5 - 0
PixiEditor/Models/DataHolders/Document/Document.cs

@@ -95,6 +95,8 @@ namespace PixiEditor.Models.DataHolders
             }
             }
         }
         }
 
 
+        public bool Disposed { get; private set; } = false;
+
         public ExecutionTrigger<Size> CenterViewportTrigger { get; } = new();
         public ExecutionTrigger<Size> CenterViewportTrigger { get; } = new();
         public ExecutionTrigger<double> ZoomViewportTrigger { get; } = new();
         public ExecutionTrigger<double> ZoomViewportTrigger { get; } = new();
 
 
@@ -187,6 +189,9 @@ namespace PixiEditor.Models.DataHolders
 
 
         public void Dispose()
         public void Dispose()
         {
         {
+            if (Disposed)
+                return;
+            Disposed = true;
             DisposeLayerBitmaps();
             DisposeLayerBitmaps();
             UndoManager.Dispose();
             UndoManager.Dispose();
 
 

+ 5 - 0
PixiEditor/Models/DataHolders/Surface.cs

@@ -23,6 +23,8 @@ namespace PixiEditor.Models.DataHolders
         public int Width { get; }
         public int Width { get; }
         public int Height { get; }
         public int Height { get; }
 
 
+        public bool Disposed { get; private set; } = false;
+
         private SKPaint drawingPaint = new SKPaint() { BlendMode = SKBlendMode.Src };
         private SKPaint drawingPaint = new SKPaint() { BlendMode = SKBlendMode.Src };
         private IntPtr surfaceBuffer;
         private IntPtr surfaceBuffer;
 
 
@@ -152,6 +154,9 @@ namespace PixiEditor.Models.DataHolders
 
 
         public void Dispose()
         public void Dispose()
         {
         {
+            if (Disposed)
+                return;
+            Disposed = true;
             SkiaSurface.Dispose();
             SkiaSurface.Dispose();
             drawingPaint.Dispose();
             drawingPaint.Dispose();
             Marshal.FreeHGlobal(surfaceBuffer);
             Marshal.FreeHGlobal(surfaceBuffer);

+ 4 - 5
PixiEditor/ViewModels/SubViewModels/Main/DocumentViewModel.cs

@@ -1,9 +1,8 @@
-using System;
-using System.Linq;
-using PixiEditor.Helpers;
+using PixiEditor.Helpers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Dialogs;
 using PixiEditor.Models.Enums;
 using PixiEditor.Models.Enums;
+using System.Linq;
 
 
 namespace PixiEditor.ViewModels.SubViewModels.Main
 namespace PixiEditor.ViewModels.SubViewModels.Main
 {
 {
@@ -39,7 +38,7 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             {
             {
                 Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Horizontal);
                 Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Horizontal);
             }
             }
-            else if(parameter is "Vertical")
+            else if (parameter is "Vertical")
             {
             {
                 Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Vertical);
                 Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Vertical);
             }
             }
@@ -109,4 +108,4 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
             Owner.BitmapManager.ActiveDocument.CenterContent();
             Owner.BitmapManager.ActiveDocument.CenterContent();
         }
         }
     }
     }
-}
+}

+ 5 - 2
PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs

@@ -1,7 +1,6 @@
 using PixiEditor.Helpers;
 using PixiEditor.Helpers;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Controllers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
-using PixiEditor.Views.UserControls;
 using PixiEditor.Views.UserControls.Layers;
 using PixiEditor.Views.UserControls.Layers;
 using System;
 using System;
 using System.Linq;
 using System.Linq;
@@ -73,7 +72,11 @@ namespace PixiEditor.ViewModels.SubViewModels.Main
 
 
         public bool CanDeleteSelected(object parameter)
         public bool CanDeleteSelected(object parameter)
         {
         {
-            return (parameter is not null and (Layer or LayerGroup)) || (Owner.BitmapManager?.ActiveDocument?.ActiveLayer != null);
+            return (
+                (
+                    parameter is not null and (Layer or LayerGroup)) || (Owner.BitmapManager?.ActiveDocument?.ActiveLayer != null)
+                )
+                && Owner.BitmapManager.ActiveDocument != null;
         }
         }
 
 
         public void DeleteSelected(object parameter)
         public void DeleteSelected(object parameter)

+ 3 - 2
PixiEditor/Views/MainWindow.xaml

@@ -258,11 +258,12 @@
         </StackPanel>
         </StackPanel>
         <Grid Grid.Column="1" Grid.Row="2" Background="#303030" >
         <Grid Grid.Column="1" Grid.Row="2" Background="#303030" >
             <Grid AllowDrop="True" Drop="MainWindow_Drop">
             <Grid AllowDrop="True" Drop="MainWindow_Drop">
-                <DockingManager ActiveContent="{Binding BitmapManager.ActiveDocument, Mode=Default, Converter={StaticResource DockingManagerActiveContentConverter}}"
-                                           DocumentsSource="{Binding BitmapManager.Documents}" >
+                <DockingManager ActiveContent="{Binding BitmapManager.ActiveDocument, Mode=TwoWay, Converter={StaticResource DockingManagerActiveContentConverter}}"
+                                           DocumentsSource="{Binding BitmapManager.Documents}">
                     <DockingManager.Theme>
                     <DockingManager.Theme>
                         <avalonDockTheme:PixiEditorDockTheme />
                         <avalonDockTheme:PixiEditorDockTheme />
                     </DockingManager.Theme>
                     </DockingManager.Theme>
+
                     <avalondock:DockingManager.LayoutItemContainerStyleSelector>
                     <avalondock:DockingManager.LayoutItemContainerStyleSelector>
                         <ui:PanelsStyleSelector>
                         <ui:PanelsStyleSelector>
                             <ui:PanelsStyleSelector.DocumentTabStyle>
                             <ui:PanelsStyleSelector.DocumentTabStyle>

+ 6 - 2
PixiEditor/Views/UserControls/Layers/LayersManager.xaml.cs

@@ -1,5 +1,4 @@
-using PixiEditor.Helpers;
-using PixiEditor.Models.Controllers;
+using PixiEditor.Models.Controllers;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.DataHolders;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Layers;
 using PixiEditor.Models.Undo;
 using PixiEditor.Models.Undo;
@@ -125,6 +124,9 @@ namespace PixiEditor.Views.UserControls.Layers
             {
             {
                 var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
                 var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
 
 
+                if (group.Opacity == value)
+                    return;
+
                 var processArgs = new object[] { group.GroupGuid, value };
                 var processArgs = new object[] { group.GroupGuid, value };
                 var reverseProcessArgs = new object[] { group.GroupGuid, group.Opacity };
                 var reverseProcessArgs = new object[] { group.GroupGuid, group.Opacity };
 
 
@@ -198,6 +200,8 @@ namespace PixiEditor.Views.UserControls.Layers
         private void HandleLayerOpacityChange(float val, Layer layer)
         private void HandleLayerOpacityChange(float val, Layer layer)
         {
         {
             float oldOpacity = layer.Opacity;
             float oldOpacity = layer.Opacity;
+            if (oldOpacity == val)
+                return;
 
 
             var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
             var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;
 
 

+ 6 - 0
PixiEditor/Views/UserControls/PlainLayerView.xaml.cs

@@ -40,9 +40,15 @@ namespace PixiEditor.Views.UserControls
             var view = (PlainLayerView)sender;
             var view = (PlainLayerView)sender;
             if (args.OldValue != null)
             if (args.OldValue != null)
                 ((Layer)args.OldValue).LayerBitmapChanged -= view.OnLayerBitmapChanged;
                 ((Layer)args.OldValue).LayerBitmapChanged -= view.OnLayerBitmapChanged;
+
             if (args.NewValue != null)
             if (args.NewValue != null)
             {
             {
                 var layer = ((Layer)args.NewValue);
                 var layer = ((Layer)args.NewValue);
+                if (layer.LayerBitmap.Disposed)
+                {
+                    view.TargetLayer = null;
+                    return;
+                }
                 layer.LayerBitmapChanged += view.OnLayerBitmapChanged;
                 layer.LayerBitmapChanged += view.OnLayerBitmapChanged;
                 view.Resize(layer.Width, layer.Height);
                 view.Resize(layer.Width, layer.Height);
             }
             }