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.

114 lines
2.7KB

  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. VBlankAttachment::VBlankAttachment (Component* c, std::function<void()> callbackIn)
  21. : owner (c),
  22. callback (std::move (callbackIn))
  23. {
  24. jassert (owner != nullptr && callback);
  25. updateOwner();
  26. updatePeer();
  27. }
  28. VBlankAttachment::VBlankAttachment (VBlankAttachment&& other)
  29. : VBlankAttachment (other.owner, std::move (other.callback))
  30. {
  31. other.cleanup();
  32. }
  33. VBlankAttachment& VBlankAttachment::operator= (VBlankAttachment&& other)
  34. {
  35. cleanup();
  36. owner = other.owner;
  37. callback = std::move (other.callback);
  38. updateOwner();
  39. updatePeer();
  40. other.cleanup();
  41. return *this;
  42. }
  43. VBlankAttachment::~VBlankAttachment()
  44. {
  45. cleanup();
  46. }
  47. void VBlankAttachment::onVBlank()
  48. {
  49. callback();
  50. }
  51. void VBlankAttachment::componentParentHierarchyChanged (Component&)
  52. {
  53. updatePeer();
  54. }
  55. void VBlankAttachment::updateOwner()
  56. {
  57. if (auto previousLastOwner = std::exchange (lastOwner, owner); previousLastOwner != owner)
  58. {
  59. if (previousLastOwner != nullptr)
  60. previousLastOwner->removeComponentListener (this);
  61. if (owner != nullptr)
  62. owner->addComponentListener (this);
  63. }
  64. }
  65. void VBlankAttachment::updatePeer()
  66. {
  67. if (owner != nullptr)
  68. {
  69. if (auto* peer = owner->getPeer())
  70. {
  71. peer->addVBlankListener (this);
  72. if (lastPeer != peer && ComponentPeer::isValidPeer (lastPeer))
  73. lastPeer->removeVBlankListener (this);
  74. lastPeer = peer;
  75. }
  76. }
  77. else if (auto peer = std::exchange (lastPeer, nullptr); ComponentPeer::isValidPeer (peer))
  78. {
  79. peer->removeVBlankListener (this);
  80. }
  81. }
  82. void VBlankAttachment::cleanup()
  83. {
  84. owner = nullptr;
  85. updateOwner();
  86. updatePeer();
  87. }
  88. } // namespace juce