36 for (
auto* a : actions)
45 for (
int i = actions.size(); --i >= 0;)
46 if (! actions.getUnchecked (i)->undo())
52 int getTotalSize()
const
56 for (
auto* a : actions)
57 total += a->getSizeInUnits();
88 return totalUnitsStored;
124 if (action->perform())
128 if (
actionSet !=
nullptr && ! newTransaction)
135 totalUnitsStored -=
lastAction->getSizeInUnits();
143 transactions.insert (nextIndex,
actionSet);
147 totalUnitsStored += action->getSizeInUnits();
148 actionSet->actions.add (std::move (action));
149 newTransaction =
false;
151 moveFutureTransactionsToStash();
152 dropOldTransactionsIfTooLarge();
161void UndoManager::moveFutureTransactionsToStash()
163 if (nextIndex < transactions.size())
165 stashedFutureTransactions.clear();
167 while (nextIndex < transactions.size())
169 auto* removed = transactions.removeAndReturn (nextIndex);
170 stashedFutureTransactions.add (removed);
171 totalUnitsStored -= removed->getTotalSize();
176void UndoManager::restoreStashedFutureTransactions()
178 while (nextIndex < transactions.size())
180 totalUnitsStored -= transactions.getUnchecked (nextIndex)->getTotalSize();
181 transactions.remove (nextIndex);
184 for (
auto*
stashed : stashedFutureTransactions)
187 totalUnitsStored +=
stashed->getTotalSize();
190 stashedFutureTransactions.clearQuick (
false);
193void UndoManager::dropOldTransactionsIfTooLarge()
196 && totalUnitsStored > maxNumUnitsToKeep
197 && transactions.size() > minimumTransactionsToKeep)
199 totalUnitsStored -= transactions.getFirst()->getTotalSize();
200 transactions.remove (0);
205 jassert (totalUnitsStored >= 0);
216 newTransaction =
true;
223 newTransactionName = newName;
224 else if (
auto* action = getCurrentSet())
225 action->name = newName;
230 if (
auto* action = getCurrentSet())
233 return newTransactionName;
238UndoManager::ActionSet* UndoManager::getNextSet()
const {
return transactions[nextIndex]; }
247 if (
auto* s = getCurrentSet())
266 if (
auto* s = getNextSet())
285 if (
auto* s = getCurrentSet())
293 if (
auto* s = getNextSet())
303 for (
int i = nextIndex;;)
305 if (
auto* t = transactions[--i])
316 for (
int i = nextIndex;;)
318 if (
auto* t = transactions[i++])
327 if (
auto* s = getCurrentSet())
335 if (
auto* s = getNextSet())
343 if ((! newTransaction) &&
undo())
345 restoreStashedFutureTransactions();
354 if (! newTransaction)
355 if (
auto* s = getCurrentSet())
356 for (
auto* a : s->actions)
362 if (! newTransaction)
363 if (
auto* s = getCurrentSet())
364 return s->actions.size();
Holds a resizable array of primitive or copy-by-value objects.
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.
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.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...