30struct UndoManager::ActionSet
32 ActionSet (
const String& transactionName) : name (transactionName)
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();
63 OwnedArray<UndoableAction> actions;
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 (std::move (action));
150 newTransaction =
false;
152 moveFutureTransactionsToStash();
153 dropOldTransactionsIfTooLarge();
162void UndoManager::moveFutureTransactionsToStash()
164 if (nextIndex < transactions.
size())
166 stashedFutureTransactions.
clear();
168 while (nextIndex < transactions.
size())
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();
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;
238UndoManager::ActionSet* UndoManager::getCurrentSet()
const {
return transactions[nextIndex - 1]; }
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();
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)