Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_UndoManager.cpp 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. struct UndoManager::ActionSet
  21. {
  22. ActionSet (const String& transactionName) : name (transactionName)
  23. {}
  24. bool perform() const
  25. {
  26. for (auto* a : actions)
  27. if (! a->perform())
  28. return false;
  29. return true;
  30. }
  31. bool undo() const
  32. {
  33. for (int i = actions.size(); --i >= 0;)
  34. if (! actions.getUnchecked(i)->undo())
  35. return false;
  36. return true;
  37. }
  38. int getTotalSize() const
  39. {
  40. int total = 0;
  41. for (auto* a : actions)
  42. total += a->getSizeInUnits();
  43. return total;
  44. }
  45. OwnedArray<UndoableAction> actions;
  46. String name;
  47. Time time { Time::getCurrentTime() };
  48. };
  49. //==============================================================================
  50. UndoManager::UndoManager (int maxNumberOfUnitsToKeep, int minimumTransactions)
  51. {
  52. setMaxNumberOfStoredUnits (maxNumberOfUnitsToKeep, minimumTransactions);
  53. }
  54. UndoManager::~UndoManager()
  55. {
  56. }
  57. //==============================================================================
  58. void UndoManager::clearUndoHistory()
  59. {
  60. transactions.clear();
  61. totalUnitsStored = 0;
  62. nextIndex = 0;
  63. sendChangeMessage();
  64. }
  65. int UndoManager::getNumberOfUnitsTakenUpByStoredCommands() const
  66. {
  67. return totalUnitsStored;
  68. }
  69. void UndoManager::setMaxNumberOfStoredUnits (int maxUnits, int minTransactions)
  70. {
  71. maxNumUnitsToKeep = jmax (1, maxUnits);
  72. minimumTransactionsToKeep = jmax (1, minTransactions);
  73. }
  74. //==============================================================================
  75. bool UndoManager::perform (UndoableAction* newAction, const String& actionName)
  76. {
  77. if (perform (newAction))
  78. {
  79. if (actionName.isNotEmpty())
  80. setCurrentTransactionName (actionName);
  81. return true;
  82. }
  83. return false;
  84. }
  85. bool UndoManager::perform (UndoableAction* newAction)
  86. {
  87. if (newAction != nullptr)
  88. {
  89. std::unique_ptr<UndoableAction> action (newAction);
  90. if (isPerformingUndoRedo())
  91. {
  92. jassertfalse; // Don't call perform() recursively from the UndoableAction::perform()
  93. // or undo() methods, or else these actions will be discarded!
  94. return false;
  95. }
  96. if (action->perform())
  97. {
  98. auto* actionSet = getCurrentSet();
  99. if (actionSet != nullptr && ! newTransaction)
  100. {
  101. if (auto* lastAction = actionSet->actions.getLast())
  102. {
  103. if (auto coalescedAction = lastAction->createCoalescedAction (action.get()))
  104. {
  105. action.reset (coalescedAction);
  106. totalUnitsStored -= lastAction->getSizeInUnits();
  107. actionSet->actions.removeLast();
  108. }
  109. }
  110. }
  111. else
  112. {
  113. actionSet = new ActionSet (newTransactionName);
  114. transactions.insert (nextIndex, actionSet);
  115. ++nextIndex;
  116. }
  117. totalUnitsStored += action->getSizeInUnits();
  118. actionSet->actions.add (std::move (action));
  119. newTransaction = false;
  120. moveFutureTransactionsToStash();
  121. dropOldTransactionsIfTooLarge();
  122. sendChangeMessage();
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. void UndoManager::moveFutureTransactionsToStash()
  129. {
  130. if (nextIndex < transactions.size())
  131. {
  132. stashedFutureTransactions.clear();
  133. while (nextIndex < transactions.size())
  134. {
  135. auto* removed = transactions.removeAndReturn (nextIndex);
  136. stashedFutureTransactions.add (removed);
  137. totalUnitsStored -= removed->getTotalSize();
  138. }
  139. }
  140. }
  141. void UndoManager::restoreStashedFutureTransactions()
  142. {
  143. while (nextIndex < transactions.size())
  144. {
  145. totalUnitsStored -= transactions.getUnchecked (nextIndex)->getTotalSize();
  146. transactions.remove (nextIndex);
  147. }
  148. for (auto* stashed : stashedFutureTransactions)
  149. {
  150. transactions.add (stashed);
  151. totalUnitsStored += stashed->getTotalSize();
  152. }
  153. stashedFutureTransactions.clearQuick (false);
  154. }
  155. void UndoManager::dropOldTransactionsIfTooLarge()
  156. {
  157. while (nextIndex > 0
  158. && totalUnitsStored > maxNumUnitsToKeep
  159. && transactions.size() > minimumTransactionsToKeep)
  160. {
  161. totalUnitsStored -= transactions.getFirst()->getTotalSize();
  162. transactions.remove (0);
  163. --nextIndex;
  164. // if this fails, then some actions may not be returning
  165. // consistent results from their getSizeInUnits() method
  166. jassert (totalUnitsStored >= 0);
  167. }
  168. }
  169. void UndoManager::beginNewTransaction()
  170. {
  171. beginNewTransaction ({});
  172. }
  173. void UndoManager::beginNewTransaction (const String& actionName)
  174. {
  175. newTransaction = true;
  176. newTransactionName = actionName;
  177. }
  178. void UndoManager::setCurrentTransactionName (const String& newName)
  179. {
  180. if (newTransaction)
  181. newTransactionName = newName;
  182. else if (auto* action = getCurrentSet())
  183. action->name = newName;
  184. }
  185. String UndoManager::getCurrentTransactionName() const
  186. {
  187. if (auto* action = getCurrentSet())
  188. return action->name;
  189. return newTransactionName;
  190. }
  191. //==============================================================================
  192. UndoManager::ActionSet* UndoManager::getCurrentSet() const { return transactions[nextIndex - 1]; }
  193. UndoManager::ActionSet* UndoManager::getNextSet() const { return transactions[nextIndex]; }
  194. bool UndoManager::isPerformingUndoRedo() const { return isInsideUndoRedoCall; }
  195. bool UndoManager::canUndo() const { return getCurrentSet() != nullptr; }
  196. bool UndoManager::canRedo() const { return getNextSet() != nullptr; }
  197. bool UndoManager::undo()
  198. {
  199. if (auto* s = getCurrentSet())
  200. {
  201. const ScopedValueSetter<bool> setter (isInsideUndoRedoCall, true);
  202. if (s->undo())
  203. --nextIndex;
  204. else
  205. clearUndoHistory();
  206. beginNewTransaction();
  207. sendChangeMessage();
  208. return true;
  209. }
  210. return false;
  211. }
  212. bool UndoManager::redo()
  213. {
  214. if (auto* s = getNextSet())
  215. {
  216. const ScopedValueSetter<bool> setter (isInsideUndoRedoCall, true);
  217. if (s->perform())
  218. ++nextIndex;
  219. else
  220. clearUndoHistory();
  221. beginNewTransaction();
  222. sendChangeMessage();
  223. return true;
  224. }
  225. return false;
  226. }
  227. String UndoManager::getUndoDescription() const
  228. {
  229. if (auto* s = getCurrentSet())
  230. return s->name;
  231. return {};
  232. }
  233. String UndoManager::getRedoDescription() const
  234. {
  235. if (auto* s = getNextSet())
  236. return s->name;
  237. return {};
  238. }
  239. StringArray UndoManager::getUndoDescriptions() const
  240. {
  241. StringArray descriptions;
  242. for (int i = nextIndex;;)
  243. {
  244. if (auto* t = transactions[--i])
  245. descriptions.add (t->name);
  246. else
  247. return descriptions;
  248. }
  249. }
  250. StringArray UndoManager::getRedoDescriptions() const
  251. {
  252. StringArray descriptions;
  253. for (int i = nextIndex;;)
  254. {
  255. if (auto* t = transactions[i++])
  256. descriptions.add (t->name);
  257. else
  258. return descriptions;
  259. }
  260. }
  261. Time UndoManager::getTimeOfUndoTransaction() const
  262. {
  263. if (auto* s = getCurrentSet())
  264. return s->time;
  265. return {};
  266. }
  267. Time UndoManager::getTimeOfRedoTransaction() const
  268. {
  269. if (auto* s = getNextSet())
  270. return s->time;
  271. return Time::getCurrentTime();
  272. }
  273. bool UndoManager::undoCurrentTransactionOnly()
  274. {
  275. if ((! newTransaction) && undo())
  276. {
  277. restoreStashedFutureTransactions();
  278. return true;
  279. }
  280. return false;
  281. }
  282. void UndoManager::getActionsInCurrentTransaction (Array<const UndoableAction*>& actionsFound) const
  283. {
  284. if (! newTransaction)
  285. if (auto* s = getCurrentSet())
  286. for (auto* a : s->actions)
  287. actionsFound.add (a);
  288. }
  289. int UndoManager::getNumActionsInCurrentTransaction() const
  290. {
  291. if (! newTransaction)
  292. if (auto* s = getCurrentSet())
  293. return s->actions.size();
  294. return 0;
  295. }
  296. } // namespace juce