37 for (
auto* a : actions)
46 for (
int i = actions.size(); --i >= 0;)
47 if (! actions.getUnchecked(i)->undo())
53 int getTotalSize()
const
57 for (
auto* a : actions)
58 total += a->getSizeInUnits();
89 return totalUnitsStored;
94 maxNumUnitsToKeep = jmax (1, maxUnits);
95 minimumTransactionsToKeep = jmax (1, minTransactions);
114 if (newAction !=
nullptr)
116 std::unique_ptr<UndoableAction> action (newAction);
125 if (action->perform())
127 auto* actionSet = getCurrentSet();
129 if (actionSet !=
nullptr && ! newTransaction)
131 if (
auto* lastAction = actionSet->actions.getLast())
133 if (
auto coalescedAction = lastAction->createCoalescedAction (action.get()))
135 action.reset (coalescedAction);
136 totalUnitsStored -= lastAction->getSizeInUnits();
137 actionSet->actions.removeLast();
143 actionSet =
new ActionSet (newTransactionName);
144 transactions.insert (nextIndex, actionSet);
148 totalUnitsStored += action->getSizeInUnits();
149 actionSet->actions.add (action.release());
150 newTransaction =
false;
152 moveFutureTransactionsToStash();
153 dropOldTransactionsIfTooLarge();
162void UndoManager::moveFutureTransactionsToStash()
164 if (nextIndex < transactions.size())
166 stashedFutureTransactions.clear();
168 while (nextIndex < transactions.size())
170 auto* removed = transactions.removeAndReturn (nextIndex);
171 stashedFutureTransactions.add (removed);
172 totalUnitsStored -= removed->getTotalSize();
177void UndoManager::restoreStashedFutureTransactions()
179 while (nextIndex < transactions.size())
181 totalUnitsStored -= transactions.getUnchecked (nextIndex)->getTotalSize();
182 transactions.remove (nextIndex);
185 for (
auto* stashed : stashedFutureTransactions)
187 transactions.add (stashed);
188 totalUnitsStored += stashed->getTotalSize();
191 stashedFutureTransactions.clearQuick (
false);
194void UndoManager::dropOldTransactionsIfTooLarge()
197 && totalUnitsStored > maxNumUnitsToKeep
198 && transactions.size() > minimumTransactionsToKeep)
200 totalUnitsStored -= transactions.getFirst()->getTotalSize();
201 transactions.remove (0);
206 jassert (totalUnitsStored >= 0);
217 newTransaction =
true;
218 newTransactionName = actionName;
224 newTransactionName = newName;
225 else if (
auto* action = getCurrentSet())
226 action->name = newName;
231 if (
auto* action = getCurrentSet())
234 return newTransactionName;
239UndoManager::ActionSet* UndoManager::getNextSet()
const {
return transactions[nextIndex]; }
248 if (
auto* s = getCurrentSet())
267 if (
auto* s = getNextSet())
286 if (
auto* s = getCurrentSet())
294 if (
auto* s = getNextSet())
304 for (
int i = nextIndex;;)
306 if (
auto* t = transactions[--i])
307 descriptions.
add (t->name);
317 for (
int i = nextIndex;;)
319 if (
auto* t = transactions[i++])
320 descriptions.
add (t->name);
328 if (
auto* s = getCurrentSet())
336 if (
auto* s = getNextSet())
344 if ((! newTransaction) &&
undo())
346 restoreStashedFutureTransactions();
355 if (! newTransaction)
356 if (
auto* s = getCurrentSet())
357 for (
auto* a : s->actions)
358 actionsFound.
add (a);
363 if (! newTransaction)
364 if (
auto* s = getCurrentSet())
365 return s->actions.size();
Holds a resizable array of primitive or copy-by-value objects.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
void sendChangeMessage()
Causes an asynchronous change message to be sent to all the registered listeners.
An array designed for holding objects.
Helper class providing an RAII-based mechanism for temporarily setting and then re-setting a value.
A special array for holding a list of strings.
void add(String stringToAdd)
Appends a string at the end of the array.
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
Holds an absolute date and time.
static Time JUCE_CALLTYPE getCurrentTime() noexcept
Returns a Time object that is set to the current system time.
void beginNewTransaction()
Starts a new group of actions that together will be treated as a single transaction.
Time getTimeOfUndoTransaction() const
Returns the time to which the state would be restored if undo() was to be called.
String getCurrentTransactionName() const
Returns the name of the current transaction.
void setMaxNumberOfStoredUnits(int maxNumberOfUnitsToKeep, int minimumTransactionsToKeep)
Sets the amount of space that can be used for storing UndoableAction objects.
bool redo()
Tries to redo the last transaction that was undone.
String getRedoDescription() const
Returns the name of the transaction that will be redone when redo() is called.
StringArray getRedoDescriptions() const
Returns the names of the sequence of transactions that will be performed if redo() is repeatedly call...
bool undo()
Tries to roll-back the last transaction.
~UndoManager() override
Destructor.
bool isPerformingUndoRedo() const
Returns true if the caller code is in the middle of an undo or redo action.
void clearUndoHistory()
Deletes all stored actions in the list.
bool canUndo() const
Returns true if there's at least one action in the list to undo.
bool undoCurrentTransactionOnly()
Tries to roll-back any actions that were added to the current transaction.
int getNumberOfUnitsTakenUpByStoredCommands() const
Returns the current amount of space to use for storing UndoableAction objects.
bool perform(UndoableAction *action)
Performs an action and adds it to the undo history list.
Time getTimeOfRedoTransaction() const
Returns the time to which the state would be restored if redo() was to be called.
bool canRedo() const
Returns true if there's at least one action in the list to redo.
StringArray getUndoDescriptions() const
Returns the names of the sequence of transactions that will be performed if undo() is repeatedly call...
String getUndoDescription() const
Returns the name of the transaction that will be rolled-back when undo() is called.
UndoManager(int maxNumberOfUnitsToKeep=30000, int minimumTransactionsToKeep=30)
Creates an UndoManager.
void getActionsInCurrentTransaction(Array< const UndoableAction * > &actionsFound) const
Returns a list of the UndoableAction objects that have been performed during the transaction that is ...
int getNumActionsInCurrentTransaction() const
Returns the number of UndoableAction objects that have been performed during the transaction that is ...
void setCurrentTransactionName(const String &newName)
Changes the name stored for the current transaction.
Used by the UndoManager class to store an action which can be done and undone.