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.

170 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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 AndroidViewComponent::Pimpl : public ComponentMovementWatcher
  16. {
  17. public:
  18. Pimpl (const LocalRef<jobject>& v, Component& comp)
  19. : ComponentMovementWatcher (&comp),
  20. view (v),
  21. owner (comp)
  22. {
  23. if (owner.isShowing())
  24. componentPeerChanged();
  25. }
  26. ~Pimpl() override
  27. {
  28. removeFromParent();
  29. }
  30. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  31. {
  32. auto* topComp = owner.getTopLevelComponent();
  33. if (topComp->getPeer() != nullptr)
  34. {
  35. auto pos = topComp->getLocalPoint (&owner, Point<int>());
  36. Rectangle<int> r (pos.x, pos.y, owner.getWidth(), owner.getHeight());
  37. r *= Desktop::getInstance().getDisplays().getPrimaryDisplay()->scale;
  38. getEnv()->CallVoidMethod (view, AndroidView.layout, r.getX(), r.getY(),
  39. r.getRight(), r.getBottom());
  40. }
  41. }
  42. void componentPeerChanged() override
  43. {
  44. auto* peer = owner.getPeer();
  45. if (currentPeer != peer)
  46. {
  47. removeFromParent();
  48. currentPeer = peer;
  49. addToParent();
  50. }
  51. enum
  52. {
  53. VISIBLE = 0,
  54. INVISIBLE = 4
  55. };
  56. getEnv()->CallVoidMethod (view, AndroidView.setVisibility, owner.isShowing() ? VISIBLE : INVISIBLE);
  57. }
  58. void componentVisibilityChanged() override
  59. {
  60. componentPeerChanged();
  61. }
  62. void componentBroughtToFront (Component& comp) override
  63. {
  64. ComponentMovementWatcher::componentBroughtToFront (comp);
  65. }
  66. Rectangle<int> getViewBounds() const
  67. {
  68. auto* env = getEnv();
  69. int width = env->CallIntMethod (view, AndroidView.getWidth);
  70. int height = env->CallIntMethod (view, AndroidView.getHeight);
  71. return Rectangle<int> (width, height);
  72. }
  73. GlobalRef view;
  74. private:
  75. void addToParent()
  76. {
  77. if (currentPeer != nullptr)
  78. {
  79. jobject peerView = (jobject) currentPeer->getNativeHandle();
  80. // NB: Assuming a parent is always of ViewGroup type
  81. auto* env = getEnv();
  82. env->CallVoidMethod (peerView, AndroidViewGroup.addView, view.get());
  83. componentMovedOrResized (false, false);
  84. }
  85. }
  86. void removeFromParent()
  87. {
  88. auto* env = getEnv();
  89. auto parentView = env->CallObjectMethod (view, AndroidView.getParent);
  90. if (parentView != nullptr)
  91. {
  92. // Assuming a parent is always of ViewGroup type
  93. env->CallVoidMethod (parentView, AndroidViewGroup.removeView, view.get());
  94. }
  95. }
  96. Component& owner;
  97. ComponentPeer* currentPeer = nullptr;
  98. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  99. };
  100. //==============================================================================
  101. AndroidViewComponent::AndroidViewComponent()
  102. {
  103. }
  104. AndroidViewComponent::~AndroidViewComponent() {}
  105. void AndroidViewComponent::setView (void* view)
  106. {
  107. if (view != getView())
  108. {
  109. pimpl.reset();
  110. if (view != nullptr)
  111. {
  112. // explicitly create a new local ref here so that we don't
  113. // delete the users pointer
  114. auto* env = getEnv();
  115. auto localref = LocalRef<jobject>(env->NewLocalRef((jobject) view));
  116. pimpl.reset (new Pimpl (localref, *this));
  117. }
  118. }
  119. }
  120. void* AndroidViewComponent::getView() const
  121. {
  122. return pimpl == nullptr ? nullptr : (void*) pimpl->view;
  123. }
  124. void AndroidViewComponent::resizeToFitView()
  125. {
  126. if (pimpl != nullptr)
  127. setBounds (pimpl->getViewBounds());
  128. }
  129. void AndroidViewComponent::paint (Graphics&) {}
  130. } // namespace juce