2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
23 ==============================================================================
29 class AndroidViewComponent::Pimpl
: public ComponentMovementWatcher
32 Pimpl (const LocalRef
<jobject
>& v
, Component
& comp
)
33 : ComponentMovementWatcher (&comp
),
37 if (owner
.isShowing())
38 componentPeerChanged();
46 void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
48 auto* topComp
= owner
.getTopLevelComponent();
50 if (topComp
->getPeer() != nullptr)
52 auto pos
= topComp
->getLocalPoint (&owner
, Point
<int>());
54 Rectangle
<int> r (pos
.x
, pos
.y
, owner
.getWidth(), owner
.getHeight());
55 r
*= Desktop::getInstance().getDisplays().getPrimaryDisplay()->scale
;
57 getEnv()->CallVoidMethod (view
, AndroidView
.layout
, r
.getX(), r
.getY(),
58 r
.getRight(), r
.getBottom());
62 void componentPeerChanged() override
64 auto* peer
= owner
.getPeer();
66 if (currentPeer
!= peer
)
80 getEnv()->CallVoidMethod (view
, AndroidView
.setVisibility
, owner
.isShowing() ? VISIBLE
: INVISIBLE
);
83 void componentVisibilityChanged() override
85 componentPeerChanged();
88 void componentBroughtToFront (Component
& comp
) override
90 ComponentMovementWatcher::componentBroughtToFront (comp
);
93 Rectangle
<int> getViewBounds() const
97 int width
= env
->CallIntMethod (view
, AndroidView
.getWidth
);
98 int height
= env
->CallIntMethod (view
, AndroidView
.getHeight
);
100 return Rectangle
<int> (width
, height
);
108 if (currentPeer
!= nullptr)
110 jobject peerView
= (jobject
) currentPeer
->getNativeHandle();
112 // NB: Assuming a parent is always of ViewGroup type
113 auto* env
= getEnv();
115 env
->CallVoidMethod (peerView
, AndroidViewGroup
.addView
, view
.get());
116 componentMovedOrResized (false, false);
120 void removeFromParent()
122 auto* env
= getEnv();
123 auto parentView
= env
->CallObjectMethod (view
, AndroidView
.getParent
);
125 if (parentView
!= nullptr)
127 // Assuming a parent is always of ViewGroup type
128 env
->CallVoidMethod (parentView
, AndroidViewGroup
.removeView
, view
.get());
133 ComponentPeer
* currentPeer
= nullptr;
135 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl
)
138 //==============================================================================
139 AndroidViewComponent::AndroidViewComponent()
143 AndroidViewComponent::~AndroidViewComponent() {}
145 void AndroidViewComponent::setView (void* view
)
147 if (view
!= getView())
153 // explicitly create a new local ref here so that we don't
154 // delete the users pointer
155 auto* env
= getEnv();
156 auto localref
= LocalRef
<jobject
>(env
->NewLocalRef((jobject
) view
));
158 pimpl
.reset (new Pimpl (localref
, *this));
163 void* AndroidViewComponent::getView() const
165 return pimpl
== nullptr ? nullptr : (void*) pimpl
->view
;
168 void AndroidViewComponent::resizeToFitView()
170 if (pimpl
!= nullptr)
171 setBounds (pimpl
->getViewBounds());
174 void AndroidViewComponent::paint (Graphics
&) {}