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.

142 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. ComponentMovementWatcher::ComponentMovementWatcher (Component* const comp)
  20. : component (comp),
  21. lastPeerID (0),
  22. reentrant (false),
  23. wasShowing (comp->isShowing())
  24. {
  25. jassert (component != nullptr); // can't use this with a null pointer..
  26. component->addComponentListener (this);
  27. registerWithParentComps();
  28. }
  29. ComponentMovementWatcher::~ComponentMovementWatcher()
  30. {
  31. if (component != nullptr)
  32. component->removeComponentListener (this);
  33. unregister();
  34. }
  35. //==============================================================================
  36. void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
  37. {
  38. if (component != nullptr && ! reentrant)
  39. {
  40. const ScopedValueSetter<bool> setter (reentrant, true);
  41. ComponentPeer* const peer = component->getPeer();
  42. const uint32 peerID = peer != nullptr ? peer->getUniqueID() : 0;
  43. if (peerID != lastPeerID)
  44. {
  45. componentPeerChanged();
  46. if (component == nullptr)
  47. return;
  48. lastPeerID = peerID;
  49. }
  50. unregister();
  51. registerWithParentComps();
  52. componentMovedOrResized (*component, true, true);
  53. if (component != nullptr)
  54. componentVisibilityChanged (*component);
  55. }
  56. }
  57. void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
  58. {
  59. if (component != nullptr)
  60. {
  61. if (wasMoved)
  62. {
  63. Point<int> newPos;
  64. Component* const top = component->getTopLevelComponent();
  65. if (top != component)
  66. newPos = top->getLocalPoint (component, Point<int>());
  67. else
  68. newPos = top->getPosition();
  69. wasMoved = lastBounds.getPosition() != newPos;
  70. lastBounds.setPosition (newPos);
  71. }
  72. wasResized = (lastBounds.getWidth() != component->getWidth() || lastBounds.getHeight() != component->getHeight());
  73. lastBounds.setSize (component->getWidth(), component->getHeight());
  74. if (wasMoved || wasResized)
  75. componentMovedOrResized (wasMoved, wasResized);
  76. }
  77. }
  78. void ComponentMovementWatcher::componentBeingDeleted (Component& comp)
  79. {
  80. registeredParentComps.removeFirstMatchingValue (&comp);
  81. if (component == &comp)
  82. unregister();
  83. }
  84. void ComponentMovementWatcher::componentVisibilityChanged (Component&)
  85. {
  86. if (component != nullptr)
  87. {
  88. const bool isShowingNow = component->isShowing();
  89. if (wasShowing != isShowingNow)
  90. {
  91. wasShowing = isShowingNow;
  92. componentVisibilityChanged();
  93. }
  94. }
  95. }
  96. void ComponentMovementWatcher::registerWithParentComps()
  97. {
  98. for (Component* p = component->getParentComponent(); p != nullptr; p = p->getParentComponent())
  99. {
  100. p->addComponentListener (this);
  101. registeredParentComps.add (p);
  102. }
  103. }
  104. void ComponentMovementWatcher::unregister()
  105. {
  106. for (int i = registeredParentComps.size(); --i >= 0;)
  107. registeredParentComps.getUnchecked(i)->removeComponentListener (this);
  108. registeredParentComps.clear();
  109. }