change toast to window

This commit is contained in:
王劲鹏 2021-04-21 16:10:35 +08:00 committed by osborn
parent c087f06647
commit 7bcb711dfe
2 changed files with 51 additions and 41 deletions

View File

@ -5,7 +5,7 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QObject> #include <QObject>
#include <QQmlComponent> #include <QQmlComponent>
#include <QQuickItem> #include <QQuickWindow>
#include <QTimer> #include <QTimer>
void DoricModalPlugin::toast(QString jsValueString, QString callbackId) { void DoricModalPlugin::toast(QString jsValueString, QString callbackId) {
@ -18,56 +18,55 @@ void DoricModalPlugin::toast(QString jsValueString, QString callbackId) {
QString msg = jsValue["msg"].toString(); QString msg = jsValue["msg"].toString();
int gravity = jsValue["gravity"].toInt(); int gravity = jsValue["gravity"].toInt();
QQuickItem *rootObject =
getContext()->getRootNode()->getRootView()->parentItem();
QQmlComponent component(getContext()->getQmlEngine()); QQmlComponent component(getContext()->getQmlEngine());
const QUrl url(QStringLiteral("qrc:/doric/qml/toast.qml")); const QUrl url(QStringLiteral("qrc:/doric/qml/toast.qml"));
component.loadUrl(url); component.loadUrl(url);
if (component.isError()) { if (component.isError()) {
qCritical() << component.errorString(); qCritical() << component.errorString();
} }
QQuickItem *item = qobject_cast<QQuickItem *>(component.create()); QQuickWindow *window = qobject_cast<QQuickWindow *>(component.create());
item->setParentItem(rootObject);
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();
// init set y // init set y
if ((gravity & DoricGravity::DoricGravityBottom) == setY();
DoricGravity::DoricGravityBottom) {
item->setProperty("y", rootObject->height() - item->height() - 20);
} else if ((gravity & DoricGravity::DoricGravityTop) ==
DoricGravity::DoricGravityTop) {
item->setProperty("y", 20);
} else {
item->setProperty("y", (rootObject->height() - item->height()) / 2);
}
// update x // update x
connect(item, &QQuickItem::widthChanged, [rootObject, item]() { connect(window, &QQuickWindow::widthChanged, setX);
item->setProperty("x", (rootObject->width() - item->width()) / 2.f);
});
// update y // update y
connect( connect(window, &QQuickWindow::heightChanged, setY);
item, &QQuickItem::heightChanged, [rootObject, item, gravity]() {
if ((gravity & DoricGravity::DoricGravityBottom) ==
DoricGravity::DoricGravityBottom) {
item->setProperty("y",
rootObject->height() - item->height() - 20);
} else if ((gravity & DoricGravity::DoricGravityTop) ==
DoricGravity::DoricGravityTop) {
item->setProperty("y", 20);
} else {
item->setProperty("y",
(rootObject->height() - item->height()) / 2);
}
});
item->childItems().at(0)->childItems().at(0)->setProperty("text", msg); QTimer::singleShot(2000, qApp, [window]() { window->deleteLater(); });
QTimer::singleShot(2000, qApp, [item]() {
item->setParent(nullptr);
item->setParentItem(nullptr);
item->deleteLater();
});
}, },
DoricThreadMode::UI); DoricThreadMode::UI);
} }

View File

@ -2,10 +2,13 @@ import QtQuick 2.12
import QtQuick.Controls 2.5 import QtQuick.Controls 2.5
import QtQuick.Layouts 1.15 import QtQuick.Layouts 1.15
Rectangle { Window {
width: childrenRect.width id: window
height: childrenRect.height
flags: flags | Qt.WindowStaysOnTopHint | Qt.Tool | Qt.FramelessWindowHint | Qt.WindowTransparentForInput
color: "#bb000000" color: "#bb000000"
visible: true
ColumnLayout { ColumnLayout {
Text { Text {
@ -17,6 +20,14 @@ Rectangle {
Layout.topMargin: 15 Layout.topMargin: 15
Layout.bottomMargin: 15 Layout.bottomMargin: 15
} }
onWidthChanged: {
window.width = implicitWidth
}
onHeightChanged: {
window.height = implicitHeight
}
} }
} }