|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- /*
- ==============================================================================
-
- This file is part of the JUCE 6 technical preview.
- Copyright (c) 2020 - Raw Material Software Limited
-
- You may use this code under the terms of the GPL v3
- (see www.gnu.org/licenses).
-
- For this technical preview, this file is not subject to commercial licensing.
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- class AndroidViewComponent::Pimpl : public ComponentMovementWatcher
- {
- public:
- Pimpl (const LocalRef<jobject>& v, Component& comp)
- : ComponentMovementWatcher (&comp),
- view (v),
- owner (comp)
- {
- if (owner.isShowing())
- componentPeerChanged();
- }
-
- ~Pimpl() override
- {
- removeFromParent();
- }
-
- void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
- {
- auto* topComp = owner.getTopLevelComponent();
-
- if (topComp->getPeer() != nullptr)
- {
- auto pos = topComp->getLocalPoint (&owner, Point<int>());
-
- Rectangle<int> r (pos.x, pos.y, owner.getWidth(), owner.getHeight());
- r *= Desktop::getInstance().getDisplays().getMainDisplay().scale;
-
- getEnv()->CallVoidMethod (view, AndroidView.layout, r.getX(), r.getY(),
- r.getRight(), r.getBottom());
- }
- }
-
- void componentPeerChanged() override
- {
- auto* peer = owner.getPeer();
-
- if (currentPeer != peer)
- {
- removeFromParent();
- currentPeer = peer;
-
- addToParent();
- }
-
- enum
- {
- VISIBLE = 0,
- INVISIBLE = 4
- };
-
- getEnv()->CallVoidMethod (view, AndroidView.setVisibility, owner.isShowing() ? VISIBLE : INVISIBLE);
- }
-
- void componentVisibilityChanged() override
- {
- componentPeerChanged();
- }
-
- void componentBroughtToFront (Component& comp) override
- {
- ComponentMovementWatcher::componentBroughtToFront (comp);
- }
-
- Rectangle<int> getViewBounds() const
- {
- auto* env = getEnv();
-
- int width = env->CallIntMethod (view, AndroidView.getWidth);
- int height = env->CallIntMethod (view, AndroidView.getHeight);
-
- return Rectangle<int> (width, height);
- }
-
- GlobalRef view;
-
- private:
- void addToParent()
- {
- if (currentPeer != nullptr)
- {
- jobject peerView = (jobject) currentPeer->getNativeHandle();
-
- // NB: Assuming a parent is always of ViewGroup type
- auto* env = getEnv();
-
- env->CallVoidMethod (peerView, AndroidViewGroup.addView, view.get());
- componentMovedOrResized (false, false);
- }
- }
-
- void removeFromParent()
- {
- auto* env = getEnv();
- auto parentView = env->CallObjectMethod (view, AndroidView.getParent);
-
- if (parentView != nullptr)
- {
- // Assuming a parent is always of ViewGroup type
- env->CallVoidMethod (parentView, AndroidViewGroup.removeView, view.get());
- }
- }
-
- Component& owner;
- ComponentPeer* currentPeer = nullptr;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
- };
-
- //==============================================================================
- AndroidViewComponent::AndroidViewComponent()
- {
- }
-
- AndroidViewComponent::~AndroidViewComponent() {}
-
- void AndroidViewComponent::setView (void* view)
- {
- if (view != getView())
- {
- pimpl.reset();
-
- if (view != nullptr)
- {
- // explicitly create a new local ref here so that we don't
- // delete the users pointer
- auto* env = getEnv();
- auto localref = LocalRef<jobject>(env->NewLocalRef((jobject) view));
-
- pimpl.reset (new Pimpl (localref, *this));
- }
- }
- }
-
- void* AndroidViewComponent::getView() const
- {
- return pimpl == nullptr ? nullptr : (void*) pimpl->view;
- }
-
- void AndroidViewComponent::resizeToFitView()
- {
- if (pimpl != nullptr)
- setBounds (pimpl->getViewBounds());
- }
-
- void AndroidViewComponent::paint (Graphics&) {}
-
- } // namespace juce
|