This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-Qt/doric/plugin/DoricModalPlugin.cpp

73 lines
2.4 KiB
C++
Raw Normal View History

#include "DoricModalPlugin.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>
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);
}