diff --git a/doric-Qt/doric/plugin/DoricModalPlugin.cpp b/doric-Qt/doric/plugin/DoricModalPlugin.cpp index d7f2ada2..23c02527 100644 --- a/doric-Qt/doric/plugin/DoricModalPlugin.cpp +++ b/doric-Qt/doric/plugin/DoricModalPlugin.cpp @@ -1,5 +1,61 @@ #include "DoricModalPlugin.h" +#include "shader/DoricRootNode.h" +#include "utils/DoricLayouts.h" + +#include +#include +#include +#include +#include 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(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); } diff --git a/doric-Qt/doric/qml.qrc b/doric-Qt/doric/qml.qrc index f80bbd47..04a3532a 100644 --- a/doric-Qt/doric/qml.qrc +++ b/doric-Qt/doric/qml.qrc @@ -24,6 +24,8 @@ resources/text.qml resources/scroller.qml + resources/toast.qml + resources/util.mjs resources/gravity.mjs resources/test-layout.qml diff --git a/doric-Qt/doric/resources/toast.qml b/doric-Qt/doric/resources/toast.qml new file mode 100644 index 00000000..99ca82f0 --- /dev/null +++ b/doric-Qt/doric/resources/toast.qml @@ -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 + } + } +} + diff --git a/doric-Qt/doric/resources/view.qml b/doric-Qt/doric/resources/view.qml index b6f562e9..f8d87888 100644 --- a/doric-Qt/doric/resources/view.qml +++ b/doric-Qt/doric/resources/view.qml @@ -2,5 +2,6 @@ import QtQuick 2.12 import QtQuick.Controls 2.5 Rectangle { - + width: childrenRect.width + height: childrenRect.height } diff --git a/doric-Qt/doric/shader/DoricRootNode.cpp b/doric-Qt/doric/shader/DoricRootNode.cpp index 9ae17e9f..4e020bfe 100644 --- a/doric-Qt/doric/shader/DoricRootNode.cpp +++ b/doric-Qt/doric/shader/DoricRootNode.cpp @@ -7,6 +7,8 @@ void DoricRootNode::setRootView(QQuickItem *rootView) { this->getLayouts()->setLayoutType(DoricLayoutType::DoricStack); } +QQuickItem *DoricRootNode::getRootView() { return mView; } + void DoricRootNode::requestLayout() { getLayouts()->apply(); DoricStackNode::requestLayout(); diff --git a/doric-Qt/doric/shader/DoricRootNode.h b/doric-Qt/doric/shader/DoricRootNode.h index 33c91bb5..0499ef9c 100644 --- a/doric-Qt/doric/shader/DoricRootNode.h +++ b/doric-Qt/doric/shader/DoricRootNode.h @@ -11,6 +11,8 @@ public: void setRootView(QQuickItem *rootView); + QQuickItem *getRootView(); + virtual void requestLayout() override; };