namespace Pilz.Collections.SimpleHistory; public class HistoryStack { private readonly Stack stackPast = new(); private readonly Stack stackFuture = new(); /// /// Gets the count of history points. /// /// public int ChangesCount => stackPast.Count; /// /// Gets the current stack of all past HistoryPoints that are used for the Undo function. /// /// public HistoryPoint[] PastHistoryPoints => [.. stackPast]; /// /// Gets the current stack of all future HistoryPoints that are used for the Redo function. /// /// public HistoryPoint[] FutureHistoryPoints => [.. stackFuture]; /// /// Checks if the History has past changes. /// /// public bool HasChanges() => stackPast.Count > 0; /// /// Patch Object States and call Undo Actions. /// public HistoryPoint? Undo() { HistoryPoint? ret; if (stackPast.Count > 0) { var hp = stackPast.Pop(); hp.Undo(); stackFuture.Push(hp); ret = hp; } else ret = null; return ret; } /// /// Patch Object States and call Redo Actions. /// public HistoryPoint? Redo() { HistoryPoint? ret; if (stackFuture.Count > 0) { var hp = stackFuture.Pop(); hp.Redo(); stackPast.Push(hp); ret = hp; } else ret = null; return ret; } /// /// Clear the History. /// public void Clear() { stackPast.Clear(); stackFuture.Clear(); } /// /// Store a History Point. /// /// The History Point to add to the past changes. /// The name to set for the History Point. public void Store(HistoryPoint point, string newName) { point.Name = newName; Store(point); } /// /// Store a History Point. /// /// The History Point to add to the past changes. public void Store(HistoryPoint point) { stackPast.Push(point); stackFuture.Clear(); } }