From 70cf4acd5b547a879a206b6b453e6d7cdfb36904 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Thu, 22 Apr 2021 11:43:48 +0800 Subject: [PATCH] add alert --- doric-Qt/doric/demo/DoricDemoBridge.cpp | 3 ++ doric-Qt/doric/doric.pro | 3 ++ doric-Qt/doric/engine/DoricPromise.h | 20 +++++++ doric-Qt/doric/plugin/DoricModalPlugin.cpp | 54 +++++++++++++++++++ doric-Qt/doric/plugin/DoricModalPlugin.h | 4 ++ doric-Qt/doric/qml.qrc | 1 + doric-Qt/doric/resources/alert.qml | 38 +++++++++++++ doric-Qt/doric/utils/DoricConstant.cpp | 2 + doric-Qt/doric/utils/DoricConstant.h | 2 + .../utils/DoricDialogOnAcceptedBridge.cpp | 23 ++++++++ .../doric/utils/DoricDialogOnAcceptedBridge.h | 16 ++++++ 11 files changed, 166 insertions(+) create mode 100644 doric-Qt/doric/engine/DoricPromise.h create mode 100644 doric-Qt/doric/resources/alert.qml create mode 100644 doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.cpp create mode 100644 doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.h diff --git a/doric-Qt/doric/demo/DoricDemoBridge.cpp b/doric-Qt/doric/demo/DoricDemoBridge.cpp index 7264e4cb..9bdc30e9 100644 --- a/doric-Qt/doric/demo/DoricDemoBridge.cpp +++ b/doric-Qt/doric/demo/DoricDemoBridge.cpp @@ -5,6 +5,7 @@ #include "DoricDemoBridge.h" #include "DoricPanel.h" #include "utils/DoricMouseAreaBridge.h" +#include "utils/DoricDialogOnAcceptedBridge.h" #include "utils/DoricUtils.h" DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {} @@ -69,4 +70,6 @@ void DoricDemoBridge::navigate(QVariant route) { auto context = view->engine()->rootContext(); DoricMouseAreaBridge *mouseAreaBridge = new DoricMouseAreaBridge(); context->setContextProperty("mouseAreaBridge", mouseAreaBridge); + DoricDialogOnAcceptedBridge *dialogOnAcceptedBridge = new DoricDialogOnAcceptedBridge(); + context->setContextProperty("dialogOnAcceptedBridge", dialogOnAcceptedBridge); } diff --git a/doric-Qt/doric/doric.pro b/doric-Qt/doric/doric.pro index 81a6a2f9..595259da 100644 --- a/doric-Qt/doric/doric.pro +++ b/doric-Qt/doric/doric.pro @@ -47,6 +47,7 @@ SOURCES += \ shader/DoricViewNode.cpp \ utils/DoricConstant.cpp \ utils/DoricContextHolder.cpp \ + utils/DoricDialogOnAcceptedBridge.cpp \ utils/DoricLayouts.cpp \ utils/DoricMouseAreaBridge.cpp \ widget/flex/FlexLayout.cpp \ @@ -97,6 +98,7 @@ HEADERS += \ engine/DoricNativeJSE.h \ engine/DoricNativeLog.h \ engine/DoricNativeRequire.h \ + engine/DoricPromise.h \ engine/DoricTimerExtension.h \ engine/native/NativeExecutor.h \ engine/v8/JSValueHelper.h \ @@ -117,6 +119,7 @@ HEADERS += \ utils/DoricConstant.h \ utils/DoricContextHolder.h \ utils/DoricCountDownLatch.h \ + utils/DoricDialogOnAcceptedBridge.h \ utils/DoricLayouts.h \ utils/DoricMouseAreaBridge.h \ utils/DoricObjectFactory.h \ diff --git a/doric-Qt/doric/engine/DoricPromise.h b/doric-Qt/doric/engine/DoricPromise.h new file mode 100644 index 00000000..ceda16ac --- /dev/null +++ b/doric-Qt/doric/engine/DoricPromise.h @@ -0,0 +1,20 @@ +#ifndef DORICPROMISE_H +#define DORICPROMISE_H + +#include + +#include "DoricContext.h" +#include "utils/DoricConstant.h" + +class DoricPromise { +public: + static void resolve(DoricContext *context, QString callbackId) { + QVariantList params; + params.append(context->getContextId()); + params.append(callbackId); + context->getDriver()->invokeDoricMethod(DoricConstant::DORIC_BRIDGE_RESOLVE, + params); + } +}; + +#endif // DORICPROMISE_H diff --git a/doric-Qt/doric/plugin/DoricModalPlugin.cpp b/doric-Qt/doric/plugin/DoricModalPlugin.cpp index 92f8ea7f..0418444a 100644 --- a/doric-Qt/doric/plugin/DoricModalPlugin.cpp +++ b/doric-Qt/doric/plugin/DoricModalPlugin.cpp @@ -1,4 +1,5 @@ #include "DoricModalPlugin.h" +#include "engine/DoricPromise.h" #include "shader/DoricRootNode.h" #include "utils/DoricLayouts.h" @@ -70,3 +71,56 @@ void DoricModalPlugin::toast(QString jsValueString, QString callbackId) { }, DoricThreadMode::UI); } + +void DoricModalPlugin::alert(QString jsValueString, QString callbackId) { + getContext()->getDriver()->asyncCall( + [this, jsValueString, callbackId] { + QJsonDocument document = + QJsonDocument::fromJson(jsValueString.toUtf8()); + QJsonValue jsValue = document.object(); + + QJsonValue titleVal = jsValue["title"]; + QJsonValue msgVal = jsValue["msg"]; + QJsonValue okBtn = jsValue["okLabel"]; + + QQmlComponent component(getContext()->getQmlEngine()); + const QUrl url(QStringLiteral("qrc:/doric/qml/alert.qml")); + component.loadUrl(url); + if (component.isError()) { + qCritical() << component.errorString(); + } + QQuickWindow *window = qobject_cast(component.create()); + window->setProperty("pointer", QString::number((qint64)window)); + window->setProperty("plugin", QString::number((qint64)this)); + window->setProperty("callbackId", callbackId); + + QQuickWindow *parentWindow = + getContext()->getRootNode()->getRootView()->window(); + + std::function setX = [window, parentWindow]() { + window->setProperty("x", + (parentWindow->width() - window->width()) / 2.f + + parentWindow->x()); + }; + std::function setY = [window, parentWindow]() { + window->setProperty("y", + (parentWindow->height() - window->height()) / 2 + + parentWindow->y()); + }; + // init set x + setX(); + // init set y + setY(); + + // update x + connect(window, &QQuickWindow::widthChanged, setX); + + // update y + connect(window, &QQuickWindow::heightChanged, setY); + }, + DoricThreadMode::UI); +} + +void DoricModalPlugin::onAccept(QString callbackId) { + DoricPromise::resolve(getContext(), callbackId); +} diff --git a/doric-Qt/doric/plugin/DoricModalPlugin.h b/doric-Qt/doric/plugin/DoricModalPlugin.h index dbf4211e..902756b5 100644 --- a/doric-Qt/doric/plugin/DoricModalPlugin.h +++ b/doric-Qt/doric/plugin/DoricModalPlugin.h @@ -9,6 +9,10 @@ public: using DoricNativePlugin::DoricNativePlugin; Q_INVOKABLE void toast(QString jsValueString, QString callbackId); + + Q_INVOKABLE void alert(QString jsValueString, QString callbackId); + + void onAccept(QString callbackId); }; #endif // DORICMODALPLUGIN_H diff --git a/doric-Qt/doric/qml.qrc b/doric-Qt/doric/qml.qrc index 59d89c01..43d05370 100644 --- a/doric-Qt/doric/qml.qrc +++ b/doric-Qt/doric/qml.qrc @@ -26,6 +26,7 @@ resources/scroller.qml resources/toast.qml + resources/alert.qml resources/util.mjs resources/gravity.mjs diff --git a/doric-Qt/doric/resources/alert.qml b/doric-Qt/doric/resources/alert.qml new file mode 100644 index 00000000..1629251b --- /dev/null +++ b/doric-Qt/doric/resources/alert.qml @@ -0,0 +1,38 @@ +import QtQuick 2.12 +import QtQuick.Controls 2.12 + +Window { + id: window + + flags: flags | Qt.WindowStaysOnTopHint | Qt.Tool | Qt.FramelessWindowHint + visible: true + modality: Qt.ApplicationModal + + property var pointer + property var plugin + property var callbackId + + Dialog { + id: dialog + title: "Title" + standardButtons: Dialog.Ok + modal: true + + onAccepted: { + dialogOnAcceptedBridge.onClick(pointer, plugin, callbackId) + } + + onWidthChanged: { + window.width = implicitWidth + } + + onHeightChanged: { + window.height = implicitHeight + } + } + + Component.onCompleted: { + dialog.open() + dialog.standardButton(Dialog.Ok).text = qsTrId("OkLabel") + } +} diff --git a/doric-Qt/doric/utils/DoricConstant.cpp b/doric-Qt/doric/utils/DoricConstant.cpp index 6546fa01..9403e674 100644 --- a/doric-Qt/doric/utils/DoricConstant.cpp +++ b/doric-Qt/doric/utils/DoricConstant.cpp @@ -28,6 +28,8 @@ const QString DoricConstant::TEMPLATE_CONTEXT_DESTROY = const QString DoricConstant::GLOBAL_DORIC = "doric"; const QString DoricConstant::DORIC_CONTEXT_INVOKE = "jsCallEntityMethod"; const QString DoricConstant::DORIC_TIMER_CALLBACK = "jsCallbackTimer"; +const QString DoricConstant::DORIC_BRIDGE_RESOLVE = "jsCallResolve"; +const QString DoricConstant::DORIC_BRIDGE_REJECT = "jsCallReject"; const QString DoricConstant::DORIC_ENTITY_RESPONSE = "__response__"; const QString DoricConstant::DORIC_ENTITY_INIT = "__init__"; diff --git a/doric-Qt/doric/utils/DoricConstant.h b/doric-Qt/doric/utils/DoricConstant.h index d97b2aa4..47bb14be 100644 --- a/doric-Qt/doric/utils/DoricConstant.h +++ b/doric-Qt/doric/utils/DoricConstant.h @@ -25,6 +25,8 @@ public: static const QString GLOBAL_DORIC; static const QString DORIC_CONTEXT_INVOKE; static const QString DORIC_TIMER_CALLBACK; + static const QString DORIC_BRIDGE_RESOLVE; + static const QString DORIC_BRIDGE_REJECT; static const QString DORIC_ENTITY_RESPONSE; static const QString DORIC_ENTITY_CREATE; diff --git a/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.cpp b/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.cpp new file mode 100644 index 00000000..ece21578 --- /dev/null +++ b/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.cpp @@ -0,0 +1,23 @@ +#include "DoricDialogOnAcceptedBridge.h" +#include "plugin/DoricModalPlugin.h" + +#include + +DoricDialogOnAcceptedBridge::DoricDialogOnAcceptedBridge(QObject *parent) + : QObject(parent) {} + +void DoricDialogOnAcceptedBridge::onClick(QString windowPointer, + QString pluginPointer, + QString callbackId) { + { + QObject *object = (QObject *)(windowPointer.toULongLong()); + QQuickWindow *window = dynamic_cast(object); + window->deleteLater(); + } + + { + QObject *object = (QObject *)(pluginPointer.toULongLong()); + DoricModalPlugin *modalPlugin = dynamic_cast(object); + modalPlugin->onAccept(callbackId); + } +} diff --git a/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.h b/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.h new file mode 100644 index 00000000..659a9418 --- /dev/null +++ b/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.h @@ -0,0 +1,16 @@ +#ifndef DORICDIALOGONACCEPTEDBRIDGE_H +#define DORICDIALOGONACCEPTEDBRIDGE_H + +#include + +class DoricDialogOnAcceptedBridge : public QObject { + Q_OBJECT +public: + explicit DoricDialogOnAcceptedBridge(QObject *parent = nullptr); + + Q_INVOKABLE + void onClick(QString windowPointer, QString pluginPointer, + QString callbackId); +}; + +#endif // DORICDIALOGONACCEPTEDBRIDGE_H