use super::types::{UndoHistory, DEFAULT_MAX_HISTORY};
impl<T> UndoHistory<T> {
pub fn new() -> Self {
Self::with_max_size(DEFAULT_MAX_HISTORY)
}
pub fn with_max_size(max_size: usize) -> Self {
Self {
undo_stack: std::collections::VecDeque::with_capacity(max_size.min(100)),
redo_stack: Vec::new(),
max_size,
}
}
pub fn push(&mut self, op: T) {
self.redo_stack.clear();
self.undo_stack.push_back(op);
while self.undo_stack.len() > self.max_size {
self.undo_stack.pop_front();
}
}
#[inline]
pub fn can_undo(&self) -> bool {
!self.undo_stack.is_empty()
}
#[inline]
pub fn can_redo(&self) -> bool {
!self.redo_stack.is_empty()
}
}