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.1KB

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