From dfaa8761e5d723403220b8b23c0608df126a4029 Mon Sep 17 00:00:00 2001 From: vladest Date: Sun, 10 Sep 2017 20:08:40 +0200 Subject: [PATCH 1/2] Make sure root item is created before lambda gets called for desktop loader --- libraries/ui/src/ui/OffscreenQmlSurface.cpp | 27 ++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/libraries/ui/src/ui/OffscreenQmlSurface.cpp b/libraries/ui/src/ui/OffscreenQmlSurface.cpp index d29dc7a0f9..9015cc91d7 100644 --- a/libraries/ui/src/ui/OffscreenQmlSurface.cpp +++ b/libraries/ui/src/ui/OffscreenQmlSurface.cpp @@ -690,7 +690,6 @@ void OffscreenQmlSurface::finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext return; } - QObject* newObject = qmlComponent->beginCreate(qmlContext); if (qmlComponent->isError()) { for (const auto& error : qmlComponent->errors()) { @@ -704,7 +703,20 @@ void OffscreenQmlSurface::finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext } qmlContext->engine()->setObjectOwnership(this, QQmlEngine::CppOwnership); - f(qmlContext, newObject); + + // All quick items should be focusable + QQuickItem* newItem = qobject_cast(newObject); + if (newItem) { + // Make sure we make items focusable (critical for + // supporting keyboard shortcuts) + newItem->setFlag(QQuickItem::ItemIsFocusScope, true); + } + + // Make sure we will call callback for this codepath + // Caal this before qmlComponent->completeCreate() otherwise ghost window appears + if (newItem && _rootItem) { + f(qmlContext, newObject); + } QObject* eventBridge = qmlContext->contextProperty("eventBridge").value(); if (qmlContext != _qmlContext && eventBridge && eventBridge != this) { @@ -716,15 +728,6 @@ void OffscreenQmlSurface::finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext qmlComponent->completeCreate(); qmlComponent->deleteLater(); - - // All quick items should be focusable - QQuickItem* newItem = qobject_cast(newObject); - if (newItem) { - // Make sure we make items focusable (critical for - // supporting keyboard shortcuts) - newItem->setFlag(QQuickItem::ItemIsFocusScope, true); - } - // If we already have a root, just set a couple of flags and the ancestry if (newItem && _rootItem) { // Allow child windows to be destroyed from JS @@ -747,6 +750,8 @@ void OffscreenQmlSurface::finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext _rootItem = newItem; _rootItem->setParentItem(_quickWindow->contentItem()); _rootItem->setSize(_quickWindow->renderTargetSize()); + // Call this callback after rootitem is set, otherwise VrMenu wont work + f(qmlContext, newObject); } void OffscreenQmlSurface::updateQuick() { From 34c1cb6579b88d3099411d8123745f6ad113263c Mon Sep 17 00:00:00 2001 From: vladest Date: Mon, 11 Sep 2017 19:42:05 +0200 Subject: [PATCH 2/2] Typos and naming fixes --- libraries/ui/src/ui/OffscreenQmlSurface.cpp | 24 ++++++++++----------- libraries/ui/src/ui/OffscreenQmlSurface.h | 12 +++++------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/libraries/ui/src/ui/OffscreenQmlSurface.cpp b/libraries/ui/src/ui/OffscreenQmlSurface.cpp index 9015cc91d7..ca088fa3fe 100644 --- a/libraries/ui/src/ui/OffscreenQmlSurface.cpp +++ b/libraries/ui/src/ui/OffscreenQmlSurface.cpp @@ -639,7 +639,7 @@ void OffscreenQmlSurface::setBaseUrl(const QUrl& baseUrl) { _qmlContext->setBaseUrl(baseUrl); } -void OffscreenQmlSurface::load(const QUrl& qmlSource, bool createNewContext, std::function f) { +void OffscreenQmlSurface::load(const QUrl& qmlSource, bool createNewContext, std::function onQmlLoadedCallback) { if (QThread::currentThread() != thread()) { qCWarning(uiLogging) << "Called load on a non-surface thread"; } @@ -659,28 +659,28 @@ void OffscreenQmlSurface::load(const QUrl& qmlSource, bool createNewContext, std auto qmlComponent = new QQmlComponent(_qmlContext->engine(), finalQmlSource, QQmlComponent::PreferSynchronous); if (qmlComponent->isLoading()) { connect(qmlComponent, &QQmlComponent::statusChanged, this, - [this, qmlComponent, targetContext, f](QQmlComponent::Status) { - finishQmlLoad(qmlComponent, targetContext, f); + [this, qmlComponent, targetContext, onQmlLoadedCallback](QQmlComponent::Status) { + finishQmlLoad(qmlComponent, targetContext, onQmlLoadedCallback); }); return; } - finishQmlLoad(qmlComponent, targetContext, f); + finishQmlLoad(qmlComponent, targetContext, onQmlLoadedCallback); } -void OffscreenQmlSurface::loadInNewContext(const QUrl& qmlSource, std::function f) { - load(qmlSource, true, f); +void OffscreenQmlSurface::loadInNewContext(const QUrl& qmlSource, std::function onQmlLoadedCallback) { + load(qmlSource, true, onQmlLoadedCallback); } -void OffscreenQmlSurface::load(const QUrl& qmlSource, std::function f) { - load(qmlSource, false, f); +void OffscreenQmlSurface::load(const QUrl& qmlSource, std::function onQmlLoadedCallback) { + load(qmlSource, false, onQmlLoadedCallback); } void OffscreenQmlSurface::clearCache() { _qmlContext->engine()->clearComponentCache(); } -void OffscreenQmlSurface::finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext* qmlContext, std::function f) { +void OffscreenQmlSurface::finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext* qmlContext, std::function onQmlLoadedCallback) { disconnect(qmlComponent, &QQmlComponent::statusChanged, this, 0); if (qmlComponent->isError()) { for (const auto& error : qmlComponent->errors()) { @@ -713,9 +713,9 @@ void OffscreenQmlSurface::finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext } // Make sure we will call callback for this codepath - // Caal this before qmlComponent->completeCreate() otherwise ghost window appears + // Call this before qmlComponent->completeCreate() otherwise ghost window appears if (newItem && _rootItem) { - f(qmlContext, newObject); + onQmlLoadedCallback(qmlContext, newObject); } QObject* eventBridge = qmlContext->contextProperty("eventBridge").value(); @@ -751,7 +751,7 @@ void OffscreenQmlSurface::finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext _rootItem->setParentItem(_quickWindow->contentItem()); _rootItem->setSize(_quickWindow->renderTargetSize()); // Call this callback after rootitem is set, otherwise VrMenu wont work - f(qmlContext, newObject); + onQmlLoadedCallback(qmlContext, newObject); } void OffscreenQmlSurface::updateQuick() { diff --git a/libraries/ui/src/ui/OffscreenQmlSurface.h b/libraries/ui/src/ui/OffscreenQmlSurface.h index 7da929723c..95dabdef0f 100644 --- a/libraries/ui/src/ui/OffscreenQmlSurface.h +++ b/libraries/ui/src/ui/OffscreenQmlSurface.h @@ -50,11 +50,11 @@ public: void resize(const QSize& size, bool forceResize = false); QSize size() const; - Q_INVOKABLE void load(const QUrl& qmlSource, bool createNewContext, std::function f = [](QQmlContext*, QObject*) {}); - Q_INVOKABLE void loadInNewContext(const QUrl& qmlSource, std::function f = [](QQmlContext*, QObject*) {}); - Q_INVOKABLE void load(const QUrl& qmlSource, std::function f = [](QQmlContext*, QObject*) {}); - Q_INVOKABLE void load(const QString& qmlSourceFile, std::function f = [](QQmlContext*, QObject*) {}) { - return load(QUrl(qmlSourceFile), f); + Q_INVOKABLE void load(const QUrl& qmlSource, bool createNewContext, std::function onQmlLoadedCallback = [](QQmlContext*, QObject*) {}); + Q_INVOKABLE void loadInNewContext(const QUrl& qmlSource, std::function onQmlLoadedCallback = [](QQmlContext*, QObject*) {}); + Q_INVOKABLE void load(const QUrl& qmlSource, std::function onQmlLoadedCallback = [](QQmlContext*, QObject*) {}); + Q_INVOKABLE void load(const QString& qmlSourceFile, std::function onQmlLoadedCallback = [](QQmlContext*, QObject*) {}) { + return load(QUrl(qmlSourceFile), onQmlLoadedCallback); } void clearCache(); void setMaxFps(uint8_t maxFps) { _maxFps = maxFps; } @@ -120,7 +120,7 @@ protected: private: static QOpenGLContext* getSharedContext(); - void finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext* qmlContext, std::function f); + void finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext* qmlContext, std::function onQmlLoadedCallback); QPointF mapWindowToUi(const QPointF& sourcePosition, QObject* sourceObject); void setupFbo(); bool allowNewFrame(uint8_t fps);