add confirm

This commit is contained in:
王劲鹏 2021-04-22 13:42:38 +08:00 committed by osborn
parent dda16db50a
commit d47fa2bb7d
12 changed files with 191 additions and 47 deletions

View File

@ -5,7 +5,7 @@
#include "DoricDemoBridge.h" #include "DoricDemoBridge.h"
#include "DoricPanel.h" #include "DoricPanel.h"
#include "utils/DoricMouseAreaBridge.h" #include "utils/DoricMouseAreaBridge.h"
#include "utils/DoricDialogOnAcceptedBridge.h" #include "utils/DoricDialogBridge.h"
#include "utils/DoricUtils.h" #include "utils/DoricUtils.h"
DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {} DoricDemoBridge::DoricDemoBridge(QObject *parent) : QObject(parent) {}
@ -70,6 +70,6 @@ void DoricDemoBridge::navigate(QVariant route) {
auto context = view->engine()->rootContext(); auto context = view->engine()->rootContext();
DoricMouseAreaBridge *mouseAreaBridge = new DoricMouseAreaBridge(); DoricMouseAreaBridge *mouseAreaBridge = new DoricMouseAreaBridge();
context->setContextProperty("mouseAreaBridge", mouseAreaBridge); context->setContextProperty("mouseAreaBridge", mouseAreaBridge);
DoricDialogOnAcceptedBridge *dialogOnAcceptedBridge = new DoricDialogOnAcceptedBridge(); DoricDialogBridge *dialogBridge = new DoricDialogBridge();
context->setContextProperty("dialogOnAcceptedBridge", dialogOnAcceptedBridge); context->setContextProperty("dialogBridge", dialogBridge);
} }

View File

@ -47,7 +47,7 @@ SOURCES += \
shader/DoricViewNode.cpp \ shader/DoricViewNode.cpp \
utils/DoricConstant.cpp \ utils/DoricConstant.cpp \
utils/DoricContextHolder.cpp \ utils/DoricContextHolder.cpp \
utils/DoricDialogOnAcceptedBridge.cpp \ utils/DoricDialogBridge.cpp \
utils/DoricLayouts.cpp \ utils/DoricLayouts.cpp \
utils/DoricMouseAreaBridge.cpp \ utils/DoricMouseAreaBridge.cpp \
widget/flex/FlexLayout.cpp \ widget/flex/FlexLayout.cpp \
@ -119,7 +119,7 @@ HEADERS += \
utils/DoricConstant.h \ utils/DoricConstant.h \
utils/DoricContextHolder.h \ utils/DoricContextHolder.h \
utils/DoricCountDownLatch.h \ utils/DoricCountDownLatch.h \
utils/DoricDialogOnAcceptedBridge.h \ utils/DoricDialogBridge.h \
utils/DoricLayouts.h \ utils/DoricLayouts.h \
utils/DoricMouseAreaBridge.h \ utils/DoricMouseAreaBridge.h \
utils/DoricObjectFactory.h \ utils/DoricObjectFactory.h \

View File

@ -15,6 +15,14 @@ public:
context->getDriver()->invokeDoricMethod(DoricConstant::DORIC_BRIDGE_RESOLVE, context->getDriver()->invokeDoricMethod(DoricConstant::DORIC_BRIDGE_RESOLVE,
params); 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 #endif // DORICPROMISE_H

View File

@ -124,6 +124,64 @@ void DoricModalPlugin::alert(QString jsValueString, QString callbackId) {
DoricThreadMode::UI); 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<QQuickWindow *>(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); DoricPromise::resolve(getContext(), callbackId);
} }
void DoricModalPlugin::onRejected(QString callbackId) {
DoricPromise::reject(getContext(), callbackId);
}

View File

@ -12,7 +12,11 @@ public:
Q_INVOKABLE void alert(QString jsValueString, QString callbackId); 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 #endif // DORICMODALPLUGIN_H

View File

@ -27,6 +27,7 @@
<file alias="toast.qml">resources/toast.qml</file> <file alias="toast.qml">resources/toast.qml</file>
<file alias="alert.qml">resources/alert.qml</file> <file alias="alert.qml">resources/alert.qml</file>
<file alias="confirm.qml">resources/confirm.qml</file>
<file alias="util.mjs">resources/util.mjs</file> <file alias="util.mjs">resources/util.mjs</file>
<file alias="gravity.mjs">resources/gravity.mjs</file> <file alias="gravity.mjs">resources/gravity.mjs</file>

View File

@ -29,7 +29,7 @@ Window {
modal: true modal: true
onAccepted: { onAccepted: {
dialogOnAcceptedBridge.onClick(pointer, plugin, callbackId) dialogBridge.onAccepted(pointer, plugin, callbackId)
} }
onWidthChanged: { onWidthChanged: {

View File

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

View File

@ -0,0 +1,36 @@
#include "DoricDialogBridge.h"
#include "plugin/DoricModalPlugin.h"
#include <QQuickWindow>
DoricDialogBridge::DoricDialogBridge(QObject *parent) : QObject(parent) {}
void DoricDialogBridge::onAccepted(QString windowPointer, QString pluginPointer,
QString callbackId) {
{
QObject *object = (QObject *)(windowPointer.toULongLong());
QQuickWindow *window = dynamic_cast<QQuickWindow *>(object);
window->deleteLater();
}
{
QObject *object = (QObject *)(pluginPointer.toULongLong());
DoricModalPlugin *modalPlugin = dynamic_cast<DoricModalPlugin *>(object);
modalPlugin->onAccepted(callbackId);
}
}
void DoricDialogBridge::onRejected(QString windowPointer, QString pluginPointer,
QString callbackId) {
{
QObject *object = (QObject *)(windowPointer.toULongLong());
QQuickWindow *window = dynamic_cast<QQuickWindow *>(object);
window->deleteLater();
}
{
QObject *object = (QObject *)(pluginPointer.toULongLong());
DoricModalPlugin *modalPlugin = dynamic_cast<DoricModalPlugin *>(object);
modalPlugin->onRejected(callbackId);
}
}

View File

@ -0,0 +1,20 @@
#ifndef DORICDIALOGBRIDGE_H
#define DORICDIALOGBRIDGE_H
#include <QObject>
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

View File

@ -1,23 +0,0 @@
#include "DoricDialogOnAcceptedBridge.h"
#include "plugin/DoricModalPlugin.h"
#include <QQuickWindow>
DoricDialogOnAcceptedBridge::DoricDialogOnAcceptedBridge(QObject *parent)
: QObject(parent) {}
void DoricDialogOnAcceptedBridge::onClick(QString windowPointer,
QString pluginPointer,
QString callbackId) {
{
QObject *object = (QObject *)(windowPointer.toULongLong());
QQuickWindow *window = dynamic_cast<QQuickWindow *>(object);
window->deleteLater();
}
{
QObject *object = (QObject *)(pluginPointer.toULongLong());
DoricModalPlugin *modalPlugin = dynamic_cast<DoricModalPlugin *>(object);
modalPlugin->onAccept(callbackId);
}
}

View File

@ -1,16 +0,0 @@
#ifndef DORICDIALOGONACCEPTEDBRIDGE_H
#define DORICDIALOGONACCEPTEDBRIDGE_H
#include <QObject>
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