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.

309 lines
9.3KB

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