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 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. class DropShadower::ShadowWindow : public Component
  16. {
  17. public:
  18. ShadowWindow (Component* comp, const DropShadow& ds)
  19. : target (comp), shadow (ds)
  20. {
  21. setVisible (true);
  22. setAccessible (false);
  23. setInterceptsMouseClicks (false, false);
  24. if (comp->isOnDesktop())
  25. {
  26. setSize (1, 1); // to keep the OS happy by not having zero-size windows
  27. addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  28. | ComponentPeer::windowIsTemporary
  29. | ComponentPeer::windowIgnoresKeyPresses);
  30. }
  31. else if (Component* const parent = comp->getParentComponent())
  32. {
  33. parent->addChildComponent (this);
  34. }
  35. }
  36. void paint (Graphics& g) override
  37. {
  38. if (Component* c = target)
  39. shadow.drawForRectangle (g, getLocalArea (c, c->getLocalBounds()));
  40. }
  41. void resized() override
  42. {
  43. repaint(); // (needed for correct repainting)
  44. }
  45. float getDesktopScaleFactor() const override
  46. {
  47. if (target != nullptr)
  48. return target->getDesktopScaleFactor();
  49. return Component::getDesktopScaleFactor();
  50. }
  51. private:
  52. WeakReference<Component> target;
  53. DropShadow shadow;
  54. JUCE_DECLARE_NON_COPYABLE (ShadowWindow)
  55. };
  56. class DropShadower::ParentVisibilityChangedListener : public ComponentListener,
  57. private Timer
  58. {
  59. public:
  60. ParentVisibilityChangedListener (Component& r, ComponentListener& l)
  61. : root (&r), listener (&l)
  62. {
  63. if (auto* firstParent = root->getParentComponent())
  64. updateParentHierarchy (firstParent);
  65. if ((SystemStats::getOperatingSystemType() & SystemStats::Windows) != 0)
  66. {
  67. isOnVirtualDesktop = isWindowOnCurrentVirtualDesktop (root->getWindowHandle());
  68. startTimerHz (5);
  69. }
  70. }
  71. ~ParentVisibilityChangedListener() override
  72. {
  73. for (auto& compEntry : observedComponents)
  74. if (auto* comp = compEntry.get())
  75. comp->removeComponentListener (this);
  76. }
  77. void componentVisibilityChanged (Component&) override
  78. {
  79. listener->componentVisibilityChanged (*root);
  80. }
  81. void componentParentHierarchyChanged (Component& component) override
  82. {
  83. if (root == &component)
  84. if (auto* firstParent = root->getParentComponent())
  85. updateParentHierarchy (firstParent);
  86. }
  87. bool isWindowOnVirtualDesktop() const noexcept { return isOnVirtualDesktop; }
  88. private:
  89. class ComponentWithWeakReference
  90. {
  91. public:
  92. explicit ComponentWithWeakReference (Component& c)
  93. : ptr (&c), ref (&c) {}
  94. Component* get() const { return ref.get(); }
  95. bool operator< (const ComponentWithWeakReference& other) const { return ptr < other.ptr; }
  96. private:
  97. Component* ptr;
  98. WeakReference<Component> ref;
  99. };
  100. void updateParentHierarchy (Component* rootComponent)
  101. {
  102. const auto lastSeenComponents = std::exchange (observedComponents, [&]
  103. {
  104. std::set<ComponentWithWeakReference> result;
  105. for (auto node = rootComponent; node != nullptr; node = node->getParentComponent())
  106. result.emplace (*node);
  107. return result;
  108. }());
  109. const auto withDifference = [] (const auto& rangeA, const auto& rangeB, auto&& callback)
  110. {
  111. std::vector<ComponentWithWeakReference> result;
  112. std::set_difference (rangeA.begin(), rangeA.end(), rangeB.begin(), rangeB.end(), std::back_inserter (result));
  113. for (const auto& item : result)
  114. if (auto* c = item.get())
  115. callback (*c);
  116. };
  117. withDifference (lastSeenComponents, observedComponents, [this] (auto& comp) { comp.removeComponentListener (this); });
  118. withDifference (observedComponents, lastSeenComponents, [this] (auto& comp) { comp.addComponentListener (this); });
  119. }
  120. void timerCallback() override
  121. {
  122. WeakReference<DropShadower> deletionChecker { static_cast<DropShadower*> (listener) };
  123. const auto wasOnVirtualDesktop = std::exchange (isOnVirtualDesktop,
  124. isWindowOnCurrentVirtualDesktop (root->getWindowHandle()));
  125. // on Windows, isWindowOnCurrentVirtualDesktop() may cause synchronous messages to be dispatched
  126. // to the HWND so we need to check if the shadower is still valid after calling
  127. if (deletionChecker == nullptr)
  128. return;
  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