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)
|
|
|
|
->setProperty("text", msg);
|
|
|
|
|
|
|
|
std::function setX = [window, parentWindow]() {
|
|
|
|
window->setProperty("x",
|
|
|
|
(parentWindow->width() - window->width()) / 2.f +
|
|
|
|
parentWindow->x());
|
|
|
|
};
|
|
|
|
std::function setY = [window, parentWindow, gravity]() {
|
|
|
|
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());
|
|
|
|
window->setProperty("okLabel", okBtn.toString());
|
|
|
|
|
2021-04-22 11:43:48 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
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) {
|
2021-04-22 11:43:48 +08:00
|
|
|
DoricPromise::resolve(getContext(), callbackId);
|
|
|
|
}
|
2021-04-22 13:42:38 +08:00
|
|
|
|
|
|
|
void DoricModalPlugin::onRejected(QString callbackId) {
|
|
|
|
DoricPromise::reject(getContext(), callbackId);
|
|
|
|
}
|