Skip to content

Commit

Permalink
Fix crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Equbuxu committed Dec 12, 2021
1 parent f577f8f commit aa5b107
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,26 @@ namespace PixiEditor.Helpers.Converters
{
class DockingManagerActiveContentConverter : IValueConverter
{
private Document cachedDocument = null;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return DependencyProperty.UnsetValue;
if (value is Document document)
{
cachedDocument = document;
return document;
}
return DependencyProperty.UnsetValue;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Document document)
return document;
if (value != null && cachedDocument != null && !cachedDocument.Disposed)
return cachedDocument;
cachedDocument = null;
return DependencyProperty.UnsetValue;
}
}
Expand Down
5 changes: 5 additions & 0 deletions PixiEditor/Models/DataHolders/Document/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public Selection ActiveSelection
}
}

public bool Disposed { get; private set; } = false;

public ExecutionTrigger<Size> CenterViewportTrigger { get; } = new();
public ExecutionTrigger<double> ZoomViewportTrigger { get; } = new();

Expand Down Expand Up @@ -187,6 +189,9 @@ public void CenterContent()

public void Dispose()
{
if (Disposed)
return;
Disposed = true;
DisposeLayerBitmaps();
UndoManager.Dispose();

Expand Down
5 changes: 5 additions & 0 deletions PixiEditor/Models/DataHolders/Surface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class Surface : IDisposable
public int Width { get; }
public int Height { get; }

public bool Disposed { get; private set; } = false;

private SKPaint drawingPaint = new SKPaint() { BlendMode = SKBlendMode.Src };
private IntPtr surfaceBuffer;

Expand Down Expand Up @@ -152,6 +154,9 @@ public WriteableBitmap ToWriteableBitmap()

public void Dispose()
{
if (Disposed)
return;
Disposed = true;
SkiaSurface.Dispose();
drawingPaint.Dispose();
Marshal.FreeHGlobal(surfaceBuffer);
Expand Down
9 changes: 4 additions & 5 deletions PixiEditor/ViewModels/SubViewModels/Main/DocumentViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using System.Linq;
using PixiEditor.Helpers;
using PixiEditor.Helpers;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Dialogs;
using PixiEditor.Models.Enums;
using System.Linq;

namespace PixiEditor.ViewModels.SubViewModels.Main
{
Expand Down Expand Up @@ -39,7 +38,7 @@ public void FlipDocument(object parameter)
{
Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Horizontal);
}
else if(parameter is "Vertical")
else if (parameter is "Vertical")
{
Owner.BitmapManager.ActiveDocument?.FlipActiveDocument(FlipType.Vertical);
}
Expand Down Expand Up @@ -109,4 +108,4 @@ private void CenterContent(object property)
Owner.BitmapManager.ActiveDocument.CenterContent();
}
}
}
}
7 changes: 5 additions & 2 deletions PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using PixiEditor.Helpers;
using PixiEditor.Models.Controllers;
using PixiEditor.Models.Layers;
using PixiEditor.Views.UserControls;
using PixiEditor.Views.UserControls.Layers;
using System;
using System.Linq;
Expand Down Expand Up @@ -73,7 +72,11 @@ public void CreateGroupFromActiveLayers(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)
Expand Down
5 changes: 3 additions & 2 deletions PixiEditor/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,12 @@
</StackPanel>
<Grid Grid.Column="1" Grid.Row="2" Background="#303030" >
<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>
<avalonDockTheme:PixiEditorDockTheme />
</DockingManager.Theme>

<avalondock:DockingManager.LayoutItemContainerStyleSelector>
<ui:PanelsStyleSelector>
<ui:PanelsStyleSelector.DocumentTabStyle>
Expand Down
8 changes: 6 additions & 2 deletions PixiEditor/Views/UserControls/Layers/LayersManager.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using PixiEditor.Helpers;
using PixiEditor.Models.Controllers;
using PixiEditor.Models.Controllers;
using PixiEditor.Models.DataHolders;
using PixiEditor.Models.Layers;
using PixiEditor.Models.Undo;
Expand Down Expand Up @@ -125,6 +124,9 @@ private void HandleGroupOpacityChange(GuidStructureItem group, float value)
{
var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;

if (group.Opacity == value)
return;

var processArgs = new object[] { group.GroupGuid, value };
var reverseProcessArgs = new object[] { group.GroupGuid, group.Opacity };

Expand Down Expand Up @@ -198,6 +200,8 @@ private void NumberInput_LostFocus(object sender, RoutedEventArgs e)
private void HandleLayerOpacityChange(float val, Layer layer)
{
float oldOpacity = layer.Opacity;
if (oldOpacity == val)
return;

var doc = LayerCommandsViewModel.Owner.BitmapManager.ActiveDocument;

Expand Down
6 changes: 6 additions & 0 deletions PixiEditor/Views/UserControls/PlainLayerView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ private static void OnLayerChanged(DependencyObject sender, DependencyPropertyCh
var view = (PlainLayerView)sender;
if (args.OldValue != null)
((Layer)args.OldValue).LayerBitmapChanged -= view.OnLayerBitmapChanged;

if (args.NewValue != null)
{
var layer = ((Layer)args.NewValue);
if (layer.LayerBitmap.Disposed)
{
view.TargetLayer = null;
return;
}
layer.LayerBitmapChanged += view.OnLayerBitmapChanged;
view.Resize(layer.Width, layer.Height);
}
Expand Down

0 comments on commit aa5b107

Please sign in to comment.