The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

271 lines
8.5KB

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