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.

259 lines
7.8KB

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