The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

178 lines
4.7KB

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