/// <summary> /// The main execution method to decouple actions. Track /// commands in this method if you need to Undo or Redo actions. /// </summary> public void Execute(ICommand command) { command.Execute(); UndoneCommands.Clear(); RedoneCommands.Clear(); Commands.Push(command); }
/// <summary> /// Method to redo actions if needed. /// </summary> public void Redo() { ICommand command; UndoneCommands.TryPop(out command); if (command == null) { return; } command.Redo(); RedoneCommands.Push(command); }
/// <summary> /// Method to purge all commands from the manager object /// to avoid stack overflow. Use as needed from top level /// tracking objects. Might be worth calling every 50 /// usages, etc to free up memory. /// </summary> public void PurgeCommands() { if (RedoneCommands.Count > MaximumCommandStackSize) { var commands = RedoneCommands.Take(MaximumCommandStackSize).Reverse().ToArray(); RedoneCommands.Clear(); RedoneCommands.PushRange(commands); } if (UndoneCommands.Count > MaximumCommandStackSize) { var commands = UndoneCommands.Skip(MaximumCommandStackSize).ToArray(); UndoneCommands.Clear(); UndoneCommands.TryPopRange(commands); } if (Commands.Count > MaximumCommandStackSize) { var commands = Commands.Take(MaximumCommandStackSize).Reverse().ToArray(); Commands.Clear(); Commands.PushRange(commands); } }
/// <summary> /// Method to clear all commands from the manager object /// to avoid stack overflow. Use when done. Once this /// is called, Undo and Redo context should be gone /// along with all executed commands. /// </summary> public void ClearCommands() { RedoneCommands.Clear(); UndoneCommands.Clear(); Commands.Clear(); }