From 23522c641609e3e1c22c453826a85e61eac85bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Wed, 9 Jun 2021 16:46:33 +0800 Subject: [PATCH] navigator plugin implement push --- doric-Qt/example/doric/DoricContext.cpp | 6 +- doric-Qt/example/doric/DoricContext.h | 8 +-- doric-Qt/example/doric/DoricPanel.cpp | 7 +- doric-Qt/example/doric/DoricPanel.h | 4 +- doric-Qt/example/doric/DoricRegistry.cpp | 2 + doric-Qt/example/doric/doric.pro | 2 + .../doric/plugin/DoricNavigatorPlugin.cpp | 66 +++++++++++++++++++ .../doric/plugin/DoricNavigatorPlugin.h | 25 +++++++ 8 files changed, 108 insertions(+), 12 deletions(-) create mode 100644 doric-Qt/example/doric/plugin/DoricNavigatorPlugin.cpp create mode 100644 doric-Qt/example/doric/plugin/DoricNavigatorPlugin.h diff --git a/doric-Qt/example/doric/DoricContext.cpp b/doric-Qt/example/doric/DoricContext.cpp index 0123e5ad..975f37c8 100644 --- a/doric-Qt/example/doric/DoricContext.cpp +++ b/doric-Qt/example/doric/DoricContext.cpp @@ -83,9 +83,11 @@ QObject *DoricContext::obtainPlugin(QString name) { } } -void DoricContext::setQmlEngine(QQmlEngine *engine) { mQmlEngine = engine; } +void DoricContext::setQmlEngine(QQmlApplicationEngine *engine) { + mQmlEngine = engine; +} -QQmlEngine *DoricContext::getQmlEngine() { return mQmlEngine; } +QQmlApplicationEngine *DoricContext::getQmlEngine() { return mQmlEngine; } DoricViewNode *DoricContext::targetViewNode(QString id) { if (id == mRootNode->getId()) { diff --git a/doric-Qt/example/doric/DoricContext.h b/doric-Qt/example/doric/DoricContext.h index 7bcc40bd..75a5c4d7 100644 --- a/doric-Qt/example/doric/DoricContext.h +++ b/doric-Qt/example/doric/DoricContext.h @@ -1,7 +1,7 @@ #ifndef CONTEXT_H #define CONTEXT_H -#include +#include #include #include "DoricExport.h" @@ -21,7 +21,7 @@ private: QString extra; QVariant initParams; DoricInterfaceDriver *driver = NULL; - QQmlEngine *mQmlEngine; + QQmlApplicationEngine *mQmlEngine; QMap> mHeadNodes; @@ -45,9 +45,9 @@ public: QObject *obtainPlugin(QString name); - void setQmlEngine(QQmlEngine *engine); + void setQmlEngine(QQmlApplicationEngine *engine); - QQmlEngine *getQmlEngine(); + QQmlApplicationEngine *getQmlEngine(); DoricViewNode *targetViewNode(QString id); diff --git a/doric-Qt/example/doric/DoricPanel.cpp b/doric-Qt/example/doric/DoricPanel.cpp index 93a687b9..88ac6082 100644 --- a/doric-Qt/example/doric/DoricPanel.cpp +++ b/doric-Qt/example/doric/DoricPanel.cpp @@ -1,14 +1,13 @@ #include "DoricPanel.h" #include "shader/DoricRootNode.h" -DoricPanel::DoricPanel(QQmlEngine *qmlEngine, QQuickItem *quickItem) { +DoricPanel::DoricPanel(QQmlApplicationEngine *qmlEngine, + QQuickItem *quickItem) { mQmlEngine = qmlEngine; mQuickItem = quickItem; } -DoricPanel::~DoricPanel() { - delete mContext; -} +DoricPanel::~DoricPanel() { delete mContext; } void DoricPanel::config(QString script, QString alias, QString extra) { DoricContext *context = DoricContext::create(script, alias, extra); diff --git a/doric-Qt/example/doric/DoricPanel.h b/doric-Qt/example/doric/DoricPanel.h index a9a2e461..3d7296bc 100644 --- a/doric-Qt/example/doric/DoricPanel.h +++ b/doric-Qt/example/doric/DoricPanel.h @@ -13,11 +13,11 @@ private: int renderedWidth = -1; int renderedHeight = -1; - QQmlEngine *mQmlEngine; + QQmlApplicationEngine *mQmlEngine; QQuickItem *mQuickItem; public: - DoricPanel(QQmlEngine *qmlEngine, QQuickItem *quickItem); + DoricPanel(QQmlApplicationEngine *qmlEngine, QQuickItem *quickItem); ~DoricPanel(); diff --git a/doric-Qt/example/doric/DoricRegistry.cpp b/doric-Qt/example/doric/DoricRegistry.cpp index c5b5d11a..2695df74 100644 --- a/doric-Qt/example/doric/DoricRegistry.cpp +++ b/doric-Qt/example/doric/DoricRegistry.cpp @@ -2,6 +2,7 @@ #include "DoricLibrary.h" #include "plugin/DoricModalPlugin.h" +#include "plugin/DoricNavigatorPlugin.h" #include "plugin/DoricNetworkPlugin.h" #include "plugin/DoricNotificationPlugin.h" #include "plugin/DoricPopoverPlugin.h" @@ -31,6 +32,7 @@ DoricRegistry::DoricRegistry() { registerNativePlugin("network"); registerNativePlugin("storage"); registerNativePlugin("notification"); + registerNativePlugin("navigator"); registerViewNode("Root"); registerViewNode("Stack"); diff --git a/doric-Qt/example/doric/doric.pro b/doric-Qt/example/doric/doric.pro index c85cd41b..371bcf9a 100644 --- a/doric-Qt/example/doric/doric.pro +++ b/doric-Qt/example/doric/doric.pro @@ -37,6 +37,7 @@ SOURCES += \ engine/v8/JSValueHelper.cpp \ engine/v8/V8Executor.cpp \ plugin/DoricModalPlugin.cpp \ + plugin/DoricNavigatorPlugin.cpp \ plugin/DoricNetworkPlugin.cpp \ plugin/DoricNotificationPlugin.cpp \ plugin/DoricPopoverPlugin.cpp \ @@ -125,6 +126,7 @@ HEADERS += \ engine/v8/V8Executor.h \ plugin/DoricModalPlugin.h \ plugin/DoricNativePlugin.h \ + plugin/DoricNavigatorPlugin.h \ plugin/DoricNetworkPlugin.h \ plugin/DoricNotificationPlugin.h \ plugin/DoricPopoverPlugin.h \ diff --git a/doric-Qt/example/doric/plugin/DoricNavigatorPlugin.cpp b/doric-Qt/example/doric/plugin/DoricNavigatorPlugin.cpp new file mode 100644 index 00000000..e0eb6f7d --- /dev/null +++ b/doric-Qt/example/doric/plugin/DoricNavigatorPlugin.cpp @@ -0,0 +1,66 @@ +#include "DoricNavigatorPlugin.h" + +#include "DoricPanel.h" +#include "engine/DoricPromise.h" +#include "utils/DoricUtils.h" + +#include +#include +#include +#include +#include + +void DoricNavigatorPlugin::push(QString jsValueString, QString callbackId) { + + QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8()); + QJsonValue jsValue = document.object(); + + bool animated = true; + QString source = jsValue["source"].toString(); + QString alias = source; + QJsonValue config = jsValue["config"]; + + if (config.isObject()) { + animated = config["animated"].toBool(); + alias = config["alias"].toString(); + } + + getContext()->getDriver()->asyncCall( + [this, alias] { + QString name = alias; + QString script = DoricUtils::readAssetFile("/doric/bundles", name); + + QQmlComponent component(getContext()->getQmlEngine()); + const QUrl url(QStringLiteral("qrc:/doric/qml/panel.qml")); + component.loadUrl(url); + QObject *object = component.create(); + QQuickItem *quickItem = qobject_cast(object); + DoricPanel *panel = + new DoricPanel(getContext()->getQmlEngine(), quickItem); + quickItem->setWidth(600); + quickItem->setHeight(800); + panel->config(script, name, NULL); + + QObject *window = getContext()->getQmlEngine()->rootObjects().at(0); + QVariant arg = QVariant::fromValue(object); + QMetaObject::invokeMethod(window, "navigatorPush", + Q_ARG(QVariant, arg)); + }, + DoricThreadMode::UI); + + getContext()->getDriver()->asyncCall( + [this, callbackId] { + QVariantList args; + DoricPromise::resolve(getContext(), callbackId, args); + }, + DoricThreadMode::JS); +} + +void DoricNavigatorPlugin::pop(QString jsValueString, QString callbackId) {} + +void DoricNavigatorPlugin::popSelf(QString jsValueString, QString callbackId) {} + +void DoricNavigatorPlugin::popToRoot(QString jsValueString, + QString callbackId) {} + +void DoricNavigatorPlugin::openUrl(QString jsValueString, QString callbackId) {} diff --git a/doric-Qt/example/doric/plugin/DoricNavigatorPlugin.h b/doric-Qt/example/doric/plugin/DoricNavigatorPlugin.h new file mode 100644 index 00000000..6a5667f5 --- /dev/null +++ b/doric-Qt/example/doric/plugin/DoricNavigatorPlugin.h @@ -0,0 +1,25 @@ +#ifndef DORICNAVIGATORPLUGIN_H +#define DORICNAVIGATORPLUGIN_H + +#include "DoricExport.h" + +#include "DoricNativePlugin.h" + +class DORIC_EXPORT DoricNavigatorPlugin : public DoricNativePlugin { + Q_OBJECT + +public: + using DoricNativePlugin::DoricNativePlugin; + + Q_INVOKABLE void push(QString jsValueString, QString callbackId); + + Q_INVOKABLE void pop(QString jsValueString, QString callbackId); + + Q_INVOKABLE void popSelf(QString jsValueString, QString callbackId); + + Q_INVOKABLE void popToRoot(QString jsValueString, QString callbackId); + + Q_INVOKABLE void openUrl(QString jsValueString, QString callbackId); +}; + +#endif // DORICNAVIGATORPLUGIN_H