add prompt

This commit is contained in:
王劲鹏 2021-04-22 14:48:16 +08:00 committed by osborn
parent abccec1321
commit d624272089
7 changed files with 200 additions and 4 deletions

View File

@ -8,18 +8,26 @@
class DoricPromise {
public:
static void resolve(DoricContext *context, QString callbackId) {
static void resolve(DoricContext *context, QString callbackId,
QVariantList args) {
QVariantList params;
params.append(context->getContextId());
params.append(callbackId);
foreach (QVariant arg, args) { params.append(arg); }
context->getDriver()->invokeDoricMethod(DoricConstant::DORIC_BRIDGE_RESOLVE,
params);
}
static void reject(DoricContext *context, QString callbackId) {
static void reject(DoricContext *context, QString callbackId,
QVariantList args) {
QVariantList params;
params.append(context->getContextId());
params.append(callbackId);
foreach (QVariant arg, args) { params.append(arg); }
context->getDriver()->invokeDoricMethod(DoricConstant::DORIC_BRIDGE_REJECT,
params);
}

View File

@ -180,10 +180,79 @@ void DoricModalPlugin::confirm(QString jsValueString, QString callbackId) {
DoricThreadMode::UI);
}
void DoricModalPlugin::prompt(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/prompt.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("msg", msgVal.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);
QVariantList args;
DoricPromise::resolve(getContext(), callbackId, args);
}
void DoricModalPlugin::onAcceptedWithInput(QString callbackId, QString input) {
QVariantList args;
args.append(input);
DoricPromise::resolve(getContext(), callbackId, args);
}
void DoricModalPlugin::onRejected(QString callbackId) {
DoricPromise::reject(getContext(), callbackId);
QVariantList args;
DoricPromise::reject(getContext(), callbackId, args);
}
void DoricModalPlugin::onRejectedWithInput(QString callbackId, QString input) {
QVariantList args;
args.append(input);
DoricPromise::reject(getContext(), callbackId, args);
}

View File

@ -14,9 +14,15 @@ public:
Q_INVOKABLE void confirm(QString jsValueString, QString callbackId);
Q_INVOKABLE void prompt(QString jsValueString, QString callbackId);
void onAccepted(QString callbackId);
void onAcceptedWithInput(QString callbackId, QString input);
void onRejected(QString callbackId);
void onRejectedWithInput(QString callbackId, QString input);
};
#endif // DORICMODALPLUGIN_H

View File

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

View File

@ -0,0 +1,72 @@
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.15
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 msg
property var okLabel
property var cancelLabel
onTitleChanged: {
dialog.title = title
}
onMsgChanged: {
content.text = msg
}
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
contentItem: ColumnLayout {
Text {
id: content
}
TextArea {
id: input
Layout.fillWidth: true
}
}
onAccepted: {
dialogBridge.onAcceptedWithInput(pointer, plugin, callbackId, input.text)
}
onRejected: {
dialogBridge.onRejectedWithInput(pointer, plugin, callbackId, input.text)
}
onWidthChanged: {
window.width = implicitWidth
}
onHeightChanged: {
window.height = implicitHeight
}
}
Component.onCompleted: {
dialog.open()
}
}

View File

@ -34,3 +34,35 @@ void DoricDialogBridge::onRejected(QString windowPointer, QString pluginPointer,
modalPlugin->onRejected(callbackId);
}
}
void DoricDialogBridge::onAcceptedWithInput(QString windowPointer,
QString pluginPointer,
QString callbackId, QString input) {
{
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->onAcceptedWithInput(callbackId, input);
}
}
void DoricDialogBridge::onRejectedWithInput(QString windowPointer,
QString pluginPointer,
QString callbackId, QString input) {
{
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->onRejectedWithInput(callbackId, input);
}
}

View File

@ -12,9 +12,17 @@ public:
void onAccepted(QString windowPointer, QString pluginPointer,
QString callbackId);
Q_INVOKABLE
void onAcceptedWithInput(QString windowPointer, QString pluginPointer,
QString callbackId, QString input);
Q_INVOKABLE
void onRejected(QString windowPointer, QString pluginPointer,
QString callbackId);
Q_INVOKABLE
void onRejectedWithInput(QString windowPointer, QString pluginPointer,
QString callbackId, QString input);
};
#endif // DORICDIALOGBRIDGE_H