Audio plugin host https://kx.studio/carla
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.

330 lines
10KB

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