| | | 1 | | namespace Chronicis.Client.Utilities; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Models the undo/redo stroke stack behavior of the drawing canvas. |
| | | 5 | | /// Strokes are stored in a list; redo items in a separate stack. |
| | | 6 | | /// Undo: pop last stroke from strokes → push to redoStack. |
| | | 7 | | /// Redo: pop from redoStack → push to strokes. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class DrawingCanvasUndoRedo |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Performs an undo operation: removes the last stroke and returns it as a redo candidate. |
| | | 13 | | /// Returns null if strokes is empty. |
| | | 14 | | /// </summary> |
| | | 15 | | public static T? Undo<T>(List<T> strokes, Stack<T> redoStack) |
| | | 16 | | { |
| | 487 | 17 | | if (strokes.Count == 0) return default; |
| | 485 | 18 | | var stroke = strokes[^1]; |
| | 485 | 19 | | strokes.RemoveAt(strokes.Count - 1); |
| | 485 | 20 | | redoStack.Push(stroke); |
| | 485 | 21 | | return stroke; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Performs a redo operation: pops from redo stack and appends to strokes. |
| | | 26 | | /// Returns null if redoStack is empty. |
| | | 27 | | /// </summary> |
| | | 28 | | public static T? Redo<T>(List<T> strokes, Stack<T> redoStack) |
| | | 29 | | { |
| | 387 | 30 | | if (redoStack.Count == 0) return default; |
| | 385 | 31 | | var stroke = redoStack.Pop(); |
| | 385 | 32 | | strokes.Add(stroke); |
| | 385 | 33 | | return stroke; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Adds a stroke and clears the redo stack (new input invalidates redo history). |
| | | 38 | | /// </summary> |
| | | 39 | | public static void AddStroke<T>(List<T> strokes, Stack<T> redoStack, T stroke) |
| | | 40 | | { |
| | 7956 | 41 | | strokes.Add(stroke); |
| | 7956 | 42 | | redoStack.Clear(); |
| | 7956 | 43 | | } |
| | | 44 | | } |