2021-04-20 11:46:56 +08:00
|
|
|
#include "DoricModalPlugin.h"
|
2021-04-22 11:43:48 +08:00
|
|
|
#include "engine/DoricPromise.h"
|
2021-04-20 20:44:24 +08:00
|
|
|
#include "shader/DoricRootNode.h"
|
|
|
|
#include "utils/DoricLayouts.h"
|
|
|
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QQmlComponent>
|
2021-04-21 16:10:35 +08:00
|
|
|
#include <QQuickWindow>
|
2021-04-20 20:44:24 +08:00
|
|
|
#include <QTimer>
|
2021-04-20 11:46:56 +08:00
|
|
|
|
|
|
|
void DoricModalPlugin::toast(QString jsValueString, QString callbackId) {
|
2021-04-20 20:44:24 +08:00
|
|
|
getContext()->getDriver()->asyncCall(
|
|
|
|
[this, jsValueString] {
|
|
|
|
QJsonDocument document =
|
|
|
|
QJsonDocument::fromJson(jsValueString.toUtf8());
|
|
|
|
QJsonValue jsValue = document.object();
|
|
|
|
|
|
|
|
QString msg = jsValue["msg"].toString();
|
|
|
|
int gravity = jsValue["gravity"].toInt();
|
|
|
|
|
|
|
|
QQmlComponent component(getContext()->getQmlEngine());
|
|
|
|
const QUrl url(QStringLiteral("qrc:/doric/qml/toast.qml"));
|
|
|
|
component.loadUrl(url);
|
|
|
|
if (component.isError()) {
|
|
|
|
qCritical() << component.errorString();
|
|
|
|
}
|
2021-04-21 16:10:35 +08:00
|
|
|
QQuickWindow *window = qobject_cast<QQuickWindow *>(component.create());
|
2021-04-20 20:44:24 +08:00
|
|
|
|
2021-04-21 16:10:35 +08:00
|
|
|
QQuickWindow *parentWindow =
|
|
|
|
getContext()->getRootNode()->getRootView()->window();
|
|
|
|
|
|
|
|
window->contentItem()
|
|
|
|
->childItems()
|
|
|
|
.at(0)
|
|
|
|
->childItems()
|
|
|
|
.at(0)
|
2021-04-26 21:21:59 +08:00
|
|
|
->childItems()
|
|
|
|
.at(0)
|
2021-04-21 16:10:35 +08:00
|
|
|
->setProperty("text", msg);
|
|
|
|
|
2021-04-26 21:21:59 +08:00
|
|
|
std::function<void()> setX = [window, parentWindow]() {
|
2021-04-21 16:10:35 +08:00
|
|
|
window->setProperty("x",
|
|
|
|
(parentWindow->width() - window->width()) / 2.f +
|
|
|
|
parentWindow->x());
|
|
|
|
};
|
2021-04-26 21:21:59 +08:00
|
|
|
std::function<void()> setY = [window, parentWindow, gravity]() {
|
2021-04-21 16:10:35 +08:00
|
|
|
if ((gravity & DoricGravity::DoricGravityBottom) ==
|
|
|
|
DoricGravity::DoricGravityBottom) {
|
|
|
|
window->setProperty("y", parentWindow->height() - window->height() -
|
|
|
|
20 + parentWindow->y());
|
|
|
|
} else if ((gravity & DoricGravity::DoricGravityTop) ==
|
|
|
|
DoricGravity::DoricGravityTop) {
|
|
|
|
window->setProperty("y", 20 + parentWindow->y());
|
|
|
|
} else {
|
|
|
|
window->setProperty(
|
|
|
|
"y", (parentWindow->height() - window->height()) / 2 +
|
|
|
|
parentWindow->y());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// init set x
|
|
|
|
setX();
|
2021-04-21 11:14:21 +08:00
|
|
|
// init set y
|
2021-04-21 16:10:35 +08:00
|
|
|
setY();
|
2021-04-21 11:14:21 +08:00
|
|
|
|
|
|
|
// update x
|
2021-04-21 16:10:35 +08:00
|
|
|
connect(window, &QQuickWindow::widthChanged, setX);
|
2021-04-20 20:44:24 +08:00
|
|
|
|
2021-04-21 11:14:21 +08:00
|
|
|
// update y
|
2021-04-21 16:10:35 +08:00
|
|
|
connect(window, &QQuickWindow::heightChanged, setY);
|
2021-04-20 20:44:24 +08:00
|
|
|
|
2021-04-21 16:10:35 +08:00
|
|
|
QTimer::singleShot(2000, qApp, [window]() { window->deleteLater(); });
|
2021-04-20 20:44:24 +08:00
|
|
|
},
|
|
|
|
DoricThreadMode::UI);
|
2021-04-20 11:46:56 +08:00
|
|
|
}
|
2021-04-22 11:43:48 +08:00
|
|
|
|
|
|
|
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<QQuickWindow *>(component.create());
|
|
|
|
window->setProperty("pointer", QString::number((qint64)window));
|
|
|
|
window->setProperty("plugin", QString::number((qint64)this));
|
|
|
|
window->setProperty("callbackId", callbackId);
|
|
|
|
|
2021-04-22 11:48:22 +08:00
|
|
|
window->setProperty("title", titleVal.toString());
|
2021-04-22 14:00:56 +08:00
|
|
|
window->setProperty("msg", msgVal.toString());
|
2021-06-08 09:44:22 +08:00
|
|
|
if (okBtn.isString()) {
|
|
|
|
window->setProperty("okLabel", okBtn.toString());
|
|
|
|
} else {
|
|
|
|
window->setProperty("okLabel", "ok");
|
|
|
|
}
|
2021-04-22 11:48:22 +08:00
|
|
|
|
2021-04-22 11:43:48 +08:00
|
|
|
QQuickWindow *parentWindow =
|
|
|
|
getContext()->getRootNode()->getRootView()->window();
|
|
|
|
|
2021-04-26 21:21:59 +08:00
|
|
|
std::function<void()> setX = [window, parentWindow]() {
|
2021-04-22 11:43:48 +08:00
|
|
|
window->setProperty("x",
|
|
|
|
(parentWindow->width() - window->width()) / 2.f +
|
|
|
|
parentWindow->x());
|
|
|
|
};
|
2021-04-26 21:21:59 +08:00
|
|
|
std::function<void()> setY = [window, parentWindow]() {
|
2021-04-22 11:43:48 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:42:38 +08:00
|
|
|
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());
|
2021-04-22 14:00:56 +08:00
|
|
|
window->setProperty("msg", msgVal.toString());
|
2021-04-22 13:42:38 +08:00
|
|
|
window->setProperty("okLabel", okBtn.toString());
|
|
|
|
window->setProperty("cancelLabel", cancelBtn.toString());
|
|
|
|
|
|
|
|
QQuickWindow *parentWindow =
|
|
|
|
getContext()->getRootNode()->getRootView()->window();
|
|
|
|
|
2021-04-26 21:21:59 +08:00
|
|
|
std::function<void()> setX = [window, parentWindow]() {
|
2021-04-22 13:42:38 +08:00
|
|
|
window->setProperty("x",
|
|
|
|
(parentWindow->width() - window->width()) / 2.f +
|
|
|
|
parentWindow->x());
|
|
|
|
};
|
2021-04-26 21:21:59 +08:00
|
|
|
std::function<void()> setY = [window, parentWindow]() {
|
2021-04-22 13:42:38 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-22 14:48:16 +08:00
|
|
|
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());
|
2021-05-06 10:02:29 +08:00
|
|
|
if (okBtn.isString()) {
|
|
|
|
window->setProperty("okLabel", okBtn.toString());
|
|
|
|
} else {
|
|
|
|
window->setProperty("okLabel", "ok");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cancelBtn.isString()) {
|
|
|
|
window->setProperty("cancelLabel", cancelBtn.toString());
|
|
|
|
} else {
|
|
|
|
window->setProperty("cancelLabel", "cancel");
|
|
|
|
}
|
2021-04-22 14:48:16 +08:00
|
|
|
|
|
|
|
QQuickWindow *parentWindow =
|
|
|
|
getContext()->getRootNode()->getRootView()->window();
|
|
|
|
|
2021-04-26 21:21:59 +08:00
|
|
|
std::function<void()> setX = [window, parentWindow]() {
|
2021-04-22 14:48:16 +08:00
|
|
|
window->setProperty("x",
|
|
|
|
(parentWindow->width() - window->width()) / 2.f +
|
|
|
|
parentWindow->x());
|
|
|
|
};
|
2021-04-26 21:21:59 +08:00
|
|
|
std::function<void()> setY = [window, parentWindow]() {
|
2021-04-22 14:48:16 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-04-22 13:42:38 +08:00
|
|
|
void DoricModalPlugin::onAccepted(QString callbackId) {
|
2021-04-22 14:48:16 +08:00
|
|
|
QVariantList args;
|
|
|
|
DoricPromise::resolve(getContext(), callbackId, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoricModalPlugin::onAcceptedWithInput(QString callbackId, QString input) {
|
|
|
|
QVariantList args;
|
|
|
|
args.append(input);
|
|
|
|
DoricPromise::resolve(getContext(), callbackId, args);
|
2021-04-22 11:43:48 +08:00
|
|
|
}
|
2021-04-22 13:42:38 +08:00
|
|
|
|
|
|
|
void DoricModalPlugin::onRejected(QString callbackId) {
|
2021-04-22 14:48:16 +08:00
|
|
|
QVariantList args;
|
|
|
|
DoricPromise::reject(getContext(), callbackId, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DoricModalPlugin::onRejectedWithInput(QString callbackId, QString input) {
|
|
|
|
QVariantList args;
|
|
|
|
args.append(input);
|
|
|
|
DoricPromise::reject(getContext(), callbackId, args);
|
2021-04-22 13:42:38 +08:00
|
|
|
}
|