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.

136 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  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. ComponentMovementWatcher::ComponentMovementWatcher (Component* const comp)
  16. : component (comp),
  17. wasShowing (comp->isShowing())
  18. {
  19. jassert (component != nullptr); // can't use this with a null pointer..
  20. component->addComponentListener (this);
  21. registerWithParentComps();
  22. }
  23. ComponentMovementWatcher::~ComponentMovementWatcher()
  24. {
  25. if (component != nullptr)
  26. component->removeComponentListener (this);
  27. unregister();
  28. }
  29. //==============================================================================
  30. void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
  31. {
  32. if (component != nullptr && ! reentrant)
  33. {
  34. const ScopedValueSetter<bool> setter (reentrant, true);
  35. auto* peer = component->getPeer();
  36. auto peerID = peer != nullptr ? peer->getUniqueID() : 0;
  37. if (peerID != lastPeerID)
  38. {
  39. componentPeerChanged();
  40. if (component == nullptr)
  41. return;
  42. lastPeerID = peerID;
  43. }
  44. unregister();
  45. registerWithParentComps();
  46. componentMovedOrResized (*component, true, true);
  47. if (component != nullptr)
  48. componentVisibilityChanged (*component);
  49. }
  50. }
  51. void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
  52. {
  53. if (component != nullptr)
  54. {
  55. if (wasMoved)
  56. {
  57. Point<int> newPos;
  58. auto* top = component->getTopLevelComponent();
  59. if (top != component)
  60. newPos = top->getLocalPoint (component, Point<int>());
  61. else
  62. newPos = top->getPosition();
  63. wasMoved = lastBounds.getPosition() != newPos;
  64. lastBounds.setPosition (newPos);
  65. }
  66. wasResized = (lastBounds.getWidth() != component->getWidth() || lastBounds.getHeight() != component->getHeight());
  67. lastBounds.setSize (component->getWidth(), component->getHeight());
  68. if (wasMoved || wasResized)
  69. componentMovedOrResized (wasMoved, wasResized);
  70. }
  71. }
  72. void ComponentMovementWatcher::componentBeingDeleted (Component& comp)
  73. {
  74. registeredParentComps.removeFirstMatchingValue (&comp);
  75. if (component == &comp)
  76. unregister();
  77. }
  78. void ComponentMovementWatcher::componentVisibilityChanged (Component&)
  79. {
  80. if (component != nullptr)
  81. {
  82. const bool isShowingNow = component->isShowing();
  83. if (wasShowing != isShowingNow)
  84. {
  85. wasShowing = isShowingNow;
  86. componentVisibilityChanged();
  87. }
  88. }
  89. }
  90. void ComponentMovementWatcher::registerWithParentComps()
  91. {
  92. for (auto* p = component->getParentComponent(); p != nullptr; p = p->getParentComponent())
  93. {
  94. p->addComponentListener (this);
  95. registeredParentComps.add (p);
  96. }
  97. }
  98. void ComponentMovementWatcher::unregister()
  99. {
  100. for (auto* c : registeredParentComps)
  101. c->removeComponentListener (this);
  102. registeredParentComps.clear();
  103. }
  104. } // namespace juce