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 3.9KB

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