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.

400 lines
12KB

  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::VirtualDesktopWatcher final : public ComponentListener,
  71. private Timer
  72. {
  73. public:
  74. //==============================================================================
  75. VirtualDesktopWatcher (Component& c) : component (&c)
  76. {
  77. component->addComponentListener (this);
  78. update();
  79. }
  80. ~VirtualDesktopWatcher() override
  81. {
  82. stopTimer();
  83. if (auto* c = component.get())
  84. c->removeComponentListener (this);
  85. }
  86. bool shouldHideDropShadow() const
  87. {
  88. return hasReasonToHide;
  89. }
  90. void addListener (void* listener, std::function<void()> cb)
  91. {
  92. listeners[listener] = std::move (cb);
  93. }
  94. void removeListener (void* listener)
  95. {
  96. listeners.erase (listener);
  97. }
  98. //==============================================================================
  99. void componentParentHierarchyChanged (Component& c) override
  100. {
  101. if (component.get() == &c)
  102. update();
  103. }
  104. private:
  105. //==============================================================================
  106. void update()
  107. {
  108. bool newHasReasonToHide = false;
  109. if (! component.wasObjectDeleted() && isWindows && component->isOnDesktop())
  110. {
  111. startTimerHz (5);
  112. WeakReference<VirtualDesktopWatcher> weakThis (this);
  113. // During scaling changes this call can trigger a call to HWNDComponentPeer::handleDPIChanging()
  114. // which deletes this VirtualDesktopWatcher.
  115. newHasReasonToHide = ! detail::WindowingHelpers::isWindowOnCurrentVirtualDesktop (component->getWindowHandle());
  116. if (weakThis == nullptr)
  117. return;
  118. }
  119. else
  120. {
  121. stopTimer();
  122. }
  123. if (std::exchange (hasReasonToHide, newHasReasonToHide) != newHasReasonToHide)
  124. for (auto& l : listeners)
  125. l.second();
  126. }
  127. void timerCallback() override
  128. {
  129. update();
  130. }
  131. //==============================================================================
  132. WeakReference<Component> component;
  133. const bool isWindows = (SystemStats::getOperatingSystemType() & SystemStats::Windows) != 0;
  134. bool hasReasonToHide = false;
  135. std::map<void*, std::function<void()>> listeners;
  136. JUCE_DECLARE_WEAK_REFERENCEABLE (VirtualDesktopWatcher)
  137. };
  138. class DropShadower::ParentVisibilityChangedListener : public ComponentListener
  139. {
  140. public:
  141. ParentVisibilityChangedListener (Component& r, ComponentListener& l)
  142. : root (&r), listener (&l)
  143. {
  144. updateParentHierarchy();
  145. }
  146. ~ParentVisibilityChangedListener() override
  147. {
  148. for (auto& compEntry : observedComponents)
  149. if (auto* comp = compEntry.get())
  150. comp->removeComponentListener (this);
  151. }
  152. void componentVisibilityChanged (Component& component) override
  153. {
  154. if (root != &component)
  155. listener->componentVisibilityChanged (*root);
  156. }
  157. void componentParentHierarchyChanged (Component& component) override
  158. {
  159. if (root == &component)
  160. updateParentHierarchy();
  161. }
  162. private:
  163. class ComponentWithWeakReference
  164. {
  165. public:
  166. explicit ComponentWithWeakReference (Component& c)
  167. : ptr (&c), ref (&c) {}
  168. Component* get() const { return ref.get(); }
  169. bool operator< (const ComponentWithWeakReference& other) const { return ptr < other.ptr; }
  170. private:
  171. Component* ptr;
  172. WeakReference<Component> ref;
  173. };
  174. void updateParentHierarchy()
  175. {
  176. const auto lastSeenComponents = std::exchange (observedComponents, [&]
  177. {
  178. std::set<ComponentWithWeakReference> result;
  179. for (auto node = root; node != nullptr; node = node->getParentComponent())
  180. result.emplace (*node);
  181. return result;
  182. }());
  183. const auto withDifference = [] (const auto& rangeA, const auto& rangeB, auto&& callback)
  184. {
  185. std::vector<ComponentWithWeakReference> result;
  186. std::set_difference (rangeA.begin(), rangeA.end(), rangeB.begin(), rangeB.end(), std::back_inserter (result));
  187. for (const auto& item : result)
  188. if (auto* c = item.get())
  189. callback (*c);
  190. };
  191. withDifference (lastSeenComponents, observedComponents, [this] (auto& comp) { comp.removeComponentListener (this); });
  192. withDifference (observedComponents, lastSeenComponents, [this] (auto& comp) { comp.addComponentListener (this); });
  193. }
  194. Component* root = nullptr;
  195. ComponentListener* listener = nullptr;
  196. std::set<ComponentWithWeakReference> observedComponents;
  197. JUCE_DECLARE_NON_COPYABLE (ParentVisibilityChangedListener)
  198. JUCE_DECLARE_NON_MOVEABLE (ParentVisibilityChangedListener)
  199. };
  200. //==============================================================================
  201. DropShadower::DropShadower (const DropShadow& ds) : shadow (ds) {}
  202. DropShadower::~DropShadower()
  203. {
  204. if (virtualDesktopWatcher != nullptr)
  205. virtualDesktopWatcher->removeListener (this);
  206. if (owner != nullptr)
  207. {
  208. owner->removeComponentListener (this);
  209. owner = nullptr;
  210. }
  211. updateParent();
  212. const ScopedValueSetter<bool> setter (reentrant, true);
  213. shadowWindows.clear();
  214. }
  215. void DropShadower::setOwner (Component* componentToFollow)
  216. {
  217. if (componentToFollow != owner)
  218. {
  219. if (owner != nullptr)
  220. owner->removeComponentListener (this);
  221. // (the component can't be null)
  222. jassert (componentToFollow != nullptr);
  223. owner = componentToFollow;
  224. jassert (owner != nullptr);
  225. updateParent();
  226. owner->addComponentListener (this);
  227. // The visibility of `owner` is transitively affected by the visibility of its parents. Thus we need to trigger the
  228. // componentVisibilityChanged() event in case it changes for any of the parents.
  229. visibilityChangedListener = std::make_unique<ParentVisibilityChangedListener> (*owner,
  230. static_cast<ComponentListener&> (*this));
  231. virtualDesktopWatcher = std::make_unique<VirtualDesktopWatcher> (*owner);
  232. virtualDesktopWatcher->addListener (this, [this]() { updateShadows(); });
  233. updateShadows();
  234. }
  235. }
  236. void DropShadower::updateParent()
  237. {
  238. if (Component* p = lastParentComp)
  239. p->removeComponentListener (this);
  240. lastParentComp = owner != nullptr ? owner->getParentComponent() : nullptr;
  241. if (Component* p = lastParentComp)
  242. p->addComponentListener (this);
  243. }
  244. void DropShadower::componentMovedOrResized (Component& c, bool /*wasMoved*/, bool /*wasResized*/)
  245. {
  246. if (owner == &c)
  247. updateShadows();
  248. }
  249. void DropShadower::componentBroughtToFront (Component& c)
  250. {
  251. if (owner == &c)
  252. updateShadows();
  253. }
  254. void DropShadower::componentChildrenChanged (Component&)
  255. {
  256. updateShadows();
  257. }
  258. void DropShadower::componentParentHierarchyChanged (Component& c)
  259. {
  260. if (owner == &c)
  261. {
  262. updateParent();
  263. updateShadows();
  264. }
  265. }
  266. void DropShadower::componentVisibilityChanged (Component& c)
  267. {
  268. if (owner == &c)
  269. updateShadows();
  270. }
  271. void DropShadower::updateShadows()
  272. {
  273. if (reentrant)
  274. return;
  275. const ScopedValueSetter<bool> setter (reentrant, true);
  276. if (owner != nullptr
  277. && owner->isShowing()
  278. && owner->getWidth() > 0 && owner->getHeight() > 0
  279. && (Desktop::canUseSemiTransparentWindows() || owner->getParentComponent() != nullptr)
  280. && (virtualDesktopWatcher == nullptr || ! virtualDesktopWatcher->shouldHideDropShadow()))
  281. {
  282. while (shadowWindows.size() < 4)
  283. shadowWindows.add (new ShadowWindow (owner, shadow));
  284. const int shadowEdge = jmax (shadow.offset.x, shadow.offset.y) + shadow.radius;
  285. const int x = owner->getX();
  286. const int y = owner->getY() - shadowEdge;
  287. const int w = owner->getWidth();
  288. const int h = owner->getHeight() + shadowEdge + shadowEdge;
  289. for (int i = 4; --i >= 0;)
  290. {
  291. // there seem to be rare situations where the dropshadower may be deleted by
  292. // callbacks during this loop, so use a weak ref to watch out for this..
  293. WeakReference<Component> sw (shadowWindows[i]);
  294. if (sw != nullptr)
  295. {
  296. sw->setAlwaysOnTop (owner->isAlwaysOnTop());
  297. if (sw == nullptr)
  298. return;
  299. switch (i)
  300. {
  301. case 0: sw->setBounds (x - shadowEdge, y, shadowEdge, h); break;
  302. case 1: sw->setBounds (x + w, y, shadowEdge, h); break;
  303. case 2: sw->setBounds (x, y, w, shadowEdge); break;
  304. case 3: sw->setBounds (x, owner->getBottom(), w, shadowEdge); break;
  305. default: break;
  306. }
  307. if (sw == nullptr)
  308. return;
  309. sw->toBehind (i == 3 ? owner.get() : shadowWindows.getUnchecked (i + 1));
  310. }
  311. }
  312. }
  313. else
  314. {
  315. shadowWindows.clear();
  316. }
  317. }
  318. } // namespace juce