From d9828dca9313f3d7dd3e06beb3162674d22d3c6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Fri, 9 Apr 2021 16:39:43 +0800 Subject: [PATCH] destroy context when qquickview hidden --- doric-Qt/doric/DoricContext.cpp | 8 ++++++++ doric-Qt/doric/DoricContext.h | 4 ++++ doric-Qt/doric/DoricContextManager.cpp | 5 +++++ doric-Qt/doric/DoricContextManager.h | 2 ++ doric-Qt/doric/DoricNativeDriver.cpp | 11 +++++++---- doric-Qt/doric/DoricPanel.cpp | 4 ++++ doric-Qt/doric/DoricPanel.h | 2 ++ doric-Qt/doric/demo/DoricDemoBridge.cpp | 12 +++++++++++- doric-Qt/doric/engine/DoricJSEngine.cpp | 6 ++++++ doric-Qt/doric/engine/DoricJSEngine.h | 4 ++++ doric-Qt/doric/resources/main.qml | 2 +- doric-Qt/doric/utils/DoricConstant.cpp | 1 + doric-Qt/doric/utils/DoricConstant.h | 1 + 13 files changed, 56 insertions(+), 6 deletions(-) diff --git a/doric-Qt/doric/DoricContext.cpp b/doric-Qt/doric/DoricContext.cpp index 0ccdbbf0..244c909b 100644 --- a/doric-Qt/doric/DoricContext.cpp +++ b/doric-Qt/doric/DoricContext.cpp @@ -15,6 +15,12 @@ DoricContext::DoricContext(QString contextId, QString source, QString extra) { this->extra = extra; } +DoricContext::~DoricContext() { + QVariantList args; + callEntity(DoricConstant::DORIC_ENTITY_DESTROY, args); + DoricContextManager::getInstance()->destroyContext(this); +} + DoricContext *DoricContext::create(QString script, QString source, QString extra) { DoricContext *context = @@ -84,3 +90,5 @@ DoricViewNode *DoricContext::targetViewNode(QString id) { } return nullptr; } + +QString DoricContext::getContextId() { return mContextId; } diff --git a/doric-Qt/doric/DoricContext.h b/doric-Qt/doric/DoricContext.h index 1f752f41..feccb8c5 100644 --- a/doric-Qt/doric/DoricContext.h +++ b/doric-Qt/doric/DoricContext.h @@ -24,6 +24,8 @@ private: public: DoricContext(QString contextId, QString source, QString extra); + ~DoricContext(); + static DoricContext *create(QString script, QString source, QString extra); void init(QString initData); @@ -43,6 +45,8 @@ public: QQmlEngine *getQmlEngine(); DoricViewNode *targetViewNode(QString id); + + QString getContextId(); }; #endif // CONTEXT_H diff --git a/doric-Qt/doric/DoricContextManager.cpp b/doric-Qt/doric/DoricContextManager.cpp index 7138648a..75c4337c 100644 --- a/doric-Qt/doric/DoricContextManager.cpp +++ b/doric-Qt/doric/DoricContextManager.cpp @@ -14,3 +14,8 @@ DoricContext *DoricContextManager::createContext(QString script, QString source, DoricContext *DoricContextManager::getContext(QString contextId) { return contextMap->value(contextId); } + +void DoricContextManager::destroyContext(DoricContext *context) { + context->getDriver()->destroyContext(context->getContextId()); + contextMap->remove(context->getContextId()); +} diff --git a/doric-Qt/doric/DoricContextManager.h b/doric-Qt/doric/DoricContextManager.h index f1e0d60b..b958698d 100644 --- a/doric-Qt/doric/DoricContextManager.h +++ b/doric-Qt/doric/DoricContextManager.h @@ -25,6 +25,8 @@ public: DoricContext *createContext(QString script, QString source, QString extra); DoricContext *getContext(QString contextId); + + void destroyContext(DoricContext *context); }; #endif // CONTEXTMANAGER_H diff --git a/doric-Qt/doric/DoricNativeDriver.cpp b/doric-Qt/doric/DoricNativeDriver.cpp index 9b09b61b..84a5b024 100644 --- a/doric-Qt/doric/DoricNativeDriver.cpp +++ b/doric-Qt/doric/DoricNativeDriver.cpp @@ -14,9 +14,8 @@ void DoricNativeDriver::invokeContextEntityMethod(QString contextId, void DoricNativeDriver::invokeDoricMethod(QString method, QVariantList args) { return DoricAsyncCall::ensureRunInThreadPool( - &jsEngine.mJSThreadPool, [this, method, args] { - this->jsEngine.invokeDoricMethod(method, args); - }); + &jsEngine.mJSThreadPool, + [this, method, args] { this->jsEngine.invokeDoricMethod(method, args); }); } DoricAsyncResult *DoricNativeDriver::asyncCall(std::function lambda, @@ -40,7 +39,11 @@ void DoricNativeDriver::createContext(QString contextId, QString script, }); } -void DoricNativeDriver::destroyContext(QString contextId) {} +void DoricNativeDriver::destroyContext(QString contextId) { + DoricAsyncCall::ensureRunInThreadPool( + &jsEngine.mJSThreadPool, + [this, contextId] { this->jsEngine.destroyContext(contextId); }); +} DoricRegistry *DoricNativeDriver::getRegistry() { return this->jsEngine.getRegistry(); diff --git a/doric-Qt/doric/DoricPanel.cpp b/doric-Qt/doric/DoricPanel.cpp index 47a8bc46..93a687b9 100644 --- a/doric-Qt/doric/DoricPanel.cpp +++ b/doric-Qt/doric/DoricPanel.cpp @@ -6,6 +6,10 @@ DoricPanel::DoricPanel(QQmlEngine *qmlEngine, QQuickItem *quickItem) { mQuickItem = quickItem; } +DoricPanel::~DoricPanel() { + delete mContext; +} + void DoricPanel::config(QString script, QString alias, QString extra) { DoricContext *context = DoricContext::create(script, alias, extra); config(context); diff --git a/doric-Qt/doric/DoricPanel.h b/doric-Qt/doric/DoricPanel.h index 729c16bd..eb8b45ad 100644 --- a/doric-Qt/doric/DoricPanel.h +++ b/doric-Qt/doric/DoricPanel.h @@ -17,6 +17,8 @@ private: public: DoricPanel(QQmlEngine *qmlEngine, QQuickItem *quickItem); + ~DoricPanel(); + void config(QString script, QString alias, QString extra); void config(DoricContext *context); diff --git a/doric-Qt/doric/demo/DoricDemoBridge.cpp b/doric-Qt/doric/demo/DoricDemoBridge.cpp index e70afbb0..a487ba52 100644 --- a/doric-Qt/doric/demo/DoricDemoBridge.cpp +++ b/doric-Qt/doric/demo/DoricDemoBridge.cpp @@ -4,8 +4,8 @@ #include "DoricDemoBridge.h" #include "DoricPanel.h" -#include "utils/DoricUtils.h" #include "utils/DoricMouseAreaBridge.h" +#include "utils/DoricUtils.h" DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {} @@ -33,6 +33,10 @@ void DoricDemoBridge::navigate(QVariant route) { view->setSource(url); view->setWidth(405); view->setHeight(720); + Qt::WindowFlags flag = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | + Qt::WindowTitleHint | Qt::WindowCloseButtonHint | + Qt::CustomizeWindowHint | Qt::WindowSystemMenuHint; + view->setFlags(flag); } { @@ -46,6 +50,12 @@ void DoricDemoBridge::navigate(QVariant route) { quickItem->setParentItem(view->rootObject()); panel->config(script, name, NULL); + + connect(view, &QQuickView::visibleChanged, this, [view, panel]() { + if (!view->isVisible()) { + delete panel; + } + }); } view->show(); diff --git a/doric-Qt/doric/engine/DoricJSEngine.cpp b/doric-Qt/doric/engine/DoricJSEngine.cpp index ce758407..9b0db8ce 100644 --- a/doric-Qt/doric/engine/DoricJSEngine.cpp +++ b/doric-Qt/doric/engine/DoricJSEngine.cpp @@ -100,6 +100,12 @@ void DoricJSEngine::prepareContext(QString contextId, QString script, mJSE->loadJS(packageContextScript(contextId, script), "Context://" + source); } +void DoricJSEngine::destroyContext(QString contextId) { + QString script = + QString(DoricConstant::TEMPLATE_CONTEXT_DESTROY).replace("%s", contextId); + mJSE->loadJS(script, "_Context://" + contextId); +} + void DoricJSEngine::invokeDoricMethod(QString method, QVariantList arguments) { return mJSE->invokeObject(DoricConstant::GLOBAL_DORIC, method, arguments); } diff --git a/doric-Qt/doric/engine/DoricJSEngine.h b/doric-Qt/doric/engine/DoricJSEngine.h index 979a6779..c1606280 100644 --- a/doric-Qt/doric/engine/DoricJSEngine.h +++ b/doric-Qt/doric/engine/DoricJSEngine.h @@ -25,7 +25,11 @@ public: ~DoricJSEngine(); void prepareContext(QString contextId, QString script, QString source); + + void destroyContext(QString contextId); + void invokeDoricMethod(QString method, QVariantList arguments); + DoricRegistry *getRegistry(); }; diff --git a/doric-Qt/doric/resources/main.qml b/doric-Qt/doric/resources/main.qml index a75d0494..3745788e 100644 --- a/doric-Qt/doric/resources/main.qml +++ b/doric-Qt/doric/resources/main.qml @@ -3,7 +3,7 @@ import QtQuick.Controls 2.5 ApplicationWindow { visible: true - width: 960 + width: 405 height: 720 title: qsTr("Scroll") diff --git a/doric-Qt/doric/utils/DoricConstant.cpp b/doric-Qt/doric/utils/DoricConstant.cpp index d36cf750..6546fa01 100644 --- a/doric-Qt/doric/utils/DoricConstant.cpp +++ b/doric-Qt/doric/utils/DoricConstant.cpp @@ -33,3 +33,4 @@ const QString DoricConstant::DORIC_ENTITY_RESPONSE = "__response__"; const QString DoricConstant::DORIC_ENTITY_INIT = "__init__"; const QString DoricConstant::DORIC_ENTITY_CREATE = "__onCreate__"; const QString DoricConstant::DORIC_ENTITY_BUILD = "__build__"; +const QString DoricConstant::DORIC_ENTITY_DESTROY = "__onDestroy__"; diff --git a/doric-Qt/doric/utils/DoricConstant.h b/doric-Qt/doric/utils/DoricConstant.h index edb09d5f..d97b2aa4 100644 --- a/doric-Qt/doric/utils/DoricConstant.h +++ b/doric-Qt/doric/utils/DoricConstant.h @@ -30,6 +30,7 @@ public: static const QString DORIC_ENTITY_CREATE; static const QString DORIC_ENTITY_INIT; static const QString DORIC_ENTITY_BUILD; + static const QString DORIC_ENTITY_DESTROY; }; #endif // CONSTANT_H