From d47fa2bb7d2f238f35a1f42536386693d848151e 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 13:42:38 +0800 Subject: [PATCH] add confirm --- doric-Qt/doric/demo/DoricDemoBridge.cpp | 6 +- doric-Qt/doric/doric.pro | 4 +- doric-Qt/doric/engine/DoricPromise.h | 8 +++ doric-Qt/doric/plugin/DoricModalPlugin.cpp | 60 ++++++++++++++++++- doric-Qt/doric/plugin/DoricModalPlugin.h | 6 +- doric-Qt/doric/qml.qrc | 1 + doric-Qt/doric/resources/alert.qml | 2 +- doric-Qt/doric/resources/confirm.qml | 56 +++++++++++++++++ doric-Qt/doric/utils/DoricDialogBridge.cpp | 36 +++++++++++ doric-Qt/doric/utils/DoricDialogBridge.h | 20 +++++++ .../utils/DoricDialogOnAcceptedBridge.cpp | 23 ------- .../doric/utils/DoricDialogOnAcceptedBridge.h | 16 ----- 12 files changed, 191 insertions(+), 47 deletions(-) create mode 100644 doric-Qt/doric/resources/confirm.qml create mode 100644 doric-Qt/doric/utils/DoricDialogBridge.cpp create mode 100644 doric-Qt/doric/utils/DoricDialogBridge.h delete mode 100644 doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.cpp delete 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 9bdc30e9..14522319 100644 --- a/doric-Qt/doric/demo/DoricDemoBridge.cpp +++ b/doric-Qt/doric/demo/DoricDemoBridge.cpp @@ -5,7 +5,7 @@ #include "DoricDemoBridge.h" #include "DoricPanel.h" #include "utils/DoricMouseAreaBridge.h" -#include "utils/DoricDialogOnAcceptedBridge.h" +#include "utils/DoricDialogBridge.h" #include "utils/DoricUtils.h" DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {} @@ -70,6 +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); + DoricDialogBridge *dialogBridge = new DoricDialogBridge(); + context->setContextProperty("dialogBridge", dialogBridge); } diff --git a/doric-Qt/doric/doric.pro b/doric-Qt/doric/doric.pro index 595259da..9828149a 100644 --- a/doric-Qt/doric/doric.pro +++ b/doric-Qt/doric/doric.pro @@ -47,7 +47,7 @@ SOURCES += \ shader/DoricViewNode.cpp \ utils/DoricConstant.cpp \ utils/DoricContextHolder.cpp \ - utils/DoricDialogOnAcceptedBridge.cpp \ + utils/DoricDialogBridge.cpp \ utils/DoricLayouts.cpp \ utils/DoricMouseAreaBridge.cpp \ widget/flex/FlexLayout.cpp \ @@ -119,7 +119,7 @@ HEADERS += \ utils/DoricConstant.h \ utils/DoricContextHolder.h \ utils/DoricCountDownLatch.h \ - utils/DoricDialogOnAcceptedBridge.h \ + utils/DoricDialogBridge.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 index ceda16ac..45ff901b 100644 --- a/doric-Qt/doric/engine/DoricPromise.h +++ b/doric-Qt/doric/engine/DoricPromise.h @@ -15,6 +15,14 @@ public: context->getDriver()->invokeDoricMethod(DoricConstant::DORIC_BRIDGE_RESOLVE, params); } + + static void reject(DoricContext *context, QString callbackId) { + QVariantList params; + params.append(context->getContextId()); + params.append(callbackId); + context->getDriver()->invokeDoricMethod(DoricConstant::DORIC_BRIDGE_REJECT, + params); + } }; #endif // DORICPROMISE_H diff --git a/doric-Qt/doric/plugin/DoricModalPlugin.cpp b/doric-Qt/doric/plugin/DoricModalPlugin.cpp index c366ba3a..0054e04d 100644 --- a/doric-Qt/doric/plugin/DoricModalPlugin.cpp +++ b/doric-Qt/doric/plugin/DoricModalPlugin.cpp @@ -124,6 +124,64 @@ void DoricModalPlugin::alert(QString jsValueString, QString callbackId) { DoricThreadMode::UI); } -void DoricModalPlugin::onAccept(QString callbackId) { +void DoricModalPlugin::confirm(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"]; + QJsonValue cancelBtn = jsValue["cancelLabel"]; + + QQmlComponent component(getContext()->getQmlEngine()); + const QUrl url(QStringLiteral("qrc:/doric/qml/confirm.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); + + window->setProperty("title", titleVal.toString()); + window->setProperty("okLabel", okBtn.toString()); + window->setProperty("cancelLabel", cancelBtn.toString()); + + 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::onAccepted(QString callbackId) { DoricPromise::resolve(getContext(), callbackId); } + +void DoricModalPlugin::onRejected(QString callbackId) { + DoricPromise::reject(getContext(), callbackId); +} diff --git a/doric-Qt/doric/plugin/DoricModalPlugin.h b/doric-Qt/doric/plugin/DoricModalPlugin.h index 902756b5..c2c6be4a 100644 --- a/doric-Qt/doric/plugin/DoricModalPlugin.h +++ b/doric-Qt/doric/plugin/DoricModalPlugin.h @@ -12,7 +12,11 @@ public: Q_INVOKABLE void alert(QString jsValueString, QString callbackId); - void onAccept(QString callbackId); + Q_INVOKABLE void confirm(QString jsValueString, QString callbackId); + + void onAccepted(QString callbackId); + + void onRejected(QString callbackId); }; #endif // DORICMODALPLUGIN_H diff --git a/doric-Qt/doric/qml.qrc b/doric-Qt/doric/qml.qrc index 43d05370..d927342d 100644 --- a/doric-Qt/doric/qml.qrc +++ b/doric-Qt/doric/qml.qrc @@ -27,6 +27,7 @@ resources/toast.qml resources/alert.qml + resources/confirm.qml resources/util.mjs resources/gravity.mjs diff --git a/doric-Qt/doric/resources/alert.qml b/doric-Qt/doric/resources/alert.qml index 66c9cec6..5db745ce 100644 --- a/doric-Qt/doric/resources/alert.qml +++ b/doric-Qt/doric/resources/alert.qml @@ -29,7 +29,7 @@ Window { modal: true onAccepted: { - dialogOnAcceptedBridge.onClick(pointer, plugin, callbackId) + dialogBridge.onAccepted(pointer, plugin, callbackId) } onWidthChanged: { diff --git a/doric-Qt/doric/resources/confirm.qml b/doric-Qt/doric/resources/confirm.qml new file mode 100644 index 00000000..7f90382f --- /dev/null +++ b/doric-Qt/doric/resources/confirm.qml @@ -0,0 +1,56 @@ +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 + + property var title + property var okLabel + property var cancelLabel + + onTitleChanged: { + dialog.title = title + } + + onOkLabelChanged: { + dialog.standardButton(Dialog.Ok).text = qsTrId(okLabel) + } + + onCancelLabelChanged: { + dialog.standardButton(Dialog.Cancel).text = qsTrId(cancelLabel) + } + + Dialog { + id: dialog + standardButtons: Dialog.Ok | Dialog.Cancel + modal: true + + onAccepted: { + dialogBridge.onAccepted(pointer, plugin, callbackId) + } + + onRejected: { + dialogBridge.onRejected(pointer, plugin, callbackId) + } + + onWidthChanged: { + window.width = implicitWidth + } + + onHeightChanged: { + window.height = implicitHeight + } + } + + Component.onCompleted: { + dialog.open() + } +} diff --git a/doric-Qt/doric/utils/DoricDialogBridge.cpp b/doric-Qt/doric/utils/DoricDialogBridge.cpp new file mode 100644 index 00000000..63d59301 --- /dev/null +++ b/doric-Qt/doric/utils/DoricDialogBridge.cpp @@ -0,0 +1,36 @@ +#include "DoricDialogBridge.h" +#include "plugin/DoricModalPlugin.h" + +#include + +DoricDialogBridge::DoricDialogBridge(QObject *parent) : QObject(parent) {} + +void DoricDialogBridge::onAccepted(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->onAccepted(callbackId); + } +} + +void DoricDialogBridge::onRejected(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->onRejected(callbackId); + } +} diff --git a/doric-Qt/doric/utils/DoricDialogBridge.h b/doric-Qt/doric/utils/DoricDialogBridge.h new file mode 100644 index 00000000..c2a205fd --- /dev/null +++ b/doric-Qt/doric/utils/DoricDialogBridge.h @@ -0,0 +1,20 @@ +#ifndef DORICDIALOGBRIDGE_H +#define DORICDIALOGBRIDGE_H + +#include + +class DoricDialogBridge : public QObject { + Q_OBJECT +public: + explicit DoricDialogBridge(QObject *parent = nullptr); + + Q_INVOKABLE + void onAccepted(QString windowPointer, QString pluginPointer, + QString callbackId); + + Q_INVOKABLE + void onRejected(QString windowPointer, QString pluginPointer, + QString callbackId); +}; + +#endif // DORICDIALOGBRIDGE_H diff --git a/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.cpp b/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.cpp deleted file mode 100644 index ece21578..00000000 --- a/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#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 deleted file mode 100644 index 659a9418..00000000 --- a/doric-Qt/doric/utils/DoricDialogOnAcceptedBridge.h +++ /dev/null @@ -1,16 +0,0 @@ -#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