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.

325 lines
10KB

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