Class UndoStack
An undo stack maintains a stack of commands that have been applied in your application.
New commands are pushed on the stack using Push(Undo
UndoStack keeps track of the current command. This is the command which will be executed by the next call to Redo(). The index of this command is returned by index. The state of the edited object can be rolled forward or back using index. If the top-most command on the stack has already been redone, index is equal to count.
UndoStack provides *command compression*, command *macros*, and supports the concept of a *clean state*.
Inherited Members
Namespace: Unity.AppUI.Undo
Assembly: Unity.AppUI.Undo.dll
Syntax
public class UndoStack
Constructors
UndoStack()
Default constructor.
Declaration
public UndoStack()
Properties
this[int]
Gets the command at the given index.
Declaration
public UndoCommand this[int idx] { get; }
Parameters
Type | Name | Description |
---|---|---|
int | idx | The index of the command. |
Property Value
Type | Description |
---|---|
Undo |
The command at the given index, or null if the index is out of range. |
canRedo
Weather the redo stack is empty.
Declaration
public bool canRedo { get; }
Property Value
Type | Description |
---|---|
bool |
canUndo
Weather the undo stack is empty.
Declaration
public bool canUndo { get; }
Property Value
Type | Description |
---|---|
bool |
cleanIndex
The index of the clean state. -1 if there is no clean state.
Declaration
public int cleanIndex { get; }
Property Value
Type | Description |
---|---|
int |
commands
The whole commands list.
Declaration
public IEnumerable<UndoCommand> commands { get; }
Property Value
Type | Description |
---|---|
IEnumerable<Undo |
count
The number of commands in the stack. Macros are counted as a single command.
Declaration
public int count { get; }
Property Value
Type | Description |
---|---|
int |
index
The index of the last command in the undo stack.
Declaration
public int index { get; set; }
Property Value
Type | Description |
---|---|
int |
isClean
Weather the undo stack is in a clean state. The clean state is useful when the application supports saving and restoring the state of an object.
Declaration
public bool isClean { get; }
Property Value
Type | Description |
---|---|
bool |
lastCommand
The last command in the undo stack.
Declaration
public UndoCommand lastCommand { get; }
Property Value
Type | Description |
---|---|
Undo |
memorySize
The total memory size of the undo stack.
Declaration
public ulong memorySize { get; }
Property Value
Type | Description |
---|---|
ulong |
undoLimit
The maximum number of memory units that can be stored in the undo stack.
Declaration
public ulong undoLimit { get; set; }
Property Value
Type | Description |
---|---|
ulong |
Methods
BeginMacro(string)
Begins composition of a macro command with the given text description.
An empty command described by the specified text is pushed on the stack.
Any subsequent commands pushed on the stack will be appended to the macro command's children until
End
While a macro is being composed, the stack is disabled. This means that:
- Events are not emitted.
- can
- Calling Undo() or Redo() has no effect.
The stack is re-enabled when the macro is ended.
Here is an example of how to use macros:
undoStack.BeginMacro("Insert Red Text");
undoStack.Push(new InsertText(...));
undoStack.Push(new SetRedText(...));
undoStack.EndMacro();
This code is equivalent to:
var insertRedText = new UndoCommand("Insert Text");
new InsertText(..., insertRedText);
new SetRedText(..., insertRedText);
undoStack.Push(insertRedText);
Declaration
public void BeginMacro(string name)
Parameters
Type | Name | Description |
---|---|---|
string | name | The name of the macro command. |
Remarks
Nested calls to Begin
See Also
Clear()
Clears the undo and redo stacks.
Declaration
public void Clear()
EndMacro()
Ends composition of a macro command.
If this is the outermost macro in a set nested macros,
this function emits index
Declaration
public void EndMacro()
Exceptions
Type | Condition |
---|---|
Invalid |
Thrown if no macro is being composed. |
See Also
Push(UndoCommand)
Pushes the given command on the undo stack.
Declaration
public void Push(UndoCommand command)
Parameters
Type | Name | Description |
---|---|---|
Undo |
command | The command to push. |
Remarks
If the current command is a macro command, the pushed command is added to the macro.
Redo()
Calls Redo() on the last command in the redo stack.
Declaration
public void Redo()
ResetClean()
Sets the clean state to -1.
Declaration
public void ResetClean()
SetClean()
Sets the clean state to the last command in the undo stack.
Declaration
public void SetClean()
Undo()
Calls Undo() on the last command in the undo stack.
Declaration
public void Undo()
Events
cleanStateChanged
Emitted when the clean state changes.
Declaration
public event Action cleanStateChanged
Event Type
Type | Description |
---|---|
Action |
indexChanged
Emitted the current Command index changes.
Declaration
public event Action<int> indexChanged