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.

juce_ComponentMovementWatcher.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. namespace juce
  20. {
  21. ComponentMovementWatcher::ComponentMovementWatcher (Component* const comp)
  22. : component (comp),
  23. lastPeerID (0),
  24. reentrant (false),
  25. wasShowing (comp->isShowing())
  26. {
  27. jassert (component != nullptr); // can't use this with a null pointer..
  28. component->addComponentListener (this);
  29. registerWithParentComps();
  30. }
  31. ComponentMovementWatcher::~ComponentMovementWatcher()
  32. {
  33. if (component != nullptr)
  34. component->removeComponentListener (this);
  35. unregister();
  36. }
  37. //==============================================================================
  38. void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
  39. {
  40. if (component != nullptr && ! reentrant)
  41. {
  42. const ScopedValueSetter<bool> setter (reentrant, true);
  43. ComponentPeer* const peer = component->getPeer();
  44. const uint32 peerID = peer != nullptr ? peer->getUniqueID() : 0;
  45. if (peerID != lastPeerID)
  46. {
  47. componentPeerChanged();
  48. if (component == nullptr)
  49. return;
  50. lastPeerID = peerID;
  51. }
  52. unregister();
  53. registerWithParentComps();
  54. componentMovedOrResized (*component, true, true);
  55. if (component != nullptr)
  56. componentVisibilityChanged (*component);
  57. }
  58. }
  59. void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
  60. {
  61. if (component != nullptr)
  62. {
  63. if (wasMoved)
  64. {
  65. Point<int> newPos;
  66. Component* const top = component->getTopLevelComponent();
  67. if (top != component)
  68. newPos = top->getLocalPoint (component, Point<int>());
  69. else
  70. newPos = top->getPosition();
  71. wasMoved = lastBounds.getPosition() != newPos;
  72. lastBounds.setPosition (newPos);
  73. }
  74. wasResized = (lastBounds.getWidth() != component->getWidth() || lastBounds.getHeight() != component->getHeight());
  75. lastBounds.setSize (component->getWidth(), component->getHeight());
  76. if (wasMoved || wasResized)
  77. componentMovedOrResized (wasMoved, wasResized);
  78. }
  79. }
  80. void ComponentMovementWatcher::componentBeingDeleted (Component& comp)
  81. {
  82. registeredParentComps.removeFirstMatchingValue (&comp);
  83. if (component == &comp)
  84. unregister();
  85. }
  86. void ComponentMovementWatcher::componentVisibilityChanged (Component&)
  87. {
  88. if (component != nullptr)
  89. {
  90. const bool isShowingNow = component->isShowing();
  91. if (wasShowing != isShowingNow)
  92. {
  93. wasShowing = isShowingNow;
  94. componentVisibilityChanged();
  95. }
  96. }
  97. }
  98. void ComponentMovementWatcher::registerWithParentComps()
  99. {
  100. for (Component* p = component->getParentComponent(); p != nullptr; p = p->getParentComponent())
  101. {
  102. p->addComponentListener (this);
  103. registeredParentComps.add (p);
  104. }
  105. }
  106. void ComponentMovementWatcher::unregister()
  107. {
  108. for (int i = registeredParentComps.size(); --i >= 0;)
  109. registeredParentComps.getUnchecked(i)->removeComponentListener (this);
  110. registeredParentComps.clear();
  111. }
  112. } // namespace juce