diff --git a/modules/juce_data_structures/undomanager/juce_UndoManager.cpp b/modules/juce_data_structures/undomanager/juce_UndoManager.cpp index 3557b90d86..07948c69eb 100644 --- a/modules/juce_data_structures/undomanager/juce_UndoManager.cpp +++ b/modules/juce_data_structures/undomanager/juce_UndoManager.cpp @@ -29,10 +29,6 @@ struct UndoManager::ActionSet time (Time::getCurrentTime()) {} - OwnedArray actions; - String name; - Time time; - bool perform() const { for (int i = 0; i < actions.size(); ++i) @@ -60,6 +56,10 @@ struct UndoManager::ActionSet return total; } + + OwnedArray actions; + String name; + Time time; }; //============================================================================== @@ -101,6 +101,19 @@ void UndoManager::setMaxNumberOfStoredUnits (const int maxNumberOfUnitsToKeep, //============================================================================== bool UndoManager::perform (UndoableAction* const newAction, const String& actionName) +{ + if (perform (newAction)) + { + if (actionName.isNotEmpty()) + setCurrentTransactionName (actionName); + + return true; + } + + return false; +} + +bool UndoManager::perform (UndoableAction* const newAction) { if (newAction != nullptr) { @@ -113,9 +126,6 @@ bool UndoManager::perform (UndoableAction* const newAction, const String& action return false; } - if (actionName.isNotEmpty()) - currentTransactionName = actionName; - if (action->perform()) { ActionSet* actionSet = getCurrentSet(); @@ -134,7 +144,7 @@ bool UndoManager::perform (UndoableAction* const newAction, const String& action } else { - actionSet = new ActionSet (currentTransactionName); + actionSet = new ActionSet (newTransactionName); transactions.insert (nextIndex, actionSet); ++nextIndex; } @@ -174,23 +184,31 @@ void UndoManager::clearFutureTransactions() } } -void UndoManager::beginNewTransaction (const String& actionName) +void UndoManager::beginNewTransaction() noexcept { - newTransaction = true; - currentTransactionName = actionName; + beginNewTransaction (String()); } -void UndoManager::setCurrentTransactionName (const String& newName) +void UndoManager::beginNewTransaction (const String& actionName) noexcept { - currentTransactionName = newName; + newTransaction = true; + newTransactionName = actionName; +} + +void UndoManager::setCurrentTransactionName (const String& newName) noexcept +{ + if (newTransaction) + newTransactionName = newName; + else if (ActionSet* action = getCurrentSet()) + action->name = newName; } //============================================================================== UndoManager::ActionSet* UndoManager::getCurrentSet() const noexcept { return transactions [nextIndex - 1]; } UndoManager::ActionSet* UndoManager::getNextSet() const noexcept { return transactions [nextIndex]; } -bool UndoManager::canUndo() const { return getCurrentSet() != nullptr; } -bool UndoManager::canRedo() const { return getNextSet() != nullptr; } +bool UndoManager::canUndo() const noexcept { return getCurrentSet() != nullptr; } +bool UndoManager::canRedo() const noexcept { return getNextSet() != nullptr; } bool UndoManager::undo() { @@ -267,7 +285,7 @@ bool UndoManager::undoCurrentTransactionOnly() return newTransaction ? false : undo(); } -void UndoManager::getActionsInCurrentTransaction (Array & actionsFound) const +void UndoManager::getActionsInCurrentTransaction (Array& actionsFound) const { if (! newTransaction) if (const ActionSet* const s = getCurrentSet()) diff --git a/modules/juce_data_structures/undomanager/juce_UndoManager.h b/modules/juce_data_structures/undomanager/juce_UndoManager.h index eee9cbcfa7..a3b691b692 100644 --- a/modules/juce_data_structures/undomanager/juce_UndoManager.h +++ b/modules/juce_data_structures/undomanager/juce_UndoManager.h @@ -98,16 +98,32 @@ public: //============================================================================== /** Performs an action and adds it to the undo history list. - @param action the action to perform - this will be deleted by the UndoManager - when no longer needed + @param action the action to perform - this object will be deleted by + the UndoManager when no longer needed + @returns true if the command succeeds - see UndoableAction::perform + @see beginNewTransaction + */ + bool perform (UndoableAction* action); + + /** Performs an action and also gives it a name. + + @param action the action to perform - this object will be deleted by + the UndoManager when no longer needed @param actionName if this string is non-empty, the current transaction will be given this name; if it's empty, the current transaction name will be left unchanged. See setCurrentTransactionName() @returns true if the command succeeds - see UndoableAction::perform @see beginNewTransaction */ - bool perform (UndoableAction* action, - const String& actionName = String()); + bool perform (UndoableAction* action, const String& actionName); + + /** Starts a new group of actions that together will be treated as a single transaction. + + All actions that are passed to the perform() method between calls to this + method are grouped together and undone/redone together by a single call to + undo() or redo(). + */ + void beginNewTransaction() noexcept; /** Starts a new group of actions that together will be treated as a single transaction. @@ -118,7 +134,7 @@ public: @param actionName a description of the transaction that is about to be performed */ - void beginNewTransaction (const String& actionName = String()); + void beginNewTransaction (const String& actionName) noexcept; /** Changes the name stored for the current transaction. @@ -126,19 +142,15 @@ public: called, but this can be used to change that name without starting a new transaction. */ - void setCurrentTransactionName (const String& newName); + void setCurrentTransactionName (const String& newName) noexcept; //============================================================================== /** Returns true if there's at least one action in the list to undo. @see getUndoDescription, undo, canRedo */ - bool canUndo() const; - - /** Returns the description of the transaction that would be next to get undone. - - The description returned is the one that was passed into beginNewTransaction - before the set of actions was performed. + bool canUndo() const noexcept; + /** Returns the name of the transaction that will be rolled-back when undo() is called. @see undo */ String getUndoDescription() const; @@ -172,7 +184,7 @@ public: The first item in the list is the earliest action performed. */ - void getActionsInCurrentTransaction (Array & actionsFound) const; + void getActionsInCurrentTransaction (Array& actionsFound) const; /** Returns the number of UndoableAction objects that have been performed during the transaction that is currently open. @@ -194,12 +206,9 @@ public: /** Returns true if there's at least one action in the list to redo. @see getRedoDescription, redo, canUndo */ - bool canRedo() const; - - /** Returns the description of the transaction that would be next to get redone. - The description returned is the one that was passed into beginNewTransaction - before the set of actions was performed. + bool canRedo() const noexcept; + /** Returns the name of the transaction that will be redone when redo() is called. @see redo */ String getRedoDescription() const; @@ -216,7 +225,7 @@ private: struct ActionSet; friend struct ContainerDeletePolicy; OwnedArray transactions; - String currentTransactionName; + String newTransactionName; int totalUnitsStored, maxNumUnitsToKeep, minimumTransactionsToKeep, nextIndex; bool newTransaction, reentrancyCheck; ActionSet* getCurrentSet() const noexcept;