Make sure loaded components get the right parent context

This commit is contained in:
Brad Davis 2017-11-13 14:16:04 -08:00
parent 5cf9c1bb33
commit 8f85c7650b
3 changed files with 8 additions and 10 deletions

View file

@ -64,8 +64,8 @@ Windows.ScrollingWindow {
function loadWebContent(source, url, injectJavaScriptUrl) { function loadWebContent(source, url, injectJavaScriptUrl) {
loader.load(source, function() { loader.load(source, function() {
loader.item.url = url
loader.item.scriptURL = injectJavaScriptUrl; loader.item.scriptURL = injectJavaScriptUrl;
loader.item.url = url;
if (loader.item.hasOwnProperty("closeButtonVisible")) { if (loader.item.hasOwnProperty("closeButtonVisible")) {
loader.item.closeButtonVisible = false; loader.item.closeButtonVisible = false;
} }
@ -146,11 +146,6 @@ Windows.ScrollingWindow {
} }
function load(newSource, callback) { function load(newSource, callback) {
if (loader.source == newSource) {
loader.loaded();
return;
}
if (loader.item) { if (loader.item) {
loader.item.destroy(); loader.item.destroy();
loader.item = null; loader.item = null;

View file

@ -697,13 +697,16 @@ QQuickItem* OffscreenQmlSurface::getRootItem() {
return _rootItem; return _rootItem;
} }
QQmlContext* OffscreenQmlSurface::contextForUrl(const QUrl& qmlSource, bool forceNewContext) { QQmlContext* OffscreenQmlSurface::contextForUrl(const QUrl& qmlSource, QQuickItem* parent, bool forceNewContext) {
// Get any whitelist functionality // Get any whitelist functionality
QList<QmlContextCallback> callbacks = getQmlWhitelist()->getCallbacksForUrl(qmlSource); QList<QmlContextCallback> callbacks = getQmlWhitelist()->getCallbacksForUrl(qmlSource);
// If we have whitelisted content, we must load a new context // If we have whitelisted content, we must load a new context
forceNewContext |= !callbacks.empty(); forceNewContext |= !callbacks.empty();
QQmlContext* targetContext = _qmlContext; QQmlContext* targetContext = parent ? QQmlEngine::contextForObject(parent) : _qmlContext;
if (!targetContext) {
targetContext = _qmlContext;
}
if (_rootItem && forceNewContext) { if (_rootItem && forceNewContext) {
targetContext = new QQmlContext(targetContext); targetContext = new QQmlContext(targetContext);
} }
@ -737,7 +740,7 @@ void OffscreenQmlSurface::loadInternal(const QUrl& qmlSource, bool createNewCont
finalQmlSource = _qmlContext->resolvedUrl(qmlSource); finalQmlSource = _qmlContext->resolvedUrl(qmlSource);
} }
auto targetContext = contextForUrl(finalQmlSource, createNewContext); auto targetContext = contextForUrl(finalQmlSource, parent, createNewContext);
auto qmlComponent = new QQmlComponent(_qmlContext->engine(), finalQmlSource, QQmlComponent::PreferSynchronous); auto qmlComponent = new QQmlComponent(_qmlContext->engine(), finalQmlSource, QQmlComponent::PreferSynchronous);
if (qmlComponent->isLoading()) { if (qmlComponent->isLoading()) {
connect(qmlComponent, &QQmlComponent::statusChanged, this, [=](QQmlComponent::Status) { connect(qmlComponent, &QQmlComponent::statusChanged, this, [=](QQmlComponent::Status) {

View file

@ -130,7 +130,7 @@ protected:
private: private:
static QOpenGLContext* getSharedContext(); static QOpenGLContext* getSharedContext();
QQmlContext* contextForUrl(const QUrl& url, bool forceNewContext = false); QQmlContext* contextForUrl(const QUrl& url, QQuickItem* parent, bool forceNewContext = false);
void loadInternal(const QUrl& qmlSource, bool createNewContext, QQuickItem* parent, const QmlContextObjectCallback& onQmlLoadedCallback); void loadInternal(const QUrl& qmlSource, bool createNewContext, QQuickItem* parent, const QmlContextObjectCallback& onQmlLoadedCallback);
void finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext* qmlContext, QQuickItem* parent, const QmlContextObjectCallback& onQmlLoadedCallback); void finishQmlLoad(QQmlComponent* qmlComponent, QQmlContext* qmlContext, QQuickItem* parent, const QmlContextObjectCallback& onQmlLoadedCallback);
QPointF mapWindowToUi(const QPointF& sourcePosition, QObject* sourceObject); QPointF mapWindowToUi(const QPointF& sourcePosition, QObject* sourceObject);