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.

134 lines
3.6KB

  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. class UIViewComponent::Pimpl : public ComponentMovementWatcher
  22. {
  23. public:
  24. Pimpl (UIView* v, Component& comp)
  25. : ComponentMovementWatcher (&comp),
  26. view (v),
  27. owner (comp)
  28. {
  29. [view retain];
  30. if (owner.isShowing())
  31. componentPeerChanged();
  32. }
  33. ~Pimpl()
  34. {
  35. [view removeFromSuperview];
  36. [view release];
  37. }
  38. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  39. {
  40. auto* topComp = owner.getTopLevelComponent();
  41. if (topComp->getPeer() != nullptr)
  42. {
  43. auto pos = topComp->getLocalPoint (&owner, Point<int>());
  44. [view setFrame: CGRectMake ((float) pos.x, (float) pos.y,
  45. (float) owner.getWidth(), (float) owner.getHeight())];
  46. }
  47. }
  48. void componentPeerChanged() override
  49. {
  50. auto* peer = owner.getPeer();
  51. if (currentPeer != peer)
  52. {
  53. if ([view superview] != nil)
  54. [view removeFromSuperview]; // Must be careful not to call this unless it's required - e.g. some Apple AU views
  55. // override the call and use it as a sign that they're being deleted, which breaks everything..
  56. currentPeer = peer;
  57. if (peer != nullptr)
  58. {
  59. UIView* peerView = (UIView*) peer->getNativeHandle();
  60. [peerView addSubview: view];
  61. componentMovedOrResized (false, false);
  62. }
  63. }
  64. [view setHidden: ! owner.isShowing()];
  65. }
  66. void componentVisibilityChanged() override
  67. {
  68. componentPeerChanged();
  69. }
  70. Rectangle<int> getViewBounds() const
  71. {
  72. CGRect r = [view frame];
  73. return Rectangle<int> ((int) r.size.width, (int) r.size.height);
  74. }
  75. UIView* const view;
  76. private:
  77. Component& owner;
  78. ComponentPeer* currentPeer = nullptr;
  79. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  80. };
  81. //==============================================================================
  82. UIViewComponent::UIViewComponent() {}
  83. UIViewComponent::~UIViewComponent() {}
  84. void UIViewComponent::setView (void* const view)
  85. {
  86. if (view != getView())
  87. {
  88. pimpl = nullptr;
  89. if (view != nullptr)
  90. pimpl = new Pimpl ((UIView*) view, *this);
  91. }
  92. }
  93. void* UIViewComponent::getView() const
  94. {
  95. return pimpl == nullptr ? nullptr : pimpl->view;
  96. }
  97. void UIViewComponent::resizeToFitView()
  98. {
  99. if (pimpl != nullptr)
  100. setBounds (pimpl->getViewBounds());
  101. }
  102. void UIViewComponent::paint (Graphics&) {}
  103. } // namespace juce