diff --git a/modules/juce_gui_extra/embedding/juce_AndroidViewComponent.h b/modules/juce_gui_extra/embedding/juce_AndroidViewComponent.h index 12e84015f4..e9244deb9c 100644 --- a/modules/juce_gui_extra/embedding/juce_AndroidViewComponent.h +++ b/modules/juce_gui_extra/embedding/juce_AndroidViewComponent.h @@ -46,8 +46,12 @@ class JUCE_API AndroidViewComponent : public Component { public: //============================================================================== - /** Create an initially-empty container. */ - AndroidViewComponent(); + /** Create an initially-empty container. The optional flag should be left as + false in most of the cases. Currently it is only set to true as a workaround + for a web browser bug, where scrolling would be very slow and it would + randomly scroll in an opposite direction of scrolling. + */ + AndroidViewComponent (bool embedAsSiblingRatherThanChild = false); /** Destructor. */ ~AndroidViewComponent(); @@ -73,6 +77,8 @@ private: class Pimpl; std::unique_ptr pimpl; + bool embedAsSiblingRatherThanChild; + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AndroidViewComponent) }; diff --git a/modules/juce_gui_extra/native/juce_AndroidViewComponent.cpp b/modules/juce_gui_extra/native/juce_AndroidViewComponent.cpp index ca23529720..42cfc352b7 100644 --- a/modules/juce_gui_extra/native/juce_AndroidViewComponent.cpp +++ b/modules/juce_gui_extra/native/juce_AndroidViewComponent.cpp @@ -30,10 +30,11 @@ namespace juce class AndroidViewComponent::Pimpl : public ComponentMovementWatcher { public: - Pimpl (jobject v, Component& comp) + Pimpl (jobject v, Component& comp, bool makeSiblingRatherThanChild = false) : ComponentMovementWatcher (&comp), view (v), - owner (comp) + owner (comp), + embedAsSiblingRatherThanChild (makeSiblingRatherThanChild) { if (owner.isShowing()) componentPeerChanged(); @@ -87,6 +88,15 @@ public: componentPeerChanged(); } + void componentBroughtToFront (Component& comp) override + { + ComponentMovementWatcher::componentBroughtToFront (comp); + + // Ensure that the native component doesn't get obscured. + if (embedAsSiblingRatherThanChild) + getEnv()->CallVoidMethod (view, AndroidView.bringToFront); + } + Rectangle getViewBounds() const { auto* env = getEnv(); @@ -106,11 +116,22 @@ private: { jobject peerView = (jobject) currentPeer->getNativeHandle(); + // NB: Assuming a parent is always of ViewGroup type auto* env = getEnv(); - auto parentView = env->CallObjectMethod (peerView, AndroidView.getParent); - // Assuming a parent is always of ViewGroup type - env->CallVoidMethod (parentView, AndroidViewGroup.addView, view.get()); + if (embedAsSiblingRatherThanChild) + { + // This is a workaround for a bug in a web browser component where + // scrolling would be very slow and occassionally would scroll in + // opposite direction to dragging direction. In normal circumstances, + // the native view should be a child of peerView instead. + auto parentView = LocalRef (env->CallObjectMethod (peerView, AndroidView.getParent)); + env->CallVoidMethod (parentView, AndroidViewGroup.addView, view.get()); + } + else + { + env->CallVoidMethod (peerView, AndroidViewGroup.addView, view.get()); + } componentMovedOrResized (false, false); } @@ -129,13 +150,18 @@ private: } Component& owner; + bool embedAsSiblingRatherThanChild; ComponentPeer* currentPeer = nullptr; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl) }; //============================================================================== -AndroidViewComponent::AndroidViewComponent() {} +AndroidViewComponent::AndroidViewComponent (bool makeSiblingRatherThanChild) + : embedAsSiblingRatherThanChild (makeSiblingRatherThanChild) +{ +} + AndroidViewComponent::~AndroidViewComponent() {} void AndroidViewComponent::setView (void* view) @@ -145,7 +171,7 @@ void AndroidViewComponent::setView (void* view) pimpl.reset(); if (view != nullptr) - pimpl.reset (new Pimpl ((jobject) view, *this)); + pimpl.reset (new Pimpl ((jobject) view, *this, embedAsSiblingRatherThanChild)); } } diff --git a/modules/juce_gui_extra/native/juce_android_WebBrowserComponent.cpp b/modules/juce_gui_extra/native/juce_android_WebBrowserComponent.cpp index 30c1cbfb11..736efa0456 100644 --- a/modules/juce_gui_extra/native/juce_android_WebBrowserComponent.cpp +++ b/modules/juce_gui_extra/native/juce_android_WebBrowserComponent.cpp @@ -101,7 +101,8 @@ class WebBrowserComponent::Pimpl : public AndroidViewComponent, { public: Pimpl (WebBrowserComponent& o) - : owner (o) + : AndroidViewComponent (true), + owner (o) { auto* env = getEnv();