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.

166 lines
4.4KB

  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 (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. Rectangle<int> getViewBounds() const
  69. {
  70. auto* env = getEnv();
  71. int width = env->CallIntMethod (view, AndroidView.getWidth);
  72. int height = env->CallIntMethod (view, AndroidView.getHeight);
  73. return Rectangle<int> (width, height);
  74. }
  75. GlobalRef view;
  76. private:
  77. void addToParent()
  78. {
  79. if (currentPeer != nullptr)
  80. {
  81. jobject peerView = (jobject) currentPeer->getNativeHandle();
  82. auto* env = getEnv();
  83. auto parentView = env->CallObjectMethod (peerView, AndroidView.getParent);
  84. // Assuming a parent is always of ViewGroup type
  85. env->CallVoidMethod (parentView, AndroidViewGroup.addView, view.get());
  86. componentMovedOrResized (false, false);
  87. }
  88. }
  89. void removeFromParent()
  90. {
  91. auto* env = getEnv();
  92. auto parentView = env->CallObjectMethod (view, AndroidView.getParent);
  93. if (parentView != 0)
  94. {
  95. // Assuming a parent is always of ViewGroup type
  96. env->CallVoidMethod (parentView, AndroidViewGroup.removeView, view.get());
  97. }
  98. }
  99. Component& owner;
  100. ComponentPeer* currentPeer = nullptr;
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  102. };
  103. //==============================================================================
  104. AndroidViewComponent::AndroidViewComponent() {}
  105. AndroidViewComponent::~AndroidViewComponent() {}
  106. void AndroidViewComponent::setView (void* const view)
  107. {
  108. if (view != getView())
  109. {
  110. pimpl = nullptr;
  111. if (view != nullptr)
  112. pimpl = new Pimpl ((jobject) view, *this);
  113. }
  114. }
  115. void* AndroidViewComponent::getView() const
  116. {
  117. return pimpl == nullptr ? nullptr : (void*) pimpl->view;
  118. }
  119. void AndroidViewComponent::resizeToFitView()
  120. {
  121. if (pimpl != nullptr)
  122. setBounds (pimpl->getViewBounds());
  123. }
  124. void AndroidViewComponent::paint (Graphics&) {}
  125. } // namespace juce