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.

370 lines
9.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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