UndoViewModel.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using Avalonia.Input;
  2. using PixiEditor.AvaloniaUI.Models.Commands.Attributes.Commands;
  3. using PixiEditor.AvaloniaUI.Models.Commands.Attributes.Evaluators;
  4. namespace PixiEditor.AvaloniaUI.ViewModels.SubViewModels;
  5. [Command.Group("PixiEditor.Undo", "UNDO")]
  6. internal class UndoViewModel : SubViewModel<ViewModelMain>
  7. {
  8. public UndoViewModel(ViewModelMain owner)
  9. : base(owner)
  10. {
  11. }
  12. /// <summary>
  13. /// Redo last action.
  14. /// </summary>
  15. [Command.Basic("PixiEditor.Undo.Redo", "REDO", "REDO_DESCRIPTIVE", CanExecute = "PixiEditor.Undo.CanRedo", Key = Key.Y, Modifiers = KeyModifiers.Control,
  16. IconPath = "Redo.png", MenuItemPath = "EDIT/REDO", MenuItemOrder = 1)]
  17. public void Redo()
  18. {
  19. var doc = Owner.DocumentManagerSubViewModel.ActiveDocument;
  20. if (doc is null || (!doc.UpdateableChangeActive && !doc.HasSavedRedo))
  21. return;
  22. doc.Operations.Redo();
  23. }
  24. /// <summary>
  25. /// Undo last action.
  26. /// </summary>
  27. [Command.Basic("PixiEditor.Undo.Undo", "UNDO", "UNDO_DESCRIPTIVE", CanExecute = "PixiEditor.Undo.CanUndo", Key = Key.Z, Modifiers = KeyModifiers.Control,
  28. IconPath = "Undo.png", MenuItemPath = "EDIT/UNDO", MenuItemOrder = 0)]
  29. public void Undo()
  30. {
  31. var doc = Owner.DocumentManagerSubViewModel.ActiveDocument;
  32. if (doc is null || (!doc.UpdateableChangeActive && !doc.HasSavedUndo))
  33. return;
  34. doc.Operations.Undo();
  35. }
  36. /// <summary>
  37. /// Returns true if undo can be done.
  38. /// </summary>
  39. /// <param name="property">CommandParameter.</param>
  40. /// <returns>True if can undo.</returns>
  41. [Evaluator.CanExecute("PixiEditor.Undo.CanUndo")]
  42. public bool CanUndo()
  43. {
  44. var doc = Owner.DocumentManagerSubViewModel.ActiveDocument;
  45. if (doc is null)
  46. return false;
  47. return doc.UpdateableChangeActive || doc.HasSavedUndo;
  48. }
  49. /// <summary>
  50. /// Returns true if redo can be done.
  51. /// </summary>
  52. /// <param name="property">CommandProperty.</param>
  53. /// <returns>True if can redo.</returns>
  54. [Evaluator.CanExecute("PixiEditor.Undo.CanRedo")]
  55. public bool CanRedo()
  56. {
  57. var doc = Owner.DocumentManagerSubViewModel.ActiveDocument;
  58. if (doc is null)
  59. return false;
  60. return doc.UpdateableChangeActive || doc.HasSavedRedo;
  61. }
  62. }