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.

juce_AndroidViewComponent.cpp 4.7KB

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