30 struct 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();
162 void 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();
177 void 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);
194 void 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;
238 UndoManager::ActionSet* UndoManager::getCurrentSet()
const {
return transactions[nextIndex - 1]; }
239 UndoManager::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();
String getUndoDescription() const
bool undoCurrentTransactionOnly()
bool isNotEmpty() const noexcept
StringArray getUndoDescriptions() const
Time getTimeOfUndoTransaction() const
void add(const ElementType &newElement)
void beginNewTransaction()
int getNumberOfUnitsTakenUpByStoredCommands() const
static Time JUCE_CALLTYPE getCurrentTime() noexcept
void setCurrentTransactionName(const String &newName)
bool isPerformingUndoRedo() const
Time getTimeOfRedoTransaction() const
bool perform(UndoableAction *action)
void setMaxNumberOfStoredUnits(int maxNumberOfUnitsToKeep, int minimumTransactionsToKeep)
StringArray getRedoDescriptions() const
int getNumActionsInCurrentTransaction() const
String getCurrentTransactionName() const
String getRedoDescription() const
void getActionsInCurrentTransaction(Array< const UndoableAction *> &actionsFound) const
UndoManager(int maxNumberOfUnitsToKeep=30000, int minimumTransactionsToKeep=30)
void add(String stringToAdd)