implements toast

This commit is contained in:
王劲鹏 2021-04-20 20:44:24 +08:00 committed by osborn
parent f488f023fa
commit 7202bbfe67
6 changed files with 87 additions and 2 deletions

View File

@ -1,5 +1,61 @@
#include "DoricModalPlugin.h" #include "DoricModalPlugin.h"
#include "shader/DoricRootNode.h"
#include "utils/DoricLayouts.h"
#include <QJsonDocument>
#include <QObject>
#include <QQmlComponent>
#include <QQuickItem>
#include <QTimer>
void DoricModalPlugin::toast(QString jsValueString, QString callbackId) { void DoricModalPlugin::toast(QString jsValueString, QString callbackId) {
qDebug() << "toast"; 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();
QQuickItem *rootObject =
getContext()->getRootNode()->getRootView()->parentItem();
QQmlComponent component(getContext()->getQmlEngine());
const QUrl url(QStringLiteral("qrc:/doric/qml/toast.qml"));
component.loadUrl(url);
if (component.isError()) {
qCritical() << component.errorString();
}
QQuickItem *item = qobject_cast<QQuickItem *>(component.create());
item->setParentItem(rootObject);
item->childItems().at(0)->childItems().at(0)->setProperty("text", msg);
item->setProperty("y", rootObject->height() - item->height() - 20);
connect(item, &QQuickItem::widthChanged, [rootObject, item]() {
item->setProperty("x", (rootObject->width() - item->width()) / 2.f);
});
connect(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() - 88) / 2);
}
});
QTimer::singleShot(2000, qApp, [item]() {
item->setParent(nullptr);
item->setParentItem(nullptr);
item->deleteLater();
});
},
DoricThreadMode::UI);
} }

View File

@ -24,6 +24,8 @@
<file alias="text.qml">resources/text.qml</file> <file alias="text.qml">resources/text.qml</file>
<file alias="scroller.qml">resources/scroller.qml</file> <file alias="scroller.qml">resources/scroller.qml</file>
<file alias="toast.qml">resources/toast.qml</file>
<file alias="util.mjs">resources/util.mjs</file> <file alias="util.mjs">resources/util.mjs</file>
<file alias="gravity.mjs">resources/gravity.mjs</file> <file alias="gravity.mjs">resources/gravity.mjs</file>
<file alias="test-layout.qml">resources/test-layout.qml</file> <file alias="test-layout.qml">resources/test-layout.qml</file>

View File

@ -0,0 +1,22 @@
import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.15
Rectangle {
width: childrenRect.width
height: childrenRect.height
color: "#bb000000"
ColumnLayout {
Text {
text: "toast"
font.pixelSize: 20
color: 'white'
Layout.leftMargin: 5
Layout.rightMargin: 5
Layout.topMargin: 15
Layout.bottomMargin: 15
}
}
}

View File

@ -2,5 +2,6 @@ import QtQuick 2.12
import QtQuick.Controls 2.5 import QtQuick.Controls 2.5
Rectangle { Rectangle {
width: childrenRect.width
height: childrenRect.height
} }

View File

@ -7,6 +7,8 @@ void DoricRootNode::setRootView(QQuickItem *rootView) {
this->getLayouts()->setLayoutType(DoricLayoutType::DoricStack); this->getLayouts()->setLayoutType(DoricLayoutType::DoricStack);
} }
QQuickItem *DoricRootNode::getRootView() { return mView; }
void DoricRootNode::requestLayout() { void DoricRootNode::requestLayout() {
getLayouts()->apply(); getLayouts()->apply();
DoricStackNode::requestLayout(); DoricStackNode::requestLayout();

View File

@ -11,6 +11,8 @@ public:
void setRootView(QQuickItem *rootView); void setRootView(QQuickItem *rootView);
QQuickItem *getRootView();
virtual void requestLayout() override; virtual void requestLayout() override;
}; };