UndoViewModel.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using PixiEditor.Helpers;
  2. using PixiEditor.Models.Undo;
  3. using System;
  4. using System.IO;
  5. namespace PixiEditor.ViewModels.SubViewModels.Main
  6. {
  7. public class UndoViewModel : SubViewModel<ViewModelMain>
  8. {
  9. public RelayCommand UndoCommand { get; set; }
  10. public RelayCommand RedoCommand { get; set; }
  11. public event EventHandler UndoRedoCalled;
  12. public UndoViewModel(ViewModelMain owner)
  13. : base(owner)
  14. {
  15. UndoCommand = new RelayCommand(Undo, CanUndo);
  16. RedoCommand = new RelayCommand(Redo, CanRedo);
  17. var result = Directory.CreateDirectory(StorageBasedChange.DefaultUndoChangeLocation);
  18. //ClearUndoTempDirectory();
  19. }
  20. /// <summary>
  21. /// Redo last action.
  22. /// </summary>
  23. /// <param name="parameter">CommandProperty.</param>
  24. public void Redo(object parameter)
  25. {
  26. UndoRedoCalled?.Invoke(this, EventArgs.Empty);
  27. //sometimes CanRedo gets changed after UndoRedoCalled invoke, so check again (normally this is checked by the relaycommand)
  28. if (CanRedo(null))
  29. {
  30. Owner.BitmapManager.ActiveDocument.UndoManager.Redo();
  31. Owner.BitmapManager.ActiveDocument.ChangesSaved = false;
  32. }
  33. }
  34. /// <summary>
  35. /// Undo last action.
  36. /// </summary>
  37. /// <param name="parameter">CommandParameter.</param>
  38. public void Undo(object parameter)
  39. {
  40. UndoRedoCalled?.Invoke(this, EventArgs.Empty);
  41. //sometimes CanUndo gets changed after UndoRedoCalled invoke, so check again (normally this is checked by the relaycommand)
  42. if (CanUndo(null))
  43. {
  44. Owner.BitmapManager.ActiveDocument.UndoManager.Undo();
  45. Owner.BitmapManager.ActiveDocument.ChangesSaved = false;
  46. }
  47. }
  48. /// <summary>
  49. /// Removes all files from %tmp%/PixiEditor/UndoStack/.
  50. /// </summary>
  51. public void ClearUndoTempDirectory()
  52. {
  53. DirectoryInfo dirInfo = new DirectoryInfo(StorageBasedChange.DefaultUndoChangeLocation);
  54. foreach (FileInfo file in dirInfo.GetFiles())
  55. {
  56. file.Delete();
  57. }
  58. }
  59. /// <summary>
  60. /// Returns true if undo can be done.
  61. /// </summary>
  62. /// <param name="property">CommandParameter.</param>
  63. /// <returns>True if can undo.</returns>
  64. private bool CanUndo(object property)
  65. {
  66. return Owner.BitmapManager.ActiveDocument?.UndoManager.CanUndo ?? false;
  67. }
  68. /// <summary>
  69. /// Returns true if redo can be done.
  70. /// </summary>
  71. /// <param name="property">CommandProperty.</param>
  72. /// <returns>True if can redo.</returns>
  73. private bool CanRedo(object property)
  74. {
  75. return Owner.BitmapManager.ActiveDocument?.UndoManager.CanRedo ?? false;
  76. }
  77. }
  78. }