add popover & head nodes

This commit is contained in:
王劲鹏 2021-04-22 20:46:00 +08:00 committed by osborn
parent 80b97772d9
commit c0ce3b3ae9
6 changed files with 187 additions and 0 deletions

View File

@ -92,3 +92,32 @@ DoricViewNode *DoricContext::targetViewNode(QString id) {
} }
QString DoricContext::getContextId() { return mContextId; } QString DoricContext::getContextId() { return mContextId; }
QList<DoricViewNode *> DoricContext::allHeadNodes(QString type) {
return mHeadNodes[type].values();
}
void DoricContext::addHeadNode(QString type, DoricViewNode *viewNode) {
if (mHeadNodes.contains(type)) {
QMap<QString, DoricViewNode *> map = mHeadNodes[type];
map.insert(viewNode->getId(), viewNode);
} else {
QMap<QString, DoricViewNode *> map;
map.insert(viewNode->getId(), viewNode);
mHeadNodes.insert(type, map);
}
}
void DoricContext::removeHeadNode(QString type, DoricViewNode *viewNode) {
if (mHeadNodes.contains(type)) {
QMap<QString, DoricViewNode *> map = mHeadNodes[type];
map.remove(viewNode->getId());
}
}
void DoricContext::clearHeadNodes(QString type) {
if (mHeadNodes.contains(type)) {
QMap<QString, DoricViewNode *> map = mHeadNodes[type];
map.clear();
}
}

View File

@ -21,6 +21,8 @@ private:
DoricInterfaceDriver *driver = NULL; DoricInterfaceDriver *driver = NULL;
QQmlEngine *mQmlEngine; QQmlEngine *mQmlEngine;
QMap<QString, QMap<QString, DoricViewNode *>> mHeadNodes;
public: public:
DoricContext(QString contextId, QString source, QString extra); DoricContext(QString contextId, QString source, QString extra);
@ -47,6 +49,14 @@ public:
DoricViewNode *targetViewNode(QString id); DoricViewNode *targetViewNode(QString id);
QString getContextId(); QString getContextId();
QList<DoricViewNode *> allHeadNodes(QString type);
void addHeadNode(QString type, DoricViewNode *viewNode);
void removeHeadNode(QString type, DoricViewNode *viewNode);
void clearHeadNodes(QString type);
}; };
#endif // CONTEXT_H #endif // CONTEXT_H

View File

