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.

133 lines
3.6KB

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