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.

126 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. class UIViewComponent::Pimpl : public ComponentMovementWatcher
  16. {
  17. public:
  18. Pimpl (UIView* v, Component& comp)
  19. : ComponentMovementWatcher (&comp),
  20. view (v),
  21. owner (comp)
  22. {
  23. [view retain];
  24. if (owner.isShowing())
  25. componentPeerChanged();
  26. }
  27. ~Pimpl() override
  28. {
  29. [view removeFromSuperview];
  30. [view release];
  31. }
  32. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  33. {
  34. auto* topComp = owner.getTopLevelComponent();
  35. if (topComp->getPeer() != nullptr)
  36. {
  37. auto pos = topComp->getLocalPoint (&owner, Point<int>());
  38. [view setFrame: CGRectMake ((float) pos.x, (float) pos.y,
  39. (float) owner.getWidth(), (float) owner.getHeight())];
  40. }
  41. }
  42. void componentPeerChanged() override
  43. {
  44. auto* peer = owner.getPeer();
  45. if (currentPeer != peer)
  46. {
  47. if ([view superview] != nil)
  48. [view removeFromSuperview]; // Must be careful not to call this unless it's required - e.g. some Apple AU views
  49. // override the call and use it as a sign that they're being deleted, which breaks everything..
  50. currentPeer = peer;
  51. if (peer != nullptr)
  52. {
  53. UIView* peerView = (UIView*) peer->getNativeHandle();
  54. [peerView addSubview: view];
  55. componentMovedOrResized (false, false);
  56. }
  57. }
  58. [view setHidden: ! owner.isShowing()];
  59. }
  60. void componentVisibilityChanged() override
  61. {
  62. componentPeerChanged();
  63. }
  64. Rectangle<int> getViewBounds() const
  65. {
  66. CGRect r = [view frame];
  67. return Rectangle<int> ((int) r.size.width, (int) r.size.height);
  68. }
  69. UIView* const view;
  70. private:
  71. Component& owner;
  72. ComponentPeer* currentPeer = nullptr;
  73. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  74. };
  75. //==============================================================================
  76. UIViewComponent::UIViewComponent() {}
  77. UIViewComponent::~UIViewComponent() {}
  78. void UIViewComponent::setView (void* view)
  79. {
  80. if (view != getView())
  81. {
  82. pimpl.reset();
  83. if (view != nullptr)
  84. pimpl.reset (new Pimpl ((UIView*) view, *this));
  85. }
  86. }
  87. void* UIViewComponent::getView() const
  88. {
  89. return pimpl == nullptr ? nullptr : pimpl->view;
  90. }
  91. void UIViewComponent::resizeToFitView()
  92. {
  93. if (pimpl != nullptr)
  94. setBounds (pimpl->getViewBounds());
  95. }
  96. void UIViewComponent::paint (Graphics&) {}
  97. } // namespace juce