From 4bf952260fab20d23b944e19b9cd31fc23523baa Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Mon, 18 Jan 2021 12:57:44 -0800 Subject: [PATCH 1/8] fix crashes when running with qml disabled in VR --- libraries/ui/src/OffscreenUi.cpp | 77 ++++++++++--------- .../ui/src/ui/TabletScriptingInterface.cpp | 2 + 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/libraries/ui/src/OffscreenUi.cpp b/libraries/ui/src/OffscreenUi.cpp index 34184057e0..26e77dcb5f 100644 --- a/libraries/ui/src/OffscreenUi.cpp +++ b/libraries/ui/src/OffscreenUi.cpp @@ -189,10 +189,12 @@ void OffscreenUi::show(const QUrl& url, const QString& name, std::function f) { @@ -208,11 +210,14 @@ void OffscreenUi::toggle(const QUrl& url, const QString& name, std::functionfindChild(name); - if (item) { - return QQmlProperty(item, OFFSCREEN_VISIBILITY_PROPERTY).read().toBool(); - } else { - return false; + auto rootItem = getRootItem(); + if (rootItem) { + QQuickItem* item = rootItem->findChild(name); + if (item) { + return QQmlProperty(item, OFFSCREEN_VISIBILITY_PROPERTY).read().toBool(); + } } + return false; } class MessageBoxListener : public ModalDialogListener { @@ -280,12 +287,11 @@ QQuickItem* OffscreenUi::createMessageBox(Icon icon, const QString& title, const bool invokeResult; auto tabletScriptingInterface = DependencyManager::get(); TabletProxy* tablet = dynamic_cast(tabletScriptingInterface->getTablet("com.highfidelity.interface.tablet.system")); - if (tablet->getToolbarMode()) { + if (tablet->getToolbarMode() && _desktop) { invokeResult = QMetaObject::invokeMethod(_desktop, "messageBox", Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, QVariant::fromValue(map))); - } else { - QQuickItem* tabletRoot = tablet->getTabletRoot(); + } else if (QQuickItem* tabletRoot = tablet->getTabletRoot()) { invokeResult = QMetaObject::invokeMethod(tabletRoot, "messageBox", Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, QVariant::fromValue(map))); @@ -533,21 +539,21 @@ ModalDialogListener* OffscreenUi::customInputDialogAsync(const Icon icon, const } void OffscreenUi::togglePinned() { - bool invokeResult = QMetaObject::invokeMethod(_desktop, "togglePinned"); + bool invokeResult = _desktop && QMetaObject::invokeMethod(_desktop, "togglePinned"); if (!invokeResult) { qWarning() << "Failed to toggle window visibility"; } } void OffscreenUi::setPinned(bool pinned) { - bool invokeResult = QMetaObject::invokeMethod(_desktop, "setPinned", Q_ARG(QVariant, pinned)); + bool invokeResult = _desktop && QMetaObject::invokeMethod(_desktop, "setPinned", Q_ARG(QVariant, pinned)); if (!invokeResult) { qWarning() << "Failed to set window visibility"; } } void OffscreenUi::setConstrainToolbarToCenterX(bool constrained) { - bool invokeResult = QMetaObject::invokeMethod(_desktop, "setConstrainToolbarToCenterX", Q_ARG(QVariant, constrained)); + bool invokeResult = _desktop && QMetaObject::invokeMethod(_desktop, "setConstrainToolbarToCenterX", Q_ARG(QVariant, constrained)); if (!invokeResult) { qWarning() << "Failed to set toolbar constraint"; } @@ -575,17 +581,17 @@ QQuickItem* OffscreenUi::createInputDialog(const Icon icon, const QString& title TabletProxy* tablet = dynamic_cast(tabletScriptingInterface->getTablet("com.highfidelity.interface.tablet.system")); bool invokeResult; - if (tablet->getToolbarMode()) { + if (tablet->getToolbarMode() && _desktop) { invokeResult = QMetaObject::invokeMethod(_desktop, "inputDialog", Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, QVariant::fromValue(map))); - } else { - QQuickItem* tabletRoot = tablet->getTabletRoot(); + } else if (QQuickItem* tabletRoot = tablet->getTabletRoot()) { invokeResult = QMetaObject::invokeMethod(tabletRoot, "inputDialog", Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, QVariant::fromValue(map))); emit tabletScriptingInterface->tabletNotification(); } + if (!invokeResult) { qWarning() << "Failed to create message box"; return nullptr; @@ -603,12 +609,11 @@ QQuickItem* OffscreenUi::createCustomInputDialog(const Icon icon, const QString& TabletProxy* tablet = dynamic_cast(tabletScriptingInterface->getTablet("com.highfidelity.interface.tablet.system")); bool invokeResult; - if (tablet->getToolbarMode()) { + if (tablet->getToolbarMode() && _desktop) { invokeResult = QMetaObject::invokeMethod(_desktop, "inputDialog", Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, QVariant::fromValue(map))); - } else { - QQuickItem* tabletRoot = tablet->getTabletRoot(); + } else if (QQuickItem* tabletRoot = tablet->getTabletRoot()) { invokeResult = QMetaObject::invokeMethod(tabletRoot, "inputDialog", Q_RETURN_ARG(QVariant, result), Q_ARG(QVariant, QVariant::fromValue(map))); @@ -718,7 +723,7 @@ QObject* OffscreenUi::getRootMenu() { } void OffscreenUi::unfocusWindows() { - bool invokeResult = QMetaObject::invokeMethod(_desktop, "unfocusWindows"); + bool invokeResult = _desktop && QMetaObject::invokeMethod(_desktop, "unfocusWindows"); Q_ASSERT(invokeResult); } @@ -752,12 +757,11 @@ QString OffscreenUi::fileDialog(const QVariantMap& properties) { bool invokeResult; auto tabletScriptingInterface = DependencyManager::get(); TabletProxy* tablet = dynamic_cast(tabletScriptingInterface->getTablet("com.highfidelity.interface.tablet.system")); - if (tablet->getToolbarMode()) { + if (tablet->getToolbarMode() && _desktop) { invokeResult = QMetaObject::invokeMethod(_desktop, "fileDialog", Q_RETURN_ARG(QVariant, buildDialogResult), Q_ARG(QVariant, QVariant::fromValue(properties))); - } else { - QQuickItem* tabletRoot = tablet->getTabletRoot(); + } else if (QQuickItem* tabletRoot = tablet->getTabletRoot()) { invokeResult = QMetaObject::invokeMethod(tabletRoot, "fileDialog", Q_RETURN_ARG(QVariant, buildDialogResult), Q_ARG(QVariant, QVariant::fromValue(properties))); @@ -782,12 +786,11 @@ ModalDialogListener* OffscreenUi::fileDialogAsync(const QVariantMap& properties) bool invokeResult; auto tabletScriptingInterface = DependencyManager::get(); TabletProxy* tablet = dynamic_cast(tabletScriptingInterface->getTablet("com.highfidelity.interface.tablet.system")); - if (tablet->getToolbarMode()) { + if (tablet->getToolbarMode() && _desktop) { invokeResult = QMetaObject::invokeMethod(_desktop, "fileDialog", Q_RETURN_ARG(QVariant, buildDialogResult), Q_ARG(QVariant, QVariant::fromValue(properties))); - } else { - QQuickItem* tabletRoot = tablet->getTabletRoot(); + } else if (QQuickItem* tabletRoot = tablet->getTabletRoot()) { invokeResult = QMetaObject::invokeMethod(tabletRoot, "fileDialog", Q_RETURN_ARG(QVariant, buildDialogResult), Q_ARG(QVariant, QVariant::fromValue(properties))); @@ -1003,12 +1006,11 @@ QString OffscreenUi::assetDialog(const QVariantMap& properties) { bool invokeResult; auto tabletScriptingInterface = DependencyManager::get(); TabletProxy* tablet = dynamic_cast(tabletScriptingInterface->getTablet("com.highfidelity.interface.tablet.system")); - if (tablet->getToolbarMode()) { + if (tablet->getToolbarMode() && _desktop) { invokeResult = QMetaObject::invokeMethod(_desktop, "assetDialog", Q_RETURN_ARG(QVariant, buildDialogResult), Q_ARG(QVariant, QVariant::fromValue(properties))); - } else { - QQuickItem* tabletRoot = tablet->getTabletRoot(); + } else if (QQuickItem* tabletRoot = tablet->getTabletRoot()) { invokeResult = QMetaObject::invokeMethod(tabletRoot, "assetDialog", Q_RETURN_ARG(QVariant, buildDialogResult), Q_ARG(QVariant, QVariant::fromValue(properties))); @@ -1034,12 +1036,11 @@ ModalDialogListener *OffscreenUi::assetDialogAsync(const QVariantMap& properties bool invokeResult; auto tabletScriptingInterface = DependencyManager::get(); TabletProxy* tablet = dynamic_cast(tabletScriptingInterface->getTablet("com.highfidelity.interface.tablet.system")); - if (tablet->getToolbarMode()) { + if (tablet->getToolbarMode() && _desktop) { invokeResult = QMetaObject::invokeMethod(_desktop, "assetDialog", Q_RETURN_ARG(QVariant, buildDialogResult), Q_ARG(QVariant, QVariant::fromValue(properties))); - } else { - QQuickItem* tabletRoot = tablet->getTabletRoot(); + } else if (QQuickItem* tabletRoot = tablet->getTabletRoot()) { invokeResult = QMetaObject::invokeMethod(tabletRoot, "assetDialog", Q_RETURN_ARG(QVariant, buildDialogResult), Q_ARG(QVariant, QVariant::fromValue(properties))); diff --git a/libraries/ui/src/ui/TabletScriptingInterface.cpp b/libraries/ui/src/ui/TabletScriptingInterface.cpp index 040bb750d0..c09888758c 100644 --- a/libraries/ui/src/ui/TabletScriptingInterface.cpp +++ b/libraries/ui/src/ui/TabletScriptingInterface.cpp @@ -385,9 +385,11 @@ void TabletProxy::setToolbarMode(bool toolbarMode) { connect(tabletRootWindow, &QmlWindowClass::fromQml, this, &TabletProxy::fromQml); #endif } else { +#if !defined(DISABLE_QML) if (_currentPathLoaded != TABLET_HOME_SOURCE_URL) { loadHomeScreen(true); } +#endif //check if running scripts window opened and save it for reopen in Tablet if (offscreenUi->isVisible("RunningScripts")) { offscreenUi->hide("RunningScripts"); From 3335c040e50018f2cf0732dc2015731a3e2a19b5 Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Mon, 18 Jan 2021 18:39:12 -0800 Subject: [PATCH 2/8] hurr durr I'm an idiot --- .../ui/src/ui/TabletScriptingInterface.cpp | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/libraries/ui/src/ui/TabletScriptingInterface.cpp b/libraries/ui/src/ui/TabletScriptingInterface.cpp index c09888758c..61c74dc17c 100644 --- a/libraries/ui/src/ui/TabletScriptingInterface.cpp +++ b/libraries/ui/src/ui/TabletScriptingInterface.cpp @@ -321,8 +321,8 @@ void TabletScriptingInterface::processEvent(const QKeyEvent* event) { QObject* TabletScriptingInterface::getFlags() { Q_ASSERT(QThread::currentThread() == qApp->thread()); - auto offscreenUi = DependencyManager::get(); - return offscreenUi->getFlags(); + auto offscreenUI = DependencyManager::get(); + return offscreenUI ? offscreenUI->getFlags() : nullptr; } // @@ -364,8 +364,6 @@ void TabletProxy::setToolbarMode(bool toolbarMode) { _toolbarMode = toolbarMode; - auto offscreenUi = DependencyManager::get(); - if (toolbarMode) { #if !defined(DISABLE_QML) closeDialog(); @@ -385,18 +383,21 @@ void TabletProxy::setToolbarMode(bool toolbarMode) { connect(tabletRootWindow, &QmlWindowClass::fromQml, this, &TabletProxy::fromQml); #endif } else { -#if !defined(DISABLE_QML) if (_currentPathLoaded != TABLET_HOME_SOURCE_URL) { loadHomeScreen(true); } -#endif - //check if running scripts window opened and save it for reopen in Tablet - if (offscreenUi->isVisible("RunningScripts")) { - offscreenUi->hide("RunningScripts"); - _showRunningScripts = true; + + auto offscreenUI = DependencyManager::get(); + if (offscreenUI) { + //check if running scripts window opened and save it for reopen in Tablet + if (offscreenUI->isVisible("RunningScripts")) { + offscreenUI->hide("RunningScripts"); + _showRunningScripts = true; + } + + offscreenUI->hideDesktopWindows(); } - offscreenUi->hideDesktopWindows(); // destroy desktop window if (_desktopWindow) { _desktopWindow->deleteLater(); @@ -579,9 +580,9 @@ void TabletProxy::gotoMenuScreen(const QString& submenu) { root = _desktopWindow->asQuickItem(); } - if (root) { - auto offscreenUi = DependencyManager::get(); - QObject* menu = offscreenUi->getRootMenu(); + auto offscreenUI = DependencyManager::get(); + if (root && offscreenUI) { + QObject* menu = offscreenUI->getRootMenu(); QMetaObject::invokeMethod(root, "setMenuProperties", Q_ARG(QVariant, QVariant::fromValue(menu)), Q_ARG(const QVariant&, QVariant(submenu))); QMetaObject::invokeMethod(root, "loadSource", Q_ARG(const QVariant&, QVariant(VRMENU_SOURCE_URL))); _state = State::Menu; From 2667af3682981f21fe3728adb38446f5a3312b49 Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Wed, 20 Jan 2021 21:25:30 -0800 Subject: [PATCH 3/8] more safeguards --- interface/src/AboutUtil.cpp | 10 ++--- interface/src/Bookmarks.cpp | 1 - interface/src/avatar/AvatarPackager.cpp | 4 +- .../AssetMappingsScriptingInterface.cpp | 5 +++ .../scripting/DesktopScriptingInterface.cpp | 7 +++- .../src/scripting/HMDScriptingInterface.cpp | 10 +++-- interface/src/ui/ApplicationOverlay.cpp | 4 +- interface/src/ui/InteractiveWindow.cpp | 7 ++-- interface/src/ui/overlays/Overlays.cpp | 8 ++-- interface/src/ui/overlays/QmlOverlay.cpp | 8 +++- libraries/ui/src/DockWidget.cpp | 17 ++++---- libraries/ui/src/InfoView.cpp | 23 ++++++----- libraries/ui/src/OffscreenQmlElement.h | 41 +++++++++++-------- libraries/ui/src/QmlFragmentClass.cpp | 4 -- libraries/ui/src/QmlWebWindowClass.cpp | 2 - libraries/ui/src/QmlWindowClass.cpp | 2 - plugins/openvr/src/OpenVrHelpers.cpp | 14 +++---- 17 files changed, 94 insertions(+), 73 deletions(-) diff --git a/interface/src/AboutUtil.cpp b/interface/src/AboutUtil.cpp index b9bea2d85c..5340fcd248 100644 --- a/interface/src/AboutUtil.cpp +++ b/interface/src/AboutUtil.cpp @@ -57,15 +57,15 @@ void AboutUtil::openUrl(const QString& url) const { auto tablet = DependencyManager::get()->getTablet("com.highfidelity.interface.tablet.system"); auto hmd = DependencyManager::get(); - auto offscreenUi = DependencyManager::get(); + auto offscreenUI = DependencyManager::get(); - if (tablet->getToolbarMode()) { - offscreenUi->load("Browser.qml", [=](QQmlContext* context, QObject* newObject) { + if (tablet->getToolbarMode() && offscreenUI) { + offscreenUI->load("Browser.qml", [=](QQmlContext* context, QObject* newObject) { newObject->setProperty("url", url); }); } else { - if (!hmd->getShouldShowTablet() && !qApp->isHMDMode()) { - offscreenUi->load("Browser.qml", [=](QQmlContext* context, QObject* newObject) { + if (!hmd->getShouldShowTablet() && !qApp->isHMDMode() && offscreenUI) { + offscreenUI->load("Browser.qml", [=](QQmlContext* context, QObject* newObject) { newObject->setProperty("url", url); }); } else { diff --git a/interface/src/Bookmarks.cpp b/interface/src/Bookmarks.cpp index 9a8d8eb279..263723ebe0 100644 --- a/interface/src/Bookmarks.cpp +++ b/interface/src/Bookmarks.cpp @@ -61,7 +61,6 @@ void Bookmarks::deleteBookmark(const QString& bookmarkName) { void Bookmarks::addBookmarkToFile(const QString& bookmarkName, const QVariant& bookmark) { Menu* menubar = Menu::getInstance(); if (contains(bookmarkName)) { - auto offscreenUi = DependencyManager::get(); ModalDialogListener* dlg = OffscreenUi::asyncWarning("Duplicate Bookmark", "The bookmark name you entered already exists in your list.", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); diff --git a/interface/src/avatar/AvatarPackager.cpp b/interface/src/avatar/AvatarPackager.cpp index 90def7ad43..d43b7d9575 100644 --- a/interface/src/avatar/AvatarPackager.cpp +++ b/interface/src/avatar/AvatarPackager.cpp @@ -58,7 +58,9 @@ bool AvatarPackager::open() { if (tablet->getToolbarMode()) { static const QUrl url{ "hifi/AvatarPackagerWindow.qml" }; - DependencyManager::get()->show(url, "AvatarPackager", packageModelDialogCreated); + if (auto offscreenUI = DependencyManager::get()) { + offscreenUI->show(url, "AvatarPackager", packageModelDialogCreated); + } return true; } diff --git a/interface/src/scripting/AssetMappingsScriptingInterface.cpp b/interface/src/scripting/AssetMappingsScriptingInterface.cpp index c5769ef4bb..a6fbd8b3fd 100644 --- a/interface/src/scripting/AssetMappingsScriptingInterface.cpp +++ b/interface/src/scripting/AssetMappingsScriptingInterface.cpp @@ -77,6 +77,11 @@ void AssetMappingsScriptingInterface::uploadFile(QString path, QString mapping, "Specifying a new folder name will automatically create that folder for you."; auto offscreenUi = DependencyManager::get(); + if (!offscreenUi) { + completedCallback.call({ -1 }); + return; + } + auto result = offscreenUi->inputDialog(OffscreenUi::ICON_INFORMATION, "Specify Asset Path", dropEvent ? dropHelpText : helpText, mapping); diff --git a/interface/src/scripting/DesktopScriptingInterface.cpp b/interface/src/scripting/DesktopScriptingInterface.cpp index f78f7853ca..e527561b05 100644 --- a/interface/src/scripting/DesktopScriptingInterface.cpp +++ b/interface/src/scripting/DesktopScriptingInterface.cpp @@ -99,11 +99,16 @@ void DesktopScriptingInterface::setHUDAlpha(float alpha) { } void DesktopScriptingInterface::show(const QString& path, const QString& title) { + auto offscreenUI = DependencyManager::get(); + if (!offscreenUI) { + return; + } + if (QThread::currentThread() != thread()) { QMetaObject::invokeMethod(this, "show", Qt::QueuedConnection, Q_ARG(QString, path), Q_ARG(QString, title)); return; } - DependencyManager::get()->show(path, title); + offscreenUI->show(path, title); } InteractiveWindowPointer DesktopScriptingInterface::createWindow(const QString& sourceUrl, const QVariantMap& properties) { diff --git a/interface/src/scripting/HMDScriptingInterface.cpp b/interface/src/scripting/HMDScriptingInterface.cpp index 8f7ae7c4dc..79c0452a45 100644 --- a/interface/src/scripting/HMDScriptingInterface.cpp +++ b/interface/src/scripting/HMDScriptingInterface.cpp @@ -96,8 +96,9 @@ bool HMDScriptingInterface::shouldShowHandControllers() const { void HMDScriptingInterface::activateHMDHandMouse() { QWriteLocker lock(&_hmdHandMouseLock); - auto offscreenUi = DependencyManager::get(); - offscreenUi->getDesktop()->setProperty("hmdHandMouseActive", true); + if (auto offscreenUI = DependencyManager::get()) { + offscreenUI->getDesktop()->setProperty("hmdHandMouseActive", true); + } _hmdHandMouseCount++; } @@ -105,8 +106,9 @@ void HMDScriptingInterface::deactivateHMDHandMouse() { QWriteLocker lock(&_hmdHandMouseLock); _hmdHandMouseCount = std::max(_hmdHandMouseCount - 1, 0); if (_hmdHandMouseCount == 0) { - auto offscreenUi = DependencyManager::get(); - offscreenUi->getDesktop()->setProperty("hmdHandMouseActive", false); + if (auto offscreenUI = DependencyManager::get()) { + offscreenUI->getDesktop()->setProperty("hmdHandMouseActive", false); + } } } diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp index e91b1d725c..144f64c385 100644 --- a/interface/src/ui/ApplicationOverlay.cpp +++ b/interface/src/ui/ApplicationOverlay.cpp @@ -100,10 +100,10 @@ void ApplicationOverlay::renderQmlUi(RenderArgs* renderArgs) { // threads, we need to use a sync object to deteremine when // the current UI texture is no longer being read from, and only // then release it back to the UI for re-use - auto offscreenUi = DependencyManager::get(); + auto offscreenUI = DependencyManager::get(); OffscreenQmlSurface::TextureAndFence newTextureAndFence; - bool newTextureAvailable = offscreenUi->fetchTexture(newTextureAndFence); + bool newTextureAvailable = offscreenUI ? offscreenUI->fetchTexture(newTextureAndFence) : false; if (newTextureAvailable) { _uiTexture->setExternalTexture(newTextureAndFence.first, newTextureAndFence.second); } diff --git a/interface/src/ui/InteractiveWindow.cpp b/interface/src/ui/InteractiveWindow.cpp index 0ac1f05737..420efbf448 100644 --- a/interface/src/ui/InteractiveWindow.cpp +++ b/interface/src/ui/InteractiveWindow.cpp @@ -362,10 +362,11 @@ InteractiveWindow::InteractiveWindow(const QString& sourceUrl, const QVariantMap object->setObjectName("InteractiveWindow"); object->setProperty(SOURCE_PROPERTY, sourceURL); }; - auto offscreenUi = DependencyManager::get(); - // Build the event bridge and wrapper on the main thread - offscreenUi->loadInNewContext(CONTENT_WINDOW_QML, objectInitLambda, contextInitLambda); + if (auto offscreenUi = DependencyManager::get()) { + // Build the event bridge and wrapper on the main thread + offscreenUi->loadInNewContext(CONTENT_WINDOW_QML, objectInitLambda, contextInitLambda); + } } } diff --git a/interface/src/ui/overlays/Overlays.cpp b/interface/src/ui/overlays/Overlays.cpp index 5e43c5df8d..e9e310e68b 100644 --- a/interface/src/ui/overlays/Overlays.cpp +++ b/interface/src/ui/overlays/Overlays.cpp @@ -1212,8 +1212,8 @@ float Overlays::width() { return result; } - auto offscreenUi = DependencyManager::get(); - return offscreenUi->getWindow()->size().width(); + auto offscreenUI = DependencyManager::get(); + return offscreenUI ? offscreenUI->getWindow()->size().width() : -1.0f; } float Overlays::height() { @@ -1224,8 +1224,8 @@ float Overlays::height() { return result; } - auto offscreenUi = DependencyManager::get(); - return offscreenUi->getWindow()->size().height(); + auto offscreenUI = DependencyManager::get(); + return offscreenUI ? offscreenUI->getWindow()->size().height() : -1.0f; } void Overlays::mousePressOnPointerEvent(const QUuid& id, const PointerEvent& event) { diff --git a/interface/src/ui/overlays/QmlOverlay.cpp b/interface/src/ui/overlays/QmlOverlay.cpp index 2afb29bb91..c097e7dd97 100644 --- a/interface/src/ui/overlays/QmlOverlay.cpp +++ b/interface/src/ui/overlays/QmlOverlay.cpp @@ -24,13 +24,17 @@ QmlOverlay::QmlOverlay(const QUrl& url, const QmlOverlay* overlay) } void QmlOverlay::buildQmlElement(const QUrl& url) { + auto offscreenUI = DependencyManager::get(); + if (!offscreenUI) { + return; + } + if (QThread::currentThread() != thread()) { QMetaObject::invokeMethod(this, "buildQmlElement", Q_ARG(QUrl, url)); return; } - auto offscreenUi = DependencyManager::get(); - offscreenUi->load(url, [=](QQmlContext* context, QObject* object) { + offscreenUI->load(url, [=](QQmlContext* context, QObject* object) { _qmlElement = dynamic_cast(object); connect(_qmlElement, &QObject::destroyed, this, &QmlOverlay::qmlElementDestroyed); }); diff --git a/libraries/ui/src/DockWidget.cpp b/libraries/ui/src/DockWidget.cpp index 13c4d9a548..f899def2c6 100644 --- a/libraries/ui/src/DockWidget.cpp +++ b/libraries/ui/src/DockWidget.cpp @@ -26,14 +26,15 @@ static void quickViewDeleter(QQuickView* quickView) { } DockWidget::DockWidget(const QString& title, QWidget* parent) : QDockWidget(title, parent) { - auto offscreenUi = DependencyManager::get(); - auto qmlEngine = offscreenUi->getSurfaceContext()->engine(); - _quickView = std::shared_ptr(new QQuickView(qmlEngine, nullptr), quickViewDeleter); - _quickView->setFormat(getDefaultOpenGLSurfaceFormat()); - QWidget* widget = QWidget::createWindowContainer(_quickView.get()); - setWidget(widget); - QWidget* headerWidget = new QWidget(); - setTitleBarWidget(headerWidget); + if (auto offscreenUI = DependencyManager::get()) { + auto qmlEngine = offscreenUI->getSurfaceContext()->engine(); + _quickView = std::shared_ptr(new QQuickView(qmlEngine, nullptr), quickViewDeleter); + _quickView->setFormat(getDefaultOpenGLSurfaceFormat()); + QWidget* widget = QWidget::createWindowContainer(_quickView.get()); + setWidget(widget); + QWidget* headerWidget = new QWidget(); + setTitleBarWidget(headerWidget); + } } void DockWidget::setSource(const QUrl& url) { diff --git a/libraries/ui/src/InfoView.cpp b/libraries/ui/src/InfoView.cpp index 478401c6f8..f98d514f5d 100644 --- a/libraries/ui/src/InfoView.cpp +++ b/libraries/ui/src/InfoView.cpp @@ -65,17 +65,18 @@ void InfoView::show(const QString& path, bool firstOrChangedOnly, QString urlQue } infoVersion.set(version); } - auto offscreenUi = DependencyManager::get(); - QString infoViewName(NAME + "_" + path); - offscreenUi->show(QML, NAME + "_" + path, [=](QQmlContext* context, QObject* newObject){ - QQuickItem* item = dynamic_cast(newObject); - item->setWidth(1024); - item->setHeight(720); - InfoView* newInfoView = newObject->findChild(); - Q_ASSERT(newInfoView); - newInfoView->parent()->setObjectName(infoViewName); - newInfoView->setUrl(url); - }); + if (auto offscreenUI = DependencyManager::get()) { + QString infoViewName(NAME + "_" + path); + offscreenUI->show(QML, NAME + "_" + path, [=](QQmlContext* context, QObject* newObject){ + QQuickItem* item = dynamic_cast(newObject); + item->setWidth(1024); + item->setHeight(720); + InfoView* newInfoView = newObject->findChild(); + Q_ASSERT(newInfoView); + newInfoView->parent()->setObjectName(infoViewName); + newInfoView->setUrl(url); + }); + } } QUrl InfoView::url() { diff --git a/libraries/ui/src/OffscreenQmlElement.h b/libraries/ui/src/OffscreenQmlElement.h index 69009533c6..ac1bcb0866 100644 --- a/libraries/ui/src/OffscreenQmlElement.h +++ b/libraries/ui/src/OffscreenQmlElement.h @@ -53,25 +53,30 @@ private: } \ \ void x::show(std::function f) { \ - auto offscreenUi = DependencyManager::get(); \ + auto offscreenUI = DependencyManager::get(); \ if (!registered) { \ x::registerType(); \ } \ - offscreenUi->show(QML, NAME, f); \ + if (offscreenUI) { \ + offscreenUI->show(QML, NAME, f); \ + } \ } \ \ void x::hide() { \ - auto offscreenUi = DependencyManager::get(); \ - offscreenUi->hide(NAME); \ + if (auto offscreenUI = DependencyManager::get()) { \ + offscreenUI->hide(NAME); \ + } \ } \ \ void x::toggle(std::function f) { \ - auto offscreenUi = DependencyManager::get(); \ - offscreenUi->toggle(QML, NAME, f); \ + if (auto offscreenUI = DependencyManager::get()) { \ + offscreenUI->toggle(QML, NAME, f); \ + } \ } \ void x::load(std::function f) { \ - auto offscreenUi = DependencyManager::get(); \ - offscreenUi->load(QML, f); \ + if (auto offscreenUI = DependencyManager::get()) { \ + offscreenUI->load(QML, f); \ + } \ } #define HIFI_QML_DEF_LAMBDA(x, f) \ @@ -82,21 +87,25 @@ private: qmlRegisterType("Hifi", 1, 0, NAME.toLocal8Bit().constData()); \ } \ void x::show() { \ - auto offscreenUi = DependencyManager::get(); \ - offscreenUi->show(QML, NAME, f); \ + if (auto offscreenUI = DependencyManager::get()) { \ + offscreenUI->show(QML, NAME, f); \ + } \ } \ void x::hide() { \ - auto offscreenUi = DependencyManager::get(); \ - offscreenUi->hide(NAME); \ + if (auto offscreenUI = DependencyManager::get()) { \ + offscreenUI->hide(NAME); \ + } \ } \ \ void x::toggle() { \ - auto offscreenUi = DependencyManager::get(); \ - offscreenUi->toggle(QML, NAME, f); \ + if (auto offscreenUI = DependencyManager::get()) { \ + offscreenUI->toggle(QML, NAME, f); \ + } \ } \ void x::load() { \ - auto offscreenUi = DependencyManager::get(); \ - offscreenUi->load(QML, f); \ + if (auto offscreenUI = DependencyManager::get()) { \ + offscreenUI->load(QML, f); \ + } \ } #endif diff --git a/libraries/ui/src/QmlFragmentClass.cpp b/libraries/ui/src/QmlFragmentClass.cpp index fbd045fdb1..1219094afc 100644 --- a/libraries/ui/src/QmlFragmentClass.cpp +++ b/libraries/ui/src/QmlFragmentClass.cpp @@ -14,9 +14,6 @@ #include -#include "OffscreenUi.h" - - std::mutex QmlFragmentClass::_mutex; std::map QmlFragmentClass::_fragments; @@ -40,7 +37,6 @@ QScriptValue QmlFragmentClass::internal_constructor(QScriptContext* context, QSc } auto properties = parseArguments(context); - auto offscreenUi = DependencyManager::get(); QmlFragmentClass* retVal = new QmlFragmentClass(restricted, qml.toString()); Q_ASSERT(retVal); if (QThread::currentThread() != qApp->thread()) { diff --git a/libraries/ui/src/QmlWebWindowClass.cpp b/libraries/ui/src/QmlWebWindowClass.cpp index 282161497a..c7851d416f 100644 --- a/libraries/ui/src/QmlWebWindowClass.cpp +++ b/libraries/ui/src/QmlWebWindowClass.cpp @@ -14,7 +14,6 @@ #include #include -#include "OffscreenUi.h" static const char* const URL_PROPERTY = "source"; static const char* const SCRIPT_PROPERTY = "scriptUrl"; @@ -22,7 +21,6 @@ static const char* const SCRIPT_PROPERTY = "scriptUrl"; // Method called by Qt scripts to create a new web window in the overlay QScriptValue QmlWebWindowClass::internal_constructor(QScriptContext* context, QScriptEngine* engine, bool restricted) { auto properties = parseArguments(context); - auto offscreenUi = DependencyManager::get(); QmlWebWindowClass* retVal = new QmlWebWindowClass(restricted); Q_ASSERT(retVal); if (QThread::currentThread() != qApp->thread()) { diff --git a/libraries/ui/src/QmlWindowClass.cpp b/libraries/ui/src/QmlWindowClass.cpp index 13a289a5fd..ae2292dc09 100644 --- a/libraries/ui/src/QmlWindowClass.cpp +++ b/libraries/ui/src/QmlWindowClass.cpp @@ -72,7 +72,6 @@ QVariantMap QmlWindowClass::parseArguments(QScriptContext* context) { // Method called by Qt scripts to create a new web window in the overlay QScriptValue QmlWindowClass::internal_constructor(QScriptContext* context, QScriptEngine* engine, bool restricted) { auto properties = parseArguments(context); - auto offscreenUi = DependencyManager::get(); QmlWindowClass* retVal = new QmlWindowClass(restricted); Q_ASSERT(retVal); if (QThread::currentThread() != qApp->thread()) { @@ -349,7 +348,6 @@ void QmlWindowClass::raise() { return; } - auto offscreenUi = DependencyManager::get(); if (_qmlWindow) { QMetaObject::invokeMethod(asQuickItem(), "raise", Qt::DirectConnection); } diff --git a/plugins/openvr/src/OpenVrHelpers.cpp b/plugins/openvr/src/OpenVrHelpers.cpp index ce7625eedb..eed2242602 100644 --- a/plugins/openvr/src/OpenVrHelpers.cpp +++ b/plugins/openvr/src/OpenVrHelpers.cpp @@ -203,14 +203,14 @@ void updateFromOpenVrKeyboardInput() { } void finishOpenVrKeyboardInput() { - auto offscreenUi = DependencyManager::get(); + auto offscreenUI = DependencyManager::get(); updateFromOpenVrKeyboardInput(); // Simulate an enter press on the top level window to trigger the action - if (0 == (_currentHints & Qt::ImhMultiLine)) { + if (0 == (_currentHints & Qt::ImhMultiLine) && offscreenUI) { auto keyPress = QKeyEvent(QEvent::KeyPress, Qt::Key_Return, Qt::KeyboardModifiers(), QString("\n")); auto keyRelease = QKeyEvent(QEvent::KeyRelease, Qt::Key_Return, Qt::KeyboardModifiers()); - qApp->sendEvent(offscreenUi->getWindow(), &keyPress); - qApp->sendEvent(offscreenUi->getWindow(), &keyRelease); + qApp->sendEvent(offscreenUI->getWindow(), &keyPress); + qApp->sendEvent(offscreenUI->getWindow(), &keyRelease); } } @@ -221,10 +221,8 @@ void enableOpenVrKeyboard(PluginContainer* container) { if (disableSteamVrKeyboard) { return; } - auto offscreenUi = DependencyManager::get(); _overlay = vr::VROverlay(); - auto menu = container->getPrimaryMenu(); auto action = menu->getActionForOption(MenuOption::Overlays); @@ -282,7 +280,9 @@ void handleOpenVrEvents() { case vr::VREvent_KeyboardClosed: _keyboardFocusObject = nullptr; _keyboardShown = false; - DependencyManager::get()->unfocusWindows(); + if (auto offscreenUI = DependencyManager::get()) { + offscreenUI->unfocusWindows(); + } break; default: From ff6f7233e499d8a021501586fe4c5580000a730b Mon Sep 17 00:00:00 2001 From: HifiExperiments Date: Thu, 21 Jan 2021 17:38:55 -0800 Subject: [PATCH 4/8] I'm a big dumb idiot --- .../scripting/WindowScriptingInterface.cpp | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/interface/src/scripting/WindowScriptingInterface.cpp b/interface/src/scripting/WindowScriptingInterface.cpp index 14a0d04023..98335e5d3a 100644 --- a/interface/src/scripting/WindowScriptingInterface.cpp +++ b/interface/src/scripting/WindowScriptingInterface.cpp @@ -199,9 +199,9 @@ void WindowScriptingInterface::setInterstitialModeEnabled(bool enableInterstitia DependencyManager::get()->getDomainHandler().setInterstitialModeEnabled(enableInterstitialMode); } -bool WindowScriptingInterface::isPointOnDesktopWindow(QVariant point) { - auto offscreenUi = DependencyManager::get(); - return offscreenUi->isPointOnDesktopWindow(point); +bool WindowScriptingInterface::isPointOnDesktopWindow(QVariant point) { + auto offscreenUI = DependencyManager::get(); + return offscreenUI ? offscreenUI->isPointOnDesktopWindow(point) : false; } /// Makes sure that the reticle is visible, use this in blocking forms that require a reticle and @@ -553,12 +553,14 @@ int WindowScriptingInterface::openMessageBox(QString title, QString text, int bu * @typedef {number} Window.MessageBoxButton */ int WindowScriptingInterface::createMessageBox(QString title, QString text, int buttons, int defaultButton) { - auto messageBox = DependencyManager::get()->createMessageBox(OffscreenUi::ICON_INFORMATION, title, text, - static_cast>(buttons), static_cast(defaultButton)); - connect(messageBox, SIGNAL(selected(int)), this, SLOT(onMessageBoxSelected(int))); + if (auto offscreenUI = DependencyManager::get()) { + auto messageBox = offscreenUI->createMessageBox(OffscreenUi::ICON_INFORMATION, title, text, + static_cast>(buttons), static_cast(defaultButton)); + connect(messageBox, SIGNAL(selected(int)), this, SLOT(onMessageBoxSelected(int))); - _lastMessageBoxID += 1; - _messageBoxes.insert(_lastMessageBoxID, messageBox); + _lastMessageBoxID += 1; + _messageBoxes.insert(_lastMessageBoxID, messageBox); + } return _lastMessageBoxID; } @@ -646,13 +648,17 @@ void WindowScriptingInterface::setActiveDisplayPlugin(int index) { } void WindowScriptingInterface::openWebBrowser(const QString& url) { + auto offscreenUI = DependencyManager::get(); + if (!offscreenUI) { + return; + } + if (QThread::currentThread() != thread()) { QMetaObject::invokeMethod(this, "openWebBrowser", Q_ARG(const QString&, url)); return; } - auto offscreenUi = DependencyManager::get(); - offscreenUi->load("Browser.qml", [=](QQmlContext* context, QObject* newObject) { + offscreenUI->load("Browser.qml", [=](QQmlContext* context, QObject* newObject) { if (!url.isEmpty()) { newObject->setProperty("url", url); } From 64e9cf688473e1661c5873c2b1bc8105b66e850c Mon Sep 17 00:00:00 2001 From: HifiExperiments <53453710+HifiExperiments@users.noreply.github.com> Date: Fri, 29 Jan 2021 17:10:56 -0800 Subject: [PATCH 5/8] Update interface/src/ui/InteractiveWindow.cpp Co-authored-by: David Rowe --- interface/src/ui/InteractiveWindow.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interface/src/ui/InteractiveWindow.cpp b/interface/src/ui/InteractiveWindow.cpp index 420efbf448..daf80acf00 100644 --- a/interface/src/ui/InteractiveWindow.cpp +++ b/interface/src/ui/InteractiveWindow.cpp @@ -363,9 +363,9 @@ InteractiveWindow::InteractiveWindow(const QString& sourceUrl, const QVariantMap object->setProperty(SOURCE_PROPERTY, sourceURL); }; - if (auto offscreenUi = DependencyManager::get()) { + if (auto offscreenUI = DependencyManager::get()) { // Build the event bridge and wrapper on the main thread - offscreenUi->loadInNewContext(CONTENT_WINDOW_QML, objectInitLambda, contextInitLambda); + offscreenUI->loadInNewContext(CONTENT_WINDOW_QML, objectInitLambda, contextInitLambda); } } } From 3d9142d8cb7ed016ff2ce49c4b5fa82bef8bdd31 Mon Sep 17 00:00:00 2001 From: HifiExperiments <53453710+HifiExperiments@users.noreply.github.com> Date: Fri, 29 Jan 2021 17:11:10 -0800 Subject: [PATCH 6/8] Update libraries/ui/src/InfoView.cpp Co-authored-by: David Rowe --- libraries/ui/src/InfoView.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ui/src/InfoView.cpp b/libraries/ui/src/InfoView.cpp index f98d514f5d..c14ff6bf64 100644 --- a/libraries/ui/src/InfoView.cpp +++ b/libraries/ui/src/InfoView.cpp @@ -67,7 +67,7 @@ void InfoView::show(const QString& path, bool firstOrChangedOnly, QString urlQue } if (auto offscreenUI = DependencyManager::get()) { QString infoViewName(NAME + "_" + path); - offscreenUI->show(QML, NAME + "_" + path, [=](QQmlContext* context, QObject* newObject){ + offscreenUI->show(QML, NAME + "_" + path, [=] (QQmlContext* context, QObject* newObject) { QQuickItem* item = dynamic_cast(newObject); item->setWidth(1024); item->setHeight(720); From bae12e4e39f6c952b7fd29b1d10bbdcae75bb943 Mon Sep 17 00:00:00 2001 From: HifiExperiments <53453710+HifiExperiments@users.noreply.github.com> Date: Fri, 29 Jan 2021 17:11:24 -0800 Subject: [PATCH 7/8] Update interface/src/scripting/AssetMappingsScriptingInterface.cpp Co-authored-by: David Rowe --- interface/src/scripting/AssetMappingsScriptingInterface.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interface/src/scripting/AssetMappingsScriptingInterface.cpp b/interface/src/scripting/AssetMappingsScriptingInterface.cpp index a6fbd8b3fd..f5087bff45 100644 --- a/interface/src/scripting/AssetMappingsScriptingInterface.cpp +++ b/interface/src/scripting/AssetMappingsScriptingInterface.cpp @@ -76,8 +76,8 @@ void AssetMappingsScriptingInterface::uploadFile(QString path, QString mapping, "Use the field below to place your file in a specific folder or to rename it. " "Specifying a new folder name will automatically create that folder for you."; - auto offscreenUi = DependencyManager::get(); - if (!offscreenUi) { + auto offscreenUI = DependencyManager::get(); + if (!offscreenUI) { completedCallback.call({ -1 }); return; } From f2c945284608dfcb35cb6688c573cb05acfd43db Mon Sep 17 00:00:00 2001 From: HifiExperiments <53453710+HifiExperiments@users.noreply.github.com> Date: Fri, 29 Jan 2021 22:12:48 -0800 Subject: [PATCH 8/8] Update AssetMappingsScriptingInterface.cpp --- interface/src/scripting/AssetMappingsScriptingInterface.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/interface/src/scripting/AssetMappingsScriptingInterface.cpp b/interface/src/scripting/AssetMappingsScriptingInterface.cpp index f5087bff45..5b90474d23 100644 --- a/interface/src/scripting/AssetMappingsScriptingInterface.cpp +++ b/interface/src/scripting/AssetMappingsScriptingInterface.cpp @@ -82,7 +82,7 @@ void AssetMappingsScriptingInterface::uploadFile(QString path, QString mapping, return; } - auto result = offscreenUi->inputDialog(OffscreenUi::ICON_INFORMATION, "Specify Asset Path", + auto result = offscreenUI->inputDialog(OffscreenUi::ICON_INFORMATION, "Specify Asset Path", dropEvent ? dropHelpText : helpText, mapping); if (!result.isValid() || result.toString() == "") { @@ -99,7 +99,7 @@ void AssetMappingsScriptingInterface::uploadFile(QString path, QString mapping, // Check for override if (isKnownMapping(mapping)) { auto message = mapping + "\n" + "This file already exists. Do you want to overwrite it?"; - auto button = offscreenUi->messageBox(OffscreenUi::ICON_QUESTION, "Overwrite File", message, + auto button = offscreenUI->messageBox(OffscreenUi::ICON_QUESTION, "Overwrite File", message, QMessageBox::Yes | QMessageBox::No, QMessageBox::No); if (button == QMessageBox::No) { completedCallback.call({ -1 });