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_ApplicationCommandManager.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. ApplicationCommandManager::ApplicationCommandManager()
  21. {
  22. keyMappings.reset (new KeyPressMappingSet (*this));
  23. Desktop::getInstance().addFocusChangeListener (this);
  24. }
  25. ApplicationCommandManager::~ApplicationCommandManager()
  26. {
  27. Desktop::getInstance().removeFocusChangeListener (this);
  28. keyMappings.reset();
  29. }
  30. //==============================================================================
  31. void ApplicationCommandManager::clearCommands()
  32. {
  33. commands.clear();
  34. keyMappings->clearAllKeyPresses();
  35. triggerAsyncUpdate();
  36. }
  37. void ApplicationCommandManager::registerCommand (const ApplicationCommandInfo& newCommand)
  38. {
  39. // zero isn't a valid command ID!
  40. jassert (newCommand.commandID != 0);
  41. // the name isn't optional!
  42. jassert (newCommand.shortName.isNotEmpty());
  43. if (auto* command = getMutableCommandForID (newCommand.commandID))
  44. {
  45. // Trying to re-register the same command ID with different parameters can often indicate a typo.
  46. // This assertion is here because I've found it useful catching some mistakes, but it may also cause
  47. // false alarms if you're deliberately updating some flags for a command.
  48. jassert (newCommand.shortName == getCommandForID (newCommand.commandID)->shortName
  49. && newCommand.categoryName == getCommandForID (newCommand.commandID)->categoryName
  50. && newCommand.defaultKeypresses == getCommandForID (newCommand.commandID)->defaultKeypresses
  51. && (newCommand.flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor))
  52. == (getCommandForID (newCommand.commandID)->flags & (ApplicationCommandInfo::wantsKeyUpDownCallbacks | ApplicationCommandInfo::hiddenFromKeyEditor | ApplicationCommandInfo::readOnlyInKeyEditor)));
  53. *command = newCommand;
  54. }
  55. else
  56. {
  57. auto* newInfo = new ApplicationCommandInfo (newCommand);
  58. newInfo->flags &= ~ApplicationCommandInfo::isTicked;
  59. commands.add (newInfo);
  60. keyMappings->resetToDefaultMapping (newCommand.commandID);
  61. triggerAsyncUpdate();
  62. }
  63. }
  64. void ApplicationCommandManager::registerAllCommandsForTarget (ApplicationCommandTarget* target)
  65. {
  66. if (target != nullptr)
  67. {
  68. Array<CommandID> commandIDs;
  69. target->getAllCommands (commandIDs);
  70. for (int i = 0; i < commandIDs.size(); ++i)
  71. {
  72. ApplicationCommandInfo info (commandIDs.getUnchecked(i));
  73. target->getCommandInfo (info.commandID, info);
  74. registerCommand (info);
  75. }
  76. }
  77. }
  78. void ApplicationCommandManager::removeCommand (const CommandID commandID)
  79. {
  80. for (int i = commands.size(); --i >= 0;)
  81. {
  82. if (commands.getUnchecked (i)->commandID == commandID)
  83. {
  84. commands.remove (i);
  85. triggerAsyncUpdate();
  86. const Array<KeyPress> keys (keyMappings->getKeyPressesAssignedToCommand (commandID));
  87. for (int j = keys.size(); --j >= 0;)
  88. keyMappings->removeKeyPress (keys.getReference (j));
  89. }
  90. }
  91. }
  92. void ApplicationCommandManager::commandStatusChanged()
  93. {
  94. triggerAsyncUpdate();
  95. }
  96. //==============================================================================
  97. ApplicationCommandInfo* ApplicationCommandManager::getMutableCommandForID (CommandID commandID) const noexcept
  98. {
  99. for (int i = commands.size(); --i >= 0;)
  100. if (commands.getUnchecked(i)->commandID == commandID)
  101. return commands.getUnchecked(i);
  102. return nullptr;
  103. }
  104. const ApplicationCommandInfo* ApplicationCommandManager::getCommandForID (CommandID commandID) const noexcept
  105. {
  106. return getMutableCommandForID (commandID);
  107. }
  108. String ApplicationCommandManager::getNameOfCommand (CommandID commandID) const noexcept
  109. {
  110. if (auto* ci = getCommandForID (commandID))
  111. return ci->shortName;
  112. return {};
  113. }
  114. String ApplicationCommandManager::getDescriptionOfCommand (CommandID commandID) const noexcept
  115. {
  116. if (auto* ci = getCommandForID (commandID))
  117. return ci->description.isNotEmpty() ? ci->description
  118. : ci->shortName;
  119. return {};
  120. }
  121. StringArray ApplicationCommandManager::getCommandCategories() const
  122. {
  123. StringArray s;
  124. for (int i = 0; i < commands.size(); ++i)
  125. s.addIfNotAlreadyThere (commands.getUnchecked(i)->categoryName, false);
  126. return s;
  127. }
  128. Array<CommandID> ApplicationCommandManager::getCommandsInCategory (const String& categoryName) const
  129. {
  130. Array<CommandID> results;
  131. for (int i = 0; i < commands.size(); ++i)
  132. if (commands.getUnchecked(i)->categoryName == categoryName)
  133. results.add (commands.getUnchecked(i)->commandID);
  134. return results;
  135. }
  136. //==============================================================================
  137. bool ApplicationCommandManager::invokeDirectly (CommandID commandID, bool asynchronously)
  138. {
  139. ApplicationCommandTarget::InvocationInfo info (commandID);
  140. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::direct;
  141. return invoke (info, asynchronously);
  142. }
  143. bool ApplicationCommandManager::invoke (const ApplicationCommandTarget::InvocationInfo& inf, bool asynchronously)
  144. {
  145. // This call isn't thread-safe for use from a non-UI thread without locking the message
  146. // manager first..
  147. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  148. bool ok = false;
  149. ApplicationCommandInfo commandInfo (0);
  150. if (auto* target = getTargetForCommand (inf.commandID, commandInfo))
  151. {
  152. ApplicationCommandTarget::InvocationInfo info (inf);
  153. info.commandFlags = commandInfo.flags;
  154. sendListenerInvokeCallback (info);
  155. ok = target->invoke (info, asynchronously);
  156. commandStatusChanged();
  157. }
  158. return ok;
  159. }
  160. //==============================================================================
  161. ApplicationCommandTarget* ApplicationCommandManager::getFirstCommandTarget (CommandID)
  162. {
  163. return firstTarget != nullptr ? firstTarget
  164. : findDefaultComponentTarget();
  165. }
  166. void ApplicationCommandManager::setFirstCommandTarget (ApplicationCommandTarget* newTarget) noexcept
  167. {
  168. firstTarget = newTarget;
  169. }
  170. ApplicationCommandTarget* ApplicationCommandManager::getTargetForCommand (CommandID commandID,
  171. ApplicationCommandInfo& upToDateInfo)
  172. {
  173. auto* target = getFirstCommandTarget (commandID);
  174. if (target == nullptr)
  175. target = JUCEApplication::getInstance();
  176. if (target != nullptr)
  177. target = target->getTargetForCommand (commandID);
  178. if (target != nullptr)
  179. {
  180. upToDateInfo.commandID = commandID;
  181. target->getCommandInfo (commandID, upToDateInfo);
  182. }
  183. return target;
  184. }
  185. //==============================================================================
  186. ApplicationCommandTarget* ApplicationCommandManager::findTargetForComponent (Component* c)
  187. {
  188. auto* target = dynamic_cast<ApplicationCommandTarget*> (c);
  189. if (target == nullptr && c != nullptr)
  190. target = c->findParentComponentOfClass<ApplicationCommandTarget>();
  191. return target;
  192. }
  193. ApplicationCommandTarget* ApplicationCommandManager::findDefaultComponentTarget()
  194. {
  195. auto* c = Component::getCurrentlyFocusedComponent();
  196. if (c == nullptr)
  197. {
  198. if (auto* activeWindow = TopLevelWindow::getActiveTopLevelWindow())
  199. {
  200. if (auto* peer = activeWindow->getPeer())
  201. {
  202. c = peer->getLastFocusedSubcomponent();
  203. if (c == nullptr)
  204. c = activeWindow;
  205. }
  206. }
  207. }
  208. if (c == nullptr)
  209. {
  210. auto& desktop = Desktop::getInstance();
  211. // getting a bit desperate now: try all desktop comps..
  212. for (int i = desktop.getNumComponents(); --i >= 0;)
  213. if (auto* component = desktop.getComponent (i))
  214. if (isForegroundOrEmbeddedProcess (component))
  215. if (auto* peer = component->getPeer())
  216. if (auto* target = findTargetForComponent (peer->getLastFocusedSubcomponent()))
  217. return target;
  218. }
  219. if (c != nullptr)
  220. {
  221. // if we're focused on a ResizableWindow, chances are that it's the content
  222. // component that really should get the event. And if not, the event will
  223. // still be passed up to the top level window anyway, so let's send it to the
  224. // content comp.
  225. if (auto* resizableWindow = dynamic_cast<ResizableWindow*> (c))
  226. if (auto* content = resizableWindow->getContentComponent())
  227. c = content;
  228. if (auto* target = findTargetForComponent (c))
  229. return target;
  230. }
  231. return JUCEApplication::getInstance();
  232. }
  233. //==============================================================================
  234. void ApplicationCommandManager::addListener (ApplicationCommandManagerListener* listener)
  235. {
  236. listeners.add (listener);
  237. }
  238. void ApplicationCommandManager::removeListener (ApplicationCommandManagerListener* listener)
  239. {
  240. listeners.remove (listener);
  241. }
  242. void ApplicationCommandManager::sendListenerInvokeCallback (const ApplicationCommandTarget::InvocationInfo& info)
  243. {
  244. listeners.call ([&] (ApplicationCommandManagerListener& l) { l.applicationCommandInvoked (info); });
  245. }
  246. void ApplicationCommandManager::handleAsyncUpdate()
  247. {
  248. listeners.call ([] (ApplicationCommandManagerListener& l) { l.applicationCommandListChanged(); });
  249. }
  250. void ApplicationCommandManager::globalFocusChanged (Component*)
  251. {
  252. commandStatusChanged();
  253. }
  254. } // namespace juce