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.

163 lines
4.3KB

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