|
| 1 | +using System.Windows; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | + |
| 4 | +namespace GhostDraw.Services; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Manages the history of completed drawing actions for undo functionality. |
| 8 | +/// Uses a stable GUID identifier on each drawable element's Tag property. |
| 9 | +/// </summary> |
| 10 | +public class DrawingHistory |
| 11 | +{ |
| 12 | + private readonly ILogger<DrawingHistory> _logger; |
| 13 | + |
| 14 | + // Stack of completed drawing actions (most recent at the top) |
| 15 | + private readonly Stack<HistoryEntry> _undoStack = new(); |
| 16 | + |
| 17 | + // Dictionary for O(1) lookup when eraser needs to remove history entries |
| 18 | + private readonly Dictionary<Guid, HistoryEntry> _elementIdToEntry = new(); |
| 19 | + |
| 20 | + public DrawingHistory(ILogger<DrawingHistory> logger) |
| 21 | + { |
| 22 | + _logger = logger; |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Records a completed drawing action. Assigns a unique ID to the element. |
| 27 | + /// </summary> |
| 28 | + /// <param name="element">The UIElement that was added to the canvas</param> |
| 29 | + public void RecordAction(UIElement element) |
| 30 | + { |
| 31 | + try |
| 32 | + { |
| 33 | + if (element == null) |
| 34 | + { |
| 35 | + _logger.LogWarning("Attempted to record null element"); |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Assign a unique ID to this element using Tag (cast to FrameworkElement) |
| 40 | + if (element is FrameworkElement frameworkElement) |
| 41 | + { |
| 42 | + var id = Guid.NewGuid(); |
| 43 | + frameworkElement.Tag = id; |
| 44 | + |
| 45 | + var entry = new HistoryEntry(id, element); |
| 46 | + _undoStack.Push(entry); |
| 47 | + _elementIdToEntry[id] = entry; |
| 48 | + |
| 49 | + _logger.LogDebug("Action recorded: ID={Id}, Type={Type}, StackSize={StackSize}", |
| 50 | + id, element.GetType().Name, _undoStack.Count); |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + _logger.LogWarning("Element is not a FrameworkElement, cannot assign Tag"); |
| 55 | + } |
| 56 | + } |
| 57 | + catch (Exception ex) |
| 58 | + { |
| 59 | + _logger.LogError(ex, "Failed to record action"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Removes the most recent completed action from history. |
| 65 | + /// Returns the element to be removed from the canvas, or null if history is empty. |
| 66 | + /// </summary> |
| 67 | + public UIElement? UndoLastAction() |
| 68 | + { |
| 69 | + try |
| 70 | + { |
| 71 | + while (_undoStack.Count > 0) |
| 72 | + { |
| 73 | + var entry = _undoStack.Pop(); |
| 74 | + |
| 75 | + // Remove from dictionary |
| 76 | + _elementIdToEntry.Remove(entry.Id); |
| 77 | + |
| 78 | + // Check if element is still valid (not already removed) |
| 79 | + if (entry.Element != null) |
| 80 | + { |
| 81 | + _logger.LogInformation("Undo: Removing element ID={Id}, Type={Type}, RemainingActions={Count}", |
| 82 | + entry.Id, entry.Element.GetType().Name, _undoStack.Count); |
| 83 | + return entry.Element; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + // Element was already removed (e.g., by eraser), skip to next |
| 88 | + _logger.LogDebug("Undo: Skipping null element ID={Id} (already removed)", entry.Id); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + _logger.LogDebug("Undo: History stack is empty"); |
| 93 | + return null; |
| 94 | + } |
| 95 | + catch (Exception ex) |
| 96 | + { |
| 97 | + _logger.LogError(ex, "Failed to undo last action"); |
| 98 | + return null; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Removes an element from the history (called when eraser deletes it). |
| 104 | + /// This ensures erased elements can never be restored by undo. |
| 105 | + /// </summary> |
| 106 | + /// <param name="element">The element that was erased</param> |
| 107 | + public void RemoveFromHistory(UIElement element) |
| 108 | + { |
| 109 | + try |
| 110 | + { |
| 111 | + if (element is FrameworkElement frameworkElement && frameworkElement.Tag is Guid id) |
| 112 | + { |
| 113 | + if (_elementIdToEntry.TryGetValue(id, out var entry)) |
| 114 | + { |
| 115 | + // Mark the entry's element as null so it will be skipped during undo |
| 116 | + entry.Element = null; |
| 117 | + _elementIdToEntry.Remove(id); |
| 118 | + |
| 119 | + _logger.LogDebug("Element removed from history: ID={Id}, Type={Type}", |
| 120 | + id, element.GetType().Name); |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + _logger.LogDebug("Element not found in history dictionary: ID={Id}", id); |
| 125 | + } |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + _logger.LogDebug("Element has no GUID tag, cannot remove from history"); |
| 130 | + } |
| 131 | + } |
| 132 | + catch (Exception ex) |
| 133 | + { |
| 134 | + _logger.LogError(ex, "Failed to remove element from history"); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Clears all history (called when canvas is cleared or drawing mode exits) |
| 140 | + /// </summary> |
| 141 | + public void Clear() |
| 142 | + { |
| 143 | + try |
| 144 | + { |
| 145 | + var count = _undoStack.Count; |
| 146 | + _undoStack.Clear(); |
| 147 | + _elementIdToEntry.Clear(); |
| 148 | + |
| 149 | + if (count > 0) |
| 150 | + { |
| 151 | + _logger.LogInformation("History cleared: {Count} entries removed", count); |
| 152 | + } |
| 153 | + } |
| 154 | + catch (Exception ex) |
| 155 | + { |
| 156 | + _logger.LogError(ex, "Failed to clear history"); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Returns the number of actions that can be undone |
| 162 | + /// </summary> |
| 163 | + public int Count => _undoStack.Count; |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Represents a single action in the history |
| 167 | + /// </summary> |
| 168 | + private class HistoryEntry |
| 169 | + { |
| 170 | + public Guid Id { get; } |
| 171 | + public UIElement? Element { get; set; } |
| 172 | + |
| 173 | + public HistoryEntry(Guid id, UIElement element) |
| 174 | + { |
| 175 | + Id = id; |
| 176 | + Element = element; |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments