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_ApplicationCommandTarget.cpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. class ApplicationCommandTarget::CommandMessage : public MessageManager::MessageBase
  16. {
  17. public:
  18. CommandMessage (ApplicationCommandTarget* const target, const InvocationInfo& inf)
  19. : owner (target), info (inf)
  20. {
  21. }
  22. void messageCallback() override
  23. {
  24. if (ApplicationCommandTarget* const target = owner)
  25. target->tryToInvoke (info, false);
  26. }
  27. private:
  28. WeakReference<ApplicationCommandTarget> owner;
  29. const InvocationInfo info;
  30. JUCE_DECLARE_NON_COPYABLE (CommandMessage)
  31. };
  32. //==============================================================================
  33. ApplicationCommandTarget::ApplicationCommandTarget() {}
  34. ApplicationCommandTarget::~ApplicationCommandTarget() {}
  35. //==============================================================================
  36. bool ApplicationCommandTarget::tryToInvoke (const InvocationInfo& info, const bool async)
  37. {
  38. if (isCommandActive (info.commandID))
  39. {
  40. if (async)
  41. {
  42. (new CommandMessage (this, info))->post();
  43. return true;
  44. }
  45. if (perform (info))
  46. return true;
  47. // Hmm.. your target claimed that it could perform this command, but failed to do so.
  48. // If it can't do it at the moment for some reason, it should clear the 'isActive' flag
  49. // when it returns the command's info.
  50. jassertfalse;
  51. }
  52. return false;
  53. }
  54. ApplicationCommandTarget* ApplicationCommandTarget::findFirstTargetParentComponent()
  55. {
  56. if (Component* const c = dynamic_cast<Component*> (this))
  57. return c->findParentComponentOfClass<ApplicationCommandTarget>();
  58. return nullptr;
  59. }
  60. ApplicationCommandTarget* ApplicationCommandTarget::getTargetForCommand (const CommandID commandID)
  61. {
  62. ApplicationCommandTarget* target = this;
  63. int depth = 0;
  64. while (target != nullptr)
  65. {
  66. Array<CommandID> commandIDs;
  67. target->getAllCommands (commandIDs);
  68. if (commandIDs.contains (commandID))
  69. return target;
  70. target = target->getNextCommandTarget();
  71. ++depth;
  72. jassert (depth < 100); // could be a recursive command chain??
  73. jassert (target != this); // definitely a recursive command chain!
  74. if (depth > 100 || target == this)
  75. break;
  76. }
  77. if (target == nullptr)
  78. {
  79. target = JUCEApplication::getInstance();
  80. if (target != nullptr)
  81. {
  82. Array<CommandID> commandIDs;
  83. target->getAllCommands (commandIDs);
  84. if (commandIDs.contains (commandID))
  85. return target;
  86. }
  87. }
  88. return nullptr;
  89. }
  90. bool ApplicationCommandTarget::isCommandActive (const CommandID commandID)
  91. {
  92. ApplicationCommandInfo info (commandID);
  93. info.flags = ApplicationCommandInfo::isDisabled;
  94. getCommandInfo (commandID, info);
  95. return (info.flags & ApplicationCommandInfo::isDisabled) == 0;
  96. }
  97. //==============================================================================
  98. bool ApplicationCommandTarget::invoke (const InvocationInfo& info, const bool async)
  99. {
  100. ApplicationCommandTarget* target = this;
  101. int depth = 0;
  102. while (target != nullptr)
  103. {
  104. if (target->tryToInvoke (info, async))
  105. return true;
  106. target = target->getNextCommandTarget();
  107. ++depth;
  108. jassert (depth < 100); // could be a recursive command chain??
  109. jassert (target != this); // definitely a recursive command chain!
  110. if (depth > 100 || target == this)
  111. break;
  112. }
  113. if (target == nullptr)
  114. {
  115. target = JUCEApplication::getInstance();
  116. if (target != nullptr)
  117. return target->tryToInvoke (info, async);
  118. }
  119. return false;
  120. }
  121. bool ApplicationCommandTarget::invokeDirectly (const CommandID commandID, const bool asynchronously)
  122. {
  123. ApplicationCommandTarget::InvocationInfo info (commandID);
  124. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::direct;
  125. return invoke (info, asynchronously);
  126. }
  127. //==============================================================================
  128. ApplicationCommandTarget::InvocationInfo::InvocationInfo (const CommandID command)
  129. : commandID (command),
  130. commandFlags (0),
  131. invocationMethod (direct),
  132. originatingComponent (nullptr),
  133. isKeyDown (false),
  134. millisecsSinceKeyPressed (0)
  135. {
  136. }
  137. } // namespace juce