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.

321 lines
11KB

  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. 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 && Process::isForegroundProcess())
  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* peer = desktop.getComponent(i)->getPeer())
  214. if (auto* target = findTargetForComponent (peer->getLastFocusedSubcomponent()))
  215. return target;
  216. }
  217. if (c != nullptr)
  218. {
  219. // if we're focused on a ResizableWindow, chances are that it's the content
  220. // component that really should get the event. And if not, the event will
  221. // still be passed up to the top level window anyway, so let's send it to the
  222. // content comp.
  223. if (auto* resizableWindow = dynamic_cast<ResizableWindow*> (c))
  224. if (auto* content = resizableWindow->getContentComponent())
  225. c = content;
  226. if (auto* target = findTargetForComponent (c))
  227. return target;
  228. }
  229. return JUCEApplication::getInstance();
  230. }
  231. //==============================================================================
  232. void ApplicationCommandManager::addListener (ApplicationCommandManagerListener* listener)
  233. {
  234. listeners.add (listener);
  235. }
  236. void ApplicationCommandManager::removeListener (ApplicationCommandManagerListener* listener)
  237. {
  238. listeners.remove (listener);
  239. }
  240. void ApplicationCommandManager::sendListenerInvokeCallback (const ApplicationCommandTarget::InvocationInfo& info)
  241. {
  242. listeners.call ([&] (ApplicationCommandManagerListener& l) { l.applicationCommandInvoked (info); });
  243. }
  244. void ApplicationCommandManager::handleAsyncUpdate()
  245. {
  246. listeners.call ([] (ApplicationCommandManagerListener& l) { l.applicationCommandListChanged(); });
  247. }
  248. void ApplicationCommandManager::globalFocusChanged (Component*)
  249. {
  250. commandStatusChanged();
  251. }
  252. } // namespace juce