using PixiEditor.Helpers; using PixiEditor.Models.Undo; using System; using System.IO; namespace PixiEditor.ViewModels.SubViewModels.Main { public class UndoViewModel : SubViewModel { public RelayCommand UndoCommand { get; set; } public RelayCommand RedoCommand { get; set; } public event EventHandler UndoRedoCalled; public UndoViewModel(ViewModelMain owner) : base(owner) { UndoCommand = new RelayCommand(Undo, CanUndo); RedoCommand = new RelayCommand(Redo, CanRedo); var result = Directory.CreateDirectory(StorageBasedChange.DefaultUndoChangeLocation); //ClearUndoTempDirectory(); } /// /// Redo last action. /// /// CommandProperty. public void Redo(object parameter) { UndoRedoCalled?.Invoke(this, EventArgs.Empty); //sometimes CanRedo gets changed after UndoRedoCalled invoke, so check again (normally this is checked by the relaycommand) if (CanRedo(null)) { Owner.BitmapManager.ActiveDocument.UndoManager.Redo(); Owner.BitmapManager.ActiveDocument.ChangesSaved = false; } } /// /// Undo last action. /// /// CommandParameter. public void Undo(object parameter) { UndoRedoCalled?.Invoke(this, EventArgs.Empty); //sometimes CanUndo gets changed after UndoRedoCalled invoke, so check again (normally this is checked by the relaycommand) if (CanUndo(null)) { Owner.BitmapManager.ActiveDocument.UndoManager.Undo(); Owner.BitmapManager.ActiveDocument.ChangesSaved = false; } } /// /// Removes all files from %tmp%/PixiEditor/UndoStack/. /// public void ClearUndoTempDirectory() { DirectoryInfo dirInfo = new DirectoryInfo(StorageBasedChange.DefaultUndoChangeLocation); foreach (FileInfo file in dirInfo.GetFiles()) { file.Delete(); } } /// /// Returns true if undo can be done. /// /// CommandParameter. /// True if can undo. private bool CanUndo(object property) { return Owner.BitmapManager.ActiveDocument?.UndoManager.CanUndo ?? false; } /// /// Returns true if redo can be done. /// /// CommandProperty. /// True if can redo. private bool CanRedo(object property) { return Owner.BitmapManager.ActiveDocument?.UndoManager.CanRedo ?? false; } } }