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.

266 lines
8.3KB

  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. CallOutBox::CallOutBox (Component& c, Rectangle<int> area, Component* const parent)
  21. : content (c)
  22. {
  23. addAndMakeVisible (content);
  24. if (parent != nullptr)
  25. {
  26. parent->addChildComponent (this);
  27. updatePosition (area, parent->getLocalBounds());
  28. setVisible (true);
  29. }
  30. else
  31. {
  32. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  33. updatePosition (area, Desktop::getInstance().getDisplays().findDisplayForRect (area).userArea);
  34. addToDesktop (ComponentPeer::windowIsTemporary);
  35. startTimer (100);
  36. }
  37. creationTime = Time::getCurrentTime();
  38. }
  39. //==============================================================================
  40. class CallOutBoxCallback : public ModalComponentManager::Callback,
  41. private Timer
  42. {
  43. public:
  44. CallOutBoxCallback (std::unique_ptr<Component> c, const Rectangle<int>& area, Component* parent)
  45. : content (std::move (c)),
  46. callout (*content, area, parent)
  47. {
  48. callout.setVisible (true);
  49. callout.enterModalState (true, this);
  50. startTimer (200);
  51. }
  52. void modalStateFinished (int) override {}
  53. void timerCallback() override
  54. {
  55. if (! Process::isForegroundProcess())
  56. callout.dismiss();
  57. }
  58. std::unique_ptr<Component> content;
  59. CallOutBox callout;
  60. JUCE_DECLARE_NON_COPYABLE (CallOutBoxCallback)
  61. };
  62. CallOutBox& CallOutBox::launchAsynchronously (std::unique_ptr<Component> content, Rectangle<int> area, Component* parent)
  63. {
  64. jassert (content != nullptr); // must be a valid content component!
  65. return (new CallOutBoxCallback (std::move (content), area, parent))->callout;
  66. }
  67. //==============================================================================
  68. void CallOutBox::setArrowSize (const float newSize)
  69. {
  70. arrowSize = newSize;
  71. refreshPath();
  72. }
  73. int CallOutBox::getBorderSize() const noexcept
  74. {
  75. return jmax (getLookAndFeel().getCallOutBoxBorderSize (*this), (int) arrowSize);
  76. }
  77. void CallOutBox::lookAndFeelChanged()
  78. {
  79. resized();
  80. repaint();
  81. }
  82. void CallOutBox::paint (Graphics& g)
  83. {
  84. getLookAndFeel().drawCallOutBoxBackground (*this, g, outline, background);
  85. }
  86. void CallOutBox::resized()
  87. {
  88. auto borderSpace = getBorderSize();
  89. content.setTopLeftPosition (borderSpace, borderSpace);
  90. refreshPath();
  91. }
  92. void CallOutBox::moved()
  93. {
  94. refreshPath();
  95. }
  96. void CallOutBox::childBoundsChanged (Component*)
  97. {
  98. updatePosition (targetArea, availableArea);
  99. }
  100. bool CallOutBox::hitTest (int x, int y)
  101. {
  102. return outline.contains ((float) x, (float) y);
  103. }
  104. void CallOutBox::inputAttemptWhenModal()
  105. {
  106. if (dismissalMouseClicksAreAlwaysConsumed
  107. || targetArea.contains (getMouseXYRelative() + getBounds().getPosition()))
  108. {
  109. // if you click on the area that originally popped-up the callout, you expect it
  110. // to get rid of the box, but deleting the box here allows the click to pass through and
  111. // probably re-trigger it, so we need to dismiss the box asynchronously to consume the click..
  112. // For touchscreens, we make sure not to dismiss the CallOutBox immediately,
  113. // as Windows still sends touch events before the CallOutBox had a chance
  114. // to really open.
  115. auto elapsed = Time::getCurrentTime() - creationTime;
  116. if (elapsed.inMilliseconds() > 200)
  117. dismiss();
  118. }
  119. else
  120. {
  121. exitModalState (0);
  122. setVisible (false);
  123. }
  124. }
  125. void CallOutBox::setDismissalMouseClicksAreAlwaysConsumed (bool b) noexcept
  126. {
  127. dismissalMouseClicksAreAlwaysConsumed = b;
  128. }
  129. static constexpr int callOutBoxDismissCommandId = 0x4f83a04b;
  130. void CallOutBox::handleCommandMessage (int commandId)
  131. {
  132. Component::handleCommandMessage (commandId);
  133. if (commandId == callOutBoxDismissCommandId)
  134. {
  135. exitModalState (0);
  136. setVisible (false);
  137. }
  138. }
  139. void CallOutBox::dismiss()
  140. {
  141. postCommandMessage (callOutBoxDismissCommandId);
  142. }
  143. bool CallOutBox::keyPressed (const KeyPress& key)
  144. {
  145. if (key.isKeyCode (KeyPress::escapeKey))
  146. {
  147. inputAttemptWhenModal();
  148. return true;
  149. }
  150. return false;
  151. }
  152. void CallOutBox::updatePosition (const Rectangle<int>& newAreaToPointTo, const Rectangle<int>& newAreaToFitIn)
  153. {
  154. targetArea = newAreaToPointTo;
  155. availableArea = newAreaToFitIn;
  156. auto borderSpace = getBorderSize();
  157. auto newBounds = getLocalArea (&content, Rectangle<int> (content.getWidth() + borderSpace * 2,
  158. content.getHeight() + borderSpace * 2));
  159. auto hw = newBounds.getWidth() / 2;
  160. auto hh = newBounds.getHeight() / 2;
  161. auto hwReduced = (float) (hw - borderSpace * 2);
  162. auto hhReduced = (float) (hh - borderSpace * 2);
  163. auto arrowIndent = (float) borderSpace - arrowSize;
  164. Point<float> targets[4] = { { (float) targetArea.getCentreX(), (float) targetArea.getBottom() },
  165. { (float) targetArea.getRight(), (float) targetArea.getCentreY() },
  166. { (float) targetArea.getX(), (float) targetArea.getCentreY() },
  167. { (float) targetArea.getCentreX(), (float) targetArea.getY() } };
  168. Line<float> lines[4] = { { targets[0].translated (-hwReduced, hh - arrowIndent), targets[0].translated (hwReduced, hh - arrowIndent) },
  169. { targets[1].translated (hw - arrowIndent, -hhReduced), targets[1].translated (hw - arrowIndent, hhReduced) },
  170. { targets[2].translated (-(hw - arrowIndent), -hhReduced), targets[2].translated (-(hw - arrowIndent), hhReduced) },
  171. { targets[3].translated (-hwReduced, -(hh - arrowIndent)), targets[3].translated (hwReduced, -(hh - arrowIndent)) } };
  172. auto centrePointArea = newAreaToFitIn.reduced (hw, hh).toFloat();
  173. auto targetCentre = targetArea.getCentre().toFloat();
  174. float nearest = 1.0e9f;
  175. for (int i = 0; i < 4; ++i)
  176. {
  177. Line<float> constrainedLine (centrePointArea.getConstrainedPoint (lines[i].getStart()),
  178. centrePointArea.getConstrainedPoint (lines[i].getEnd()));
  179. auto centre = constrainedLine.findNearestPointTo (targetCentre);
  180. auto distanceFromCentre = centre.getDistanceFrom (targets[i]);
  181. if (! centrePointArea.intersects (lines[i]))
  182. distanceFromCentre += 1000.0f;
  183. if (distanceFromCentre < nearest)
  184. {
  185. nearest = distanceFromCentre;
  186. targetPoint = targets[i];
  187. newBounds.setPosition ((int) (centre.x - (float) hw),
  188. (int) (centre.y - (float) hh));
  189. }
  190. }
  191. setBounds (newBounds);
  192. }
  193. void CallOutBox::refreshPath()
  194. {
  195. repaint();
  196. background = {};
  197. outline.clear();
  198. const float gap = 4.5f;
  199. outline.addBubble (getLocalArea (&content, content.getLocalBounds().toFloat()).expanded (gap, gap),
  200. getLocalBounds().toFloat(),
  201. targetPoint - getPosition().toFloat(),
  202. getLookAndFeel().getCallOutBoxCornerSize (*this), arrowSize * 0.7f);
  203. }
  204. void CallOutBox::timerCallback()
  205. {
  206. toFront (true);
  207. stopTimer();
  208. }
  209. } // namespace juce