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.

337 lines
11KB

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