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.

349 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. One of these objects holds a list of all the commands your app can perform,
  18. and despatches these commands when needed.
  19. Application commands are a good way to trigger actions in your app, e.g. "Quit",
  20. "Copy", "Paste", etc. Menus, buttons and keypresses can all be given commands
  21. to invoke automatically, which means you don't have to handle the result of a menu
  22. or button click manually. Commands are despatched to ApplicationCommandTarget objects
  23. which can choose which events they want to handle.
  24. This architecture also allows for nested ApplicationCommandTargets, so that for example
  25. you could have two different objects, one inside the other, both of which can respond to
  26. a "delete" command. Depending on which one has focus, the command will be sent to the
  27. appropriate place, regardless of whether it was triggered by a menu, keypress or some other
  28. method.
  29. To set up your app to use commands, you'll need to do the following:
  30. - Create a global ApplicationCommandManager to hold the list of all possible
  31. commands. (This will also manage a set of key-mappings for them).
  32. - Make some of your UI components (or other objects) inherit from ApplicationCommandTarget.
  33. This allows the object to provide a list of commands that it can perform, and
  34. to handle them.
  35. - Register each type of command using ApplicationCommandManager::registerAllCommandsForTarget(),
  36. or ApplicationCommandManager::registerCommand().
  37. - If you want key-presses to trigger your commands, use the ApplicationCommandManager::getKeyMappings()
  38. method to access the key-mapper object, which you will need to register as a key-listener
  39. in whatever top-level component you're using. See the KeyPressMappingSet class for more help
  40. about setting this up.
  41. - Use methods such as PopupMenu::addCommandItem() or Button::setCommandToTrigger() to
  42. cause these commands to be invoked automatically.
  43. - Commands can be invoked directly by your code using ApplicationCommandManager::invokeDirectly().
  44. When a command is invoked, the ApplicationCommandManager will try to choose the best
  45. ApplicationCommandTarget to receive the specified command. To do this it will use the
  46. current keyboard focus to see which component might be interested, and will search the
  47. component hierarchy for those that also implement the ApplicationCommandTarget interface.
  48. If an ApplicationCommandTarget isn't interested in the command that is being invoked, then
  49. the next one in line will be tried (see the ApplicationCommandTarget::getNextCommandTarget()
  50. method), and so on until ApplicationCommandTarget::getNextCommandTarget() returns nullptr.
  51. At this point if the command still hasn't been performed, it will be passed to the current
  52. JUCEApplication object (which is itself an ApplicationCommandTarget).
  53. To exert some custom control over which ApplicationCommandTarget is chosen to invoke a command,
  54. you can override the ApplicationCommandManager::getFirstCommandTarget() method and choose
  55. the object yourself.
  56. @see ApplicationCommandTarget, ApplicationCommandInfo
  57. @tags{GUI}
  58. */
  59. class JUCE_API ApplicationCommandManager : private AsyncUpdater,
  60. private FocusChangeListener
  61. {
  62. public:
  63. //==============================================================================
  64. /** Creates an ApplicationCommandManager.
  65. Once created, you'll need to register all your app's commands with it, using
  66. ApplicationCommandManager::registerAllCommandsForTarget() or
  67. ApplicationCommandManager::registerCommand().
  68. */
  69. ApplicationCommandManager();
  70. /** Destructor.
  71. Make sure that you don't delete this if pointers to it are still being used by
  72. objects such as PopupMenus or Buttons.
  73. */
  74. ~ApplicationCommandManager() override;
  75. //==============================================================================
  76. /** Clears the current list of all commands.
  77. Note that this will also clear the contents of the KeyPressMappingSet.
  78. */
  79. void clearCommands();
  80. /** Adds a command to the list of registered commands.
  81. @see registerAllCommandsForTarget
  82. */
  83. void registerCommand (const ApplicationCommandInfo& newCommand);
  84. /** Adds all the commands that this target publishes to the manager's list.
  85. This will use ApplicationCommandTarget::getAllCommands() and ApplicationCommandTarget::getCommandInfo()
  86. to get details about all the commands that this target can do, and will call
  87. registerCommand() to add each one to the manger's list.
  88. @see registerCommand
  89. */
  90. void registerAllCommandsForTarget (ApplicationCommandTarget* target);
  91. /** Removes the command with a specified ID.
  92. Note that this will also remove any key mappings that are mapped to the command.
  93. */
  94. void removeCommand (CommandID commandID);
  95. /** This should be called to tell the manager that one of its registered commands may have changed
  96. its active status.
  97. Because the command manager only finds out whether a command is active or inactive by querying
  98. the current ApplicationCommandTarget, this is used to tell it that things may have changed. It
  99. allows things like buttons to update their enablement, etc.
  100. This method will cause an asynchronous call to ApplicationCommandManagerListener::applicationCommandListChanged()
  101. for any registered listeners.
  102. */
  103. void commandStatusChanged();
  104. //==============================================================================
  105. /** Returns the number of commands that have been registered.
  106. @see registerCommand
  107. */
  108. int getNumCommands() const noexcept { return commands.size(); }
  109. /** Returns the details about one of the registered commands.
  110. The index is between 0 and (getNumCommands() - 1).
  111. */
  112. const ApplicationCommandInfo* getCommandForIndex (int index) const noexcept { return commands [index]; }
  113. /** Returns the details about a given command ID.
  114. This will search the list of registered commands for one with the given command
  115. ID number, and return its associated info. If no matching command is found, this
  116. will return nullptr.
  117. */
  118. const ApplicationCommandInfo* getCommandForID (CommandID commandID) const noexcept;
  119. /** Returns the name field for a command.
  120. An empty string is returned if no command with this ID has been registered.
  121. @see getDescriptionOfCommand
  122. */
  123. String getNameOfCommand (CommandID commandID) const noexcept;
  124. /** Returns the description field for a command.
  125. An empty string is returned if no command with this ID has been registered. If the
  126. command has no description, this will return its short name field instead.
  127. @see getNameOfCommand
  128. */
  129. String getDescriptionOfCommand (CommandID commandID) const noexcept;
  130. /** Returns the list of categories.
  131. This will go through all registered commands, and return a list of all the distinct
  132. categoryName values from their ApplicationCommandInfo structure.
  133. @see getCommandsInCategory()
  134. */
  135. StringArray getCommandCategories() const;
  136. /** Returns a list of all the command UIDs in a particular category.
  137. @see getCommandCategories()
  138. */
  139. Array<CommandID> getCommandsInCategory (const String& categoryName) const;
  140. //==============================================================================
  141. /** Returns the manager's internal set of key mappings.
  142. This object can be used to edit the keypresses. To actually link this object up
  143. to invoke commands when a key is pressed, see the comments for the KeyPressMappingSet
  144. class.
  145. @see KeyPressMappingSet
  146. */
  147. KeyPressMappingSet* getKeyMappings() const noexcept { return keyMappings.get(); }
  148. //==============================================================================
  149. /** Invokes the given command directly, sending it to the default target.
  150. This is just an easy way to call invoke() without having to fill out the InvocationInfo
  151. structure.
  152. */
  153. bool invokeDirectly (CommandID commandID, bool asynchronously);
  154. /** Sends a command to the default target.
  155. This will choose a target using getFirstCommandTarget(), and send the specified command
  156. to it using the ApplicationCommandTarget::invoke() method. This means that if the
  157. first target can't handle the command, it will be passed on to targets further down the
  158. chain (see ApplicationCommandTarget::invoke() for more info).
  159. @param invocationInfo this must be correctly filled-in, describing the context for
  160. the invocation.
  161. @param asynchronously if false, the command will be performed before this method returns.
  162. If true, a message will be posted so that the command will be performed
  163. later on the message thread, and this method will return immediately.
  164. @see ApplicationCommandTarget::invoke
  165. */
  166. bool invoke (const ApplicationCommandTarget::InvocationInfo& invocationInfo,
  167. bool asynchronously);
  168. //==============================================================================
  169. /** Chooses the ApplicationCommandTarget to which a command should be sent.
  170. Whenever the manager needs to know which target a command should be sent to, it calls
  171. this method to determine the first one to try.
  172. By default, this method will return the target that was set by calling setFirstCommandTarget().
  173. If no target is set, it will return the result of findDefaultComponentTarget().
  174. If you need to make sure all commands go via your own custom target, then you can
  175. either use setFirstCommandTarget() to specify a single target, or override this method
  176. if you need more complex logic to choose one.
  177. It may return nullptr if no targets are available.
  178. @see getTargetForCommand, invoke, invokeDirectly
  179. */
  180. virtual ApplicationCommandTarget* getFirstCommandTarget (CommandID commandID);
  181. /** Sets a target to be returned by getFirstCommandTarget().
  182. If this is set to nullptr, then getFirstCommandTarget() will by default return the
  183. result of findDefaultComponentTarget().
  184. If you use this to set a target, make sure you call setFirstCommandTarget(nullptr)
  185. before deleting the target object.
  186. */
  187. void setFirstCommandTarget (ApplicationCommandTarget* newTarget) noexcept;
  188. /** Tries to find the best target to use to perform a given command.
  189. This will call getFirstCommandTarget() to find the preferred target, and will
  190. check whether that target can handle the given command. If it can't, then it'll use
  191. ApplicationCommandTarget::getNextCommandTarget() to find the next one to try, and
  192. so on until no more are available.
  193. If no targets are found that can perform the command, this method will return nullptr.
  194. If a target is found, then it will get the target to fill-in the upToDateInfo
  195. structure with the latest info about that command, so that the caller can see
  196. whether the command is disabled, ticked, etc.
  197. */
  198. ApplicationCommandTarget* getTargetForCommand (CommandID commandID,
  199. ApplicationCommandInfo& upToDateInfo);
  200. //==============================================================================
  201. /** Registers a listener that will be called when various events occur. */
  202. void addListener (ApplicationCommandManagerListener* listener);
  203. /** Deregisters a previously-added listener. */
  204. void removeListener (ApplicationCommandManagerListener* listener);
  205. //==============================================================================
  206. /** Looks for a suitable command target based on which Components have the keyboard focus.
  207. This is used by the default implementation of ApplicationCommandTarget::getFirstCommandTarget(),
  208. but is exposed here in case it's useful.
  209. It tries to pick the best ApplicationCommandTarget by looking at focused components, top level
  210. windows, etc., and using the findTargetForComponent() method.
  211. */
  212. static ApplicationCommandTarget* findDefaultComponentTarget();
  213. /** Examines this component and all its parents in turn, looking for the first one
  214. which is an ApplicationCommandTarget.
  215. Returns the first ApplicationCommandTarget that it finds, or nullptr if none of them
  216. implement that class.
  217. */
  218. static ApplicationCommandTarget* findTargetForComponent (Component*);
  219. private:
  220. //==============================================================================
  221. OwnedArray<ApplicationCommandInfo> commands;
  222. ListenerList<ApplicationCommandManagerListener> listeners;
  223. std::unique_ptr<KeyPressMappingSet> keyMappings;
  224. ApplicationCommandTarget* firstTarget = nullptr;
  225. void sendListenerInvokeCallback (const ApplicationCommandTarget::InvocationInfo&);
  226. void handleAsyncUpdate() override;
  227. void globalFocusChanged (Component*) override;
  228. ApplicationCommandInfo* getMutableCommandForID (CommandID) const noexcept;
  229. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  230. // This is just here to cause a compile error in old code that hasn't been changed to use the new
  231. // version of this method.
  232. virtual short getFirstCommandTarget() { return 0; }
  233. #endif
  234. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ApplicationCommandManager)
  235. };
  236. //==============================================================================
  237. /**
  238. A listener that receives callbacks from an ApplicationCommandManager when
  239. commands are invoked or the command list is changed.
  240. @see ApplicationCommandManager::addListener, ApplicationCommandManager::removeListener
  241. @tags{GUI}
  242. */
  243. class JUCE_API ApplicationCommandManagerListener
  244. {
  245. public:
  246. //==============================================================================
  247. /** Destructor. */
  248. virtual ~ApplicationCommandManagerListener() = default;
  249. /** Called when an app command is about to be invoked. */
  250. virtual void applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo&) = 0;
  251. /** Called when commands are registered or deregistered from the
  252. command manager, or when commands are made active or inactive.
  253. Note that if you're using this to watch for changes to whether a command is disabled,
  254. you'll need to make sure that ApplicationCommandManager::commandStatusChanged() is called
  255. whenever the status of your command might have changed.
  256. */
  257. virtual void applicationCommandListChanged() = 0;
  258. };
  259. } // namespace juce