@ -1,6 +1,7 @@
#include "DoricRegistry.h" #include "DoricRegistry.h"
#include "plugin/DoricModalPlugin.h" #include "plugin/DoricModalPlugin.h"
#include "plugin/DoricPopoverPlugin.h"
#include "plugin/DoricShaderPlugin.h" #include "plugin/DoricShaderPlugin.h"
#include "shader/DoricHLayoutNode.h" #include "shader/DoricHLayoutNode.h"
@ -13,6 +14,7 @@
DoricRegistry::DoricRegistry() { DoricRegistry::DoricRegistry() {
registerNativePlugin<DoricShaderPlugin>("shader"); registerNativePlugin<DoricShaderPlugin>("shader");
registerNativePlugin<DoricModalPlugin>("modal"); registerNativePlugin<DoricModalPlugin>("modal");
registerNativePlugin<DoricPopoverPlugin>("popover");
registerViewNode<DoricRootNode>("Root"); registerViewNode<DoricRootNode>("Root");
registerViewNode<DoricStackNode>("Stack"); registerViewNode<DoricStackNode>("Stack");

View File

@ -35,6 +35,7 @@ SOURCES += \
engine/v8/V8Executor.cpp \ engine/v8/V8Executor.cpp \
main.cpp \ main.cpp \
plugin/DoricModalPlugin.cpp \ plugin/DoricModalPlugin.cpp \
plugin/DoricPopoverPlugin.cpp \
plugin/DoricShaderPlugin.cpp \ plugin/DoricShaderPlugin.cpp \
shader/DoricGroupNode.cpp \ shader/DoricGroupNode.cpp \
shader/DoricHLayoutNode.cpp \ shader/DoricHLayoutNode.cpp \
@ -105,6 +106,7 @@ HEADERS += \
engine/v8/V8Executor.h \ engine/v8/V8Executor.h \
plugin/DoricModalPlugin.h \ plugin/DoricModalPlugin.h \
plugin/DoricNativePlugin.h \ plugin/DoricNativePlugin.h \
plugin/DoricPopoverPlugin.h \
plugin/DoricShaderPlugin.h \ plugin/DoricShaderPlugin.h \
shader/DoricGroupNode.h \ shader/DoricGroupNode.h \
shader/DoricHLayoutNode.h \ shader/DoricHLayoutNode.h \

View File

@ -0,0 +1,117 @@
#include "DoricPopoverPlugin.h"
#include "engine/DoricPromise.h"
#include "shader/DoricRootNode.h"
#include "shader/DoricViewNode.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QQuickWindow>
void DoricPopoverPlugin::show(QString jsValueString, QString callbackId) {
getContext()->getDriver()->asyncCall(
[this, jsValueString, callbackId] {
QJsonDocument document =
QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
QQuickItem *rootItem =
getContext()->getRootNode()->getRootView()->window()->contentItem();
if (this->fullScreenView == nullptr) {
QQmlComponent component(getContext()->getQmlEngine());
const QUrl url(QStringLiteral("qrc:/doric/qml/stack.qml"));
component.loadUrl(url);
if (component.isError()) {
qCritical() << component.errorString();
}
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
item->setWidth(rootItem->width());
item->setHeight(rootItem->height());
DoricLayouts *layout = new DoricLayouts();
layout->setWidth(item->width());
layout->setHeight(item->height());
layout->setView(item);
layout->setLayoutType(DoricLayoutType::DoricStack);
item->setProperty("doricLayout", QString::number((qint64)layout));
item->setParentItem(rootItem);
this->fullScreenView = item;
} else {
DoricLayouts *layout =
(DoricLayouts *)(this->fullScreenView->property("doricLayout")
.toULongLong());
layout->setWidth(rootItem->width());
layout->setHeight(rootItem->height());
}
this->fullScreenView->setVisible(true);
QString viewId = jsValue["id"].toString();
QString type = jsValue["type"].toString();
DoricViewNode *viewNode = getContext()->targetViewNode(viewId);
if (viewNode == nullptr) {
viewNode = DoricViewNode::create(getContext(), type);
viewNode->setId(viewId);
viewNode->init(nullptr);
viewNode->getNodeView()->setParentItem(this->fullScreenView);
}
viewNode->blend(jsValue["props"]);
DoricLayouts *layout =
(DoricLayouts *)(this->fullScreenView->property("doricLayout")
.toULongLong());
layout->apply();
getContext()->addHeadNode(TYPE, viewNode);
QVariantList args;
DoricPromise::resolve(getContext(), callbackId, args);
},
DoricThreadMode::UI);
}
void DoricPopoverPlugin::dismiss(QString jsValueString, QString callbackId) {
getContext()->getDriver()->asyncCall(
[this, jsValueString] {
QJsonDocument document =
QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
if (jsValue.toObject().contains("id")) {
QString viewId = jsValue["id"].toString();
DoricViewNode *viewNode = getContext()->targetViewNode(viewId);
this->dismissViewNode(viewNode);
} else {
this->dismissPopover();
}
},
DoricThreadMode::UI);
}
void DoricPopoverPlugin::dismissViewNode(DoricViewNode *viewNode) {
if (viewNode != nullptr) {
getContext()->removeHeadNode(TYPE, viewNode);
viewNode->getNodeView()->setParent(nullptr);
viewNode->getNodeView()->setParentItem(nullptr);
viewNode->getNodeView()->deleteLater();
}
if (getContext()->allHeadNodes(TYPE).size() == 0) {
this->fullScreenView->setVisible(false);
}
}
void DoricPopoverPlugin::dismissPopover() {
foreach (DoricViewNode *node, getContext()->allHeadNodes(TYPE)) {
dismissViewNode(node);
}
}

View File

@ -0,0 +1,27 @@
#ifndef DORICPOPOVERPLUGIN_H
#define DORICPOPOVERPLUGIN_H
#include "DoricNativePlugin.h"
#include <QQuickItem>
static QString TYPE = "popover";
class DoricPopoverPlugin : public DoricNativePlugin {
Q_OBJECT
public:
using DoricNativePlugin::DoricNativePlugin;
Q_INVOKABLE void show(QString jsValueString, QString callbackId);
Q_INVOKABLE void dismiss(QString jsValueString, QString callbackId);
private:
QQuickItem *fullScreenView = nullptr;
void dismissViewNode(DoricViewNode *node);
void dismissPopover();
};
#endif // DORICPOPOVERPLUGIN_H