diff --git a/interface/resources/qml/controls/FlickableWebViewCore.qml b/interface/resources/qml/controls/FlickableWebViewCore.qml index 8e7db44b7d..943f15e1de 100644 --- a/interface/resources/qml/controls/FlickableWebViewCore.qml +++ b/interface/resources/qml/controls/FlickableWebViewCore.qml @@ -21,6 +21,7 @@ Item { signal newViewRequestedCallback(var request) signal loadingChangedCallback(var loadRequest) + width: parent.width property bool interactive: false @@ -29,6 +30,10 @@ Item { id: hifi } + function stop() { + webViewCore.stop(); + } + function unfocus() { webViewCore.runJavaScript("if (document.activeElement) document.activeElement.blur();", function(result) { console.log('unfocus completed: ', result); diff --git a/interface/resources/qml/controls/WebView.qml b/interface/resources/qml/controls/WebView.qml index 931c64e1ef..71bf69fdc8 100644 --- a/interface/resources/qml/controls/WebView.qml +++ b/interface/resources/qml/controls/WebView.qml @@ -21,6 +21,10 @@ Item { property bool passwordField: false property alias flickable: webroot.interactive + function stop() { + webroot.stop(); + } + // FIXME - Keyboard HMD only: Make Interface either set keyboardRaised property directly in OffscreenQmlSurface // or provide HMDinfo object to QML in RenderableWebEntityItem and do the following. /* diff --git a/libraries/entities-renderer/src/RenderableWebEntityItem.cpp b/libraries/entities-renderer/src/RenderableWebEntityItem.cpp index 7ad74d1eee..693e3d0cf4 100644 --- a/libraries/entities-renderer/src/RenderableWebEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableWebEntityItem.cpp @@ -318,8 +318,10 @@ bool WebEntityRenderer::buildWebSurface(const TypedEntityPointer& entity) { void WebEntityRenderer::destroyWebSurface() { QSharedPointer webSurface; + ContentType contentType{ ContentType::NoContent }; withWriteLock([&] { webSurface.swap(_webSurface); + std::swap(contentType, _contentType); }); if (webSurface) { @@ -328,12 +330,9 @@ void WebEntityRenderer::destroyWebSurface() { // Fix for crash in QtWebEngineCore when rapidly switching domains // Call stop on the QWebEngineView before destroying OffscreenQMLSurface. - if (rootItem) { - QObject* obj = rootItem->findChild("webEngineView"); - if (obj) { - // stop loading - QMetaObject::invokeMethod(obj, "stop"); - } + if (rootItem && contentType == ContentType::HtmlContent) { + // stop loading + QMetaObject::invokeMethod(rootItem, "stop"); } webSurface->pause(); diff --git a/tests/qml/src/main.cpp b/tests/qml/src/main.cpp index ec4012898f..349ac55d88 100644 --- a/tests/qml/src/main.cpp +++ b/tests/qml/src/main.cpp @@ -45,19 +45,15 @@ #include namespace gl { - extern void initModuleGl(); +extern void initModuleGl(); } class QTestItem : public QQuickItem { Q_OBJECT public: - QTestItem(QQuickItem* parent = nullptr) : QQuickItem(parent) { - qDebug() << __FUNCTION__; - } + QTestItem(QQuickItem* parent = nullptr) : QQuickItem(parent) { qDebug() << __FUNCTION__; } - ~QTestItem() { - qDebug() << __FUNCTION__; - } + ~QTestItem() { qDebug() << __FUNCTION__; } }; QUrl getTestResource(const QString& relativePath) { @@ -71,7 +67,6 @@ QUrl getTestResource(const QString& relativePath) { return QUrl::fromLocalFile(dir + relativePath); } - #define DIVISIONS_X 5 #define DIVISIONS_Y 5 @@ -164,61 +159,41 @@ void TestWindow::resizeWindow(const QSize& size) { } static const int DEFAULT_MAX_FPS = 10; -static const int YOUTUBE_MAX_FPS = 30; static const QString CONTROL_URL{ "/qml/controls/WebEntityView.qml" }; static const char* URL_PROPERTY{ "url" }; -QString getSourceUrl() { +QString getSourceUrl(bool video) { static const std::vector SOURCE_URLS{ "https://www.reddit.com/wiki/random", "https://en.wikipedia.org/wiki/Wikipedia:Random", "https://slashdot.org/", - //"https://www.youtube.com/watch?v=gDXwhHm4GhM", - //"https://www.youtube.com/watch?v=Ch_hoYPPeGc", }; - auto index = rand() % SOURCE_URLS.size(); - return SOURCE_URLS[index]; + static const std::vector VIDEO_SOURCE_URLS{ + "https://www.youtube.com/watch?v=gDXwhHm4GhM", + "https://www.youtube.com/watch?v=Ch_hoYPPeGc", + }; + + const auto& sourceUrls = video ? VIDEO_SOURCE_URLS : SOURCE_URLS; + auto index = rand() % sourceUrls.size(); + return sourceUrls[index]; } -#define CACHE_WEBVIEWS 0 - -#if CACHE_WEBVIEWS -static std::list _cache; -#endif - -hifi::qml::QmlContextObjectCallback callback = [](QQmlContext* context, QQuickItem* item) { - item->setProperty(URL_PROPERTY, getSourceUrl()); -}; - -void TestWindow::buildSurface(QmlInfo& qmlInfo, bool allowVideo) { +void TestWindow::buildSurface(QmlInfo& qmlInfo, bool video) { ++_surfaceCount; auto lifetimeSecs = (uint32_t)(5.0f + (randFloat() * 10.0f)); auto lifetimeUsecs = (USECS_PER_SECOND * lifetimeSecs); qmlInfo.lifetime = lifetimeUsecs + usecTimestampNow(); qmlInfo.texture = 0; -#if CACHE_WEBVIEWS - if (_cache.empty()) { - _cache.emplace_back(new hifi::qml::OffscreenSurface()); - auto& surface = _cache.back(); - surface->load(getTestResource(CONTROL_URL)); - surface->setMaxFps(DEFAULT_MAX_FPS); - } - qmlInfo.surface = _cache.front(); - _cache.pop_front(); -#else qmlInfo.surface.reset(new hifi::qml::OffscreenSurface()); - qmlInfo.surface->load(getTestResource(CONTROL_URL)); + qmlInfo.surface->load(getTestResource(CONTROL_URL), [video](QQmlContext* context, QQuickItem* item) { + item->setProperty(URL_PROPERTY, getSourceUrl(video)); + }); qmlInfo.surface->setMaxFps(DEFAULT_MAX_FPS); -#endif - qmlInfo.surface->resize(_qmlSize); - auto url = allowVideo ? "https://www.youtube.com/watch?v=gDXwhHm4GhM" : getSourceUrl(); - qmlInfo.surface->getRootItem()->setProperty(URL_PROPERTY, url); qmlInfo.surface->resume(); } - void TestWindow::destroySurface(QmlInfo& qmlInfo) { auto& surface = qmlInfo.surface; auto webView = surface->getRootItem(); @@ -228,9 +203,6 @@ void TestWindow::destroySurface(QmlInfo& qmlInfo) { webView->setProperty(URL_PROPERTY, "about:blank"); } surface->pause(); -#if CACHE_WEBVIEWS - _cache.push_back(surface); -#endif surface.reset(); } @@ -296,7 +268,6 @@ void TestWindow::draw() { _glf.glViewport(0, 0, size.width(), size.height()); _glf.glClearColor(1, 0, 0, 1); _glf.glClear(GL_COLOR_BUFFER_BIT); - for (uint32_t x = 0; x < DIVISIONS_X; ++x) { for (uint32_t y = 0; y < DIVISIONS_Y; ++y) { auto& qmlInfo = _surfaces[x][y]; @@ -313,8 +284,6 @@ void TestWindow::draw() { GL_COLOR_BUFFER_BIT, GL_NEAREST); } } - _glf.glFlush(); - _glContext.swapBuffers(this); } @@ -325,8 +294,7 @@ void TestWindow::resizeEvent(QResizeEvent* ev) { int main(int argc, char** argv) { QGuiApplication app(argc, argv); TestWindow window; - app.exec(); - return 0; + return app.exec(); } -#include "main.moc" \ No newline at end of file +#include "main.moc"