destroy context when qquickview hidden

This commit is contained in:
王劲鹏 2021-04-09 16:39:43 +08:00 committed by osborn
parent 7402fc2c0c
commit d9828dca93
13 changed files with 56 additions and 6 deletions

View File

@ -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; }

View File

@ -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

View File

@ -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());
}

View File

@ -25,6 +25,8 @@ public:
DoricContext *createContext(QString script, QString source, QString extra);
DoricContext *getContext(QString contextId);
void destroyContext(DoricContext *context);
};
#endif // CONTEXTMANAGER_H

View File

@ -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<void()> 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();

View File

@ -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);

View File

@ -17,6 +17,8 @@ private:
public:
DoricPanel(QQmlEngine *qmlEngine, QQuickItem *quickItem);
~DoricPanel();
void config(QString script, QString alias, QString extra);
void config(DoricContext *context);

View File

@ -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();

View File

@ -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);
}

View File

@ -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();
};

View File

@ -3,7 +3,7 @@ import QtQuick.Controls 2.5
ApplicationWindow {
visible: true
width: 960
width: 405
height: 720
title: qsTr("Scroll")

View File

@ -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__";

View File

@ -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