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.

319 lines
9.9KB

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