add alert

This commit is contained in:
王劲鹏
2021-04-22 11:43:48 +08:00
committed by osborn
parent 7bcb711dfe
commit 70cf4acd5b
11 changed files with 166 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
#include "DoricModalPlugin.h"
#include "engine/DoricPromise.h"
#include "shader/DoricRootNode.h"
#include "utils/DoricLayouts.h"
@@ -70,3 +71,56 @@ void DoricModalPlugin::toast(QString jsValueString, QString callbackId) {
},
DoricThreadMode::UI);
}
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);
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::onAccept(QString callbackId) {
DoricPromise::resolve(getContext(), callbackId);
}

View File

@@ -9,6 +9,10 @@ public:
using DoricNativePlugin::DoricNativePlugin;
Q_INVOKABLE void toast(QString jsValueString, QString callbackId);
Q_INVOKABLE void alert(QString jsValueString, QString callbackId);
void onAccept(QString callbackId);
};
#endif // DORICMODALPLUGIN_H