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_DropShadower.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 DropShadower::ShadowWindow : public Component
  16. {
  17. public:
  18. ShadowWindow (Component* comp, const DropShadow& ds)
  19. : target (comp), shadow (ds)
  20. {
  21. setVisible (true);
  22. setInterceptsMouseClicks (false, false);
  23. if (comp->isOnDesktop())
  24. {
  25. setSize (1, 1); // to keep the OS happy by not having zero-size windows
  26. addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  27. | ComponentPeer::windowIsTemporary
  28. | ComponentPeer::windowIgnoresKeyPresses);
  29. }
  30. else if (Component* const parent = comp->getParentComponent())
  31. {
  32. parent->addChildComponent (this);
  33. }
  34. }
  35. void paint (Graphics& g) override
  36. {
  37. if (Component* c = target)
  38. shadow.drawForRectangle (g, getLocalArea (c, c->getLocalBounds()));
  39. }
  40. void resized() override
  41. {
  42. repaint(); // (needed for correct repainting)
  43. }
  44. float getDesktopScaleFactor() const override
  45. {
  46. if (target != nullptr)
  47. return target->getDesktopScaleFactor();
  48. return Component::getDesktopScaleFactor();
  49. }
  50. private:
  51. WeakReference<Component> target;
  52. DropShadow shadow;
  53. JUCE_DECLARE_NON_COPYABLE (ShadowWindow)
  54. };
  55. //==============================================================================
  56. DropShadower::DropShadower (const DropShadow& ds)
  57. : owner (nullptr), shadow (ds), reentrant (false)
  58. {
  59. }
  60. DropShadower::~DropShadower()
  61. {
  62. if (owner != nullptr)
  63. {
  64. owner->removeComponentListener (this);
  65. owner = nullptr;
  66. }
  67. updateParent();
  68. reentrant = true;
  69. shadowWindows.clear();
  70. }
  71. void DropShadower::setOwner (Component* componentToFollow)
  72. {
  73. if (componentToFollow != owner)
  74. {
  75. if (owner != nullptr)
  76. owner->removeComponentListener (this);
  77. // (the component can't be null)
  78. jassert (componentToFollow != nullptr);
  79. owner = componentToFollow;
  80. jassert (owner != nullptr);
  81. updateParent();
  82. owner->addComponentListener (this);
  83. updateShadows();
  84. }
  85. }
  86. void DropShadower::updateParent()
  87. {
  88. if (Component* p = lastParentComp)
  89. p->removeComponentListener (this);
  90. lastParentComp = owner != nullptr ? owner->getParentComponent() : nullptr;
  91. if (Component* p = lastParentComp)
  92. p->addComponentListener (this);
  93. }
  94. void DropShadower::componentMovedOrResized (Component& c, bool /*wasMoved*/, bool /*wasResized*/)
  95. {
  96. if (owner == &c)
  97. updateShadows();
  98. }
  99. void DropShadower::componentBroughtToFront (Component& c)
  100. {
  101. if (owner == &c)
  102. updateShadows();
  103. }
  104. void DropShadower::componentChildrenChanged (Component&)
  105. {
  106. updateShadows();
  107. }
  108. void DropShadower::componentParentHierarchyChanged (Component& c)
  109. {
  110. if (owner == &c)
  111. {
  112. updateParent();
  113. updateShadows();
  114. }
  115. }
  116. void DropShadower::componentVisibilityChanged (Component& c)
  117. {
  118. if (owner == &c)
  119. updateShadows();
  120. }
  121. void DropShadower::updateShadows()
  122. {
  123. if (reentrant)
  124. return;
  125. const ScopedValueSetter<bool> setter (reentrant, true, false);
  126. if (owner == nullptr)
  127. {
  128. shadowWindows.clear();
  129. return;
  130. }
  131. if (owner->isShowing()
  132. && owner->getWidth() > 0 && owner->getHeight() > 0
  133. && (Desktop::canUseSemiTransparentWindows() || owner->getParentComponent() != nullptr))
  134. {
  135. while (shadowWindows.size() < 4)
  136. shadowWindows.add (new ShadowWindow (owner, shadow));
  137. const int shadowEdge = jmax (shadow.offset.x, shadow.offset.y) + shadow.radius;
  138. const int x = owner->getX();
  139. const int y = owner->getY() - shadowEdge;
  140. const int w = owner->getWidth();
  141. const int h = owner->getHeight() + shadowEdge + shadowEdge;
  142. for (int i = 4; --i >= 0;)
  143. {
  144. // there seem to be rare situations where the dropshadower may be deleted by
  145. // callbacks during this loop, so use a weak ref to watch out for this..
  146. WeakReference<Component> sw (shadowWindows[i]);
  147. if (sw != nullptr)
  148. {
  149. sw->setAlwaysOnTop (owner->isAlwaysOnTop());
  150. if (sw == nullptr)
  151. return;
  152. switch (i)
  153. {
  154. case 0: sw->setBounds (x - shadowEdge, y, shadowEdge, h); break;
  155. case 1: sw->setBounds (x + w, y, shadowEdge, h); break;
  156. case 2: sw->setBounds (x, y, w, shadowEdge); break;
  157. case 3: sw->setBounds (x, owner->getBottom(), w, shadowEdge); break;
  158. default: break;
  159. }
  160. if (sw == nullptr)
  161. return;
  162. sw->toBehind (i == 3 ? owner : shadowWindows.getUnchecked (i + 1));
  163. }
  164. }
  165. }
  166. else
  167. {
  168. shadowWindows.clear();
  169. }
  170. }
  171. } // namespace juce