pub struct UndoRedoManager { /* private fields */ }Expand description
Manager for undo and redo operations.
The UndoRedoManager maintains multiple stacks of commands:
- Each stack has an undo stack for commands that can be undone
- Each stack has a redo stack for commands that have been undone and can be redone
It also supports:
- Grouping multiple commands as a single unit using begin_composite/end_composite
- Merging commands of the same type when appropriate
- Switching between different stacks
Implementations§
Source§impl UndoRedoManager
impl UndoRedoManager
Sourcepub fn set_event_hub(&mut self, event_hub: &Arc<EventHub>)
pub fn set_event_hub(&mut self, event_hub: &Arc<EventHub>)
Inject the event hub to allow sending undo/redo related events
Sourcepub fn undo(&mut self, stack_id: Option<u64>) -> Result<()>
pub fn undo(&mut self, stack_id: Option<u64>) -> Result<()>
Undoes the most recent command on the specified stack.
If stack_id is None, the global stack (ID 0) is used.
The undone command is moved to the redo stack. Returns Ok(()) if successful or if there are no commands to undo.
Sourcepub fn redo(&mut self, stack_id: Option<u64>) -> Result<()>
pub fn redo(&mut self, stack_id: Option<u64>) -> Result<()>
Redoes the most recently undone command on the specified stack.
If stack_id is None, the global stack (ID 0) is used.
The redone command is moved back to the undo stack. Returns Ok(()) if successful or if there are no commands to redo.
Sourcepub fn begin_composite(&mut self, stack_id: Option<u64>)
pub fn begin_composite(&mut self, stack_id: Option<u64>)
Begins a composite command group.
All commands added between begin_composite and end_composite will be treated as a single command. This is useful for operations that logically represent a single action but require multiple commands to implement.
§Example
let mut manager = UndoRedoManager::new();
manager.begin_composite();
manager.add_command(Box::new(Command1::new()));
manager.add_command(Box::new(Command2::new()));
manager.end_composite();
// Now undo() will undo both commands as a single unitSourcepub fn end_composite(&mut self)
pub fn end_composite(&mut self)
Ends the current composite command group and adds it to the specified undo stack.
If no commands were added to the composite, nothing is added to the undo stack. If this is a nested composite, only the outermost composite is added to the undo stack.
pub fn cancel_composite(&mut self)
Sourcepub fn add_command(&mut self, command: Box<dyn UndoRedoCommand>)
pub fn add_command(&mut self, command: Box<dyn UndoRedoCommand>)
Adds a command to the global undo stack (ID 0).
Sourcepub fn add_command_to_stack(
&mut self,
command: Box<dyn UndoRedoCommand>,
stack_id: Option<u64>,
) -> Result<()>
pub fn add_command_to_stack( &mut self, command: Box<dyn UndoRedoCommand>, stack_id: Option<u64>, ) -> Result<()>
Adds a command to the specified undo stack.
If stack_id is None, the global stack (ID 0) is used.
This method handles several cases:
- If a composite command is in progress, the command is added to the composite
- If the command can be merged with the last command on the specified undo stack, they are merged
- Otherwise, the command is added to the specified undo stack as a new entry
In all cases, the redo stack of the stack is cleared when a new command is added.
Sourcepub fn can_undo(&self, stack_id: Option<u64>) -> bool
pub fn can_undo(&self, stack_id: Option<u64>) -> bool
Returns true if there are commands that can be undone on the specified stack.
If stack_id is None, the global stack (ID 0) is used.
Sourcepub fn can_redo(&self, stack_id: Option<u64>) -> bool
pub fn can_redo(&self, stack_id: Option<u64>) -> bool
Returns true if there are commands that can be redone on the specified stack.
If stack_id is None, the global stack (ID 0) is used.
Sourcepub fn clear_stack(&mut self, stack_id: u64)
pub fn clear_stack(&mut self, stack_id: u64)
Clears the undo and redo history for a specific stack.
This method removes all commands from both the undo and redo stacks of the specified stack.
Sourcepub fn clear_all_stacks(&mut self)
pub fn clear_all_stacks(&mut self)
Clears all undo and redo history from all stacks.
Sourcepub fn create_new_stack(&mut self) -> u64
pub fn create_new_stack(&mut self) -> u64
Creates a new undo/redo stack and returns its ID.
Sourcepub fn delete_stack(&mut self, stack_id: u64) -> Result<()>
pub fn delete_stack(&mut self, stack_id: u64) -> Result<()>
Deletes an undo/redo stack by its ID.
The default stack (ID 0) cannot be deleted.
Sourcepub fn get_stack_size(&self, stack_id: u64) -> usize
pub fn get_stack_size(&self, stack_id: u64) -> usize
Gets the size of the undo stack for a specific stack.