29struct UndoManager::ActionSet
31 ActionSet (
const String& transactionName) : name (transactionName)
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();
62 OwnedArray<UndoableAction> actions;
88 return totalUnitsStored;
93 maxNumUnitsToKeep = jmax (1, maxUnits);
94 minimumTransactionsToKeep = jmax (1, minTransactions);
113 if (newAction !=
nullptr)
115 std::unique_ptr<UndoableAction> action (newAction);
124 if (action->perform())
126 auto* actionSet = getCurrentSet();
128 if (actionSet !=
nullptr && ! newTransaction)
130 if (
auto* lastAction = actionSet->actions.getLast())
132 if (
auto coalescedAction = lastAction->createCoalescedAction (action.get()))
134 action.reset (coalescedAction);
135 totalUnitsStored -= lastAction->getSizeInUnits();
136 actionSet->actions.removeLast();
142 actionSet =
new ActionSet (newTransactionName);
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())
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)
186 transactions.
add (stashed);
187 totalUnitsStored += stashed->getTotalSize();
190 stashedFutureTransactions.clearQuick (
false);
193void UndoManager::dropOldTransactionsIfTooLarge()
196 && totalUnitsStored > maxNumUnitsToKeep
197 && transactions.
size() > minimumTransactionsToKeep)
199 totalUnitsStored -= transactions.
getFirst()->getTotalSize();
205 jassert (totalUnitsStored >= 0);
216 newTransaction =
true;
217 newTransactionName = actionName;
223 newTransactionName = newName;
224 else if (
auto* action = getCurrentSet())
225 action->name = newName;
230 if (
auto* action = getCurrentSet())
233 return newTransactionName;
237UndoManager::ActionSet* UndoManager::getCurrentSet()
const {
return transactions[nextIndex - 1]; }
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])
306 descriptions.
add (t->name);
316 for (
int i = nextIndex;;)
318 if (
auto* t = transactions[i++])
319 descriptions.
add (t->name);
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)
357 actionsFound.
add (a);
362 if (! newTransaction)
363 if (
auto* s = getCurrentSet())
364 return s->actions.size();
void add(const ElementType &newElement)
int size() const noexcept
ObjectClass * getUnchecked(int index) const noexcept
ObjectClass * removeAndReturn(int indexToRemove)
void remove(int indexToRemove, bool deleteObject=true)
ObjectClass * getFirst() const noexcept
void clear(bool deleteObjects=true)
ObjectClass * add(ObjectClass *newObject)
ObjectClass * insert(int indexToInsertAt, ObjectClass *newObject)
void add(String stringToAdd)
bool isNotEmpty() const noexcept
static Time JUCE_CALLTYPE getCurrentTime() noexcept
void beginNewTransaction()
Time getTimeOfUndoTransaction() const
String getCurrentTransactionName() const
void setMaxNumberOfStoredUnits(int maxNumberOfUnitsToKeep, int minimumTransactionsToKeep)
String getRedoDescription() const
StringArray getRedoDescriptions() const
bool isPerformingUndoRedo() const
bool undoCurrentTransactionOnly()
int getNumberOfUnitsTakenUpByStoredCommands() const
bool perform(UndoableAction *action)
Time getTimeOfRedoTransaction() const
StringArray getUndoDescriptions() const
String getUndoDescription() const
UndoManager(int maxNumberOfUnitsToKeep=30000, int minimumTransactionsToKeep=30)
void getActionsInCurrentTransaction(Array< const UndoableAction * > &actionsFound) const
int getNumActionsInCurrentTransaction() const
void setCurrentTransactionName(const String &newName)