Skip to content

Commit

Permalink
Make unique undo dir for each pixieditor instance (also remove storag…
Browse files Browse the repository at this point in the history
…ebasedchange files on dispose)
  • Loading branch information
Equbuxu committed Dec 18, 2021
1 parent ffe2eab commit b49384d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
4 changes: 4 additions & 0 deletions PixiEditor/Models/Controllers/UndoManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public void AddUndoChange(Change change, bool invokedInsideSetter = false)
// Clears RedoStack if last move wasn't redo or undo and if redo stack is greater than 0.
if (lastChangeWasUndo == false && RedoStack.Count > 0)
{
foreach (var redo in RedoStack)
{
redo.Dispose();
}
RedoStack.Clear();
}

Expand Down
31 changes: 24 additions & 7 deletions PixiEditor/Models/Undo/StorageBasedChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ namespace PixiEditor.Models.Undo
/// <summary>
/// A class that allows to save layers on disk and load them on Undo/Redo.
/// </summary>
public class StorageBasedChange
public class StorageBasedChange : IDisposable
{
public static string DefaultUndoChangeLocation => Path.Join(Path.GetTempPath(), "PixiEditor", "UndoStack");
public static string DefaultUndoChangeLocation { get; } = Path.Join(Path.GetTempPath(), "PixiEditor", Guid.NewGuid().ToString(), "UndoStack");

public string UndoChangeLocation { get; set; }

Expand Down Expand Up @@ -162,7 +162,9 @@ public Change ToChange(Action<Layer[], UndoLayer[], object[]> undoProcess, objec
redoProcess(parameters);
};

return new Change(finalUndoProcess, processArgs, finalRedoProcess, redoProcessParameters, description);
var change = new Change(finalUndoProcess, processArgs, finalRedoProcess, redoProcessParameters, description);
change.DisposeProcess = (_, _) => Dispose();
return change;
}

/// <summary>
Expand Down Expand Up @@ -190,7 +192,9 @@ public Change ToChange(Action<Layer[], UndoLayer[], object[]> undoRedoProcess, o
undoRedoProcess(layers, StoredLayers, processParameters);
};

return new Change(finalProcess, processArgs, finalProcess, processArgs, description);
var change = new Change(finalProcess, processArgs, finalProcess, processArgs, description);
change.DisposeProcess = (_, _) => Dispose();
return change;
}

/// <summary>
Expand All @@ -215,7 +219,9 @@ public Change ToChange(Action<Layer[], UndoLayer[]> undoProcess, Action<object[]
redoProcess(parameters);
};

return new Change(finalUndoProcess, null, finalRedoProcess, redoProcessParameters, description);
var change = new Change(finalUndoProcess, null, finalRedoProcess, redoProcessParameters, description);
change.DisposeProcess = (_, _) => Dispose();
return change;
}

/// <summary>
Expand All @@ -240,7 +246,9 @@ public Change ToChange(Action<object[]> undoProcess, object[] undoProcessParamet
redoProcess(layers, StoredLayers);
};

return new Change(finalUndoProcess, undoProcessParameters, finalRedoProcess, null, description);
var change = new Change(finalUndoProcess, undoProcessParameters, finalRedoProcess, null, description);
change.DisposeProcess = (_, _) => Dispose();
return change;
}

/// <summary>
Expand All @@ -266,7 +274,9 @@ public Change ToChange(Action<object[]> undoProcess, object[] undoProcessParamet
redoProcess(layers, StoredLayers, parameters);
};

return new Change(finalUndoProcess, undoProcessParameters, finalRedoProcess, redoProcessArgs, description);
var change = new Change(finalUndoProcess, undoProcessParameters, finalRedoProcess, redoProcessArgs, description);
change.DisposeProcess = (_, _) => Dispose();
return change;
}

/// <summary>
Expand Down Expand Up @@ -363,5 +373,12 @@ private static void ApplyChunkToLayer(Layer layer, UndoLayer layerData, Surface

layer.LayerBitmap = targetSizeSurface;
}

public void Dispose()
{
var layers = LoadLayersFromDevice();
foreach (var layer in layers)
layer.LayerBitmap.Dispose();
}
}
}
8 changes: 3 additions & 5 deletions PixiEditor/ViewModels/SubViewModels/Main/UndoViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ public UndoViewModel(ViewModelMain owner)
{
UndoCommand = new RelayCommand(Undo, CanUndo);
RedoCommand = new RelayCommand(Redo, CanRedo);
if (!Directory.Exists(StorageBasedChange.DefaultUndoChangeLocation))
{
Directory.CreateDirectory(StorageBasedChange.DefaultUndoChangeLocation);
}

ClearUndoTempDirectory();
var result = Directory.CreateDirectory(StorageBasedChange.DefaultUndoChangeLocation);

//ClearUndoTempDirectory();
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion PixiEditor/ViewModels/ViewModelMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,17 @@ private bool RemoveDocumentWithSaveConfirmation()
if (result == ConfirmationType.Yes)
{
FileSubViewModel.SaveDocument(false);
//cancel was pressed in the save file dialog
if (!BitmapManager.ActiveDocument.ChangesSaved)
return false;
}
}

if (result != ConfirmationType.Canceled)
{
BitmapManager.Documents.Remove(BitmapManager.ActiveDocument);
var doc = BitmapManager.ActiveDocument;
BitmapManager.Documents.Remove(doc);
doc.Dispose();

return true;
}
Expand Down

0 comments on commit b49384d

Please sign in to comment.