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.

320 lines
11KB

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