From 764e00d9cfd1da60b6d117a853b5b4a8b67000e5 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 20 Jun 2018 15:48:40 +1200 Subject: [PATCH] Stop desktop app window's QML source from running when window is closed --- libraries/qml/src/qml/OffscreenSurface.cpp | 7 +++++++ .../ui/src/ui/TabletScriptingInterface.cpp | 21 +++++++++++++++++++ .../ui/src/ui/TabletScriptingInterface.h | 3 +++ 3 files changed, 31 insertions(+) diff --git a/libraries/qml/src/qml/OffscreenSurface.cpp b/libraries/qml/src/qml/OffscreenSurface.cpp index 688f3fdb5f..f0c3dfdffd 100644 --- a/libraries/qml/src/qml/OffscreenSurface.cpp +++ b/libraries/qml/src/qml/OffscreenSurface.cpp @@ -286,6 +286,13 @@ void OffscreenSurface::loadInternal(const QUrl& qmlSource, if (QThread::currentThread() != thread()) { qFatal("Called load on a non-surface thread"); } + + // For desktop toolbar mode window: stop script when window is closed. + if (qmlSource.isEmpty()) { + getSurfaceContext()->engine()->quit(); + return; + } + // Synchronous loading may take a while; restart the deadlock timer QMetaObject::invokeMethod(qApp, "updateHeartbeat", Qt::DirectConnection); diff --git a/libraries/ui/src/ui/TabletScriptingInterface.cpp b/libraries/ui/src/ui/TabletScriptingInterface.cpp index 2c52e669a0..6f00e046af 100644 --- a/libraries/ui/src/ui/TabletScriptingInterface.cpp +++ b/libraries/ui/src/ui/TabletScriptingInterface.cpp @@ -648,6 +648,26 @@ void TabletProxy::loadQMLSource(const QVariant& path, bool resizable) { } } +void TabletProxy::stopQMLSource() { + // For desktop toolbar mode dialogs. + if (!_toolbarMode || !_desktopWindow) { + qCDebug(uiLogging) << "tablet cannot clear QML because not desktop toolbar mode"; + return; + } + + auto root = _desktopWindow->asQuickItem(); + if (root) { + QMetaObject::invokeMethod(root, "loadSource", Q_ARG(const QVariant&, "")); + if (!_currentPathLoaded.toString().isEmpty()) { + emit screenChanged(QVariant("QML"), ""); + } + _currentPathLoaded = ""; + _state = State::Home; + } else { + qCDebug(uiLogging) << "tablet cannot clear QML because _desktopWindow is null"; + } +} + bool TabletProxy::pushOntoStack(const QVariant& path) { if (QThread::currentThread() != thread()) { bool result = false; @@ -719,6 +739,7 @@ void TabletProxy::loadHomeScreen(bool forceOntoHomeScreen) { // close desktop window if (_desktopWindow->asQuickItem()) { QMetaObject::invokeMethod(_desktopWindow->asQuickItem(), "setShown", Q_ARG(const QVariant&, QVariant(false))); + stopQMLSource(); // Stop the currently loaded QML running. } } _state = State::Home; diff --git a/libraries/ui/src/ui/TabletScriptingInterface.h b/libraries/ui/src/ui/TabletScriptingInterface.h index 1ab29ca3fd..2d37402d01 100644 --- a/libraries/ui/src/ui/TabletScriptingInterface.h +++ b/libraries/ui/src/ui/TabletScriptingInterface.h @@ -443,6 +443,9 @@ protected: bool _showRunningScripts { false }; TabletButtonListModel _buttons; + +private: + void stopQMLSource(); }; Q_DECLARE_METATYPE(TabletProxy*);