From 99e7f8ee70339565b9308da88f1909016300733f Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Tue, 1 Apr 2025 19:35:16 +0800 Subject: [PATCH] history: Ensure unique History in redos and undos when push. (#761) --- crates/ui/src/history.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/crates/ui/src/history.rs b/crates/ui/src/history.rs index 11c89a2a..92cd43f5 100644 --- a/crates/ui/src/history.rs +++ b/crates/ui/src/history.rs @@ -13,6 +13,11 @@ pub trait HistoryItem: Clone + PartialEq { /// This is now used in Input for undo/redo operations. You can also use this in /// your own models to keep track of changes, for example to track the tab /// history for prev/next features. +/// +/// ## Use cases +/// +/// - Undo/redo operations in Input +/// - Tracking tab history for prev/next features #[derive(Debug)] pub struct History { undos: Vec, @@ -86,6 +91,7 @@ where if self.unique { self.undos.retain(|c| *c != item); + self.redos.retain(|c| *c != item); } let mut item = item; @@ -251,6 +257,13 @@ mod tests { assert_eq!(changes.len(), 1); assert_eq!(changes[0].tab_index, 1); + assert_eq!(history.redos().len(), 1); + // Push duplicate, should be ignored + history.push(2.into()); + + assert_eq!(history.undos().len(), 2); + assert_eq!(history.redos().len(), 1); + // Redo the last undone change let changes = history.redo().unwrap(); assert_eq!(changes.len(), 1); @@ -260,7 +273,7 @@ mod tests { history.push(3.into()); // Check the version and undo stack - assert_eq!(history.version(), 6); + assert_eq!(history.version(), 7); assert_eq!(history.undos().len(), 4); // Undo all changes