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.

304 lines
9.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. class DropShadower::ShadowWindow : public Component
  21. {
  22. public:
  23. ShadowWindow (Component* comp, const DropShadow& ds)
  24. : target (comp), shadow (ds)
  25. {
  26. setVisible (true);
  27. setAccessible (false);
  28. setInterceptsMouseClicks (false, false);
  29. if (comp->isOnDesktop())
  30. {
  31. setSize (1, 1); // to keep the OS happy by not having zero-size windows
  32. addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  33. | ComponentPeer::windowIsTemporary
  34. | ComponentPeer::windowIgnoresKeyPresses);
  35. }
  36. else if (Component* const parent = comp->getParentComponent())
  37. {
  38. parent->addChildComponent (this);
  39. }
  40. }
  41. void paint (Graphics& g) override
  42. {
  43. if (Component* c = target)
  44. shadow.drawForRectangle (g, getLocalArea (c, c->getLocalBounds()));
  45. }
  46. void resized() override
  47. {
  48. repaint(); // (needed for correct repainting)
  49. }
  50. float getDesktopScaleFactor() const override
  51. {
  52. if (target != nullptr)
  53. return target->getDesktopScaleFactor();
  54. return Component::getDesktopScaleFactor();
  55. }
  56. private:
  57. WeakReference<Component> target;
  58. DropShadow shadow;
  59. JUCE_DECLARE_NON_COPYABLE (ShadowWindow)
  60. };
  61. class ParentVisibilityChangedListener : public ComponentListener
  62. {
  63. public:
  64. ParentVisibilityChangedListener (Component& r, ComponentListener& l)
  65. : root (&r), listener (&l)
  66. {
  67. if (auto* firstParent = root->getParentComponent())
  68. updateParentHierarchy (firstParent);
  69. }
  70. ~ParentVisibilityChangedListener() override
  71. {
  72. for (auto& compEntry : observedComponents)
  73. if (auto* comp = compEntry.get())
  74. comp->removeComponentListener (this);
  75. }
  76. void componentVisibilityChanged (Component&) override
  77. {
  78. listener->componentVisibilityChanged (*root);
  79. }
  80. void componentParentHierarchyChanged (Component& component) override
  81. {
  82. if (root == &component)
  83. if (auto* firstParent = root->getParentComponent())
  84. updateParentHierarchy (firstParent);
  85. }
  86. private:
  87. class ComponentWithWeakReference
  88. {
  89. public:
  90. explicit ComponentWithWeakReference (Component& c)
  91. : ptr (&c), ref (&c) {}
  92. Component* get() const { return ref.get(); }
  93. bool operator< (const ComponentWithWeakReference& other) const { return ptr < other.ptr; }
  94. private:
  95. Component* ptr;
  96. WeakReference<Component> ref;
  97. };
  98. void updateParentHierarchy (Component* rootComponent)
  99. {
  100. const auto lastSeenComponents = std::exchange (observedComponents, [&]
  101. {
  102. std::set<ComponentWithWeakReference> result;
  103. for (auto node = rootComponent; node != nullptr; node = node->getParentComponent())
  104. result.emplace (*node);
  105. return result;
  106. }());
  107. const auto withDifference = [] (const auto& rangeA, const auto& rangeB, auto&& callback)
  108. {
  109. std::vector<ComponentWithWeakReference> result;
  110. std::set_difference (rangeA.begin(), rangeA.end(), rangeB.begin(), rangeB.end(), std::back_inserter (result));
  111. for (const auto& item : result)
  112. if (auto* c = item.get())
  113. callback (*c);
  114. };
  115. withDifference (lastSeenComponents, observedComponents, [this] (auto& comp) { comp.removeComponentListener (this); });
  116. withDifference (observedComponents, lastSeenComponents, [this] (auto& comp) { comp.addComponentListener (this); });
  117. }
  118. Component* root = nullptr;
  119. ComponentListener* listener = nullptr;
  120. std::set<ComponentWithWeakReference> observedComponents;
  121. JUCE_DECLARE_NON_COPYABLE (ParentVisibilityChangedListener)
  122. JUCE_DECLARE_NON_MOVEABLE (ParentVisibilityChangedListener)
  123. };
  124. //==============================================================================
  125. DropShadower::DropShadower (const DropShadow& ds) : shadow (ds) {}
  126. DropShadower::~DropShadower()
  127. {
  128. if (owner != nullptr)
  129. {
  130. owner->removeComponentListener (this);
  131. owner = nullptr;
  132. }
  133. updateParent();
  134. const ScopedValueSetter<bool> setter (reentrant, true);
  135. shadowWindows.clear();
  136. }
  137. void DropShadower::setOwner (Component* componentToFollow)
  138. {
  139. if (componentToFollow != owner)
  140. {
  141. if (owner != nullptr)
  142. owner->removeComponentListener (this);
  143. // (the component can't be null)
  144. jassert (componentToFollow != nullptr);
  145. owner = componentToFollow;
  146. jassert (owner != nullptr);
  147. updateParent();
  148. owner->addComponentListener (this);
  149. // The visibility of `owner` is transitively affected by the visibility of its parents. Thus we need to trigger the
  150. // componentVisibilityChanged() event in case it changes for any of the parents.
  151. visibilityChangedListener = std::make_unique<ParentVisibilityChangedListener> (*owner,
  152. static_cast<ComponentListener&> (*this));
  153. updateShadows();
  154. }
  155. }
  156. void DropShadower::updateParent()
  157. {
  158. if (Component* p = lastParentComp)
  159. p->removeComponentListener (this);
  160. lastParentComp = owner != nullptr ? owner->getParentComponent() : nullptr;
  161. if (Component* p = lastParentComp)
  162. p->addComponentListener (this);
  163. }
  164. void DropShadower::componentMovedOrResized (Component& c, bool /*wasMoved*/, bool /*wasResized*/)
  165. {
  166. if (owner == &c)
  167. updateShadows();
  168. }
  169. void DropShadower::componentBroughtToFront (Component& c)
  170. {
  171. if (owner == &c)
  172. updateShadows();
  173. }
  174. void DropShadower::componentChildrenChanged (Component&)
  175. {
  176. updateShadows();
  177. }
  178. void DropShadower::componentParentHierarchyChanged (Component& c)
  179. {
  180. if (owner == &c)
  181. {
  182. updateParent();
  183. updateShadows();
  184. }
  185. }
  186. void DropShadower::componentVisibilityChanged (Component& c)
  187. {
  188. if (owner == &c)
  189. updateShadows();
  190. }
  191. void DropShadower::updateShadows()
  192. {
  193. if (reentrant)
  194. return;
  195. const ScopedValueSetter<bool> setter (reentrant, true);
  196. if (owner == nullptr)
  197. {
  198. shadowWindows.clear();
  199. return;
  200. }
  201. if (owner->isShowing()
  202. && owner->getWidth() > 0 && owner->getHeight() > 0
  203. && (Desktop::canUseSemiTransparentWindows() || owner->getParentComponent() != nullptr))
  204. {
  205. while (shadowWindows.size() < 4)
  206. shadowWindows.add (new ShadowWindow (owner, shadow));
  207. const int shadowEdge = jmax (shadow.offset.x, shadow.offset.y) + shadow.radius;
  208. const int x = owner->getX();
  209. const int y = owner->getY() - shadowEdge;
  210. const int w = owner->getWidth();
  211. const int h = owner->getHeight() + shadowEdge + shadowEdge;
  212. for (int i = 4; --i >= 0;)
  213. {
  214. // there seem to be rare situations where the dropshadower may be deleted by
  215. // callbacks during this loop, so use a weak ref to watch out for this..
  216. WeakReference<Component> sw (shadowWindows[i]);
  217. if (sw != nullptr)
  218. {
  219. sw->setAlwaysOnTop (owner->isAlwaysOnTop());
  220. if (sw == nullptr)
  221. return;
  222. switch (i)
  223. {
  224. case 0: sw->setBounds (x - shadowEdge, y, shadowEdge, h); break;
  225. case 1: sw->setBounds (x + w, y, shadowEdge, h); break;
  226. case 2: sw->setBounds (x, y, w, shadowEdge); break;
  227. case 3: sw->setBounds (x, owner->getBottom(), w, shadowEdge); break;
  228. default: break;
  229. }
  230. if (sw == nullptr)
  231. return;
  232. sw->toBehind (i == 3 ? owner.get() : shadowWindows.getUnchecked (i + 1));
  233. }
  234. }
  235. }
  236. else
  237. {
  238. shadowWindows.clear();
  239. }
  240. }
  241. } // namespace juce