1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//! Undo/redo manager.
//!
//! Extracted from lib.rs (P1-13).
use std::sync::Arc;
/// A single action group representing an undo/redo step.
pub struct UndoGroup {
/// Descriptive label of the action (e.g. "Type", "Delete").
pub label: String,
/// Time when the action was recorded, in seconds.
pub timestamp: f32,
/// Closure to revert the action.
pub undo: Arc<dyn Fn() + Send + Sync>,
/// Closure to re-apply the action.
pub redo: Arc<dyn Fn() + Send + Sync>,
}
impl Clone for UndoGroup {
/// Clone the undo/redo group. The closures are shared via Arc.
fn clone(&self) -> Self {
Self {
label: self.label.clone(),
timestamp: self.timestamp,
undo: Arc::clone(&self.undo),
redo: Arc::clone(&self.redo),
}
}
}
impl std::fmt::Debug for UndoGroup {
/// Debug format helper to avoid printing closures.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("UndoGroup")
.field("label", &self.label)
.field("timestamp", &self.timestamp)
.finish()
}
}
/// Unified manager for undo and redo stacks.
/// Supports grouping of actions, max undo depth clamping, and coalescing.
pub struct UndoManager {
/// History stack of undo/redo groups.
pub(crate) stack: Vec<UndoGroup>,
/// Current position/index in the stack.
pub(crate) position: usize,
/// Maximum allowed undo steps before discarding oldest.
max_depth: usize,
/// Time window in seconds to coalesce consecutive actions of the same type.
coalesce_window: f32,
}
impl Default for UndoManager {
/// Create a default UndoManager with a depth of 100 and a 0.5s coalesce window.
fn default() -> Self {
Self {
stack: Vec::new(),
position: 0,
max_depth: 100,
coalesce_window: 0.5,
}
}
}
impl Clone for UndoManager {
/// Clone the undo manager, preserving stacks and position.
fn clone(&self) -> Self {
Self {
stack: self.stack.clone(),
position: self.position,
max_depth: self.max_depth,
coalesce_window: self.coalesce_window,
}
}
}
impl std::fmt::Debug for UndoManager {
/// Debug format helper for UndoManager.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("UndoManager")
.field("stack_len", &self.stack.len())
.field("position", &self.position)
.field("max_depth", &self.max_depth)
.field("coalesce_window", &self.coalesce_window)
.finish()
}
}
impl UndoManager {
/// Create a new UndoManager with custom settings.
pub fn new(max_depth: usize, coalesce_window: f32) -> Self {
Self {
stack: Vec::new(),
position: 0,
max_depth,
coalesce_window,
}
}
/// Push a new undo/redo group to the stack, clearing any forward redo history.
pub fn push(
&mut self,
label: &str,
undo: impl Fn() + Send + Sync + 'static,
redo: impl Fn() + Send + Sync + 'static,
) {
if self.position < self.stack.len() {
self.stack.truncate(self.position);
}
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs_f32();
self.stack.push(UndoGroup {
label: label.to_string(),
timestamp,
undo: Arc::new(undo),
redo: Arc::new(redo),
});
if self.stack.len() > self.max_depth {
self.stack.remove(0);
}
self.position = self.stack.len();
}
/// Perform the undo action if possible, moving the position back.
/// Returns the undo closure to be executed outside of any state lock.
pub fn undo(&mut self) -> Option<Arc<dyn Fn() + Send + Sync>> {
if self.can_undo() {
self.position -= 1;
Some(Arc::clone(&self.stack[self.position].undo))
} else {
None
}
}
/// Perform the redo action if possible, moving the position forward.
/// Returns the redo closure to be executed outside of any state lock.
pub fn redo(&mut self) -> Option<Arc<dyn Fn() + Send + Sync>> {
if self.can_redo() {
let group = &self.stack[self.position];
self.position += 1;
Some(Arc::clone(&group.redo))
} else {
None
}
}
/// Returns true if there is an action that can be undone.
pub fn can_undo(&self) -> bool {
self.position > 0
}
/// Returns true if there is an action that can be redone.
pub fn can_redo(&self) -> bool {
self.position < self.stack.len()
}
/// Clear all undo/redo history.
pub fn clear(&mut self) {
self.stack.clear();
self.position = 0;
}
/// Push a new coalesceable action. If the last action in the stack matches the label,
/// is within the coalesce window, and the position is at the end of the stack, their undo/redo
/// functions will be combined instead of creating a new group.
pub fn push_coalesceable(
&mut self,
label: &str,
undo: impl Fn() + Send + Sync + 'static,
redo: impl Fn() + Send + Sync + 'static,
) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs_f32();
if self.position == self.stack.len() && !self.stack.is_empty() {
let last_idx = self.stack.len() - 1;
let last = &self.stack[last_idx];
if last.label == label && (now - last.timestamp).abs() <= self.coalesce_window {
let old_undo = Arc::clone(&last.undo);
let old_redo = Arc::clone(&last.redo);
let new_undo = Arc::new(undo);
let new_redo = Arc::new(redo);
self.stack[last_idx].undo = Arc::new(move || {
new_undo();
old_undo();
});
self.stack[last_idx].redo = Arc::new(move || {
old_redo();
new_redo();
});
self.stack[last_idx].timestamp = now;
return;
}
}
self.push(label, undo, redo);
}
}